blob: d594cfe7efaf22ef4733a07f730857fa51d64860 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
Chris Lattner6831a812006-02-13 09:18:02 +000044 // Libcall insertion helpers.
45
46 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
47 /// legalized. We use this to ensure that calls are properly serialized
48 /// against each other, including inserted libcalls.
49 SDOperand LastCALLSEQ_END;
50
51 /// IsLegalizingCall - This member is used *only* for purposes of providing
52 /// helpful assertions that a libcall isn't created while another call is
53 /// being legalized (which could lead to non-serialized call sequences).
54 bool IsLegalizingCall;
55
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000057 Legal, // The target natively supports this operation.
58 Promote, // This operation should be executed in a larger type.
59 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000060 };
Chris Lattner6831a812006-02-13 09:18:02 +000061
Chris Lattner3e928bb2005-01-07 07:47:09 +000062 /// ValueTypeActions - This is a bitvector that contains two bits for each
63 /// value type, where the two bits correspond to the LegalizeAction enum.
64 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000065 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000066
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
70 std::map<SDOperand, SDOperand> LegalizedNodes;
71
Chris Lattner03c85462005-01-15 05:21:40 +000072 /// PromotedNodes - For nodes that are below legal width, and that have more
73 /// than one use, this map indicates what promoted value to use. This allows
74 /// us to avoid promoting the same thing more than once.
75 std::map<SDOperand, SDOperand> PromotedNodes;
76
Chris Lattnerc7029802006-03-18 01:44:44 +000077 /// ExpandedNodes - For nodes that need to be expanded this map indicates
78 /// which which operands are the expanded version of the input. This allows
79 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
81
Chris Lattnerc7029802006-03-18 01:44:44 +000082 /// SplitNodes - For vector nodes that need to be split, this map indicates
83 /// which which operands are the split version of the input. This allows us
84 /// to avoid splitting the same node more than once.
85 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
86
87 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
88 /// concrete packed types, this contains the mapping of ones we have already
89 /// processed to the result.
90 std::map<SDOperand, SDOperand> PackedNodes;
91
Chris Lattner8afc48e2005-01-07 22:28:47 +000092 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000093 LegalizedNodes.insert(std::make_pair(From, To));
94 // If someone requests legalization of the new node, return itself.
95 if (From != To)
96 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000097 }
Chris Lattner03c85462005-01-15 05:21:40 +000098 void AddPromotedOperand(SDOperand From, SDOperand To) {
99 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
100 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000101 // If someone requests legalization of the new node, return itself.
102 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000103 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000104
Chris Lattner3e928bb2005-01-07 07:47:09 +0000105public:
106
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000107 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108
Chris Lattner3e928bb2005-01-07 07:47:09 +0000109 /// getTypeAction - Return how we should legalize values of this type, either
110 /// it is already legal or we need to expand it into multiple registers of
111 /// smaller integer type, or we need to promote it to a larger type.
112 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000113 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000114 }
115
116 /// isTypeLegal - Return true if this type is legal on this target.
117 ///
118 bool isTypeLegal(MVT::ValueType VT) const {
119 return getTypeAction(VT) == Legal;
120 }
121
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122 void LegalizeDAG();
123
Chris Lattner456a93a2006-01-28 07:39:30 +0000124private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000125 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
126 /// appropriate for its type.
127 void HandleOp(SDOperand Op);
128
129 /// LegalizeOp - We know that the specified value has a legal type.
130 /// Recursively ensure that the operands have legal types, then return the
131 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000133
134 /// PromoteOp - Given an operation that produces a value in an invalid type,
135 /// promote it to compute the value into a larger type. The produced value
136 /// will have the correct bits for the low portion of the register, but no
137 /// guarantee is made about the top bits: it may be zero, sign-extended, or
138 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000139 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140
Chris Lattnerc7029802006-03-18 01:44:44 +0000141 /// ExpandOp - Expand the specified SDOperand into its two component pieces
142 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
143 /// the LegalizeNodes map is filled in for any results that are not expanded,
144 /// the ExpandedNodes map is filled in for any results that are expanded, and
145 /// the Lo/Hi values are returned. This applies to integer types and Vector
146 /// types.
147 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
148
149 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
150 /// two smaller values of MVT::Vector type.
151 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
152
153 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
154 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
155 /// this is called, we know that PackedVT is the right type for the result and
156 /// we know that this type is legal for the target.
157 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
158
Chris Lattner6831a812006-02-13 09:18:02 +0000159 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
160
Nate Begeman750ac1b2006-02-01 07:19:44 +0000161 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
162
Chris Lattner77e77a62005-01-21 06:05:23 +0000163 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
164 SDOperand &Hi);
165 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
166 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000167
Chris Lattner35481892005-12-23 00:16:34 +0000168 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000169 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
170 SDOperand LegalOp,
171 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000172 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
173 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000174 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
175 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000176
Chris Lattner456a93a2006-01-28 07:39:30 +0000177 SDOperand ExpandBSWAP(SDOperand Op);
178 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000179 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
180 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000181 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
182 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000183
Chris Lattner3e928bb2005-01-07 07:47:09 +0000184 SDOperand getIntPtrConstant(uint64_t Val) {
185 return DAG.getConstant(Val, TLI.getPointerTy());
186 }
187};
188}
189
Chris Lattnerc7029802006-03-18 01:44:44 +0000190/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
191/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000192static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000193 switch (VecOp) {
194 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000195 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
196 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
197 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
198 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
199 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
200 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
201 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
202 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000203 }
204}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000205
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000206SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
207 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
208 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000209 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000210 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000211}
212
Chris Lattner32fca002005-10-06 01:20:27 +0000213/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
214/// not been visited yet and if all of its operands have already been visited.
215static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
216 std::map<SDNode*, unsigned> &Visited) {
217 if (++Visited[N] != N->getNumOperands())
218 return; // Haven't visited all operands yet
219
220 Order.push_back(N);
221
222 if (N->hasOneUse()) { // Tail recurse in common case.
223 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
224 return;
225 }
226
227 // Now that we have N in, add anything that uses it if all of their operands
228 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000229 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
230 ComputeTopDownOrdering(*UI, Order, Visited);
231}
232
Chris Lattner1618beb2005-07-29 00:11:56 +0000233
Chris Lattner3e928bb2005-01-07 07:47:09 +0000234void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000235 LastCALLSEQ_END = DAG.getEntryNode();
236 IsLegalizingCall = false;
237
Chris Lattnerab510a72005-10-02 17:49:46 +0000238 // The legalize process is inherently a bottom-up recursive process (users
239 // legalize their uses before themselves). Given infinite stack space, we
240 // could just start legalizing on the root and traverse the whole graph. In
241 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000242 // blocks. To avoid this problem, compute an ordering of the nodes where each
243 // node is only legalized after all of its operands are legalized.
244 std::map<SDNode*, unsigned> Visited;
245 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000246
Chris Lattner32fca002005-10-06 01:20:27 +0000247 // Compute ordering from all of the leaves in the graphs, those (like the
248 // entry node) that have no operands.
249 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
250 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000251 if (I->getNumOperands() == 0) {
252 Visited[I] = 0 - 1U;
253 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000254 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000255 }
256
Chris Lattnerde202b32005-11-09 23:47:37 +0000257 assert(Order.size() == Visited.size() &&
258 Order.size() ==
259 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000260 "Error: DAG is cyclic!");
261 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000262
Chris Lattnerc7029802006-03-18 01:44:44 +0000263 for (unsigned i = 0, e = Order.size(); i != e; ++i)
264 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000265
266 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000267 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000268 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
269 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270
271 ExpandedNodes.clear();
272 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000273 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000274 SplitNodes.clear();
275 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000276
277 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000278 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279}
280
Chris Lattner6831a812006-02-13 09:18:02 +0000281
282/// FindCallEndFromCallStart - Given a chained node that is part of a call
283/// sequence, find the CALLSEQ_END node that terminates the call sequence.
284static SDNode *FindCallEndFromCallStart(SDNode *Node) {
285 if (Node->getOpcode() == ISD::CALLSEQ_END)
286 return Node;
287 if (Node->use_empty())
288 return 0; // No CallSeqEnd
289
290 // The chain is usually at the end.
291 SDOperand TheChain(Node, Node->getNumValues()-1);
292 if (TheChain.getValueType() != MVT::Other) {
293 // Sometimes it's at the beginning.
294 TheChain = SDOperand(Node, 0);
295 if (TheChain.getValueType() != MVT::Other) {
296 // Otherwise, hunt for it.
297 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
298 if (Node->getValueType(i) == MVT::Other) {
299 TheChain = SDOperand(Node, i);
300 break;
301 }
302
303 // Otherwise, we walked into a node without a chain.
304 if (TheChain.getValueType() != MVT::Other)
305 return 0;
306 }
307 }
308
309 for (SDNode::use_iterator UI = Node->use_begin(),
310 E = Node->use_end(); UI != E; ++UI) {
311
312 // Make sure to only follow users of our token chain.
313 SDNode *User = *UI;
314 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
315 if (User->getOperand(i) == TheChain)
316 if (SDNode *Result = FindCallEndFromCallStart(User))
317 return Result;
318 }
319 return 0;
320}
321
322/// FindCallStartFromCallEnd - Given a chained node that is part of a call
323/// sequence, find the CALLSEQ_START node that initiates the call sequence.
324static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
325 assert(Node && "Didn't find callseq_start for a call??");
326 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
327
328 assert(Node->getOperand(0).getValueType() == MVT::Other &&
329 "Node doesn't have a token chain argument!");
330 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
331}
332
333/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
334/// see if any uses can reach Dest. If no dest operands can get to dest,
335/// legalize them, legalize ourself, and return false, otherwise, return true.
336bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
337 SDNode *Dest) {
338 if (N == Dest) return true; // N certainly leads to Dest :)
339
340 // If the first result of this node has been already legalized, then it cannot
341 // reach N.
342 switch (getTypeAction(N->getValueType(0))) {
343 case Legal:
344 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
345 break;
346 case Promote:
347 if (PromotedNodes.count(SDOperand(N, 0))) return false;
348 break;
349 case Expand:
350 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
351 break;
352 }
353
354 // Okay, this node has not already been legalized. Check and legalize all
355 // operands. If none lead to Dest, then we can legalize this node.
356 bool OperandsLeadToDest = false;
357 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
358 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
359 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
360
361 if (OperandsLeadToDest) return true;
362
363 // Okay, this node looks safe, legalize it and return false.
364 switch (getTypeAction(N->getValueType(0))) {
365 case Legal:
366 LegalizeOp(SDOperand(N, 0));
367 break;
368 case Promote:
369 PromoteOp(SDOperand(N, 0));
370 break;
371 case Expand: {
372 SDOperand X, Y;
373 ExpandOp(SDOperand(N, 0), X, Y);
374 break;
375 }
376 }
377 return false;
378}
379
Chris Lattnerc7029802006-03-18 01:44:44 +0000380/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
381/// appropriate for its type.
382void SelectionDAGLegalize::HandleOp(SDOperand Op) {
383 switch (getTypeAction(Op.getValueType())) {
384 default: assert(0 && "Bad type action!");
385 case Legal: LegalizeOp(Op); break;
386 case Promote: PromoteOp(Op); break;
387 case Expand:
388 if (Op.getValueType() != MVT::Vector) {
389 SDOperand X, Y;
390 ExpandOp(Op, X, Y);
391 } else {
392 SDNode *N = Op.Val;
393 unsigned NumOps = N->getNumOperands();
394 unsigned NumElements =
395 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
396 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
397 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
398 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
399 // In the common case, this is a legal vector type, convert it to the
400 // packed operation and type now.
401 PackVectorOp(Op, PackedVT);
402 } else if (NumElements == 1) {
403 // Otherwise, if this is a single element vector, convert it to a
404 // scalar operation.
405 PackVectorOp(Op, EVT);
406 } else {
407 // Otherwise, this is a multiple element vector that isn't supported.
408 // Split it in half and legalize both parts.
409 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000410 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000411 }
412 }
413 break;
414 }
415}
Chris Lattner6831a812006-02-13 09:18:02 +0000416
417
Chris Lattnerc7029802006-03-18 01:44:44 +0000418/// LegalizeOp - We know that the specified value has a legal type.
419/// Recursively ensure that the operands have legal types, then return the
420/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000421SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000422 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000423 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000424 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000425
Chris Lattner3e928bb2005-01-07 07:47:09 +0000426 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000427 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000428 if (Node->getNumValues() > 1) {
429 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000430 if (getTypeAction(Node->getValueType(i)) != Legal) {
431 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000432 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000433 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000434 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000435 }
436 }
437
Chris Lattner45982da2005-05-12 16:53:42 +0000438 // Note that LegalizeOp may be reentered even from single-use nodes, which
439 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000440 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
441 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000442
Nate Begeman9373a812005-08-10 20:51:12 +0000443 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000444 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000445 bool isCustom = false;
446
Chris Lattner3e928bb2005-01-07 07:47:09 +0000447 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000448 case ISD::FrameIndex:
449 case ISD::EntryToken:
450 case ISD::Register:
451 case ISD::BasicBlock:
452 case ISD::TargetFrameIndex:
453 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000454 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000455 case ISD::TargetConstantPool:
456 case ISD::TargetGlobalAddress:
457 case ISD::TargetExternalSymbol:
458 case ISD::VALUETYPE:
459 case ISD::SRCVALUE:
460 case ISD::STRING:
461 case ISD::CONDCODE:
462 // Primitives must all be legal.
463 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
464 "This must be legal!");
465 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000467 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
468 // If this is a target node, legalize it by legalizing the operands then
469 // passing it through.
470 std::vector<SDOperand> Ops;
471 bool Changed = false;
472 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
473 Ops.push_back(LegalizeOp(Node->getOperand(i)));
474 Changed = Changed || Node->getOperand(i) != Ops.back();
475 }
476 if (Changed)
477 if (Node->getNumValues() == 1)
478 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
479 else {
480 std::vector<MVT::ValueType> VTs(Node->value_begin(),
481 Node->value_end());
482 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
483 }
484
485 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
486 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
487 return Result.getValue(Op.ResNo);
488 }
489 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
491 assert(0 && "Do not know how to legalize this operator!");
492 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000494 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000495 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000496 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
497 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000498 case TargetLowering::Custom:
499 Tmp1 = TLI.LowerOperation(Op, DAG);
500 if (Tmp1.Val) Result = Tmp1;
501 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000502 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000503 break;
504 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000505 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000506 case ISD::AssertSext:
507 case ISD::AssertZext:
508 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000510 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000511 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000512 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000513 Result = Node->getOperand(Op.ResNo);
514 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000515 case ISD::CopyFromReg:
516 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000517 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000518 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000519 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000520 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000521 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000522 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000523 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
525 } else {
526 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
527 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000528 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
529 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000530 // Since CopyFromReg produces two values, make sure to remember that we
531 // legalized both of them.
532 AddLegalizedOperand(Op.getValue(0), Result);
533 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
534 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000535 case ISD::UNDEF: {
536 MVT::ValueType VT = Op.getValueType();
537 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000538 default: assert(0 && "This action is not supported yet!");
539 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000540 if (MVT::isInteger(VT))
541 Result = DAG.getConstant(0, VT);
542 else if (MVT::isFloatingPoint(VT))
543 Result = DAG.getConstantFP(0, VT);
544 else
545 assert(0 && "Unknown value type!");
546 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000547 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000548 break;
549 }
550 break;
551 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000552
553 case ISD::LOCATION:
554 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
555 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
556
557 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
558 case TargetLowering::Promote:
559 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000560 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000561 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000562 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
563 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
564
565 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
566 const std::string &FName =
567 cast<StringSDNode>(Node->getOperand(3))->getValue();
568 const std::string &DirName =
569 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000570 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000571
Jim Laskeye81aecb2005-12-21 20:51:37 +0000572 std::vector<SDOperand> Ops;
573 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000574 SDOperand LineOp = Node->getOperand(1);
575 SDOperand ColOp = Node->getOperand(2);
576
577 if (useDEBUG_LOC) {
578 Ops.push_back(LineOp); // line #
579 Ops.push_back(ColOp); // col #
580 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
581 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
582 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000583 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
584 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000585 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
586 Ops.push_back(DAG.getConstant(ID, MVT::i32));
587 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
588 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000589 } else {
590 Result = Tmp1; // chain
591 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000592 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000593 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000594 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000595 if (Tmp1 != Node->getOperand(0) ||
596 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000597 std::vector<SDOperand> Ops;
598 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000599 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
600 Ops.push_back(Node->getOperand(1)); // line # must be legal.
601 Ops.push_back(Node->getOperand(2)); // col # must be legal.
602 } else {
603 // Otherwise promote them.
604 Ops.push_back(PromoteOp(Node->getOperand(1)));
605 Ops.push_back(PromoteOp(Node->getOperand(2)));
606 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000607 Ops.push_back(Node->getOperand(3)); // filename must be legal.
608 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000609 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000610 }
611 break;
612 }
613 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000614
615 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000616 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000617 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000618 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000619 case TargetLowering::Legal:
620 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
621 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
622 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
623 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000625 break;
626 }
627 break;
628
629 case ISD::DEBUG_LABEL:
630 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
631 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000632 default: assert(0 && "This action is not supported yet!");
633 case TargetLowering::Legal:
634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
635 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000637 break;
638 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000639 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000640
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 case ISD::Constant:
642 // We know we don't need to expand constants here, constants only have one
643 // value and we check that it is fine above.
644
645 // FIXME: Maybe we should handle things like targets that don't support full
646 // 32-bit immediates?
647 break;
648 case ISD::ConstantFP: {
649 // Spill FP immediates to the constant pool if the target cannot directly
650 // codegen them. Targets often have some immediate values that can be
651 // efficiently generated into an FP register without a load. We explicitly
652 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000653 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
654
655 // Check to see if this FP immediate is already legal.
656 bool isLegal = false;
657 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
658 E = TLI.legal_fpimm_end(); I != E; ++I)
659 if (CFP->isExactlyValue(*I)) {
660 isLegal = true;
661 break;
662 }
663
Chris Lattner3181a772006-01-29 06:26:56 +0000664 // If this is a legal constant, turn it into a TargetConstantFP node.
665 if (isLegal) {
666 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
667 break;
668 }
669
670 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
671 default: assert(0 && "This action is not supported yet!");
672 case TargetLowering::Custom:
673 Tmp3 = TLI.LowerOperation(Result, DAG);
674 if (Tmp3.Val) {
675 Result = Tmp3;
676 break;
677 }
678 // FALLTHROUGH
679 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000680 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000681 bool Extend = false;
682
Chris Lattner456a93a2006-01-28 07:39:30 +0000683 // If a FP immediate is precise when represented as a float and if the
684 // target can do an extending load from float to double, we put it into
685 // the constant pool as a float, even if it's is statically typed as a
686 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000687 MVT::ValueType VT = CFP->getValueType(0);
688 bool isDouble = VT == MVT::f64;
689 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
690 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000691 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
692 // Only do this if the target has a native EXTLOAD instruction from
693 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000694 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000695 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
696 VT = MVT::f32;
697 Extend = true;
698 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000699
Chris Lattner456a93a2006-01-28 07:39:30 +0000700 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000701 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000702 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
703 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000704 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000705 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
706 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000707 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000708 }
709 break;
710 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000711 case ISD::TokenFactor:
712 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000713 Tmp1 = LegalizeOp(Node->getOperand(0));
714 Tmp2 = LegalizeOp(Node->getOperand(1));
715 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
716 } else if (Node->getNumOperands() == 3) {
717 Tmp1 = LegalizeOp(Node->getOperand(0));
718 Tmp2 = LegalizeOp(Node->getOperand(1));
719 Tmp3 = LegalizeOp(Node->getOperand(2));
720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000721 } else {
722 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000723 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000724 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
725 Ops.push_back(LegalizeOp(Node->getOperand(i)));
726 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000727 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000728 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000729
Chris Lattnerb2827b02006-03-19 00:52:58 +0000730 case ISD::BUILD_VECTOR:
731 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000732 default: assert(0 && "This action is not supported yet!");
733 case TargetLowering::Custom:
734 Tmp3 = TLI.LowerOperation(Result, DAG);
735 if (Tmp3.Val) {
736 Result = Tmp3;
737 break;
738 }
739 // FALLTHROUGH
740 case TargetLowering::Expand: {
741 // We assume that built vectors are not legal, and will be immediately
742 // spilled to memory. If the values are all constants, turn this into a
743 // load from the constant pool.
744 bool isConstant = true;
745 for (SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
746 I != E; ++I) {
747 if (!isa<ConstantFPSDNode>(I) && !isa<ConstantSDNode>(I) &&
748 I->getOpcode() != ISD::UNDEF) {
749 isConstant = false;
Chris Lattnerb2827b02006-03-19 00:52:58 +0000750 break;
751 }
752 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000753
754 // Create a ConstantPacked, and put it in the constant pool.
755 if (isConstant) {
756 MVT::ValueType VT = Node->getValueType(0);
757 const Type *OpNTy =
758 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
759 std::vector<Constant*> CV;
760 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
761 if (ConstantFPSDNode *V =
762 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
763 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
764 } else if (ConstantSDNode *V =
765 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
766 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
767 } else {
768 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
769 CV.push_back(UndefValue::get(OpNTy));
770 }
771 }
772 Constant *CP = ConstantPacked::get(CV);
773 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
774 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
775 DAG.getSrcValue(NULL));
776 break;
777 }
778
779 // Otherwise, this isn't a constant entry. Allocate a sufficiently
780 // aligned object on the stack, store each element into it, then load
781 // the result as a vector.
Chris Lattner5fcd0352006-03-19 04:18:56 +0000782 MVT::ValueType VT = Node->getValueType(0);
783 // Create the stack frame object.
784 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
785 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
786 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
787 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
788
789 // Emit a store of each element to the stack slot.
790 std::vector<SDOperand> Stores;
791 bool isLittleEndian = TLI.isLittleEndian();
792 unsigned TypeByteSize =
793 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
794 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
795 // Store (in the right endianness) the elements to memory.
796 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner74881902006-03-19 05:46:04 +0000797 // Ignore undef elements.
798 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
799
Chris Lattner5fcd0352006-03-19 04:18:56 +0000800 unsigned Offset;
801 if (isLittleEndian)
802 Offset = TypeByteSize*i;
803 else
804 Offset = TypeByteSize*(e-i-1);
805
806 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
807 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
808
809 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
810 Node->getOperand(i), Idx,
811 DAG.getSrcValue(NULL)));
812 }
Chris Lattner74881902006-03-19 05:46:04 +0000813
814 SDOperand StoreChain;
815 if (!Stores.empty()) // Not all undef elements?
816 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
817 else
818 StoreChain = DAG.getEntryNode();
Chris Lattner5fcd0352006-03-19 04:18:56 +0000819
820 // Result is a load from the stack slot.
821 Result = DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
Chris Lattnerb2827b02006-03-19 00:52:58 +0000822 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000823 }
824 }
825 break;
826 case ISD::INSERT_VECTOR_ELT:
827 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
828 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
829 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
830 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
831
832 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
833 Node->getValueType(0))) {
834 default: assert(0 && "This action is not supported yet!");
835 case TargetLowering::Legal:
836 break;
837 case TargetLowering::Custom:
838 Tmp3 = TLI.LowerOperation(Result, DAG);
839 if (Tmp3.Val) {
840 Result = Tmp3;
841 break;
842 }
843 // FALLTHROUGH
844 case TargetLowering::Expand: {
845 // If the target doesn't support this, we have to spill the input vector
846 // to a temporary stack slot, update the element, then reload it. This is
847 // badness. We could also load the value into a vector register (either
848 // with a "move to register" or "extload into register" instruction, then
849 // permute it into place, if the idx is a constant and if the idx is
850 // supported by the target.
851 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
852 break;
853 }
854 }
855 break;
Chris Lattner6831a812006-02-13 09:18:02 +0000856 case ISD::CALLSEQ_START: {
857 SDNode *CallEnd = FindCallEndFromCallStart(Node);
858
859 // Recursively Legalize all of the inputs of the call end that do not lead
860 // to this call start. This ensures that any libcalls that need be inserted
861 // are inserted *before* the CALLSEQ_START.
862 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
863 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
864
865 // Now that we legalized all of the inputs (which may have inserted
866 // libcalls) create the new CALLSEQ_START node.
867 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
868
869 // Merge in the last call, to ensure that this call start after the last
870 // call ended.
871 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
872 Tmp1 = LegalizeOp(Tmp1);
873
874 // Do not try to legalize the target-specific arguments (#1+).
875 if (Tmp1 != Node->getOperand(0)) {
876 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
877 Ops[0] = Tmp1;
878 Result = DAG.UpdateNodeOperands(Result, Ops);
879 }
880
881 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000882 AddLegalizedOperand(Op.getValue(0), Result);
883 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
884 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
885
Chris Lattner6831a812006-02-13 09:18:02 +0000886 // Now that the callseq_start and all of the non-call nodes above this call
887 // sequence have been legalized, legalize the call itself. During this
888 // process, no libcalls can/will be inserted, guaranteeing that no calls
889 // can overlap.
890 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
891 SDOperand InCallSEQ = LastCALLSEQ_END;
892 // Note that we are selecting this call!
893 LastCALLSEQ_END = SDOperand(CallEnd, 0);
894 IsLegalizingCall = true;
895
896 // Legalize the call, starting from the CALLSEQ_END.
897 LegalizeOp(LastCALLSEQ_END);
898 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
899 return Result;
900 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000901 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000902 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
903 // will cause this node to be legalized as well as handling libcalls right.
904 if (LastCALLSEQ_END.Val != Node) {
905 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
906 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
907 assert(I != LegalizedNodes.end() &&
908 "Legalizing the call start should have legalized this node!");
909 return I->second;
910 }
911
912 // Otherwise, the call start has been legalized and everything is going
913 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000914 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000915 // Do not try to legalize the target-specific arguments (#1+), except for
916 // an optional flag input.
917 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
918 if (Tmp1 != Node->getOperand(0)) {
919 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
920 Ops[0] = Tmp1;
921 Result = DAG.UpdateNodeOperands(Result, Ops);
922 }
923 } else {
924 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
925 if (Tmp1 != Node->getOperand(0) ||
926 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
927 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
928 Ops[0] = Tmp1;
929 Ops.back() = Tmp2;
930 Result = DAG.UpdateNodeOperands(Result, Ops);
931 }
Chris Lattner6a542892006-01-24 05:48:21 +0000932 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000933 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000934 // This finishes up call legalization.
935 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000936
937 // If the CALLSEQ_END node has a flag, remember that we legalized it.
938 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
939 if (Node->getNumValues() == 2)
940 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
941 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000942 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
944 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
945 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000946 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000947
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000948 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000949 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000950 switch (TLI.getOperationAction(Node->getOpcode(),
951 Node->getValueType(0))) {
952 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000953 case TargetLowering::Expand: {
954 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
955 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
956 " not tell us which reg is the stack pointer!");
957 SDOperand Chain = Tmp1.getOperand(0);
958 SDOperand Size = Tmp2.getOperand(1);
959 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
960 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
961 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000962 Tmp1 = LegalizeOp(Tmp1);
963 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000964 break;
965 }
966 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000967 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
968 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000969 Tmp1 = LegalizeOp(Tmp3);
970 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000971 }
Chris Lattner903d2782006-01-15 08:54:32 +0000972 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000973 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000974 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000975 }
Chris Lattner903d2782006-01-15 08:54:32 +0000976 // Since this op produce two values, make sure to remember that we
977 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000978 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
979 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000980 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000981 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000982 case ISD::INLINEASM:
983 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
984 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000985 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000986 Tmp2 = Tmp3 = SDOperand(0, 0);
987 else
988 Tmp3 = LegalizeOp(Tmp2);
989
990 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
991 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
992 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000993 if (Tmp3.Val) Ops.back() = Tmp3;
994 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000995 }
996
997 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000998 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000999 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1000 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +00001001 case ISD::BR:
1002 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001003 // Ensure that libcalls are emitted before a branch.
1004 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1005 Tmp1 = LegalizeOp(Tmp1);
1006 LastCALLSEQ_END = DAG.getEntryNode();
1007
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001009 break;
1010
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001011 case ISD::BRCOND:
1012 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001013 // Ensure that libcalls are emitted before a return.
1014 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1015 Tmp1 = LegalizeOp(Tmp1);
1016 LastCALLSEQ_END = DAG.getEntryNode();
1017
Chris Lattner47e92232005-01-18 19:27:06 +00001018 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1019 case Expand: assert(0 && "It's impossible to expand bools");
1020 case Legal:
1021 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1022 break;
1023 case Promote:
1024 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1025 break;
1026 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001027
1028 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001029 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001030
1031 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1032 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001033 case TargetLowering::Legal: break;
1034 case TargetLowering::Custom:
1035 Tmp1 = TLI.LowerOperation(Result, DAG);
1036 if (Tmp1.Val) Result = Tmp1;
1037 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001038 case TargetLowering::Expand:
1039 // Expand brcond's setcc into its constituent parts and create a BR_CC
1040 // Node.
1041 if (Tmp2.getOpcode() == ISD::SETCC) {
1042 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1043 Tmp2.getOperand(0), Tmp2.getOperand(1),
1044 Node->getOperand(2));
1045 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001046 // Make sure the condition is either zero or one. It may have been
1047 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001048 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1049 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1050 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001051
Nate Begeman7cbd5252005-08-16 19:49:35 +00001052 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1053 DAG.getCondCode(ISD::SETNE), Tmp2,
1054 DAG.getConstant(0, Tmp2.getValueType()),
1055 Node->getOperand(2));
1056 }
1057 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001058 }
1059 break;
1060 case ISD::BR_CC:
1061 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001062 // Ensure that libcalls are emitted before a branch.
1063 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1064 Tmp1 = LegalizeOp(Tmp1);
1065 LastCALLSEQ_END = DAG.getEntryNode();
1066
Nate Begeman750ac1b2006-02-01 07:19:44 +00001067 Tmp2 = Node->getOperand(2); // LHS
1068 Tmp3 = Node->getOperand(3); // RHS
1069 Tmp4 = Node->getOperand(1); // CC
1070
1071 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1072
1073 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1074 // the LHS is a legal SETCC itself. In this case, we need to compare
1075 // the result against zero to select between true and false values.
1076 if (Tmp3.Val == 0) {
1077 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1078 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001079 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001080
1081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1082 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001083
Chris Lattner181b7a32005-12-17 23:46:46 +00001084 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1085 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001086 case TargetLowering::Legal: break;
1087 case TargetLowering::Custom:
1088 Tmp4 = TLI.LowerOperation(Result, DAG);
1089 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001090 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001091 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001092 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001093 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001094 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1095 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001096
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001097 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001098 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1099 Tmp2 = Result.getValue(0);
1100 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001101
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001102 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1103 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001104 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001105 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001106 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001107 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001108 Tmp2 = LegalizeOp(Tmp1);
1109 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001110 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001111 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001112 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001113 // Since loads produce two values, make sure to remember that we
1114 // legalized both of them.
1115 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1116 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1117 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001118 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001119 case ISD::EXTLOAD:
1120 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001121 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001122 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1123 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001124
Chris Lattner5f056bf2005-07-10 01:55:33 +00001125 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001126 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001127 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001128 case TargetLowering::Promote:
1129 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001130 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1131 DAG.getValueType(MVT::i8));
1132 Tmp1 = Result.getValue(0);
1133 Tmp2 = Result.getValue(1);
1134 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001135 case TargetLowering::Custom:
1136 isCustom = true;
1137 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001138 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1140 Node->getOperand(3));
1141 Tmp1 = Result.getValue(0);
1142 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001143
1144 if (isCustom) {
1145 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1146 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001147 Tmp1 = LegalizeOp(Tmp3);
1148 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001149 }
1150 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001151 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001152 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001153 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001154 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1155 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001156 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001157 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1158 Tmp2 = LegalizeOp(Load.getValue(1));
1159 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001160 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001161 assert(Node->getOpcode() != ISD::EXTLOAD &&
1162 "EXTLOAD should always be supported!");
1163 // Turn the unsupported load into an EXTLOAD followed by an explicit
1164 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001165 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1166 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001167 SDOperand ValRes;
1168 if (Node->getOpcode() == ISD::SEXTLOAD)
1169 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001170 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001171 else
1172 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001173 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1174 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1175 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001176 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001177 // Since loads produce two values, make sure to remember that we legalized
1178 // both of them.
1179 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1180 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1181 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001182 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001183 case ISD::EXTRACT_ELEMENT: {
1184 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1185 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001186 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001187 case Legal:
1188 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1189 // 1 -> Hi
1190 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1191 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1192 TLI.getShiftAmountTy()));
1193 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1194 } else {
1195 // 0 -> Lo
1196 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1197 Node->getOperand(0));
1198 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001199 break;
1200 case Expand:
1201 // Get both the low and high parts.
1202 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1203 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1204 Result = Tmp2; // 1 -> Hi
1205 else
1206 Result = Tmp1; // 0 -> Lo
1207 break;
1208 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001210 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001211
1212 case ISD::CopyToReg:
1213 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001214
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001215 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001216 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001217 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001218 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001219 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001220 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001221 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001222 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001223 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001224 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001225 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1226 Tmp3);
1227 } else {
1228 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001229 }
1230
1231 // Since this produces two values, make sure to remember that we legalized
1232 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001233 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001234 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001235 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001236 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001237 break;
1238
1239 case ISD::RET:
1240 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001241
1242 // Ensure that libcalls are emitted before a return.
1243 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1244 Tmp1 = LegalizeOp(Tmp1);
1245 LastCALLSEQ_END = DAG.getEntryNode();
1246
Chris Lattner3e928bb2005-01-07 07:47:09 +00001247 switch (Node->getNumOperands()) {
1248 case 2: // ret val
1249 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1250 case Legal:
1251 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001252 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001253 break;
1254 case Expand: {
1255 SDOperand Lo, Hi;
1256 ExpandOp(Node->getOperand(1), Lo, Hi);
1257 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001258 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001259 }
1260 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001261 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1263 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001264 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001265 }
1266 break;
1267 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001268 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001269 break;
1270 default: { // ret <values>
1271 std::vector<SDOperand> NewValues;
1272 NewValues.push_back(Tmp1);
1273 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1274 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1275 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001276 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001277 break;
1278 case Expand: {
1279 SDOperand Lo, Hi;
1280 ExpandOp(Node->getOperand(i), Lo, Hi);
1281 NewValues.push_back(Lo);
1282 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001283 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001284 }
1285 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001286 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001287 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001288
1289 if (NewValues.size() == Node->getNumOperands())
1290 Result = DAG.UpdateNodeOperands(Result, NewValues);
1291 else
1292 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001293 break;
1294 }
1295 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001296
Chris Lattner6862dbc2006-01-29 21:02:23 +00001297 if (Result.getOpcode() == ISD::RET) {
1298 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1299 default: assert(0 && "This action is not supported yet!");
1300 case TargetLowering::Legal: break;
1301 case TargetLowering::Custom:
1302 Tmp1 = TLI.LowerOperation(Result, DAG);
1303 if (Tmp1.Val) Result = Tmp1;
1304 break;
1305 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001306 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001307 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001308 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001309 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1310 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1311
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001312 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001313 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001314 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001315 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001316 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001317 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001318 } else {
1319 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001320 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001321 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001322 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1323 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001324 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001325 }
1326
Chris Lattner3e928bb2005-01-07 07:47:09 +00001327 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1328 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001329 Tmp3 = LegalizeOp(Node->getOperand(1));
1330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1331 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001332
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001333 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001334 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1335 default: assert(0 && "This action is not supported yet!");
1336 case TargetLowering::Legal: break;
1337 case TargetLowering::Custom:
1338 Tmp1 = TLI.LowerOperation(Result, DAG);
1339 if (Tmp1.Val) Result = Tmp1;
1340 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001341 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001342 break;
1343 }
1344 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001345 // Truncate the value and store the result.
1346 Tmp3 = PromoteOp(Node->getOperand(1));
1347 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001348 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001349 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001350 break;
1351
Chris Lattner3e928bb2005-01-07 07:47:09 +00001352 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001353 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001354 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001355
1356 // If this is a vector type, then we have to calculate the increment as
1357 // the product of the element size in bytes, and the number of elements
1358 // in the high half of the vector.
1359 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1360 SDNode *InVal = Node->getOperand(1).Val;
1361 unsigned NumElems =
1362 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1363 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1364
1365 // Figure out if there is a Packed type corresponding to this Vector
1366 // type. If so, convert to the packed type.
1367 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1368 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1369 // Turn this into a normal store of the packed type.
1370 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1371 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1372 Node->getOperand(3));
1373 break;
1374 } else if (NumElems == 1) {
1375 // Turn this into a normal store of the scalar type.
1376 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1377 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1378 Node->getOperand(3));
1379 break;
1380 } else {
1381 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1382 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1383 }
1384 } else {
1385 ExpandOp(Node->getOperand(1), Lo, Hi);
1386 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1387 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001388
1389 if (!TLI.isLittleEndian())
1390 std::swap(Lo, Hi);
1391
Chris Lattneredb1add2005-05-11 04:51:16 +00001392 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1393 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001394 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1395 getIntPtrConstant(IncrementSize));
1396 assert(isTypeLegal(Tmp2.getValueType()) &&
1397 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001398 // FIXME: This sets the srcvalue of both halves to be the same, which is
1399 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001400 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1401 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001402 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1403 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001404 }
1405 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001406 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001407 case ISD::PCMARKER:
1408 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001410 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001411 case ISD::STACKSAVE:
1412 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001413 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1414 Tmp1 = Result.getValue(0);
1415 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001416
Chris Lattner140d53c2006-01-13 02:50:02 +00001417 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1418 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001419 case TargetLowering::Legal: break;
1420 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001421 Tmp3 = TLI.LowerOperation(Result, DAG);
1422 if (Tmp3.Val) {
1423 Tmp1 = LegalizeOp(Tmp3);
1424 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001425 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001426 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001427 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001428 // Expand to CopyFromReg if the target set
1429 // StackPointerRegisterToSaveRestore.
1430 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001431 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001432 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001433 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001434 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001435 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1436 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001437 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001438 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001439 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001440
1441 // Since stacksave produce two values, make sure to remember that we
1442 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001443 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1444 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1445 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001446
Chris Lattner140d53c2006-01-13 02:50:02 +00001447 case ISD::STACKRESTORE:
1448 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1449 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001450 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001451
1452 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1453 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001454 case TargetLowering::Legal: break;
1455 case TargetLowering::Custom:
1456 Tmp1 = TLI.LowerOperation(Result, DAG);
1457 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001458 break;
1459 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001460 // Expand to CopyToReg if the target set
1461 // StackPointerRegisterToSaveRestore.
1462 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1463 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1464 } else {
1465 Result = Tmp1;
1466 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001467 break;
1468 }
1469 break;
1470
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001471 case ISD::READCYCLECOUNTER:
1472 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001473 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001474
1475 // Since rdcc produce two values, make sure to remember that we legalized
1476 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001477 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001478 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001479 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001480
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001481 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001482 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1483 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1484
Chris Lattner456a93a2006-01-28 07:39:30 +00001485 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1486 "Cannot handle illegal TRUNCSTORE yet!");
1487 Tmp2 = LegalizeOp(Node->getOperand(1));
1488
1489 // The only promote case we handle is TRUNCSTORE:i1 X into
1490 // -> TRUNCSTORE:i8 (and X, 1)
1491 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1492 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1493 TargetLowering::Promote) {
1494 // Promote the bool to a mask then store.
1495 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1496 DAG.getConstant(1, Tmp2.getValueType()));
1497 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1498 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001499
Chris Lattner456a93a2006-01-28 07:39:30 +00001500 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1501 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001502 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1503 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001504 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001505
Chris Lattner456a93a2006-01-28 07:39:30 +00001506 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1507 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1508 default: assert(0 && "This action is not supported yet!");
1509 case TargetLowering::Legal: break;
1510 case TargetLowering::Custom:
1511 Tmp1 = TLI.LowerOperation(Result, DAG);
1512 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001513 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001514 }
1515 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001516 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001517 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001518 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1519 case Expand: assert(0 && "It's impossible to expand bools");
1520 case Legal:
1521 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1522 break;
1523 case Promote:
1524 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1525 break;
1526 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001527 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001528 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001529
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001530 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001531
Nate Begemanb942a3d2005-08-23 04:29:48 +00001532 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001533 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001534 case TargetLowering::Legal: break;
1535 case TargetLowering::Custom: {
1536 Tmp1 = TLI.LowerOperation(Result, DAG);
1537 if (Tmp1.Val) Result = Tmp1;
1538 break;
1539 }
Nate Begeman9373a812005-08-10 20:51:12 +00001540 case TargetLowering::Expand:
1541 if (Tmp1.getOpcode() == ISD::SETCC) {
1542 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1543 Tmp2, Tmp3,
1544 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1545 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001546 // Make sure the condition is either zero or one. It may have been
1547 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001548 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1549 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1550 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001551 Result = DAG.getSelectCC(Tmp1,
1552 DAG.getConstant(0, Tmp1.getValueType()),
1553 Tmp2, Tmp3, ISD::SETNE);
1554 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001555 break;
1556 case TargetLowering::Promote: {
1557 MVT::ValueType NVT =
1558 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1559 unsigned ExtOp, TruncOp;
1560 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001561 ExtOp = ISD::ANY_EXTEND;
1562 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001563 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001564 ExtOp = ISD::FP_EXTEND;
1565 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001566 }
1567 // Promote each of the values to the new type.
1568 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1569 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1570 // Perform the larger operation, then round down.
1571 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1572 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1573 break;
1574 }
1575 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001576 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001577 case ISD::SELECT_CC: {
1578 Tmp1 = Node->getOperand(0); // LHS
1579 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001580 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1581 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001582 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001583
Nate Begeman750ac1b2006-02-01 07:19:44 +00001584 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1585
1586 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1587 // the LHS is a legal SETCC itself. In this case, we need to compare
1588 // the result against zero to select between true and false values.
1589 if (Tmp2.Val == 0) {
1590 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1591 CC = DAG.getCondCode(ISD::SETNE);
1592 }
1593 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1594
1595 // Everything is legal, see if we should expand this op or something.
1596 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1597 default: assert(0 && "This action is not supported yet!");
1598 case TargetLowering::Legal: break;
1599 case TargetLowering::Custom:
1600 Tmp1 = TLI.LowerOperation(Result, DAG);
1601 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001602 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001603 }
1604 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001605 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001606 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001607 Tmp1 = Node->getOperand(0);
1608 Tmp2 = Node->getOperand(1);
1609 Tmp3 = Node->getOperand(2);
1610 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1611
1612 // If we had to Expand the SetCC operands into a SELECT node, then it may
1613 // not always be possible to return a true LHS & RHS. In this case, just
1614 // return the value we legalized, returned in the LHS
1615 if (Tmp2.Val == 0) {
1616 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001617 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001618 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001619
Chris Lattner73e142f2006-01-30 22:43:50 +00001620 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001621 default: assert(0 && "Cannot handle this action for SETCC yet!");
1622 case TargetLowering::Custom:
1623 isCustom = true;
1624 // FALLTHROUGH.
1625 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001626 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001627 if (isCustom) {
1628 Tmp3 = TLI.LowerOperation(Result, DAG);
1629 if (Tmp3.Val) Result = Tmp3;
1630 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001631 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001632 case TargetLowering::Promote: {
1633 // First step, figure out the appropriate operation to use.
1634 // Allow SETCC to not be supported for all legal data types
1635 // Mostly this targets FP
1636 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1637 MVT::ValueType OldVT = NewInTy;
1638
1639 // Scan for the appropriate larger type to use.
1640 while (1) {
1641 NewInTy = (MVT::ValueType)(NewInTy+1);
1642
1643 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1644 "Fell off of the edge of the integer world");
1645 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1646 "Fell off of the edge of the floating point world");
1647
1648 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001649 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001650 break;
1651 }
1652 if (MVT::isInteger(NewInTy))
1653 assert(0 && "Cannot promote Legal Integer SETCC yet");
1654 else {
1655 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1656 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1657 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001658 Tmp1 = LegalizeOp(Tmp1);
1659 Tmp2 = LegalizeOp(Tmp2);
1660 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001661 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001662 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001663 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001664 case TargetLowering::Expand:
1665 // Expand a setcc node into a select_cc of the same condition, lhs, and
1666 // rhs that selects between const 1 (true) and const 0 (false).
1667 MVT::ValueType VT = Node->getValueType(0);
1668 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1669 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1670 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001671 break;
1672 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001673 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001674 case ISD::MEMSET:
1675 case ISD::MEMCPY:
1676 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001677 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001678 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1679
1680 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1681 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1682 case Expand: assert(0 && "Cannot expand a byte!");
1683 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001684 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001685 break;
1686 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001687 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001688 break;
1689 }
1690 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001691 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001692 }
Chris Lattner272455b2005-02-02 03:44:41 +00001693
1694 SDOperand Tmp4;
1695 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001696 case Expand: {
1697 // Length is too big, just take the lo-part of the length.
1698 SDOperand HiPart;
1699 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1700 break;
1701 }
Chris Lattnere5605212005-01-28 22:29:18 +00001702 case Legal:
1703 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001704 break;
1705 case Promote:
1706 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001707 break;
1708 }
1709
1710 SDOperand Tmp5;
1711 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1712 case Expand: assert(0 && "Cannot expand this yet!");
1713 case Legal:
1714 Tmp5 = LegalizeOp(Node->getOperand(4));
1715 break;
1716 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001717 Tmp5 = PromoteOp(Node->getOperand(4));
1718 break;
1719 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001720
1721 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1722 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001723 case TargetLowering::Custom:
1724 isCustom = true;
1725 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001726 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001727 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001728 if (isCustom) {
1729 Tmp1 = TLI.LowerOperation(Result, DAG);
1730 if (Tmp1.Val) Result = Tmp1;
1731 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001732 break;
1733 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001734 // Otherwise, the target does not support this operation. Lower the
1735 // operation to an explicit libcall as appropriate.
1736 MVT::ValueType IntPtr = TLI.getPointerTy();
1737 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1738 std::vector<std::pair<SDOperand, const Type*> > Args;
1739
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001740 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001741 if (Node->getOpcode() == ISD::MEMSET) {
1742 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001743 // Extend the (previously legalized) ubyte argument to be an int value
1744 // for the call.
1745 if (Tmp3.getValueType() > MVT::i32)
1746 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1747 else
1748 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001749 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1750 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1751
1752 FnName = "memset";
1753 } else if (Node->getOpcode() == ISD::MEMCPY ||
1754 Node->getOpcode() == ISD::MEMMOVE) {
1755 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1756 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1757 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1758 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1759 } else {
1760 assert(0 && "Unknown op!");
1761 }
Chris Lattner45982da2005-05-12 16:53:42 +00001762
Chris Lattnere1bd8222005-01-11 05:57:22 +00001763 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001764 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001765 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001766 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001767 break;
1768 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001769 }
1770 break;
1771 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001772
Chris Lattner5b359c62005-04-02 04:00:59 +00001773 case ISD::SHL_PARTS:
1774 case ISD::SRA_PARTS:
1775 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001776 std::vector<SDOperand> Ops;
1777 bool Changed = false;
1778 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1779 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1780 Changed |= Ops.back() != Node->getOperand(i);
1781 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001782 if (Changed)
1783 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001784
Evan Cheng05a2d562006-01-09 18:31:59 +00001785 switch (TLI.getOperationAction(Node->getOpcode(),
1786 Node->getValueType(0))) {
1787 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001788 case TargetLowering::Legal: break;
1789 case TargetLowering::Custom:
1790 Tmp1 = TLI.LowerOperation(Result, DAG);
1791 if (Tmp1.Val) {
1792 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001793 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001794 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001795 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1796 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001797 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001798 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001799 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001800 return RetVal;
1801 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001802 break;
1803 }
1804
Chris Lattner2c8086f2005-04-02 05:00:07 +00001805 // Since these produce multiple values, make sure to remember that we
1806 // legalized all of them.
1807 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1808 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1809 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001810 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001811
1812 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001813 case ISD::ADD:
1814 case ISD::SUB:
1815 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001816 case ISD::MULHS:
1817 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001818 case ISD::UDIV:
1819 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001820 case ISD::AND:
1821 case ISD::OR:
1822 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001823 case ISD::SHL:
1824 case ISD::SRL:
1825 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001826 case ISD::FADD:
1827 case ISD::FSUB:
1828 case ISD::FMUL:
1829 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001830 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001831 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1832 case Expand: assert(0 && "Not possible");
1833 case Legal:
1834 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1835 break;
1836 case Promote:
1837 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1838 break;
1839 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001840
1841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001842
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001843 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001844 default: assert(0 && "Operation not supported");
1845 case TargetLowering::Legal: break;
1846 case TargetLowering::Custom:
1847 Tmp1 = TLI.LowerOperation(Result, DAG);
1848 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001849 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001850 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001851 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001852
1853 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1854 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1855 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1856 case Expand: assert(0 && "Not possible");
1857 case Legal:
1858 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1859 break;
1860 case Promote:
1861 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1862 break;
1863 }
1864
1865 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1866
1867 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1868 default: assert(0 && "Operation not supported");
1869 case TargetLowering::Custom:
1870 Tmp1 = TLI.LowerOperation(Result, DAG);
1871 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001872 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001873 case TargetLowering::Legal: break;
1874 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001875 // If this target supports fabs/fneg natively, do this efficiently.
1876 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1877 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1878 // Get the sign bit of the RHS.
1879 MVT::ValueType IVT =
1880 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1881 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1882 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1883 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1884 // Get the absolute value of the result.
1885 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1886 // Select between the nabs and abs value based on the sign bit of
1887 // the input.
1888 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1889 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1890 AbsVal),
1891 AbsVal);
1892 Result = LegalizeOp(Result);
1893 break;
1894 }
1895
1896 // Otherwise, do bitwise ops!
1897
1898 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001899 const char *FnName;
1900 if (Node->getValueType(0) == MVT::f32) {
1901 FnName = "copysignf";
1902 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1903 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1904 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1905 } else {
1906 FnName = "copysign";
1907 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1908 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1909 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1910 }
1911 SDOperand Dummy;
1912 Result = ExpandLibCall(FnName, Node, Dummy);
1913 break;
1914 }
1915 break;
1916
Nate Begeman551bf3f2006-02-17 05:43:56 +00001917 case ISD::ADDC:
1918 case ISD::SUBC:
1919 Tmp1 = LegalizeOp(Node->getOperand(0));
1920 Tmp2 = LegalizeOp(Node->getOperand(1));
1921 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1922 // Since this produces two values, make sure to remember that we legalized
1923 // both of them.
1924 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1925 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1926 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001927
Nate Begeman551bf3f2006-02-17 05:43:56 +00001928 case ISD::ADDE:
1929 case ISD::SUBE:
1930 Tmp1 = LegalizeOp(Node->getOperand(0));
1931 Tmp2 = LegalizeOp(Node->getOperand(1));
1932 Tmp3 = LegalizeOp(Node->getOperand(2));
1933 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1934 // Since this produces two values, make sure to remember that we legalized
1935 // both of them.
1936 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1937 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1938 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001939
Nate Begeman419f8b62005-10-18 00:27:41 +00001940 case ISD::BUILD_PAIR: {
1941 MVT::ValueType PairTy = Node->getValueType(0);
1942 // TODO: handle the case where the Lo and Hi operands are not of legal type
1943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1944 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1945 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001946 case TargetLowering::Promote:
1947 case TargetLowering::Custom:
1948 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001949 case TargetLowering::Legal:
1950 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1951 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1952 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001953 case TargetLowering::Expand:
1954 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1955 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1956 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1957 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1958 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001959 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001960 break;
1961 }
1962 break;
1963 }
1964
Nate Begemanc105e192005-04-06 00:23:54 +00001965 case ISD::UREM:
1966 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001967 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001968 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1969 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001970
Nate Begemanc105e192005-04-06 00:23:54 +00001971 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001972 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1973 case TargetLowering::Custom:
1974 isCustom = true;
1975 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001976 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001977 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001978 if (isCustom) {
1979 Tmp1 = TLI.LowerOperation(Result, DAG);
1980 if (Tmp1.Val) Result = Tmp1;
1981 }
Nate Begemanc105e192005-04-06 00:23:54 +00001982 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001983 case TargetLowering::Expand:
1984 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001985 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001986 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001987 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001988 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1989 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1990 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1991 } else {
1992 // Floating point mod -> fmod libcall.
1993 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1994 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001995 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001996 }
1997 break;
1998 }
1999 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002000 case ISD::VAARG: {
2001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2002 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2003
Chris Lattner5c62f332006-01-28 07:42:08 +00002004 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002005 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2006 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002007 case TargetLowering::Custom:
2008 isCustom = true;
2009 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002010 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2012 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002013 Tmp1 = Result.getValue(1);
2014
2015 if (isCustom) {
2016 Tmp2 = TLI.LowerOperation(Result, DAG);
2017 if (Tmp2.Val) {
2018 Result = LegalizeOp(Tmp2);
2019 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2020 }
2021 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002022 break;
2023 case TargetLowering::Expand: {
2024 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2025 Node->getOperand(2));
2026 // Increment the pointer, VAList, to the next vaarg
2027 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2028 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2029 TLI.getPointerTy()));
2030 // Store the incremented VAList to the legalized pointer
2031 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2032 Node->getOperand(2));
2033 // Load the actual argument out of the pointer VAList
2034 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002035 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002036 Result = LegalizeOp(Result);
2037 break;
2038 }
2039 }
2040 // Since VAARG produces two values, make sure to remember that we
2041 // legalized both of them.
2042 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002043 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2044 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002045 }
2046
2047 case ISD::VACOPY:
2048 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2049 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2050 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2051
2052 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2053 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002054 case TargetLowering::Custom:
2055 isCustom = true;
2056 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002057 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002058 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2059 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002060 if (isCustom) {
2061 Tmp1 = TLI.LowerOperation(Result, DAG);
2062 if (Tmp1.Val) Result = Tmp1;
2063 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002064 break;
2065 case TargetLowering::Expand:
2066 // This defaults to loading a pointer from the input and storing it to the
2067 // output, returning the chain.
2068 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2069 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2070 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002071 break;
2072 }
2073 break;
2074
2075 case ISD::VAEND:
2076 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2077 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2078
2079 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2080 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002081 case TargetLowering::Custom:
2082 isCustom = true;
2083 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002084 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002085 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002086 if (isCustom) {
2087 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2088 if (Tmp1.Val) Result = Tmp1;
2089 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002090 break;
2091 case TargetLowering::Expand:
2092 Result = Tmp1; // Default to a no-op, return the chain
2093 break;
2094 }
2095 break;
2096
2097 case ISD::VASTART:
2098 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2099 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2100
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2102
Nate Begemanacc398c2006-01-25 18:21:52 +00002103 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2104 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002105 case TargetLowering::Legal: break;
2106 case TargetLowering::Custom:
2107 Tmp1 = TLI.LowerOperation(Result, DAG);
2108 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002109 break;
2110 }
2111 break;
2112
Nate Begeman35ef9132006-01-11 21:21:00 +00002113 case ISD::ROTL:
2114 case ISD::ROTR:
2115 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2116 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002117
2118 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2119 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002120 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002121 break;
2122
Nate Begemand88fc032006-01-14 03:14:10 +00002123 case ISD::BSWAP:
2124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2125 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002126 case TargetLowering::Custom:
2127 assert(0 && "Cannot custom legalize this yet!");
2128 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002129 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002130 break;
2131 case TargetLowering::Promote: {
2132 MVT::ValueType OVT = Tmp1.getValueType();
2133 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2134 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002135
Chris Lattner456a93a2006-01-28 07:39:30 +00002136 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2137 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2138 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2139 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2140 break;
2141 }
2142 case TargetLowering::Expand:
2143 Result = ExpandBSWAP(Tmp1);
2144 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002145 }
2146 break;
2147
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002148 case ISD::CTPOP:
2149 case ISD::CTTZ:
2150 case ISD::CTLZ:
2151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2152 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002153 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002154 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002155 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002156 break;
2157 case TargetLowering::Promote: {
2158 MVT::ValueType OVT = Tmp1.getValueType();
2159 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002160
2161 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002162 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2163 // Perform the larger operation, then subtract if needed.
2164 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002165 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002166 case ISD::CTPOP:
2167 Result = Tmp1;
2168 break;
2169 case ISD::CTTZ:
2170 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002171 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2172 DAG.getConstant(getSizeInBits(NVT), NVT),
2173 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002174 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002175 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2176 break;
2177 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002178 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002179 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2180 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002181 getSizeInBits(OVT), NVT));
2182 break;
2183 }
2184 break;
2185 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002186 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002187 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002188 break;
2189 }
2190 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002191
Chris Lattner2c8086f2005-04-02 05:00:07 +00002192 // Unary operators
2193 case ISD::FABS:
2194 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002195 case ISD::FSQRT:
2196 case ISD::FSIN:
2197 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002198 Tmp1 = LegalizeOp(Node->getOperand(0));
2199 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002200 case TargetLowering::Promote:
2201 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002202 isCustom = true;
2203 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002204 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002205 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002206 if (isCustom) {
2207 Tmp1 = TLI.LowerOperation(Result, DAG);
2208 if (Tmp1.Val) Result = Tmp1;
2209 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002210 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002211 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002212 switch (Node->getOpcode()) {
2213 default: assert(0 && "Unreachable!");
2214 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002215 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2216 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002217 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002218 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002219 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002220 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2221 MVT::ValueType VT = Node->getValueType(0);
2222 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002223 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002224 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2225 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002226 break;
2227 }
2228 case ISD::FSQRT:
2229 case ISD::FSIN:
2230 case ISD::FCOS: {
2231 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002232 const char *FnName = 0;
2233 switch(Node->getOpcode()) {
2234 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2235 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2236 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2237 default: assert(0 && "Unreachable!");
2238 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002239 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002240 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002241 break;
2242 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002243 }
2244 break;
2245 }
2246 break;
Chris Lattner35481892005-12-23 00:16:34 +00002247
2248 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002249 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002250 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002251 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002252 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2253 Node->getOperand(0).getValueType())) {
2254 default: assert(0 && "Unknown operation action!");
2255 case TargetLowering::Expand:
2256 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2257 break;
2258 case TargetLowering::Legal:
2259 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002260 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002261 break;
2262 }
2263 }
2264 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002265 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002266 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002267 case ISD::UINT_TO_FP: {
2268 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002269 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2270 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002271 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002272 Node->getOperand(0).getValueType())) {
2273 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002274 case TargetLowering::Custom:
2275 isCustom = true;
2276 // FALLTHROUGH
2277 case TargetLowering::Legal:
2278 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002279 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002280 if (isCustom) {
2281 Tmp1 = TLI.LowerOperation(Result, DAG);
2282 if (Tmp1.Val) Result = Tmp1;
2283 }
2284 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002285 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002286 Result = ExpandLegalINT_TO_FP(isSigned,
2287 LegalizeOp(Node->getOperand(0)),
2288 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002289 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002290 case TargetLowering::Promote:
2291 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2292 Node->getValueType(0),
2293 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002294 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002295 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002296 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002297 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002298 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2299 Node->getValueType(0), Node->getOperand(0));
2300 break;
2301 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002302 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002303 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002304 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2305 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002306 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002307 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2308 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002309 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002310 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2311 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002312 break;
2313 }
2314 break;
2315 }
2316 case ISD::TRUNCATE:
2317 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2318 case Legal:
2319 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002320 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002321 break;
2322 case Expand:
2323 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2324
2325 // Since the result is legal, we should just be able to truncate the low
2326 // part of the source.
2327 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2328 break;
2329 case Promote:
2330 Result = PromoteOp(Node->getOperand(0));
2331 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2332 break;
2333 }
2334 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002335
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002336 case ISD::FP_TO_SINT:
2337 case ISD::FP_TO_UINT:
2338 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2339 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002340 Tmp1 = LegalizeOp(Node->getOperand(0));
2341
Chris Lattner1618beb2005-07-29 00:11:56 +00002342 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2343 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002344 case TargetLowering::Custom:
2345 isCustom = true;
2346 // FALLTHROUGH
2347 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002348 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002349 if (isCustom) {
2350 Tmp1 = TLI.LowerOperation(Result, DAG);
2351 if (Tmp1.Val) Result = Tmp1;
2352 }
2353 break;
2354 case TargetLowering::Promote:
2355 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2356 Node->getOpcode() == ISD::FP_TO_SINT);
2357 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002358 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002359 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2360 SDOperand True, False;
2361 MVT::ValueType VT = Node->getOperand(0).getValueType();
2362 MVT::ValueType NVT = Node->getValueType(0);
2363 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2364 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2365 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2366 Node->getOperand(0), Tmp2, ISD::SETLT);
2367 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2368 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002369 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002370 Tmp2));
2371 False = DAG.getNode(ISD::XOR, NVT, False,
2372 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002373 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2374 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002375 } else {
2376 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2377 }
2378 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002379 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002380 break;
2381 case Expand:
2382 assert(0 && "Shouldn't need to expand other operators here!");
2383 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002384 Tmp1 = PromoteOp(Node->getOperand(0));
2385 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2386 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002387 break;
2388 }
2389 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002390
Chris Lattner13c78e22005-09-02 00:18:10 +00002391 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002392 case ISD::ZERO_EXTEND:
2393 case ISD::SIGN_EXTEND:
2394 case ISD::FP_EXTEND:
2395 case ISD::FP_ROUND:
2396 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002397 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002398 case Legal:
2399 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002400 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002401 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002402 case Promote:
2403 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002404 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002405 Tmp1 = PromoteOp(Node->getOperand(0));
2406 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002407 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002408 case ISD::ZERO_EXTEND:
2409 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002410 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002411 Result = DAG.getZeroExtendInReg(Result,
2412 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002413 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002414 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002415 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002416 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002417 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002418 Result,
2419 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002420 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002421 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002422 Result = PromoteOp(Node->getOperand(0));
2423 if (Result.getValueType() != Op.getValueType())
2424 // Dynamically dead while we have only 2 FP types.
2425 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2426 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002427 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002428 Result = PromoteOp(Node->getOperand(0));
2429 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2430 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002431 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002432 }
2433 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002434 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002435 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002436 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002437 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002438
2439 // If this operation is not supported, convert it to a shl/shr or load/store
2440 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002441 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2442 default: assert(0 && "This action not supported for this op yet!");
2443 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002445 break;
2446 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002447 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002448 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002449 // NOTE: we could fall back on load/store here too for targets without
2450 // SAR. However, it is doubtful that any exist.
2451 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2452 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002453 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002454 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2455 Node->getOperand(0), ShiftCst);
2456 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2457 Result, ShiftCst);
2458 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2459 // The only way we can lower this is to turn it into a STORETRUNC,
2460 // EXTLOAD pair, targetting a temporary location (a stack slot).
2461
2462 // NOTE: there is a choice here between constantly creating new stack
2463 // slots and always reusing the same one. We currently always create
2464 // new ones, as reuse may inhibit scheduling.
2465 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2466 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2467 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2468 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002469 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002470 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2471 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2472 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002473 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002474 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002475 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2476 Result, StackSlot, DAG.getSrcValue(NULL),
2477 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002478 } else {
2479 assert(0 && "Unknown op");
2480 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002481 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002482 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002483 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002484 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002485 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002486
2487 // Make sure that the generated code is itself legal.
2488 if (Result != Op)
2489 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002490
Chris Lattner45982da2005-05-12 16:53:42 +00002491 // Note that LegalizeOp may be reentered even from single-use nodes, which
2492 // means that we always must cache transformed nodes.
2493 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002494 return Result;
2495}
2496
Chris Lattner8b6fa222005-01-15 22:16:26 +00002497/// PromoteOp - Given an operation that produces a value in an invalid type,
2498/// promote it to compute the value into a larger type. The produced value will
2499/// have the correct bits for the low portion of the register, but no guarantee
2500/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002501SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2502 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002503 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002504 assert(getTypeAction(VT) == Promote &&
2505 "Caller should expand or legalize operands that are not promotable!");
2506 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2507 "Cannot promote to smaller type!");
2508
Chris Lattner03c85462005-01-15 05:21:40 +00002509 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002510 SDOperand Result;
2511 SDNode *Node = Op.Val;
2512
Chris Lattner6fdcb252005-09-02 20:32:45 +00002513 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2514 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002515
Chris Lattner03c85462005-01-15 05:21:40 +00002516 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002517 case ISD::CopyFromReg:
2518 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002519 default:
2520 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2521 assert(0 && "Do not know how to promote this operator!");
2522 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002523 case ISD::UNDEF:
2524 Result = DAG.getNode(ISD::UNDEF, NVT);
2525 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002526 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002527 if (VT != MVT::i1)
2528 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2529 else
2530 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002531 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2532 break;
2533 case ISD::ConstantFP:
2534 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2535 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2536 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002537
Chris Lattner82fbfb62005-01-18 02:59:52 +00002538 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002539 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002540 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2541 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002542 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002543
2544 case ISD::TRUNCATE:
2545 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2546 case Legal:
2547 Result = LegalizeOp(Node->getOperand(0));
2548 assert(Result.getValueType() >= NVT &&
2549 "This truncation doesn't make sense!");
2550 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2551 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2552 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002553 case Promote:
2554 // The truncation is not required, because we don't guarantee anything
2555 // about high bits anyway.
2556 Result = PromoteOp(Node->getOperand(0));
2557 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002558 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002559 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2560 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002561 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002562 }
2563 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002564 case ISD::SIGN_EXTEND:
2565 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002566 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002567 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2568 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2569 case Legal:
2570 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002571 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002572 break;
2573 case Promote:
2574 // Promote the reg if it's smaller.
2575 Result = PromoteOp(Node->getOperand(0));
2576 // The high bits are not guaranteed to be anything. Insert an extend.
2577 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002578 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002579 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002580 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002581 Result = DAG.getZeroExtendInReg(Result,
2582 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002583 break;
2584 }
2585 break;
Chris Lattner35481892005-12-23 00:16:34 +00002586 case ISD::BIT_CONVERT:
2587 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2588 Result = PromoteOp(Result);
2589 break;
2590
Chris Lattner8b6fa222005-01-15 22:16:26 +00002591 case ISD::FP_EXTEND:
2592 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2593 case ISD::FP_ROUND:
2594 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2595 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2596 case Promote: assert(0 && "Unreachable with 2 FP types!");
2597 case Legal:
2598 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002599 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002600 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002601 break;
2602 }
2603 break;
2604
2605 case ISD::SINT_TO_FP:
2606 case ISD::UINT_TO_FP:
2607 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2608 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002609 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002610 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002611 break;
2612
2613 case Promote:
2614 Result = PromoteOp(Node->getOperand(0));
2615 if (Node->getOpcode() == ISD::SINT_TO_FP)
2616 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002617 Result,
2618 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002619 else
Chris Lattner23993562005-04-13 02:38:47 +00002620 Result = DAG.getZeroExtendInReg(Result,
2621 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002622 // No extra round required here.
2623 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002624 break;
2625 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002626 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2627 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002628 // Round if we cannot tolerate excess precision.
2629 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002630 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2631 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002632 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002633 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002634 break;
2635
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002636 case ISD::SIGN_EXTEND_INREG:
2637 Result = PromoteOp(Node->getOperand(0));
2638 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2639 Node->getOperand(1));
2640 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002641 case ISD::FP_TO_SINT:
2642 case ISD::FP_TO_UINT:
2643 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2644 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002645 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002646 break;
2647 case Promote:
2648 // The input result is prerounded, so we don't have to do anything
2649 // special.
2650 Tmp1 = PromoteOp(Node->getOperand(0));
2651 break;
2652 case Expand:
2653 assert(0 && "not implemented");
2654 }
Nate Begemand2558e32005-08-14 01:20:53 +00002655 // If we're promoting a UINT to a larger size, check to see if the new node
2656 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2657 // we can use that instead. This allows us to generate better code for
2658 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2659 // legal, such as PowerPC.
2660 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002661 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002662 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2663 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002664 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2665 } else {
2666 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2667 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002668 break;
2669
Chris Lattner2c8086f2005-04-02 05:00:07 +00002670 case ISD::FABS:
2671 case ISD::FNEG:
2672 Tmp1 = PromoteOp(Node->getOperand(0));
2673 assert(Tmp1.getValueType() == NVT);
2674 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2675 // NOTE: we do not have to do any extra rounding here for
2676 // NoExcessFPPrecision, because we know the input will have the appropriate
2677 // precision, and these operations don't modify precision at all.
2678 break;
2679
Chris Lattnerda6ba872005-04-28 21:44:33 +00002680 case ISD::FSQRT:
2681 case ISD::FSIN:
2682 case ISD::FCOS:
2683 Tmp1 = PromoteOp(Node->getOperand(0));
2684 assert(Tmp1.getValueType() == NVT);
2685 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002686 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002687 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2688 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002689 break;
2690
Chris Lattner03c85462005-01-15 05:21:40 +00002691 case ISD::AND:
2692 case ISD::OR:
2693 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002694 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002695 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002696 case ISD::MUL:
2697 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002698 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002699 // that too is okay if they are integer operations.
2700 Tmp1 = PromoteOp(Node->getOperand(0));
2701 Tmp2 = PromoteOp(Node->getOperand(1));
2702 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2703 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002704 break;
2705 case ISD::FADD:
2706 case ISD::FSUB:
2707 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002708 Tmp1 = PromoteOp(Node->getOperand(0));
2709 Tmp2 = PromoteOp(Node->getOperand(1));
2710 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2711 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2712
2713 // Floating point operations will give excess precision that we may not be
2714 // able to tolerate. If we DO allow excess precision, just leave it,
2715 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002716 // FIXME: Why would we need to round FP ops more than integer ones?
2717 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002718 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002719 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2720 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002721 break;
2722
Chris Lattner8b6fa222005-01-15 22:16:26 +00002723 case ISD::SDIV:
2724 case ISD::SREM:
2725 // These operators require that their input be sign extended.
2726 Tmp1 = PromoteOp(Node->getOperand(0));
2727 Tmp2 = PromoteOp(Node->getOperand(1));
2728 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002729 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2730 DAG.getValueType(VT));
2731 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2732 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002733 }
2734 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2735
2736 // Perform FP_ROUND: this is probably overly pessimistic.
2737 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002738 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2739 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002740 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002741 case ISD::FDIV:
2742 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002743 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002744 // These operators require that their input be fp extended.
2745 Tmp1 = PromoteOp(Node->getOperand(0));
2746 Tmp2 = PromoteOp(Node->getOperand(1));
2747 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2748
2749 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002750 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002751 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2752 DAG.getValueType(VT));
2753 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002754
2755 case ISD::UDIV:
2756 case ISD::UREM:
2757 // These operators require that their input be zero extended.
2758 Tmp1 = PromoteOp(Node->getOperand(0));
2759 Tmp2 = PromoteOp(Node->getOperand(1));
2760 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002761 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2762 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002763 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2764 break;
2765
2766 case ISD::SHL:
2767 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002768 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002769 break;
2770 case ISD::SRA:
2771 // The input value must be properly sign extended.
2772 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002773 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2774 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002775 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002776 break;
2777 case ISD::SRL:
2778 // The input value must be properly zero extended.
2779 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002780 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002781 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002782 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002783
2784 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002785 Tmp1 = Node->getOperand(0); // Get the chain.
2786 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002787 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2788 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2789 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2790 } else {
2791 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2792 Node->getOperand(2));
2793 // Increment the pointer, VAList, to the next vaarg
2794 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2795 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2796 TLI.getPointerTy()));
2797 // Store the incremented VAList to the legalized pointer
2798 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2799 Node->getOperand(2));
2800 // Load the actual argument out of the pointer VAList
2801 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2802 DAG.getSrcValue(0), VT);
2803 }
2804 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002805 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002806 break;
2807
Chris Lattner03c85462005-01-15 05:21:40 +00002808 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002809 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2810 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002811 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002812 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002813 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002814 case ISD::SEXTLOAD:
2815 case ISD::ZEXTLOAD:
2816 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002817 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2818 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002819 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002820 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002821 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002822 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002823 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002824 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2825 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002826 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002827 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002828 case ISD::SELECT_CC:
2829 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2830 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2831 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002832 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002833 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002834 case ISD::BSWAP:
2835 Tmp1 = Node->getOperand(0);
2836 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2837 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2838 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2839 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2840 TLI.getShiftAmountTy()));
2841 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002842 case ISD::CTPOP:
2843 case ISD::CTTZ:
2844 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002845 // Zero extend the argument
2846 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002847 // Perform the larger operation, then subtract if needed.
2848 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002849 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002850 case ISD::CTPOP:
2851 Result = Tmp1;
2852 break;
2853 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002854 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002855 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002856 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002857 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002858 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002859 break;
2860 case ISD::CTLZ:
2861 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002862 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2863 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002864 getSizeInBits(VT), NVT));
2865 break;
2866 }
2867 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002868 }
2869
2870 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002871
2872 // Make sure the result is itself legal.
2873 Result = LegalizeOp(Result);
2874
2875 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002876 AddPromotedOperand(Op, Result);
2877 return Result;
2878}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002879
Nate Begeman750ac1b2006-02-01 07:19:44 +00002880/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2881/// with condition CC on the current target. This usually involves legalizing
2882/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2883/// there may be no choice but to create a new SetCC node to represent the
2884/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2885/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2886void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2887 SDOperand &RHS,
2888 SDOperand &CC) {
2889 SDOperand Tmp1, Tmp2, Result;
2890
2891 switch (getTypeAction(LHS.getValueType())) {
2892 case Legal:
2893 Tmp1 = LegalizeOp(LHS); // LHS
2894 Tmp2 = LegalizeOp(RHS); // RHS
2895 break;
2896 case Promote:
2897 Tmp1 = PromoteOp(LHS); // LHS
2898 Tmp2 = PromoteOp(RHS); // RHS
2899
2900 // If this is an FP compare, the operands have already been extended.
2901 if (MVT::isInteger(LHS.getValueType())) {
2902 MVT::ValueType VT = LHS.getValueType();
2903 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2904
2905 // Otherwise, we have to insert explicit sign or zero extends. Note
2906 // that we could insert sign extends for ALL conditions, but zero extend
2907 // is cheaper on many machines (an AND instead of two shifts), so prefer
2908 // it.
2909 switch (cast<CondCodeSDNode>(CC)->get()) {
2910 default: assert(0 && "Unknown integer comparison!");
2911 case ISD::SETEQ:
2912 case ISD::SETNE:
2913 case ISD::SETUGE:
2914 case ISD::SETUGT:
2915 case ISD::SETULE:
2916 case ISD::SETULT:
2917 // ALL of these operations will work if we either sign or zero extend
2918 // the operands (including the unsigned comparisons!). Zero extend is
2919 // usually a simpler/cheaper operation, so prefer it.
2920 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2921 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2922 break;
2923 case ISD::SETGE:
2924 case ISD::SETGT:
2925 case ISD::SETLT:
2926 case ISD::SETLE:
2927 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2928 DAG.getValueType(VT));
2929 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2930 DAG.getValueType(VT));
2931 break;
2932 }
2933 }
2934 break;
2935 case Expand:
2936 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2937 ExpandOp(LHS, LHSLo, LHSHi);
2938 ExpandOp(RHS, RHSLo, RHSHi);
2939 switch (cast<CondCodeSDNode>(CC)->get()) {
2940 case ISD::SETEQ:
2941 case ISD::SETNE:
2942 if (RHSLo == RHSHi)
2943 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2944 if (RHSCST->isAllOnesValue()) {
2945 // Comparison to -1.
2946 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2947 Tmp2 = RHSLo;
2948 break;
2949 }
2950
2951 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2952 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2953 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2954 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2955 break;
2956 default:
2957 // If this is a comparison of the sign bit, just look at the top part.
2958 // X > -1, x < 0
2959 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2960 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2961 CST->getValue() == 0) || // X < 0
2962 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2963 CST->isAllOnesValue())) { // X > -1
2964 Tmp1 = LHSHi;
2965 Tmp2 = RHSHi;
2966 break;
2967 }
2968
2969 // FIXME: This generated code sucks.
2970 ISD::CondCode LowCC;
2971 switch (cast<CondCodeSDNode>(CC)->get()) {
2972 default: assert(0 && "Unknown integer setcc!");
2973 case ISD::SETLT:
2974 case ISD::SETULT: LowCC = ISD::SETULT; break;
2975 case ISD::SETGT:
2976 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2977 case ISD::SETLE:
2978 case ISD::SETULE: LowCC = ISD::SETULE; break;
2979 case ISD::SETGE:
2980 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2981 }
2982
2983 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2984 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2985 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2986
2987 // NOTE: on targets without efficient SELECT of bools, we can always use
2988 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2989 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2990 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2991 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2992 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2993 Result, Tmp1, Tmp2));
2994 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002995 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002996 }
2997 }
2998 LHS = Tmp1;
2999 RHS = Tmp2;
3000}
3001
Chris Lattner35481892005-12-23 00:16:34 +00003002/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003003/// The resultant code need not be legal. Note that SrcOp is the input operand
3004/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003005SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3006 SDOperand SrcOp) {
3007 // Create the stack frame object.
3008 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3009 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00003010 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00003011 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3012
3013 // Emit a store to the stack slot.
3014 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003015 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003016 // Result is a load from the stack slot.
3017 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3018}
3019
Chris Lattner5b359c62005-04-02 04:00:59 +00003020void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3021 SDOperand Op, SDOperand Amt,
3022 SDOperand &Lo, SDOperand &Hi) {
3023 // Expand the subcomponents.
3024 SDOperand LHSL, LHSH;
3025 ExpandOp(Op, LHSL, LHSH);
3026
3027 std::vector<SDOperand> Ops;
3028 Ops.push_back(LHSL);
3029 Ops.push_back(LHSH);
3030 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003031 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003032 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003033 Hi = Lo.getValue(1);
3034}
3035
3036
Chris Lattnere34b3962005-01-19 04:19:40 +00003037/// ExpandShift - Try to find a clever way to expand this shift operation out to
3038/// smaller elements. If we can't find a way that is more efficient than a
3039/// libcall on this target, return false. Otherwise, return true with the
3040/// low-parts expanded into Lo and Hi.
3041bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3042 SDOperand &Lo, SDOperand &Hi) {
3043 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3044 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003045
Chris Lattnere34b3962005-01-19 04:19:40 +00003046 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003047 SDOperand ShAmt = LegalizeOp(Amt);
3048 MVT::ValueType ShTy = ShAmt.getValueType();
3049 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3050 unsigned NVTBits = MVT::getSizeInBits(NVT);
3051
3052 // Handle the case when Amt is an immediate. Other cases are currently broken
3053 // and are disabled.
3054 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3055 unsigned Cst = CN->getValue();
3056 // Expand the incoming operand to be shifted, so that we have its parts
3057 SDOperand InL, InH;
3058 ExpandOp(Op, InL, InH);
3059 switch(Opc) {
3060 case ISD::SHL:
3061 if (Cst > VTBits) {
3062 Lo = DAG.getConstant(0, NVT);
3063 Hi = DAG.getConstant(0, NVT);
3064 } else if (Cst > NVTBits) {
3065 Lo = DAG.getConstant(0, NVT);
3066 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003067 } else if (Cst == NVTBits) {
3068 Lo = DAG.getConstant(0, NVT);
3069 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003070 } else {
3071 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3072 Hi = DAG.getNode(ISD::OR, NVT,
3073 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3074 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3075 }
3076 return true;
3077 case ISD::SRL:
3078 if (Cst > VTBits) {
3079 Lo = DAG.getConstant(0, NVT);
3080 Hi = DAG.getConstant(0, NVT);
3081 } else if (Cst > NVTBits) {
3082 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3083 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003084 } else if (Cst == NVTBits) {
3085 Lo = InH;
3086 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003087 } else {
3088 Lo = DAG.getNode(ISD::OR, NVT,
3089 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3090 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3091 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3092 }
3093 return true;
3094 case ISD::SRA:
3095 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003096 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003097 DAG.getConstant(NVTBits-1, ShTy));
3098 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003099 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003100 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003101 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003102 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003103 } else if (Cst == NVTBits) {
3104 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003105 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003106 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003107 } else {
3108 Lo = DAG.getNode(ISD::OR, NVT,
3109 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3110 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3111 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3112 }
3113 return true;
3114 }
3115 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003116 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003117}
Chris Lattner77e77a62005-01-21 06:05:23 +00003118
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003119
Chris Lattner77e77a62005-01-21 06:05:23 +00003120// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3121// does not fit into a register, return the lo part and set the hi part to the
3122// by-reg argument. If it does fit into a single register, return the result
3123// and leave the Hi part unset.
3124SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3125 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003126 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3127 // The input chain to this libcall is the entry node of the function.
3128 // Legalizing the call will automatically add the previous call to the
3129 // dependence.
3130 SDOperand InChain = DAG.getEntryNode();
3131
Chris Lattner77e77a62005-01-21 06:05:23 +00003132 TargetLowering::ArgListTy Args;
3133 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3134 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3135 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3136 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3137 }
3138 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003139
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003140 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003141 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003142 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003143 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3144 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003145
Chris Lattner6831a812006-02-13 09:18:02 +00003146 // Legalize the call sequence, starting with the chain. This will advance
3147 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3148 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3149 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003150 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003151 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003152 default: assert(0 && "Unknown thing");
3153 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003154 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003155 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003156 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003157 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003158 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003159 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003160 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003161}
3162
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003163
Chris Lattner77e77a62005-01-21 06:05:23 +00003164/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3165/// destination type is legal.
3166SDOperand SelectionDAGLegalize::
3167ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003168 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003169 assert(getTypeAction(Source.getValueType()) == Expand &&
3170 "This is not an expansion!");
3171 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3172
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003173 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003174 assert(Source.getValueType() == MVT::i64 &&
3175 "This only works for 64-bit -> FP");
3176 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3177 // incoming integer is set. To handle this, we dynamically test to see if
3178 // it is set, and, if so, add a fudge factor.
3179 SDOperand Lo, Hi;
3180 ExpandOp(Source, Lo, Hi);
3181
Chris Lattner66de05b2005-05-13 04:45:13 +00003182 // If this is unsigned, and not supported, first perform the conversion to
3183 // signed, then adjust the result if the sign bit is set.
3184 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3185 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3186
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003187 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3188 DAG.getConstant(0, Hi.getValueType()),
3189 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003190 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3191 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3192 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003193 uint64_t FF = 0x5f800000ULL;
3194 if (TLI.isLittleEndian()) FF <<= 32;
3195 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003196
Chris Lattner5839bf22005-08-26 17:15:30 +00003197 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003198 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3199 SDOperand FudgeInReg;
3200 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003201 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3202 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003203 else {
3204 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003205 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3206 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003207 }
Chris Lattner473a9902005-09-29 06:44:39 +00003208 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003209 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003210
Chris Lattnera88a2602005-05-14 05:33:54 +00003211 // Check to see if the target has a custom way to lower this. If so, use it.
3212 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3213 default: assert(0 && "This action not implemented for this operation!");
3214 case TargetLowering::Legal:
3215 case TargetLowering::Expand:
3216 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003217 case TargetLowering::Custom: {
3218 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3219 Source), DAG);
3220 if (NV.Val)
3221 return LegalizeOp(NV);
3222 break; // The target decided this was legal after all
3223 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003224 }
3225
Chris Lattner13689e22005-05-12 07:00:44 +00003226 // Expand the source, then glue it back together for the call. We must expand
3227 // the source in case it is shared (this pass of legalize must traverse it).
3228 SDOperand SrcLo, SrcHi;
3229 ExpandOp(Source, SrcLo, SrcHi);
3230 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3231
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003232 const char *FnName = 0;
3233 if (DestTy == MVT::f32)
3234 FnName = "__floatdisf";
3235 else {
3236 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3237 FnName = "__floatdidf";
3238 }
Chris Lattner6831a812006-02-13 09:18:02 +00003239
3240 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3241 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003242 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003243}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003244
Chris Lattner22cde6a2006-01-28 08:25:58 +00003245/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3246/// INT_TO_FP operation of the specified operand when the target requests that
3247/// we expand it. At this point, we know that the result and operand types are
3248/// legal for the target.
3249SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3250 SDOperand Op0,
3251 MVT::ValueType DestVT) {
3252 if (Op0.getValueType() == MVT::i32) {
3253 // simple 32-bit [signed|unsigned] integer to float/double expansion
3254
3255 // get the stack frame index of a 8 byte buffer
3256 MachineFunction &MF = DAG.getMachineFunction();
3257 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3258 // get address of 8 byte buffer
3259 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3260 // word offset constant for Hi/Lo address computation
3261 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3262 // set up Hi and Lo (into buffer) address based on endian
3263 SDOperand Hi, Lo;
3264 if (TLI.isLittleEndian()) {
3265 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3266 Lo = StackSlot;
3267 } else {
3268 Hi = StackSlot;
3269 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3270 }
3271 // if signed map to unsigned space
3272 SDOperand Op0Mapped;
3273 if (isSigned) {
3274 // constant used to invert sign bit (signed to unsigned mapping)
3275 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3276 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3277 } else {
3278 Op0Mapped = Op0;
3279 }
3280 // store the lo of the constructed double - based on integer input
3281 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3282 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3283 // initial hi portion of constructed double
3284 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3285 // store the hi of the constructed double - biased exponent
3286 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3287 InitialHi, Hi, DAG.getSrcValue(NULL));
3288 // load the constructed double
3289 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3290 DAG.getSrcValue(NULL));
3291 // FP constant to bias correct the final result
3292 SDOperand Bias = DAG.getConstantFP(isSigned ?
3293 BitsToDouble(0x4330000080000000ULL)
3294 : BitsToDouble(0x4330000000000000ULL),
3295 MVT::f64);
3296 // subtract the bias
3297 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3298 // final result
3299 SDOperand Result;
3300 // handle final rounding
3301 if (DestVT == MVT::f64) {
3302 // do nothing
3303 Result = Sub;
3304 } else {
3305 // if f32 then cast to f32
3306 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3307 }
3308 return Result;
3309 }
3310 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3311 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3312
3313 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3314 DAG.getConstant(0, Op0.getValueType()),
3315 ISD::SETLT);
3316 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3317 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3318 SignSet, Four, Zero);
3319
3320 // If the sign bit of the integer is set, the large number will be treated
3321 // as a negative number. To counteract this, the dynamic code adds an
3322 // offset depending on the data type.
3323 uint64_t FF;
3324 switch (Op0.getValueType()) {
3325 default: assert(0 && "Unsupported integer type!");
3326 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3327 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3328 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3329 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3330 }
3331 if (TLI.isLittleEndian()) FF <<= 32;
3332 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3333
3334 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3335 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3336 SDOperand FudgeInReg;
3337 if (DestVT == MVT::f32)
3338 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3339 DAG.getSrcValue(NULL));
3340 else {
3341 assert(DestVT == MVT::f64 && "Unexpected conversion");
3342 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3343 DAG.getEntryNode(), CPIdx,
3344 DAG.getSrcValue(NULL), MVT::f32));
3345 }
3346
3347 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3348}
3349
3350/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3351/// *INT_TO_FP operation of the specified operand when the target requests that
3352/// we promote it. At this point, we know that the result and operand types are
3353/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3354/// operation that takes a larger input.
3355SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3356 MVT::ValueType DestVT,
3357 bool isSigned) {
3358 // First step, figure out the appropriate *INT_TO_FP operation to use.
3359 MVT::ValueType NewInTy = LegalOp.getValueType();
3360
3361 unsigned OpToUse = 0;
3362
3363 // Scan for the appropriate larger type to use.
3364 while (1) {
3365 NewInTy = (MVT::ValueType)(NewInTy+1);
3366 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3367
3368 // If the target supports SINT_TO_FP of this type, use it.
3369 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3370 default: break;
3371 case TargetLowering::Legal:
3372 if (!TLI.isTypeLegal(NewInTy))
3373 break; // Can't use this datatype.
3374 // FALL THROUGH.
3375 case TargetLowering::Custom:
3376 OpToUse = ISD::SINT_TO_FP;
3377 break;
3378 }
3379 if (OpToUse) break;
3380 if (isSigned) continue;
3381
3382 // If the target supports UINT_TO_FP of this type, use it.
3383 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3384 default: break;
3385 case TargetLowering::Legal:
3386 if (!TLI.isTypeLegal(NewInTy))
3387 break; // Can't use this datatype.
3388 // FALL THROUGH.
3389 case TargetLowering::Custom:
3390 OpToUse = ISD::UINT_TO_FP;
3391 break;
3392 }
3393 if (OpToUse) break;
3394
3395 // Otherwise, try a larger type.
3396 }
3397
3398 // Okay, we found the operation and type to use. Zero extend our input to the
3399 // desired type then run the operation on it.
3400 return DAG.getNode(OpToUse, DestVT,
3401 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3402 NewInTy, LegalOp));
3403}
3404
3405/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3406/// FP_TO_*INT operation of the specified operand when the target requests that
3407/// we promote it. At this point, we know that the result and operand types are
3408/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3409/// operation that returns a larger result.
3410SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3411 MVT::ValueType DestVT,
3412 bool isSigned) {
3413 // First step, figure out the appropriate FP_TO*INT operation to use.
3414 MVT::ValueType NewOutTy = DestVT;
3415
3416 unsigned OpToUse = 0;
3417
3418 // Scan for the appropriate larger type to use.
3419 while (1) {
3420 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3421 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3422
3423 // If the target supports FP_TO_SINT returning this type, use it.
3424 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3425 default: break;
3426 case TargetLowering::Legal:
3427 if (!TLI.isTypeLegal(NewOutTy))
3428 break; // Can't use this datatype.
3429 // FALL THROUGH.
3430 case TargetLowering::Custom:
3431 OpToUse = ISD::FP_TO_SINT;
3432 break;
3433 }
3434 if (OpToUse) break;
3435
3436 // If the target supports FP_TO_UINT of this type, use it.
3437 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3438 default: break;
3439 case TargetLowering::Legal:
3440 if (!TLI.isTypeLegal(NewOutTy))
3441 break; // Can't use this datatype.
3442 // FALL THROUGH.
3443 case TargetLowering::Custom:
3444 OpToUse = ISD::FP_TO_UINT;
3445 break;
3446 }
3447 if (OpToUse) break;
3448
3449 // Otherwise, try a larger type.
3450 }
3451
3452 // Okay, we found the operation and type to use. Truncate the result of the
3453 // extended FP_TO_*INT operation to the desired size.
3454 return DAG.getNode(ISD::TRUNCATE, DestVT,
3455 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3456}
3457
3458/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3459///
3460SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3461 MVT::ValueType VT = Op.getValueType();
3462 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3463 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3464 switch (VT) {
3465 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3466 case MVT::i16:
3467 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3468 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3469 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3470 case MVT::i32:
3471 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3472 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3473 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3474 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3475 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3476 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3477 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3478 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3479 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3480 case MVT::i64:
3481 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3482 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3483 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3484 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3485 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3486 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3487 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3488 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3489 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3490 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3491 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3492 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3493 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3494 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3495 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3496 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3497 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3498 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3499 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3500 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3501 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3502 }
3503}
3504
3505/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3506///
3507SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3508 switch (Opc) {
3509 default: assert(0 && "Cannot expand this yet!");
3510 case ISD::CTPOP: {
3511 static const uint64_t mask[6] = {
3512 0x5555555555555555ULL, 0x3333333333333333ULL,
3513 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3514 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3515 };
3516 MVT::ValueType VT = Op.getValueType();
3517 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3518 unsigned len = getSizeInBits(VT);
3519 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3520 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3521 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3522 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3523 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3524 DAG.getNode(ISD::AND, VT,
3525 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3526 }
3527 return Op;
3528 }
3529 case ISD::CTLZ: {
3530 // for now, we do this:
3531 // x = x | (x >> 1);
3532 // x = x | (x >> 2);
3533 // ...
3534 // x = x | (x >>16);
3535 // x = x | (x >>32); // for 64-bit input
3536 // return popcount(~x);
3537 //
3538 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3539 MVT::ValueType VT = Op.getValueType();
3540 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3541 unsigned len = getSizeInBits(VT);
3542 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3543 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3544 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3545 }
3546 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3547 return DAG.getNode(ISD::CTPOP, VT, Op);
3548 }
3549 case ISD::CTTZ: {
3550 // for now, we use: { return popcount(~x & (x - 1)); }
3551 // unless the target has ctlz but not ctpop, in which case we use:
3552 // { return 32 - nlz(~x & (x-1)); }
3553 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3554 MVT::ValueType VT = Op.getValueType();
3555 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3556 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3557 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3558 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3559 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3560 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3561 TLI.isOperationLegal(ISD::CTLZ, VT))
3562 return DAG.getNode(ISD::SUB, VT,
3563 DAG.getConstant(getSizeInBits(VT), VT),
3564 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3565 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3566 }
3567 }
3568}
Chris Lattnere34b3962005-01-19 04:19:40 +00003569
3570
Chris Lattner3e928bb2005-01-07 07:47:09 +00003571/// ExpandOp - Expand the specified SDOperand into its two component pieces
3572/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3573/// LegalizeNodes map is filled in for any results that are not expanded, the
3574/// ExpandedNodes map is filled in for any results that are expanded, and the
3575/// Lo/Hi values are returned.
3576void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3577 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003578 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003579 SDNode *Node = Op.Val;
3580 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003581 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3582 "Cannot expand FP values!");
3583 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003584 "Cannot expand to FP value or to larger int value!");
3585
Chris Lattner6fdcb252005-09-02 20:32:45 +00003586 // See if we already expanded it.
3587 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3588 = ExpandedNodes.find(Op);
3589 if (I != ExpandedNodes.end()) {
3590 Lo = I->second.first;
3591 Hi = I->second.second;
3592 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003593 }
3594
Chris Lattner3e928bb2005-01-07 07:47:09 +00003595 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003596 case ISD::CopyFromReg:
3597 assert(0 && "CopyFromReg must be legal!");
3598 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003599 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3600 assert(0 && "Do not know how to expand this operator!");
3601 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003602 case ISD::UNDEF:
3603 Lo = DAG.getNode(ISD::UNDEF, NVT);
3604 Hi = DAG.getNode(ISD::UNDEF, NVT);
3605 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003606 case ISD::Constant: {
3607 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3608 Lo = DAG.getConstant(Cst, NVT);
3609 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3610 break;
3611 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003612 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003613 // Return the operands.
3614 Lo = Node->getOperand(0);
3615 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003616 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003617
3618 case ISD::SIGN_EXTEND_INREG:
3619 ExpandOp(Node->getOperand(0), Lo, Hi);
3620 // Sign extend the lo-part.
3621 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3622 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3623 TLI.getShiftAmountTy()));
3624 // sext_inreg the low part if needed.
3625 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3626 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003627
Nate Begemand88fc032006-01-14 03:14:10 +00003628 case ISD::BSWAP: {
3629 ExpandOp(Node->getOperand(0), Lo, Hi);
3630 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3631 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3632 Lo = TempLo;
3633 break;
3634 }
3635
Chris Lattneredb1add2005-05-11 04:51:16 +00003636 case ISD::CTPOP:
3637 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003638 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3639 DAG.getNode(ISD::CTPOP, NVT, Lo),
3640 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003641 Hi = DAG.getConstant(0, NVT);
3642 break;
3643
Chris Lattner39a8f332005-05-12 19:05:01 +00003644 case ISD::CTLZ: {
3645 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003646 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003647 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3648 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003649 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3650 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003651 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3652 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3653
3654 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3655 Hi = DAG.getConstant(0, NVT);
3656 break;
3657 }
3658
3659 case ISD::CTTZ: {
3660 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003661 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003662 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3663 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003664 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3665 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003666 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3667 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3668
3669 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3670 Hi = DAG.getConstant(0, NVT);
3671 break;
3672 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003673
Nate Begemanacc398c2006-01-25 18:21:52 +00003674 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003675 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3676 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003677 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3678 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3679
3680 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003681 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003682 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3683 if (!TLI.isLittleEndian())
3684 std::swap(Lo, Hi);
3685 break;
3686 }
3687
Chris Lattner3e928bb2005-01-07 07:47:09 +00003688 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003689 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3690 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003691 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003692
3693 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003694 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003695 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3696 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003697 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003698 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003699
3700 // Build a factor node to remember that this load is independent of the
3701 // other one.
3702 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3703 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003704
Chris Lattner3e928bb2005-01-07 07:47:09 +00003705 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003706 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003707 if (!TLI.isLittleEndian())
3708 std::swap(Lo, Hi);
3709 break;
3710 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003711 case ISD::AND:
3712 case ISD::OR:
3713 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3714 SDOperand LL, LH, RL, RH;
3715 ExpandOp(Node->getOperand(0), LL, LH);
3716 ExpandOp(Node->getOperand(1), RL, RH);
3717 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3718 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3719 break;
3720 }
3721 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003722 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003723 ExpandOp(Node->getOperand(1), LL, LH);
3724 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003725 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3726 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003727 break;
3728 }
Nate Begeman9373a812005-08-10 20:51:12 +00003729 case ISD::SELECT_CC: {
3730 SDOperand TL, TH, FL, FH;
3731 ExpandOp(Node->getOperand(2), TL, TH);
3732 ExpandOp(Node->getOperand(3), FL, FH);
3733 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3734 Node->getOperand(1), TL, FL, Node->getOperand(4));
3735 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3736 Node->getOperand(1), TH, FH, Node->getOperand(4));
3737 break;
3738 }
Nate Begeman144ff662005-10-13 17:15:37 +00003739 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003740 SDOperand Chain = Node->getOperand(0);
3741 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003742 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3743
3744 if (EVT == NVT)
3745 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3746 else
3747 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3748 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003749
3750 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003751 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003752
Nate Begeman144ff662005-10-13 17:15:37 +00003753 // The high part is obtained by SRA'ing all but one of the bits of the lo
3754 // part.
3755 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3756 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3757 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003758 break;
3759 }
3760 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003761 SDOperand Chain = Node->getOperand(0);
3762 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003763 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3764
3765 if (EVT == NVT)
3766 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3767 else
3768 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3769 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003770
3771 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003772 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003773
Nate Begeman144ff662005-10-13 17:15:37 +00003774 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003775 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003776 break;
3777 }
3778 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003779 SDOperand Chain = Node->getOperand(0);
3780 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003781 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3782
3783 if (EVT == NVT)
3784 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3785 else
3786 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3787 EVT);
3788
3789 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003790 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003791
3792 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003793 Hi = DAG.getNode(ISD::UNDEF, NVT);
3794 break;
3795 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003796 case ISD::ANY_EXTEND:
3797 // The low part is any extension of the input (which degenerates to a copy).
3798 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3799 // The high part is undefined.
3800 Hi = DAG.getNode(ISD::UNDEF, NVT);
3801 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003802 case ISD::SIGN_EXTEND: {
3803 // The low part is just a sign extension of the input (which degenerates to
3804 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003805 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003806
Chris Lattner3e928bb2005-01-07 07:47:09 +00003807 // The high part is obtained by SRA'ing all but one of the bits of the lo
3808 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003809 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003810 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3811 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003812 break;
3813 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003814 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003815 // The low part is just a zero extension of the input (which degenerates to
3816 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003817 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003818
Chris Lattner3e928bb2005-01-07 07:47:09 +00003819 // The high part is just a zero.
3820 Hi = DAG.getConstant(0, NVT);
3821 break;
Chris Lattner35481892005-12-23 00:16:34 +00003822
3823 case ISD::BIT_CONVERT: {
3824 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3825 Node->getOperand(0));
3826 ExpandOp(Tmp, Lo, Hi);
3827 break;
3828 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003829
Chris Lattner8137c9e2006-01-28 05:07:51 +00003830 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003831 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3832 TargetLowering::Custom &&
3833 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003834 Lo = TLI.LowerOperation(Op, DAG);
3835 assert(Lo.Val && "Node must be custom expanded!");
3836 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003837 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003838 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003839 break;
3840
Chris Lattner4e6c7462005-01-08 19:27:05 +00003841 // These operators cannot be expanded directly, emit them as calls to
3842 // library functions.
3843 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003844 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003845 SDOperand Op;
3846 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3847 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003848 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3849 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003850 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003851
Chris Lattnerf20d1832005-07-30 01:40:57 +00003852 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3853
Chris Lattner80a3e942005-07-29 00:33:32 +00003854 // Now that the custom expander is done, expand the result, which is still
3855 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003856 if (Op.Val) {
3857 ExpandOp(Op, Lo, Hi);
3858 break;
3859 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003860 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003861
Chris Lattner4e6c7462005-01-08 19:27:05 +00003862 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003863 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003864 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003865 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003866 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003867
Chris Lattner4e6c7462005-01-08 19:27:05 +00003868 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003869 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003870 SDOperand Op;
3871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3872 case Expand: assert(0 && "cannot expand FP!");
3873 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3874 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3875 }
3876
3877 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3878
3879 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003880 if (Op.Val) {
3881 ExpandOp(Op, Lo, Hi);
3882 break;
3883 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003884 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003885
Chris Lattner4e6c7462005-01-08 19:27:05 +00003886 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003887 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003888 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003889 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003890 break;
3891
Evan Cheng05a2d562006-01-09 18:31:59 +00003892 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003893 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003894 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003895 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003896 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003897 Op = TLI.LowerOperation(Op, DAG);
3898 if (Op.Val) {
3899 // Now that the custom expander is done, expand the result, which is
3900 // still VT.
3901 ExpandOp(Op, Lo, Hi);
3902 break;
3903 }
3904 }
3905
Chris Lattnere34b3962005-01-19 04:19:40 +00003906 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003907 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003908 break;
Chris Lattner47599822005-04-02 03:38:53 +00003909
3910 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003911 TargetLowering::LegalizeAction Action =
3912 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3913 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3914 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003915 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003916 break;
3917 }
3918
Chris Lattnere34b3962005-01-19 04:19:40 +00003919 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003920 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003921 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003922 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003923
Evan Cheng05a2d562006-01-09 18:31:59 +00003924 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003925 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003926 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003927 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003928 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003929 Op = TLI.LowerOperation(Op, DAG);
3930 if (Op.Val) {
3931 // Now that the custom expander is done, expand the result, which is
3932 // still VT.
3933 ExpandOp(Op, Lo, Hi);
3934 break;
3935 }
3936 }
3937
Chris Lattnere34b3962005-01-19 04:19:40 +00003938 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003939 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003940 break;
Chris Lattner47599822005-04-02 03:38:53 +00003941
3942 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003943 TargetLowering::LegalizeAction Action =
3944 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3945 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3946 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003947 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003948 break;
3949 }
3950
Chris Lattnere34b3962005-01-19 04:19:40 +00003951 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003952 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003953 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003954 }
3955
3956 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003957 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003958 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003959 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003960 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003961 Op = TLI.LowerOperation(Op, DAG);
3962 if (Op.Val) {
3963 // Now that the custom expander is done, expand the result, which is
3964 // still VT.
3965 ExpandOp(Op, Lo, Hi);
3966 break;
3967 }
3968 }
3969
Chris Lattnere34b3962005-01-19 04:19:40 +00003970 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003971 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003972 break;
Chris Lattner47599822005-04-02 03:38:53 +00003973
3974 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003975 TargetLowering::LegalizeAction Action =
3976 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3977 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3978 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003979 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003980 break;
3981 }
3982
Chris Lattnere34b3962005-01-19 04:19:40 +00003983 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003984 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003985 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003986 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003987
Misha Brukmanedf128a2005-04-21 22:36:52 +00003988 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003989 case ISD::SUB: {
3990 // If the target wants to custom expand this, let them.
3991 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3992 TargetLowering::Custom) {
3993 Op = TLI.LowerOperation(Op, DAG);
3994 if (Op.Val) {
3995 ExpandOp(Op, Lo, Hi);
3996 break;
3997 }
3998 }
3999
4000 // Expand the subcomponents.
4001 SDOperand LHSL, LHSH, RHSL, RHSH;
4002 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4003 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004004 std::vector<MVT::ValueType> VTs;
4005 std::vector<SDOperand> LoOps, HiOps;
4006 VTs.push_back(LHSL.getValueType());
4007 VTs.push_back(MVT::Flag);
4008 LoOps.push_back(LHSL);
4009 LoOps.push_back(RHSL);
4010 HiOps.push_back(LHSH);
4011 HiOps.push_back(RHSH);
4012 if (Node->getOpcode() == ISD::ADD) {
4013 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4014 HiOps.push_back(Lo.getValue(1));
4015 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4016 } else {
4017 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4018 HiOps.push_back(Lo.getValue(1));
4019 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4020 }
Chris Lattner84f67882005-01-20 18:52:28 +00004021 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004022 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004023 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004024 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004025 SDOperand LL, LH, RL, RH;
4026 ExpandOp(Node->getOperand(0), LL, LH);
4027 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004028 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4029 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4030 // extended the sign bit of the low half through the upper half, and if so
4031 // emit a MULHS instead of the alternate sequence that is valid for any
4032 // i64 x i64 multiply.
4033 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4034 // is RH an extension of the sign bit of RL?
4035 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4036 RH.getOperand(1).getOpcode() == ISD::Constant &&
4037 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4038 // is LH an extension of the sign bit of LL?
4039 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4040 LH.getOperand(1).getOpcode() == ISD::Constant &&
4041 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4042 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4043 } else {
4044 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4045 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4046 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4047 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4048 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4049 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004050 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4051 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004052 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004053 }
4054 break;
4055 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004056 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4057 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4058 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4059 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004060 }
4061
Chris Lattner83397362005-12-21 18:02:52 +00004062 // Make sure the resultant values have been legalized themselves, unless this
4063 // is a type that requires multi-step expansion.
4064 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4065 Lo = LegalizeOp(Lo);
4066 Hi = LegalizeOp(Hi);
4067 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004068
4069 // Remember in a map if the values will be reused later.
4070 bool isNew =
4071 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4072 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004073}
4074
Chris Lattnerc7029802006-03-18 01:44:44 +00004075/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4076/// two smaller values of MVT::Vector type.
4077void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4078 SDOperand &Hi) {
4079 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4080 SDNode *Node = Op.Val;
4081 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4082 assert(NumElements > 1 && "Cannot split a single element vector!");
4083 unsigned NewNumElts = NumElements/2;
4084 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4085 SDOperand TypeNode = *(Node->op_end()-1);
4086
4087 // See if we already split it.
4088 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4089 = SplitNodes.find(Op);
4090 if (I != SplitNodes.end()) {
4091 Lo = I->second.first;
4092 Hi = I->second.second;
4093 return;
4094 }
4095
4096 switch (Node->getOpcode()) {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004097 default: assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004098 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004099 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4100 LoOps.push_back(NewNumEltsNode);
4101 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004102 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004103
4104 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4105 HiOps.push_back(NewNumEltsNode);
4106 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004107 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004108 break;
4109 }
4110 case ISD::VADD:
4111 case ISD::VSUB:
4112 case ISD::VMUL:
4113 case ISD::VSDIV:
4114 case ISD::VUDIV:
4115 case ISD::VAND:
4116 case ISD::VOR:
4117 case ISD::VXOR: {
4118 SDOperand LL, LH, RL, RH;
4119 SplitVectorOp(Node->getOperand(0), LL, LH);
4120 SplitVectorOp(Node->getOperand(1), RL, RH);
4121
4122 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4123 NewNumEltsNode, TypeNode);
4124 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4125 NewNumEltsNode, TypeNode);
4126 break;
4127 }
4128 case ISD::VLOAD: {
4129 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4130 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4131 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4132
4133 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4134 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4135 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4136 getIntPtrConstant(IncrementSize));
4137 // FIXME: This creates a bogus srcvalue!
4138 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4139
4140 // Build a factor node to remember that this load is independent of the
4141 // other one.
4142 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4143 Hi.getValue(1));
4144
4145 // Remember that we legalized the chain.
4146 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4147 if (!TLI.isLittleEndian())
4148 std::swap(Lo, Hi);
4149 break;
4150 }
4151 }
4152
4153 // Remember in a map if the values will be reused later.
4154 bool isNew =
4155 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4156 assert(isNew && "Value already expanded?!?");
4157}
4158
4159
4160/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4161/// equivalent operation that returns a scalar (e.g. F32) or packed value
4162/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4163/// type for the result.
4164SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4165 MVT::ValueType NewVT) {
4166 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4167 SDNode *Node = Op.Val;
4168
4169 // See if we already packed it.
4170 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4171 if (I != PackedNodes.end()) return I->second;
4172
4173 SDOperand Result;
4174 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004175 default:
4176 Node->dump(); std::cerr << "\n";
4177 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004178 case ISD::VADD:
4179 case ISD::VSUB:
4180 case ISD::VMUL:
4181 case ISD::VSDIV:
4182 case ISD::VUDIV:
4183 case ISD::VAND:
4184 case ISD::VOR:
4185 case ISD::VXOR:
4186 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4187 NewVT,
4188 PackVectorOp(Node->getOperand(0), NewVT),
4189 PackVectorOp(Node->getOperand(1), NewVT));
4190 break;
4191 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004192 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4193 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004194
Chris Lattner4794a6b2006-03-19 00:07:49 +00004195 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004196
4197 // Remember that we legalized the chain.
4198 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4199 break;
4200 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004201 case ISD::VBUILD_VECTOR:
Chris Lattnerc7029802006-03-18 01:44:44 +00004202 if (!MVT::isVector(NewVT)) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004203 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004204 Result = Node->getOperand(0);
4205 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004206 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004207 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004208 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004209 }
4210 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004211 case ISD::VINSERT_VECTOR_ELT:
4212 if (!MVT::isVector(NewVT)) {
4213 // Returning a scalar? Must be the inserted element.
4214 Result = Node->getOperand(1);
4215 } else {
4216 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4217 PackVectorOp(Node->getOperand(0), NewVT),
4218 Node->getOperand(1), Node->getOperand(2));
4219 }
4220 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004221 }
4222
4223 if (TLI.isTypeLegal(NewVT))
4224 Result = LegalizeOp(Result);
4225 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4226 assert(isNew && "Value already packed?");
4227 return Result;
4228}
4229
Chris Lattner3e928bb2005-01-07 07:47:09 +00004230
4231// SelectionDAG::Legalize - This is the entry point for the file.
4232//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004233void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004234 /// run - This is the main entry point to this class.
4235 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004236 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004237}
4238