blob: 6ccd159c5c940d4202be5badb19564be15a1fd48 [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))) {
732 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;
750 break;
751 }
752 }
753
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.
782 assert(0 && "Cannot lower variable BUILD_VECTOR yet!");
783 abort();
784 break;
785 }
786 }
787 break;
Chris Lattner6831a812006-02-13 09:18:02 +0000788 case ISD::CALLSEQ_START: {
789 SDNode *CallEnd = FindCallEndFromCallStart(Node);
790
791 // Recursively Legalize all of the inputs of the call end that do not lead
792 // to this call start. This ensures that any libcalls that need be inserted
793 // are inserted *before* the CALLSEQ_START.
794 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
795 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
796
797 // Now that we legalized all of the inputs (which may have inserted
798 // libcalls) create the new CALLSEQ_START node.
799 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
800
801 // Merge in the last call, to ensure that this call start after the last
802 // call ended.
803 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
804 Tmp1 = LegalizeOp(Tmp1);
805
806 // Do not try to legalize the target-specific arguments (#1+).
807 if (Tmp1 != Node->getOperand(0)) {
808 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
809 Ops[0] = Tmp1;
810 Result = DAG.UpdateNodeOperands(Result, Ops);
811 }
812
813 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000814 AddLegalizedOperand(Op.getValue(0), Result);
815 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
816 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
817
Chris Lattner6831a812006-02-13 09:18:02 +0000818 // Now that the callseq_start and all of the non-call nodes above this call
819 // sequence have been legalized, legalize the call itself. During this
820 // process, no libcalls can/will be inserted, guaranteeing that no calls
821 // can overlap.
822 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
823 SDOperand InCallSEQ = LastCALLSEQ_END;
824 // Note that we are selecting this call!
825 LastCALLSEQ_END = SDOperand(CallEnd, 0);
826 IsLegalizingCall = true;
827
828 // Legalize the call, starting from the CALLSEQ_END.
829 LegalizeOp(LastCALLSEQ_END);
830 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
831 return Result;
832 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000833 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000834 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
835 // will cause this node to be legalized as well as handling libcalls right.
836 if (LastCALLSEQ_END.Val != Node) {
837 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
838 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
839 assert(I != LegalizedNodes.end() &&
840 "Legalizing the call start should have legalized this node!");
841 return I->second;
842 }
843
844 // Otherwise, the call start has been legalized and everything is going
845 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000846 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000847 // Do not try to legalize the target-specific arguments (#1+), except for
848 // an optional flag input.
849 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
850 if (Tmp1 != Node->getOperand(0)) {
851 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
852 Ops[0] = Tmp1;
853 Result = DAG.UpdateNodeOperands(Result, Ops);
854 }
855 } else {
856 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
857 if (Tmp1 != Node->getOperand(0) ||
858 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
859 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
860 Ops[0] = Tmp1;
861 Ops.back() = Tmp2;
862 Result = DAG.UpdateNodeOperands(Result, Ops);
863 }
Chris Lattner6a542892006-01-24 05:48:21 +0000864 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000865 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000866 // This finishes up call legalization.
867 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000868
869 // If the CALLSEQ_END node has a flag, remember that we legalized it.
870 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
871 if (Node->getNumValues() == 2)
872 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
873 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000874 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
876 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
877 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000879
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000880 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000881 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000882 switch (TLI.getOperationAction(Node->getOpcode(),
883 Node->getValueType(0))) {
884 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000885 case TargetLowering::Expand: {
886 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
887 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
888 " not tell us which reg is the stack pointer!");
889 SDOperand Chain = Tmp1.getOperand(0);
890 SDOperand Size = Tmp2.getOperand(1);
891 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
892 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
893 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000894 Tmp1 = LegalizeOp(Tmp1);
895 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000896 break;
897 }
898 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000899 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
900 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000901 Tmp1 = LegalizeOp(Tmp3);
902 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000903 }
Chris Lattner903d2782006-01-15 08:54:32 +0000904 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000905 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000906 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000907 }
Chris Lattner903d2782006-01-15 08:54:32 +0000908 // Since this op produce two values, make sure to remember that we
909 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000910 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
911 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000912 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000913 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000914 case ISD::INLINEASM:
915 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
916 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000917 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000918 Tmp2 = Tmp3 = SDOperand(0, 0);
919 else
920 Tmp3 = LegalizeOp(Tmp2);
921
922 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
923 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
924 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000925 if (Tmp3.Val) Ops.back() = Tmp3;
926 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000927 }
928
929 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000930 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000931 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
932 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000933 case ISD::BR:
934 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000935 // Ensure that libcalls are emitted before a branch.
936 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
937 Tmp1 = LegalizeOp(Tmp1);
938 LastCALLSEQ_END = DAG.getEntryNode();
939
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000940 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +0000941 break;
942
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000943 case ISD::BRCOND:
944 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000945 // Ensure that libcalls are emitted before a return.
946 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
947 Tmp1 = LegalizeOp(Tmp1);
948 LastCALLSEQ_END = DAG.getEntryNode();
949
Chris Lattner47e92232005-01-18 19:27:06 +0000950 switch (getTypeAction(Node->getOperand(1).getValueType())) {
951 case Expand: assert(0 && "It's impossible to expand bools");
952 case Legal:
953 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
954 break;
955 case Promote:
956 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
957 break;
958 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000959
960 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000961 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000962
963 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
964 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000965 case TargetLowering::Legal: break;
966 case TargetLowering::Custom:
967 Tmp1 = TLI.LowerOperation(Result, DAG);
968 if (Tmp1.Val) Result = Tmp1;
969 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000970 case TargetLowering::Expand:
971 // Expand brcond's setcc into its constituent parts and create a BR_CC
972 // Node.
973 if (Tmp2.getOpcode() == ISD::SETCC) {
974 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
975 Tmp2.getOperand(0), Tmp2.getOperand(1),
976 Node->getOperand(2));
977 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000978 // Make sure the condition is either zero or one. It may have been
979 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +0000980 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
981 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
982 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +0000983
Nate Begeman7cbd5252005-08-16 19:49:35 +0000984 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
985 DAG.getCondCode(ISD::SETNE), Tmp2,
986 DAG.getConstant(0, Tmp2.getValueType()),
987 Node->getOperand(2));
988 }
989 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000990 }
991 break;
992 case ISD::BR_CC:
993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000994 // Ensure that libcalls are emitted before a branch.
995 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
996 Tmp1 = LegalizeOp(Tmp1);
997 LastCALLSEQ_END = DAG.getEntryNode();
998
Nate Begeman750ac1b2006-02-01 07:19:44 +0000999 Tmp2 = Node->getOperand(2); // LHS
1000 Tmp3 = Node->getOperand(3); // RHS
1001 Tmp4 = Node->getOperand(1); // CC
1002
1003 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1004
1005 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1006 // the LHS is a legal SETCC itself. In this case, we need to compare
1007 // the result against zero to select between true and false values.
1008 if (Tmp3.Val == 0) {
1009 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1010 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001011 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001012
1013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1014 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001015
Chris Lattner181b7a32005-12-17 23:46:46 +00001016 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1017 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001018 case TargetLowering::Legal: break;
1019 case TargetLowering::Custom:
1020 Tmp4 = TLI.LowerOperation(Result, DAG);
1021 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001022 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001023 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001024 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001025 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001026 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1027 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001028
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001029 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001030 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1031 Tmp2 = Result.getValue(0);
1032 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001033
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001034 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1035 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001036 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001037 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001038 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001039 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001040 Tmp2 = LegalizeOp(Tmp1);
1041 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001042 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001043 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001044 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001045 // Since loads produce two values, make sure to remember that we
1046 // legalized both of them.
1047 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1048 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1049 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001050 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001051 case ISD::EXTLOAD:
1052 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001053 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001054 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1055 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001056
Chris Lattner5f056bf2005-07-10 01:55:33 +00001057 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001058 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001059 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001060 case TargetLowering::Promote:
1061 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1063 DAG.getValueType(MVT::i8));
1064 Tmp1 = Result.getValue(0);
1065 Tmp2 = Result.getValue(1);
1066 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001067 case TargetLowering::Custom:
1068 isCustom = true;
1069 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001070 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001071 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1072 Node->getOperand(3));
1073 Tmp1 = Result.getValue(0);
1074 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001075
1076 if (isCustom) {
1077 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1078 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001079 Tmp1 = LegalizeOp(Tmp3);
1080 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001081 }
1082 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001083 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001084 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001085 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001086 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1087 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001088 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001089 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1090 Tmp2 = LegalizeOp(Load.getValue(1));
1091 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001092 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001093 assert(Node->getOpcode() != ISD::EXTLOAD &&
1094 "EXTLOAD should always be supported!");
1095 // Turn the unsupported load into an EXTLOAD followed by an explicit
1096 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001097 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1098 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001099 SDOperand ValRes;
1100 if (Node->getOpcode() == ISD::SEXTLOAD)
1101 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001102 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001103 else
1104 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001105 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1106 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1107 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001108 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001109 // Since loads produce two values, make sure to remember that we legalized
1110 // both of them.
1111 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1112 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1113 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001114 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001115 case ISD::EXTRACT_ELEMENT: {
1116 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1117 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001118 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001119 case Legal:
1120 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1121 // 1 -> Hi
1122 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1123 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1124 TLI.getShiftAmountTy()));
1125 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1126 } else {
1127 // 0 -> Lo
1128 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1129 Node->getOperand(0));
1130 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001131 break;
1132 case Expand:
1133 // Get both the low and high parts.
1134 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1135 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1136 Result = Tmp2; // 1 -> Hi
1137 else
1138 Result = Tmp1; // 0 -> Lo
1139 break;
1140 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001141 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001142 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001143
1144 case ISD::CopyToReg:
1145 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001146
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001147 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001148 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001149 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001150 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001151 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001152 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001153 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001154 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001155 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001156 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001157 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1158 Tmp3);
1159 } else {
1160 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001161 }
1162
1163 // Since this produces two values, make sure to remember that we legalized
1164 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001165 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001166 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001167 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001168 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001169 break;
1170
1171 case ISD::RET:
1172 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001173
1174 // Ensure that libcalls are emitted before a return.
1175 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1176 Tmp1 = LegalizeOp(Tmp1);
1177 LastCALLSEQ_END = DAG.getEntryNode();
1178
Chris Lattner3e928bb2005-01-07 07:47:09 +00001179 switch (Node->getNumOperands()) {
1180 case 2: // ret val
1181 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1182 case Legal:
1183 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001185 break;
1186 case Expand: {
1187 SDOperand Lo, Hi;
1188 ExpandOp(Node->getOperand(1), Lo, Hi);
1189 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001190 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001191 }
1192 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001193 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001194 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1195 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001196 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001197 }
1198 break;
1199 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001200 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001201 break;
1202 default: { // ret <values>
1203 std::vector<SDOperand> NewValues;
1204 NewValues.push_back(Tmp1);
1205 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1206 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1207 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001208 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 break;
1210 case Expand: {
1211 SDOperand Lo, Hi;
1212 ExpandOp(Node->getOperand(i), Lo, Hi);
1213 NewValues.push_back(Lo);
1214 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001215 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001216 }
1217 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001218 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001219 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001220
1221 if (NewValues.size() == Node->getNumOperands())
1222 Result = DAG.UpdateNodeOperands(Result, NewValues);
1223 else
1224 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001225 break;
1226 }
1227 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001228
Chris Lattner6862dbc2006-01-29 21:02:23 +00001229 if (Result.getOpcode() == ISD::RET) {
1230 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1231 default: assert(0 && "This action is not supported yet!");
1232 case TargetLowering::Legal: break;
1233 case TargetLowering::Custom:
1234 Tmp1 = TLI.LowerOperation(Result, DAG);
1235 if (Tmp1.Val) Result = Tmp1;
1236 break;
1237 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001238 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001239 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001240 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001241 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1242 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1243
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001244 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001245 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001246 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001247 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001248 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001249 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001250 } else {
1251 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001252 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001253 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001254 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1255 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001256 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001257 }
1258
Chris Lattner3e928bb2005-01-07 07:47:09 +00001259 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1260 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001261 Tmp3 = LegalizeOp(Node->getOperand(1));
1262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1263 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001264
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001265 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001266 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1267 default: assert(0 && "This action is not supported yet!");
1268 case TargetLowering::Legal: break;
1269 case TargetLowering::Custom:
1270 Tmp1 = TLI.LowerOperation(Result, DAG);
1271 if (Tmp1.Val) Result = Tmp1;
1272 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001273 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001274 break;
1275 }
1276 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001277 // Truncate the value and store the result.
1278 Tmp3 = PromoteOp(Node->getOperand(1));
1279 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001280 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001281 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001282 break;
1283
Chris Lattner3e928bb2005-01-07 07:47:09 +00001284 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001285 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001286 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001287
1288 // If this is a vector type, then we have to calculate the increment as
1289 // the product of the element size in bytes, and the number of elements
1290 // in the high half of the vector.
1291 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1292 SDNode *InVal = Node->getOperand(1).Val;
1293 unsigned NumElems =
1294 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1295 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1296
1297 // Figure out if there is a Packed type corresponding to this Vector
1298 // type. If so, convert to the packed type.
1299 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1300 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1301 // Turn this into a normal store of the packed type.
1302 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1303 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1304 Node->getOperand(3));
1305 break;
1306 } else if (NumElems == 1) {
1307 // Turn this into a normal store of the scalar type.
1308 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1309 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1310 Node->getOperand(3));
1311 break;
1312 } else {
1313 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1314 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1315 }
1316 } else {
1317 ExpandOp(Node->getOperand(1), Lo, Hi);
1318 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1319 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001320
1321 if (!TLI.isLittleEndian())
1322 std::swap(Lo, Hi);
1323
Chris Lattneredb1add2005-05-11 04:51:16 +00001324 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1325 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001326 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1327 getIntPtrConstant(IncrementSize));
1328 assert(isTypeLegal(Tmp2.getValueType()) &&
1329 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001330 // FIXME: This sets the srcvalue of both halves to be the same, which is
1331 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001332 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1333 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001334 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1335 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001336 }
1337 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001338 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001339 case ISD::PCMARKER:
1340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001341 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001342 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001343 case ISD::STACKSAVE:
1344 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001345 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1346 Tmp1 = Result.getValue(0);
1347 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001348
Chris Lattner140d53c2006-01-13 02:50:02 +00001349 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1350 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001351 case TargetLowering::Legal: break;
1352 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001353 Tmp3 = TLI.LowerOperation(Result, DAG);
1354 if (Tmp3.Val) {
1355 Tmp1 = LegalizeOp(Tmp3);
1356 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001357 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001358 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001359 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001360 // Expand to CopyFromReg if the target set
1361 // StackPointerRegisterToSaveRestore.
1362 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001363 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001364 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001365 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001366 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001367 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1368 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001369 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001370 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001371 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001372
1373 // Since stacksave produce two values, make sure to remember that we
1374 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001375 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1376 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1377 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001378
Chris Lattner140d53c2006-01-13 02:50:02 +00001379 case ISD::STACKRESTORE:
1380 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1381 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001382 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001383
1384 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1385 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001386 case TargetLowering::Legal: break;
1387 case TargetLowering::Custom:
1388 Tmp1 = TLI.LowerOperation(Result, DAG);
1389 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001390 break;
1391 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001392 // Expand to CopyToReg if the target set
1393 // StackPointerRegisterToSaveRestore.
1394 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1395 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1396 } else {
1397 Result = Tmp1;
1398 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001399 break;
1400 }
1401 break;
1402
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001403 case ISD::READCYCLECOUNTER:
1404 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001405 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001406
1407 // Since rdcc produce two values, make sure to remember that we legalized
1408 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001409 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001410 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001411 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001412
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001413 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001414 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1415 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1416
Chris Lattner456a93a2006-01-28 07:39:30 +00001417 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1418 "Cannot handle illegal TRUNCSTORE yet!");
1419 Tmp2 = LegalizeOp(Node->getOperand(1));
1420
1421 // The only promote case we handle is TRUNCSTORE:i1 X into
1422 // -> TRUNCSTORE:i8 (and X, 1)
1423 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1424 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1425 TargetLowering::Promote) {
1426 // Promote the bool to a mask then store.
1427 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1428 DAG.getConstant(1, Tmp2.getValueType()));
1429 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1430 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001431
Chris Lattner456a93a2006-01-28 07:39:30 +00001432 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1433 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1435 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001436 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001437
Chris Lattner456a93a2006-01-28 07:39:30 +00001438 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1439 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1440 default: assert(0 && "This action is not supported yet!");
1441 case TargetLowering::Legal: break;
1442 case TargetLowering::Custom:
1443 Tmp1 = TLI.LowerOperation(Result, DAG);
1444 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001445 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001446 }
1447 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001448 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001449 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001450 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1451 case Expand: assert(0 && "It's impossible to expand bools");
1452 case Legal:
1453 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1454 break;
1455 case Promote:
1456 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1457 break;
1458 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001459 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001460 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001461
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001462 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001463
Nate Begemanb942a3d2005-08-23 04:29:48 +00001464 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001465 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001466 case TargetLowering::Legal: break;
1467 case TargetLowering::Custom: {
1468 Tmp1 = TLI.LowerOperation(Result, DAG);
1469 if (Tmp1.Val) Result = Tmp1;
1470 break;
1471 }
Nate Begeman9373a812005-08-10 20:51:12 +00001472 case TargetLowering::Expand:
1473 if (Tmp1.getOpcode() == ISD::SETCC) {
1474 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1475 Tmp2, Tmp3,
1476 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1477 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001478 // Make sure the condition is either zero or one. It may have been
1479 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001480 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1481 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1482 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001483 Result = DAG.getSelectCC(Tmp1,
1484 DAG.getConstant(0, Tmp1.getValueType()),
1485 Tmp2, Tmp3, ISD::SETNE);
1486 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001487 break;
1488 case TargetLowering::Promote: {
1489 MVT::ValueType NVT =
1490 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1491 unsigned ExtOp, TruncOp;
1492 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001493 ExtOp = ISD::ANY_EXTEND;
1494 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001495 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001496 ExtOp = ISD::FP_EXTEND;
1497 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001498 }
1499 // Promote each of the values to the new type.
1500 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1501 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1502 // Perform the larger operation, then round down.
1503 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1504 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1505 break;
1506 }
1507 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001508 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001509 case ISD::SELECT_CC: {
1510 Tmp1 = Node->getOperand(0); // LHS
1511 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001512 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1513 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001514 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001515
Nate Begeman750ac1b2006-02-01 07:19:44 +00001516 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1517
1518 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1519 // the LHS is a legal SETCC itself. In this case, we need to compare
1520 // the result against zero to select between true and false values.
1521 if (Tmp2.Val == 0) {
1522 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1523 CC = DAG.getCondCode(ISD::SETNE);
1524 }
1525 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1526
1527 // Everything is legal, see if we should expand this op or something.
1528 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1529 default: assert(0 && "This action is not supported yet!");
1530 case TargetLowering::Legal: break;
1531 case TargetLowering::Custom:
1532 Tmp1 = TLI.LowerOperation(Result, DAG);
1533 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001534 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001535 }
1536 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001537 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001538 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001539 Tmp1 = Node->getOperand(0);
1540 Tmp2 = Node->getOperand(1);
1541 Tmp3 = Node->getOperand(2);
1542 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1543
1544 // If we had to Expand the SetCC operands into a SELECT node, then it may
1545 // not always be possible to return a true LHS & RHS. In this case, just
1546 // return the value we legalized, returned in the LHS
1547 if (Tmp2.Val == 0) {
1548 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001549 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001550 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001551
Chris Lattner73e142f2006-01-30 22:43:50 +00001552 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001553 default: assert(0 && "Cannot handle this action for SETCC yet!");
1554 case TargetLowering::Custom:
1555 isCustom = true;
1556 // FALLTHROUGH.
1557 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001558 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001559 if (isCustom) {
1560 Tmp3 = TLI.LowerOperation(Result, DAG);
1561 if (Tmp3.Val) Result = Tmp3;
1562 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001563 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001564 case TargetLowering::Promote: {
1565 // First step, figure out the appropriate operation to use.
1566 // Allow SETCC to not be supported for all legal data types
1567 // Mostly this targets FP
1568 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1569 MVT::ValueType OldVT = NewInTy;
1570
1571 // Scan for the appropriate larger type to use.
1572 while (1) {
1573 NewInTy = (MVT::ValueType)(NewInTy+1);
1574
1575 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1576 "Fell off of the edge of the integer world");
1577 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1578 "Fell off of the edge of the floating point world");
1579
1580 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001581 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001582 break;
1583 }
1584 if (MVT::isInteger(NewInTy))
1585 assert(0 && "Cannot promote Legal Integer SETCC yet");
1586 else {
1587 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1588 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1589 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001590 Tmp1 = LegalizeOp(Tmp1);
1591 Tmp2 = LegalizeOp(Tmp2);
1592 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001593 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001594 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001595 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001596 case TargetLowering::Expand:
1597 // Expand a setcc node into a select_cc of the same condition, lhs, and
1598 // rhs that selects between const 1 (true) and const 0 (false).
1599 MVT::ValueType VT = Node->getValueType(0);
1600 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1601 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1602 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001603 break;
1604 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001605 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001606 case ISD::MEMSET:
1607 case ISD::MEMCPY:
1608 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001610 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1611
1612 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1613 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1614 case Expand: assert(0 && "Cannot expand a byte!");
1615 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001616 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001617 break;
1618 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001619 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001620 break;
1621 }
1622 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001623 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001624 }
Chris Lattner272455b2005-02-02 03:44:41 +00001625
1626 SDOperand Tmp4;
1627 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001628 case Expand: {
1629 // Length is too big, just take the lo-part of the length.
1630 SDOperand HiPart;
1631 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1632 break;
1633 }
Chris Lattnere5605212005-01-28 22:29:18 +00001634 case Legal:
1635 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001636 break;
1637 case Promote:
1638 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001639 break;
1640 }
1641
1642 SDOperand Tmp5;
1643 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1644 case Expand: assert(0 && "Cannot expand this yet!");
1645 case Legal:
1646 Tmp5 = LegalizeOp(Node->getOperand(4));
1647 break;
1648 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001649 Tmp5 = PromoteOp(Node->getOperand(4));
1650 break;
1651 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001652
1653 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1654 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001655 case TargetLowering::Custom:
1656 isCustom = true;
1657 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001658 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001659 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001660 if (isCustom) {
1661 Tmp1 = TLI.LowerOperation(Result, DAG);
1662 if (Tmp1.Val) Result = Tmp1;
1663 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001664 break;
1665 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001666 // Otherwise, the target does not support this operation. Lower the
1667 // operation to an explicit libcall as appropriate.
1668 MVT::ValueType IntPtr = TLI.getPointerTy();
1669 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1670 std::vector<std::pair<SDOperand, const Type*> > Args;
1671
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001672 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001673 if (Node->getOpcode() == ISD::MEMSET) {
1674 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001675 // Extend the (previously legalized) ubyte argument to be an int value
1676 // for the call.
1677 if (Tmp3.getValueType() > MVT::i32)
1678 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1679 else
1680 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001681 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1682 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1683
1684 FnName = "memset";
1685 } else if (Node->getOpcode() == ISD::MEMCPY ||
1686 Node->getOpcode() == ISD::MEMMOVE) {
1687 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1688 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1689 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1690 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1691 } else {
1692 assert(0 && "Unknown op!");
1693 }
Chris Lattner45982da2005-05-12 16:53:42 +00001694
Chris Lattnere1bd8222005-01-11 05:57:22 +00001695 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001696 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001697 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001698 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001699 break;
1700 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001701 }
1702 break;
1703 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001704
Chris Lattner5b359c62005-04-02 04:00:59 +00001705 case ISD::SHL_PARTS:
1706 case ISD::SRA_PARTS:
1707 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001708 std::vector<SDOperand> Ops;
1709 bool Changed = false;
1710 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1711 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1712 Changed |= Ops.back() != Node->getOperand(i);
1713 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001714 if (Changed)
1715 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001716
Evan Cheng05a2d562006-01-09 18:31:59 +00001717 switch (TLI.getOperationAction(Node->getOpcode(),
1718 Node->getValueType(0))) {
1719 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001720 case TargetLowering::Legal: break;
1721 case TargetLowering::Custom:
1722 Tmp1 = TLI.LowerOperation(Result, DAG);
1723 if (Tmp1.Val) {
1724 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001725 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001726 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001727 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1728 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001729 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001730 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001731 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001732 return RetVal;
1733 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001734 break;
1735 }
1736
Chris Lattner2c8086f2005-04-02 05:00:07 +00001737 // Since these produce multiple values, make sure to remember that we
1738 // legalized all of them.
1739 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1740 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1741 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001742 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001743
1744 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001745 case ISD::ADD:
1746 case ISD::SUB:
1747 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001748 case ISD::MULHS:
1749 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001750 case ISD::UDIV:
1751 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001752 case ISD::AND:
1753 case ISD::OR:
1754 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001755 case ISD::SHL:
1756 case ISD::SRL:
1757 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001758 case ISD::FADD:
1759 case ISD::FSUB:
1760 case ISD::FMUL:
1761 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001762 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001763 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1764 case Expand: assert(0 && "Not possible");
1765 case Legal:
1766 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1767 break;
1768 case Promote:
1769 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1770 break;
1771 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001772
1773 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001774
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001775 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001776 default: assert(0 && "Operation not supported");
1777 case TargetLowering::Legal: break;
1778 case TargetLowering::Custom:
1779 Tmp1 = TLI.LowerOperation(Result, DAG);
1780 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001781 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001782 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001783 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001784
1785 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1786 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1787 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1788 case Expand: assert(0 && "Not possible");
1789 case Legal:
1790 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1791 break;
1792 case Promote:
1793 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1794 break;
1795 }
1796
1797 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1798
1799 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1800 default: assert(0 && "Operation not supported");
1801 case TargetLowering::Custom:
1802 Tmp1 = TLI.LowerOperation(Result, DAG);
1803 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001804 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001805 case TargetLowering::Legal: break;
1806 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001807 // If this target supports fabs/fneg natively, do this efficiently.
1808 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1809 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1810 // Get the sign bit of the RHS.
1811 MVT::ValueType IVT =
1812 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1813 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1814 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1815 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1816 // Get the absolute value of the result.
1817 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1818 // Select between the nabs and abs value based on the sign bit of
1819 // the input.
1820 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1821 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1822 AbsVal),
1823 AbsVal);
1824 Result = LegalizeOp(Result);
1825 break;
1826 }
1827
1828 // Otherwise, do bitwise ops!
1829
1830 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001831 const char *FnName;
1832 if (Node->getValueType(0) == MVT::f32) {
1833 FnName = "copysignf";
1834 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1835 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1836 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1837 } else {
1838 FnName = "copysign";
1839 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1840 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1841 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1842 }
1843 SDOperand Dummy;
1844 Result = ExpandLibCall(FnName, Node, Dummy);
1845 break;
1846 }
1847 break;
1848
Nate Begeman551bf3f2006-02-17 05:43:56 +00001849 case ISD::ADDC:
1850 case ISD::SUBC:
1851 Tmp1 = LegalizeOp(Node->getOperand(0));
1852 Tmp2 = LegalizeOp(Node->getOperand(1));
1853 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1854 // Since this produces two values, make sure to remember that we legalized
1855 // both of them.
1856 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1857 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1858 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001859
Nate Begeman551bf3f2006-02-17 05:43:56 +00001860 case ISD::ADDE:
1861 case ISD::SUBE:
1862 Tmp1 = LegalizeOp(Node->getOperand(0));
1863 Tmp2 = LegalizeOp(Node->getOperand(1));
1864 Tmp3 = LegalizeOp(Node->getOperand(2));
1865 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1866 // Since this produces two values, make sure to remember that we legalized
1867 // both of them.
1868 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1869 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1870 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001871
Nate Begeman419f8b62005-10-18 00:27:41 +00001872 case ISD::BUILD_PAIR: {
1873 MVT::ValueType PairTy = Node->getValueType(0);
1874 // TODO: handle the case where the Lo and Hi operands are not of legal type
1875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1876 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1877 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001878 case TargetLowering::Promote:
1879 case TargetLowering::Custom:
1880 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001881 case TargetLowering::Legal:
1882 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1883 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1884 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001885 case TargetLowering::Expand:
1886 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1887 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1888 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1889 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1890 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001891 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001892 break;
1893 }
1894 break;
1895 }
1896
Nate Begemanc105e192005-04-06 00:23:54 +00001897 case ISD::UREM:
1898 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001899 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001900 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1901 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001902
Nate Begemanc105e192005-04-06 00:23:54 +00001903 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001904 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1905 case TargetLowering::Custom:
1906 isCustom = true;
1907 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001908 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001910 if (isCustom) {
1911 Tmp1 = TLI.LowerOperation(Result, DAG);
1912 if (Tmp1.Val) Result = Tmp1;
1913 }
Nate Begemanc105e192005-04-06 00:23:54 +00001914 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001915 case TargetLowering::Expand:
1916 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001917 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001918 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001919 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001920 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1921 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1922 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1923 } else {
1924 // Floating point mod -> fmod libcall.
1925 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1926 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001927 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001928 }
1929 break;
1930 }
1931 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001932 case ISD::VAARG: {
1933 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1934 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1935
Chris Lattner5c62f332006-01-28 07:42:08 +00001936 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001937 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1938 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001939 case TargetLowering::Custom:
1940 isCustom = true;
1941 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001942 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001943 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1944 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001945 Tmp1 = Result.getValue(1);
1946
1947 if (isCustom) {
1948 Tmp2 = TLI.LowerOperation(Result, DAG);
1949 if (Tmp2.Val) {
1950 Result = LegalizeOp(Tmp2);
1951 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1952 }
1953 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001954 break;
1955 case TargetLowering::Expand: {
1956 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1957 Node->getOperand(2));
1958 // Increment the pointer, VAList, to the next vaarg
1959 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1960 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1961 TLI.getPointerTy()));
1962 // Store the incremented VAList to the legalized pointer
1963 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1964 Node->getOperand(2));
1965 // Load the actual argument out of the pointer VAList
1966 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001967 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001968 Result = LegalizeOp(Result);
1969 break;
1970 }
1971 }
1972 // Since VAARG produces two values, make sure to remember that we
1973 // legalized both of them.
1974 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001975 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1976 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00001977 }
1978
1979 case ISD::VACOPY:
1980 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1981 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1982 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1983
1984 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1985 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001986 case TargetLowering::Custom:
1987 isCustom = true;
1988 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001989 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001990 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1991 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001992 if (isCustom) {
1993 Tmp1 = TLI.LowerOperation(Result, DAG);
1994 if (Tmp1.Val) Result = Tmp1;
1995 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001996 break;
1997 case TargetLowering::Expand:
1998 // This defaults to loading a pointer from the input and storing it to the
1999 // output, returning the chain.
2000 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2001 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2002 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002003 break;
2004 }
2005 break;
2006
2007 case ISD::VAEND:
2008 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2009 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2010
2011 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2012 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002013 case TargetLowering::Custom:
2014 isCustom = true;
2015 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002016 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002017 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002018 if (isCustom) {
2019 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2020 if (Tmp1.Val) Result = Tmp1;
2021 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002022 break;
2023 case TargetLowering::Expand:
2024 Result = Tmp1; // Default to a no-op, return the chain
2025 break;
2026 }
2027 break;
2028
2029 case ISD::VASTART:
2030 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2031 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2032
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002033 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2034
Nate Begemanacc398c2006-01-25 18:21:52 +00002035 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2036 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002037 case TargetLowering::Legal: break;
2038 case TargetLowering::Custom:
2039 Tmp1 = TLI.LowerOperation(Result, DAG);
2040 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002041 break;
2042 }
2043 break;
2044
Nate Begeman35ef9132006-01-11 21:21:00 +00002045 case ISD::ROTL:
2046 case ISD::ROTR:
2047 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2048 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002049
2050 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2051 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002053 break;
2054
Nate Begemand88fc032006-01-14 03:14:10 +00002055 case ISD::BSWAP:
2056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2057 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002058 case TargetLowering::Custom:
2059 assert(0 && "Cannot custom legalize this yet!");
2060 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002061 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002062 break;
2063 case TargetLowering::Promote: {
2064 MVT::ValueType OVT = Tmp1.getValueType();
2065 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2066 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002067
Chris Lattner456a93a2006-01-28 07:39:30 +00002068 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2069 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2070 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2071 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2072 break;
2073 }
2074 case TargetLowering::Expand:
2075 Result = ExpandBSWAP(Tmp1);
2076 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002077 }
2078 break;
2079
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002080 case ISD::CTPOP:
2081 case ISD::CTTZ:
2082 case ISD::CTLZ:
2083 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2084 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002085 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002086 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002087 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002088 break;
2089 case TargetLowering::Promote: {
2090 MVT::ValueType OVT = Tmp1.getValueType();
2091 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002092
2093 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002094 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2095 // Perform the larger operation, then subtract if needed.
2096 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002097 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002098 case ISD::CTPOP:
2099 Result = Tmp1;
2100 break;
2101 case ISD::CTTZ:
2102 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002103 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2104 DAG.getConstant(getSizeInBits(NVT), NVT),
2105 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002106 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002107 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2108 break;
2109 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002110 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002111 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2112 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002113 getSizeInBits(OVT), NVT));
2114 break;
2115 }
2116 break;
2117 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002118 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002119 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002120 break;
2121 }
2122 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002123
Chris Lattner2c8086f2005-04-02 05:00:07 +00002124 // Unary operators
2125 case ISD::FABS:
2126 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002127 case ISD::FSQRT:
2128 case ISD::FSIN:
2129 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002130 Tmp1 = LegalizeOp(Node->getOperand(0));
2131 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002132 case TargetLowering::Promote:
2133 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002134 isCustom = true;
2135 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002136 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002137 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002138 if (isCustom) {
2139 Tmp1 = TLI.LowerOperation(Result, DAG);
2140 if (Tmp1.Val) Result = Tmp1;
2141 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002142 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002143 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002144 switch (Node->getOpcode()) {
2145 default: assert(0 && "Unreachable!");
2146 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002147 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2148 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002149 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002150 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002151 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002152 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2153 MVT::ValueType VT = Node->getValueType(0);
2154 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002155 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002156 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2157 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002158 break;
2159 }
2160 case ISD::FSQRT:
2161 case ISD::FSIN:
2162 case ISD::FCOS: {
2163 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002164 const char *FnName = 0;
2165 switch(Node->getOpcode()) {
2166 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2167 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2168 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2169 default: assert(0 && "Unreachable!");
2170 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002171 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002172 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002173 break;
2174 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002175 }
2176 break;
2177 }
2178 break;
Chris Lattner35481892005-12-23 00:16:34 +00002179
2180 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002181 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002182 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002183 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002184 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2185 Node->getOperand(0).getValueType())) {
2186 default: assert(0 && "Unknown operation action!");
2187 case TargetLowering::Expand:
2188 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2189 break;
2190 case TargetLowering::Legal:
2191 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002192 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002193 break;
2194 }
2195 }
2196 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002197 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002198 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002199 case ISD::UINT_TO_FP: {
2200 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002201 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2202 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002203 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002204 Node->getOperand(0).getValueType())) {
2205 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002206 case TargetLowering::Custom:
2207 isCustom = true;
2208 // FALLTHROUGH
2209 case TargetLowering::Legal:
2210 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002211 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002212 if (isCustom) {
2213 Tmp1 = TLI.LowerOperation(Result, DAG);
2214 if (Tmp1.Val) Result = Tmp1;
2215 }
2216 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002217 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002218 Result = ExpandLegalINT_TO_FP(isSigned,
2219 LegalizeOp(Node->getOperand(0)),
2220 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002221 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002222 case TargetLowering::Promote:
2223 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2224 Node->getValueType(0),
2225 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002226 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002227 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002228 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002229 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002230 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2231 Node->getValueType(0), Node->getOperand(0));
2232 break;
2233 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002234 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002235 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002236 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2237 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002238 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002239 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2240 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002241 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002242 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2243 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002244 break;
2245 }
2246 break;
2247 }
2248 case ISD::TRUNCATE:
2249 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2250 case Legal:
2251 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002252 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002253 break;
2254 case Expand:
2255 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2256
2257 // Since the result is legal, we should just be able to truncate the low
2258 // part of the source.
2259 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2260 break;
2261 case Promote:
2262 Result = PromoteOp(Node->getOperand(0));
2263 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2264 break;
2265 }
2266 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002267
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002268 case ISD::FP_TO_SINT:
2269 case ISD::FP_TO_UINT:
2270 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2271 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002272 Tmp1 = LegalizeOp(Node->getOperand(0));
2273
Chris Lattner1618beb2005-07-29 00:11:56 +00002274 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2275 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002276 case TargetLowering::Custom:
2277 isCustom = true;
2278 // FALLTHROUGH
2279 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002280 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002281 if (isCustom) {
2282 Tmp1 = TLI.LowerOperation(Result, DAG);
2283 if (Tmp1.Val) Result = Tmp1;
2284 }
2285 break;
2286 case TargetLowering::Promote:
2287 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2288 Node->getOpcode() == ISD::FP_TO_SINT);
2289 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002290 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002291 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2292 SDOperand True, False;
2293 MVT::ValueType VT = Node->getOperand(0).getValueType();
2294 MVT::ValueType NVT = Node->getValueType(0);
2295 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2296 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2297 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2298 Node->getOperand(0), Tmp2, ISD::SETLT);
2299 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2300 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002301 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002302 Tmp2));
2303 False = DAG.getNode(ISD::XOR, NVT, False,
2304 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002305 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2306 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002307 } else {
2308 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2309 }
2310 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002311 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002312 break;
2313 case Expand:
2314 assert(0 && "Shouldn't need to expand other operators here!");
2315 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002316 Tmp1 = PromoteOp(Node->getOperand(0));
2317 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2318 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002319 break;
2320 }
2321 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002322
Chris Lattner13c78e22005-09-02 00:18:10 +00002323 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002324 case ISD::ZERO_EXTEND:
2325 case ISD::SIGN_EXTEND:
2326 case ISD::FP_EXTEND:
2327 case ISD::FP_ROUND:
2328 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002329 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002330 case Legal:
2331 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002332 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002333 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002334 case Promote:
2335 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002336 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002337 Tmp1 = PromoteOp(Node->getOperand(0));
2338 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002339 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002340 case ISD::ZERO_EXTEND:
2341 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002342 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002343 Result = DAG.getZeroExtendInReg(Result,
2344 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002345 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002346 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002347 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002348 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002349 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002350 Result,
2351 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002352 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002353 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002354 Result = PromoteOp(Node->getOperand(0));
2355 if (Result.getValueType() != Op.getValueType())
2356 // Dynamically dead while we have only 2 FP types.
2357 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2358 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002359 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002360 Result = PromoteOp(Node->getOperand(0));
2361 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2362 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002363 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002364 }
2365 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002366 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002367 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002368 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002369 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002370
2371 // If this operation is not supported, convert it to a shl/shr or load/store
2372 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002373 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2374 default: assert(0 && "This action not supported for this op yet!");
2375 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002376 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002377 break;
2378 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002379 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002380 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002381 // NOTE: we could fall back on load/store here too for targets without
2382 // SAR. However, it is doubtful that any exist.
2383 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2384 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002385 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002386 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2387 Node->getOperand(0), ShiftCst);
2388 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2389 Result, ShiftCst);
2390 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2391 // The only way we can lower this is to turn it into a STORETRUNC,
2392 // EXTLOAD pair, targetting a temporary location (a stack slot).
2393
2394 // NOTE: there is a choice here between constantly creating new stack
2395 // slots and always reusing the same one. We currently always create
2396 // new ones, as reuse may inhibit scheduling.
2397 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2398 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2399 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2400 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002401 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002402 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2403 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2404 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002405 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002406 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002407 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2408 Result, StackSlot, DAG.getSrcValue(NULL),
2409 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002410 } else {
2411 assert(0 && "Unknown op");
2412 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002413 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002414 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002415 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002416 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002417 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002418
2419 // Make sure that the generated code is itself legal.
2420 if (Result != Op)
2421 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002422
Chris Lattner45982da2005-05-12 16:53:42 +00002423 // Note that LegalizeOp may be reentered even from single-use nodes, which
2424 // means that we always must cache transformed nodes.
2425 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002426 return Result;
2427}
2428
Chris Lattner8b6fa222005-01-15 22:16:26 +00002429/// PromoteOp - Given an operation that produces a value in an invalid type,
2430/// promote it to compute the value into a larger type. The produced value will
2431/// have the correct bits for the low portion of the register, but no guarantee
2432/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002433SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2434 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002435 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002436 assert(getTypeAction(VT) == Promote &&
2437 "Caller should expand or legalize operands that are not promotable!");
2438 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2439 "Cannot promote to smaller type!");
2440
Chris Lattner03c85462005-01-15 05:21:40 +00002441 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002442 SDOperand Result;
2443 SDNode *Node = Op.Val;
2444
Chris Lattner6fdcb252005-09-02 20:32:45 +00002445 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2446 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002447
Chris Lattner03c85462005-01-15 05:21:40 +00002448 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002449 case ISD::CopyFromReg:
2450 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002451 default:
2452 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2453 assert(0 && "Do not know how to promote this operator!");
2454 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002455 case ISD::UNDEF:
2456 Result = DAG.getNode(ISD::UNDEF, NVT);
2457 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002458 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002459 if (VT != MVT::i1)
2460 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2461 else
2462 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002463 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2464 break;
2465 case ISD::ConstantFP:
2466 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2467 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2468 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002469
Chris Lattner82fbfb62005-01-18 02:59:52 +00002470 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002471 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002472 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2473 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002474 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002475
2476 case ISD::TRUNCATE:
2477 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2478 case Legal:
2479 Result = LegalizeOp(Node->getOperand(0));
2480 assert(Result.getValueType() >= NVT &&
2481 "This truncation doesn't make sense!");
2482 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2483 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2484 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002485 case Promote:
2486 // The truncation is not required, because we don't guarantee anything
2487 // about high bits anyway.
2488 Result = PromoteOp(Node->getOperand(0));
2489 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002490 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002491 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2492 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002493 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002494 }
2495 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002496 case ISD::SIGN_EXTEND:
2497 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002498 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002499 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2500 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2501 case Legal:
2502 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002503 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002504 break;
2505 case Promote:
2506 // Promote the reg if it's smaller.
2507 Result = PromoteOp(Node->getOperand(0));
2508 // The high bits are not guaranteed to be anything. Insert an extend.
2509 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002510 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002511 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002512 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002513 Result = DAG.getZeroExtendInReg(Result,
2514 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002515 break;
2516 }
2517 break;
Chris Lattner35481892005-12-23 00:16:34 +00002518 case ISD::BIT_CONVERT:
2519 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2520 Result = PromoteOp(Result);
2521 break;
2522
Chris Lattner8b6fa222005-01-15 22:16:26 +00002523 case ISD::FP_EXTEND:
2524 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2525 case ISD::FP_ROUND:
2526 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2527 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2528 case Promote: assert(0 && "Unreachable with 2 FP types!");
2529 case Legal:
2530 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002531 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002532 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002533 break;
2534 }
2535 break;
2536
2537 case ISD::SINT_TO_FP:
2538 case ISD::UINT_TO_FP:
2539 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2540 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002541 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002542 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002543 break;
2544
2545 case Promote:
2546 Result = PromoteOp(Node->getOperand(0));
2547 if (Node->getOpcode() == ISD::SINT_TO_FP)
2548 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002549 Result,
2550 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002551 else
Chris Lattner23993562005-04-13 02:38:47 +00002552 Result = DAG.getZeroExtendInReg(Result,
2553 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002554 // No extra round required here.
2555 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002556 break;
2557 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002558 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2559 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002560 // Round if we cannot tolerate excess precision.
2561 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002562 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2563 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002564 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002565 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002566 break;
2567
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002568 case ISD::SIGN_EXTEND_INREG:
2569 Result = PromoteOp(Node->getOperand(0));
2570 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2571 Node->getOperand(1));
2572 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002573 case ISD::FP_TO_SINT:
2574 case ISD::FP_TO_UINT:
2575 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2576 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002577 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002578 break;
2579 case Promote:
2580 // The input result is prerounded, so we don't have to do anything
2581 // special.
2582 Tmp1 = PromoteOp(Node->getOperand(0));
2583 break;
2584 case Expand:
2585 assert(0 && "not implemented");
2586 }
Nate Begemand2558e32005-08-14 01:20:53 +00002587 // If we're promoting a UINT to a larger size, check to see if the new node
2588 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2589 // we can use that instead. This allows us to generate better code for
2590 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2591 // legal, such as PowerPC.
2592 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002593 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002594 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2595 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002596 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2597 } else {
2598 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2599 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002600 break;
2601
Chris Lattner2c8086f2005-04-02 05:00:07 +00002602 case ISD::FABS:
2603 case ISD::FNEG:
2604 Tmp1 = PromoteOp(Node->getOperand(0));
2605 assert(Tmp1.getValueType() == NVT);
2606 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2607 // NOTE: we do not have to do any extra rounding here for
2608 // NoExcessFPPrecision, because we know the input will have the appropriate
2609 // precision, and these operations don't modify precision at all.
2610 break;
2611
Chris Lattnerda6ba872005-04-28 21:44:33 +00002612 case ISD::FSQRT:
2613 case ISD::FSIN:
2614 case ISD::FCOS:
2615 Tmp1 = PromoteOp(Node->getOperand(0));
2616 assert(Tmp1.getValueType() == NVT);
2617 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002618 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002619 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2620 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002621 break;
2622
Chris Lattner03c85462005-01-15 05:21:40 +00002623 case ISD::AND:
2624 case ISD::OR:
2625 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002626 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002627 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002628 case ISD::MUL:
2629 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002630 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002631 // that too is okay if they are integer operations.
2632 Tmp1 = PromoteOp(Node->getOperand(0));
2633 Tmp2 = PromoteOp(Node->getOperand(1));
2634 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2635 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002636 break;
2637 case ISD::FADD:
2638 case ISD::FSUB:
2639 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002640 Tmp1 = PromoteOp(Node->getOperand(0));
2641 Tmp2 = PromoteOp(Node->getOperand(1));
2642 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2643 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2644
2645 // Floating point operations will give excess precision that we may not be
2646 // able to tolerate. If we DO allow excess precision, just leave it,
2647 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002648 // FIXME: Why would we need to round FP ops more than integer ones?
2649 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002650 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002651 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2652 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002653 break;
2654
Chris Lattner8b6fa222005-01-15 22:16:26 +00002655 case ISD::SDIV:
2656 case ISD::SREM:
2657 // These operators require that their input be sign extended.
2658 Tmp1 = PromoteOp(Node->getOperand(0));
2659 Tmp2 = PromoteOp(Node->getOperand(1));
2660 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002661 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2662 DAG.getValueType(VT));
2663 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2664 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002665 }
2666 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2667
2668 // Perform FP_ROUND: this is probably overly pessimistic.
2669 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002670 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2671 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002672 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002673 case ISD::FDIV:
2674 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002675 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002676 // These operators require that their input be fp extended.
2677 Tmp1 = PromoteOp(Node->getOperand(0));
2678 Tmp2 = PromoteOp(Node->getOperand(1));
2679 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2680
2681 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002682 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002683 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2684 DAG.getValueType(VT));
2685 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002686
2687 case ISD::UDIV:
2688 case ISD::UREM:
2689 // These operators require that their input be zero extended.
2690 Tmp1 = PromoteOp(Node->getOperand(0));
2691 Tmp2 = PromoteOp(Node->getOperand(1));
2692 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002693 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2694 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002695 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2696 break;
2697
2698 case ISD::SHL:
2699 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002700 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002701 break;
2702 case ISD::SRA:
2703 // The input value must be properly sign extended.
2704 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002705 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2706 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002707 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002708 break;
2709 case ISD::SRL:
2710 // The input value must be properly zero extended.
2711 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002712 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002713 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002714 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002715
2716 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002717 Tmp1 = Node->getOperand(0); // Get the chain.
2718 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002719 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2720 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2721 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2722 } else {
2723 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2724 Node->getOperand(2));
2725 // Increment the pointer, VAList, to the next vaarg
2726 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2727 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2728 TLI.getPointerTy()));
2729 // Store the incremented VAList to the legalized pointer
2730 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2731 Node->getOperand(2));
2732 // Load the actual argument out of the pointer VAList
2733 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2734 DAG.getSrcValue(0), VT);
2735 }
2736 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002737 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002738 break;
2739
Chris Lattner03c85462005-01-15 05:21:40 +00002740 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002741 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2742 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002743 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002744 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002745 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002746 case ISD::SEXTLOAD:
2747 case ISD::ZEXTLOAD:
2748 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002749 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2750 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002751 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002752 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002753 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002754 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002755 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002756 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2757 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002758 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002759 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002760 case ISD::SELECT_CC:
2761 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2762 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2763 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002764 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002765 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002766 case ISD::BSWAP:
2767 Tmp1 = Node->getOperand(0);
2768 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2769 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2770 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2771 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2772 TLI.getShiftAmountTy()));
2773 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002774 case ISD::CTPOP:
2775 case ISD::CTTZ:
2776 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002777 // Zero extend the argument
2778 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002779 // Perform the larger operation, then subtract if needed.
2780 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002781 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002782 case ISD::CTPOP:
2783 Result = Tmp1;
2784 break;
2785 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002786 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002787 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002788 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002789 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002790 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002791 break;
2792 case ISD::CTLZ:
2793 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002794 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2795 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002796 getSizeInBits(VT), NVT));
2797 break;
2798 }
2799 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002800 }
2801
2802 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002803
2804 // Make sure the result is itself legal.
2805 Result = LegalizeOp(Result);
2806
2807 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002808 AddPromotedOperand(Op, Result);
2809 return Result;
2810}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002811
Nate Begeman750ac1b2006-02-01 07:19:44 +00002812/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2813/// with condition CC on the current target. This usually involves legalizing
2814/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2815/// there may be no choice but to create a new SetCC node to represent the
2816/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2817/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2818void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2819 SDOperand &RHS,
2820 SDOperand &CC) {
2821 SDOperand Tmp1, Tmp2, Result;
2822
2823 switch (getTypeAction(LHS.getValueType())) {
2824 case Legal:
2825 Tmp1 = LegalizeOp(LHS); // LHS
2826 Tmp2 = LegalizeOp(RHS); // RHS
2827 break;
2828 case Promote:
2829 Tmp1 = PromoteOp(LHS); // LHS
2830 Tmp2 = PromoteOp(RHS); // RHS
2831
2832 // If this is an FP compare, the operands have already been extended.
2833 if (MVT::isInteger(LHS.getValueType())) {
2834 MVT::ValueType VT = LHS.getValueType();
2835 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2836
2837 // Otherwise, we have to insert explicit sign or zero extends. Note
2838 // that we could insert sign extends for ALL conditions, but zero extend
2839 // is cheaper on many machines (an AND instead of two shifts), so prefer
2840 // it.
2841 switch (cast<CondCodeSDNode>(CC)->get()) {
2842 default: assert(0 && "Unknown integer comparison!");
2843 case ISD::SETEQ:
2844 case ISD::SETNE:
2845 case ISD::SETUGE:
2846 case ISD::SETUGT:
2847 case ISD::SETULE:
2848 case ISD::SETULT:
2849 // ALL of these operations will work if we either sign or zero extend
2850 // the operands (including the unsigned comparisons!). Zero extend is
2851 // usually a simpler/cheaper operation, so prefer it.
2852 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2853 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2854 break;
2855 case ISD::SETGE:
2856 case ISD::SETGT:
2857 case ISD::SETLT:
2858 case ISD::SETLE:
2859 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2860 DAG.getValueType(VT));
2861 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2862 DAG.getValueType(VT));
2863 break;
2864 }
2865 }
2866 break;
2867 case Expand:
2868 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2869 ExpandOp(LHS, LHSLo, LHSHi);
2870 ExpandOp(RHS, RHSLo, RHSHi);
2871 switch (cast<CondCodeSDNode>(CC)->get()) {
2872 case ISD::SETEQ:
2873 case ISD::SETNE:
2874 if (RHSLo == RHSHi)
2875 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2876 if (RHSCST->isAllOnesValue()) {
2877 // Comparison to -1.
2878 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2879 Tmp2 = RHSLo;
2880 break;
2881 }
2882
2883 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2884 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2885 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2886 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2887 break;
2888 default:
2889 // If this is a comparison of the sign bit, just look at the top part.
2890 // X > -1, x < 0
2891 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2892 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2893 CST->getValue() == 0) || // X < 0
2894 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2895 CST->isAllOnesValue())) { // X > -1
2896 Tmp1 = LHSHi;
2897 Tmp2 = RHSHi;
2898 break;
2899 }
2900
2901 // FIXME: This generated code sucks.
2902 ISD::CondCode LowCC;
2903 switch (cast<CondCodeSDNode>(CC)->get()) {
2904 default: assert(0 && "Unknown integer setcc!");
2905 case ISD::SETLT:
2906 case ISD::SETULT: LowCC = ISD::SETULT; break;
2907 case ISD::SETGT:
2908 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2909 case ISD::SETLE:
2910 case ISD::SETULE: LowCC = ISD::SETULE; break;
2911 case ISD::SETGE:
2912 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2913 }
2914
2915 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2916 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2917 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2918
2919 // NOTE: on targets without efficient SELECT of bools, we can always use
2920 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2921 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2922 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2923 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2924 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2925 Result, Tmp1, Tmp2));
2926 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002927 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002928 }
2929 }
2930 LHS = Tmp1;
2931 RHS = Tmp2;
2932}
2933
Chris Lattner35481892005-12-23 00:16:34 +00002934/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002935/// The resultant code need not be legal. Note that SrcOp is the input operand
2936/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002937SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2938 SDOperand SrcOp) {
2939 // Create the stack frame object.
2940 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2941 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002942 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002943 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2944
2945 // Emit a store to the stack slot.
2946 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002947 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002948 // Result is a load from the stack slot.
2949 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2950}
2951
Chris Lattner5b359c62005-04-02 04:00:59 +00002952void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2953 SDOperand Op, SDOperand Amt,
2954 SDOperand &Lo, SDOperand &Hi) {
2955 // Expand the subcomponents.
2956 SDOperand LHSL, LHSH;
2957 ExpandOp(Op, LHSL, LHSH);
2958
2959 std::vector<SDOperand> Ops;
2960 Ops.push_back(LHSL);
2961 Ops.push_back(LHSH);
2962 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002963 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002964 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002965 Hi = Lo.getValue(1);
2966}
2967
2968
Chris Lattnere34b3962005-01-19 04:19:40 +00002969/// ExpandShift - Try to find a clever way to expand this shift operation out to
2970/// smaller elements. If we can't find a way that is more efficient than a
2971/// libcall on this target, return false. Otherwise, return true with the
2972/// low-parts expanded into Lo and Hi.
2973bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2974 SDOperand &Lo, SDOperand &Hi) {
2975 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2976 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002977
Chris Lattnere34b3962005-01-19 04:19:40 +00002978 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002979 SDOperand ShAmt = LegalizeOp(Amt);
2980 MVT::ValueType ShTy = ShAmt.getValueType();
2981 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2982 unsigned NVTBits = MVT::getSizeInBits(NVT);
2983
2984 // Handle the case when Amt is an immediate. Other cases are currently broken
2985 // and are disabled.
2986 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2987 unsigned Cst = CN->getValue();
2988 // Expand the incoming operand to be shifted, so that we have its parts
2989 SDOperand InL, InH;
2990 ExpandOp(Op, InL, InH);
2991 switch(Opc) {
2992 case ISD::SHL:
2993 if (Cst > VTBits) {
2994 Lo = DAG.getConstant(0, NVT);
2995 Hi = DAG.getConstant(0, NVT);
2996 } else if (Cst > NVTBits) {
2997 Lo = DAG.getConstant(0, NVT);
2998 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002999 } else if (Cst == NVTBits) {
3000 Lo = DAG.getConstant(0, NVT);
3001 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003002 } else {
3003 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3004 Hi = DAG.getNode(ISD::OR, NVT,
3005 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3006 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3007 }
3008 return true;
3009 case ISD::SRL:
3010 if (Cst > VTBits) {
3011 Lo = DAG.getConstant(0, NVT);
3012 Hi = DAG.getConstant(0, NVT);
3013 } else if (Cst > NVTBits) {
3014 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3015 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003016 } else if (Cst == NVTBits) {
3017 Lo = InH;
3018 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003019 } else {
3020 Lo = DAG.getNode(ISD::OR, NVT,
3021 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3022 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3023 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3024 }
3025 return true;
3026 case ISD::SRA:
3027 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003028 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003029 DAG.getConstant(NVTBits-1, ShTy));
3030 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003031 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003032 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003033 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003034 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003035 } else if (Cst == NVTBits) {
3036 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003037 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003038 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003039 } else {
3040 Lo = DAG.getNode(ISD::OR, NVT,
3041 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3042 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3043 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3044 }
3045 return true;
3046 }
3047 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003048 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003049}
Chris Lattner77e77a62005-01-21 06:05:23 +00003050
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003051
Chris Lattner77e77a62005-01-21 06:05:23 +00003052// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3053// does not fit into a register, return the lo part and set the hi part to the
3054// by-reg argument. If it does fit into a single register, return the result
3055// and leave the Hi part unset.
3056SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3057 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003058 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3059 // The input chain to this libcall is the entry node of the function.
3060 // Legalizing the call will automatically add the previous call to the
3061 // dependence.
3062 SDOperand InChain = DAG.getEntryNode();
3063
Chris Lattner77e77a62005-01-21 06:05:23 +00003064 TargetLowering::ArgListTy Args;
3065 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3066 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3067 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3068 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3069 }
3070 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003071
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003072 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003073 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003074 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003075 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3076 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003077
Chris Lattner6831a812006-02-13 09:18:02 +00003078 // Legalize the call sequence, starting with the chain. This will advance
3079 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3080 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3081 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003082 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003083 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003084 default: assert(0 && "Unknown thing");
3085 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003086 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003087 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003088 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003089 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003090 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003091 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003092 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003093}
3094
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003095
Chris Lattner77e77a62005-01-21 06:05:23 +00003096/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3097/// destination type is legal.
3098SDOperand SelectionDAGLegalize::
3099ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003100 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003101 assert(getTypeAction(Source.getValueType()) == Expand &&
3102 "This is not an expansion!");
3103 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3104
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003105 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003106 assert(Source.getValueType() == MVT::i64 &&
3107 "This only works for 64-bit -> FP");
3108 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3109 // incoming integer is set. To handle this, we dynamically test to see if
3110 // it is set, and, if so, add a fudge factor.
3111 SDOperand Lo, Hi;
3112 ExpandOp(Source, Lo, Hi);
3113
Chris Lattner66de05b2005-05-13 04:45:13 +00003114 // If this is unsigned, and not supported, first perform the conversion to
3115 // signed, then adjust the result if the sign bit is set.
3116 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3117 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3118
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003119 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3120 DAG.getConstant(0, Hi.getValueType()),
3121 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003122 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3123 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3124 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003125 uint64_t FF = 0x5f800000ULL;
3126 if (TLI.isLittleEndian()) FF <<= 32;
3127 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003128
Chris Lattner5839bf22005-08-26 17:15:30 +00003129 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003130 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3131 SDOperand FudgeInReg;
3132 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003133 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3134 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003135 else {
3136 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003137 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3138 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003139 }
Chris Lattner473a9902005-09-29 06:44:39 +00003140 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003141 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003142
Chris Lattnera88a2602005-05-14 05:33:54 +00003143 // Check to see if the target has a custom way to lower this. If so, use it.
3144 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3145 default: assert(0 && "This action not implemented for this operation!");
3146 case TargetLowering::Legal:
3147 case TargetLowering::Expand:
3148 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003149 case TargetLowering::Custom: {
3150 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3151 Source), DAG);
3152 if (NV.Val)
3153 return LegalizeOp(NV);
3154 break; // The target decided this was legal after all
3155 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003156 }
3157
Chris Lattner13689e22005-05-12 07:00:44 +00003158 // Expand the source, then glue it back together for the call. We must expand
3159 // the source in case it is shared (this pass of legalize must traverse it).
3160 SDOperand SrcLo, SrcHi;
3161 ExpandOp(Source, SrcLo, SrcHi);
3162 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3163
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003164 const char *FnName = 0;
3165 if (DestTy == MVT::f32)
3166 FnName = "__floatdisf";
3167 else {
3168 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3169 FnName = "__floatdidf";
3170 }
Chris Lattner6831a812006-02-13 09:18:02 +00003171
3172 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3173 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003174 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003175}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003176
Chris Lattner22cde6a2006-01-28 08:25:58 +00003177/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3178/// INT_TO_FP operation of the specified operand when the target requests that
3179/// we expand it. At this point, we know that the result and operand types are
3180/// legal for the target.
3181SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3182 SDOperand Op0,
3183 MVT::ValueType DestVT) {
3184 if (Op0.getValueType() == MVT::i32) {
3185 // simple 32-bit [signed|unsigned] integer to float/double expansion
3186
3187 // get the stack frame index of a 8 byte buffer
3188 MachineFunction &MF = DAG.getMachineFunction();
3189 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3190 // get address of 8 byte buffer
3191 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3192 // word offset constant for Hi/Lo address computation
3193 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3194 // set up Hi and Lo (into buffer) address based on endian
3195 SDOperand Hi, Lo;
3196 if (TLI.isLittleEndian()) {
3197 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3198 Lo = StackSlot;
3199 } else {
3200 Hi = StackSlot;
3201 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3202 }
3203 // if signed map to unsigned space
3204 SDOperand Op0Mapped;
3205 if (isSigned) {
3206 // constant used to invert sign bit (signed to unsigned mapping)
3207 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3208 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3209 } else {
3210 Op0Mapped = Op0;
3211 }
3212 // store the lo of the constructed double - based on integer input
3213 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3214 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3215 // initial hi portion of constructed double
3216 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3217 // store the hi of the constructed double - biased exponent
3218 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3219 InitialHi, Hi, DAG.getSrcValue(NULL));
3220 // load the constructed double
3221 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3222 DAG.getSrcValue(NULL));
3223 // FP constant to bias correct the final result
3224 SDOperand Bias = DAG.getConstantFP(isSigned ?
3225 BitsToDouble(0x4330000080000000ULL)
3226 : BitsToDouble(0x4330000000000000ULL),
3227 MVT::f64);
3228 // subtract the bias
3229 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3230 // final result
3231 SDOperand Result;
3232 // handle final rounding
3233 if (DestVT == MVT::f64) {
3234 // do nothing
3235 Result = Sub;
3236 } else {
3237 // if f32 then cast to f32
3238 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3239 }
3240 return Result;
3241 }
3242 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3243 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3244
3245 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3246 DAG.getConstant(0, Op0.getValueType()),
3247 ISD::SETLT);
3248 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3249 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3250 SignSet, Four, Zero);
3251
3252 // If the sign bit of the integer is set, the large number will be treated
3253 // as a negative number. To counteract this, the dynamic code adds an
3254 // offset depending on the data type.
3255 uint64_t FF;
3256 switch (Op0.getValueType()) {
3257 default: assert(0 && "Unsupported integer type!");
3258 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3259 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3260 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3261 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3262 }
3263 if (TLI.isLittleEndian()) FF <<= 32;
3264 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3265
3266 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3267 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3268 SDOperand FudgeInReg;
3269 if (DestVT == MVT::f32)
3270 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3271 DAG.getSrcValue(NULL));
3272 else {
3273 assert(DestVT == MVT::f64 && "Unexpected conversion");
3274 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3275 DAG.getEntryNode(), CPIdx,
3276 DAG.getSrcValue(NULL), MVT::f32));
3277 }
3278
3279 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3280}
3281
3282/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3283/// *INT_TO_FP operation of the specified operand when the target requests that
3284/// we promote it. At this point, we know that the result and operand types are
3285/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3286/// operation that takes a larger input.
3287SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3288 MVT::ValueType DestVT,
3289 bool isSigned) {
3290 // First step, figure out the appropriate *INT_TO_FP operation to use.
3291 MVT::ValueType NewInTy = LegalOp.getValueType();
3292
3293 unsigned OpToUse = 0;
3294
3295 // Scan for the appropriate larger type to use.
3296 while (1) {
3297 NewInTy = (MVT::ValueType)(NewInTy+1);
3298 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3299
3300 // If the target supports SINT_TO_FP of this type, use it.
3301 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3302 default: break;
3303 case TargetLowering::Legal:
3304 if (!TLI.isTypeLegal(NewInTy))
3305 break; // Can't use this datatype.
3306 // FALL THROUGH.
3307 case TargetLowering::Custom:
3308 OpToUse = ISD::SINT_TO_FP;
3309 break;
3310 }
3311 if (OpToUse) break;
3312 if (isSigned) continue;
3313
3314 // If the target supports UINT_TO_FP of this type, use it.
3315 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3316 default: break;
3317 case TargetLowering::Legal:
3318 if (!TLI.isTypeLegal(NewInTy))
3319 break; // Can't use this datatype.
3320 // FALL THROUGH.
3321 case TargetLowering::Custom:
3322 OpToUse = ISD::UINT_TO_FP;
3323 break;
3324 }
3325 if (OpToUse) break;
3326
3327 // Otherwise, try a larger type.
3328 }
3329
3330 // Okay, we found the operation and type to use. Zero extend our input to the
3331 // desired type then run the operation on it.
3332 return DAG.getNode(OpToUse, DestVT,
3333 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3334 NewInTy, LegalOp));
3335}
3336
3337/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3338/// FP_TO_*INT operation of the specified operand when the target requests that
3339/// we promote it. At this point, we know that the result and operand types are
3340/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3341/// operation that returns a larger result.
3342SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3343 MVT::ValueType DestVT,
3344 bool isSigned) {
3345 // First step, figure out the appropriate FP_TO*INT operation to use.
3346 MVT::ValueType NewOutTy = DestVT;
3347
3348 unsigned OpToUse = 0;
3349
3350 // Scan for the appropriate larger type to use.
3351 while (1) {
3352 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3353 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3354
3355 // If the target supports FP_TO_SINT returning this type, use it.
3356 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3357 default: break;
3358 case TargetLowering::Legal:
3359 if (!TLI.isTypeLegal(NewOutTy))
3360 break; // Can't use this datatype.
3361 // FALL THROUGH.
3362 case TargetLowering::Custom:
3363 OpToUse = ISD::FP_TO_SINT;
3364 break;
3365 }
3366 if (OpToUse) break;
3367
3368 // If the target supports FP_TO_UINT of this type, use it.
3369 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3370 default: break;
3371 case TargetLowering::Legal:
3372 if (!TLI.isTypeLegal(NewOutTy))
3373 break; // Can't use this datatype.
3374 // FALL THROUGH.
3375 case TargetLowering::Custom:
3376 OpToUse = ISD::FP_TO_UINT;
3377 break;
3378 }
3379 if (OpToUse) break;
3380
3381 // Otherwise, try a larger type.
3382 }
3383
3384 // Okay, we found the operation and type to use. Truncate the result of the
3385 // extended FP_TO_*INT operation to the desired size.
3386 return DAG.getNode(ISD::TRUNCATE, DestVT,
3387 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3388}
3389
3390/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3391///
3392SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3393 MVT::ValueType VT = Op.getValueType();
3394 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3395 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3396 switch (VT) {
3397 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3398 case MVT::i16:
3399 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3400 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3401 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3402 case MVT::i32:
3403 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3404 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3405 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3406 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3407 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3408 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3409 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3410 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3411 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3412 case MVT::i64:
3413 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3414 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3415 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3416 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3417 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3418 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3419 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3420 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3421 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3422 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3423 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3424 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3425 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3426 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3427 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3428 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3429 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3430 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3431 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3432 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3433 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3434 }
3435}
3436
3437/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3438///
3439SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3440 switch (Opc) {
3441 default: assert(0 && "Cannot expand this yet!");
3442 case ISD::CTPOP: {
3443 static const uint64_t mask[6] = {
3444 0x5555555555555555ULL, 0x3333333333333333ULL,
3445 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3446 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3447 };
3448 MVT::ValueType VT = Op.getValueType();
3449 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3450 unsigned len = getSizeInBits(VT);
3451 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3452 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3453 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3454 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3455 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3456 DAG.getNode(ISD::AND, VT,
3457 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3458 }
3459 return Op;
3460 }
3461 case ISD::CTLZ: {
3462 // for now, we do this:
3463 // x = x | (x >> 1);
3464 // x = x | (x >> 2);
3465 // ...
3466 // x = x | (x >>16);
3467 // x = x | (x >>32); // for 64-bit input
3468 // return popcount(~x);
3469 //
3470 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3471 MVT::ValueType VT = Op.getValueType();
3472 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3473 unsigned len = getSizeInBits(VT);
3474 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3475 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3476 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3477 }
3478 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3479 return DAG.getNode(ISD::CTPOP, VT, Op);
3480 }
3481 case ISD::CTTZ: {
3482 // for now, we use: { return popcount(~x & (x - 1)); }
3483 // unless the target has ctlz but not ctpop, in which case we use:
3484 // { return 32 - nlz(~x & (x-1)); }
3485 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3486 MVT::ValueType VT = Op.getValueType();
3487 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3488 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3489 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3490 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3491 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3492 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3493 TLI.isOperationLegal(ISD::CTLZ, VT))
3494 return DAG.getNode(ISD::SUB, VT,
3495 DAG.getConstant(getSizeInBits(VT), VT),
3496 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3497 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3498 }
3499 }
3500}
Chris Lattnere34b3962005-01-19 04:19:40 +00003501
3502
Chris Lattner3e928bb2005-01-07 07:47:09 +00003503/// ExpandOp - Expand the specified SDOperand into its two component pieces
3504/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3505/// LegalizeNodes map is filled in for any results that are not expanded, the
3506/// ExpandedNodes map is filled in for any results that are expanded, and the
3507/// Lo/Hi values are returned.
3508void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3509 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003510 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003511 SDNode *Node = Op.Val;
3512 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003513 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3514 "Cannot expand FP values!");
3515 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003516 "Cannot expand to FP value or to larger int value!");
3517
Chris Lattner6fdcb252005-09-02 20:32:45 +00003518 // See if we already expanded it.
3519 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3520 = ExpandedNodes.find(Op);
3521 if (I != ExpandedNodes.end()) {
3522 Lo = I->second.first;
3523 Hi = I->second.second;
3524 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003525 }
3526
Chris Lattner3e928bb2005-01-07 07:47:09 +00003527 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003528 case ISD::CopyFromReg:
3529 assert(0 && "CopyFromReg must be legal!");
3530 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003531 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3532 assert(0 && "Do not know how to expand this operator!");
3533 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003534 case ISD::UNDEF:
3535 Lo = DAG.getNode(ISD::UNDEF, NVT);
3536 Hi = DAG.getNode(ISD::UNDEF, NVT);
3537 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003538 case ISD::Constant: {
3539 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3540 Lo = DAG.getConstant(Cst, NVT);
3541 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3542 break;
3543 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003544 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003545 // Return the operands.
3546 Lo = Node->getOperand(0);
3547 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003548 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003549
3550 case ISD::SIGN_EXTEND_INREG:
3551 ExpandOp(Node->getOperand(0), Lo, Hi);
3552 // Sign extend the lo-part.
3553 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3554 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3555 TLI.getShiftAmountTy()));
3556 // sext_inreg the low part if needed.
3557 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3558 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003559
Nate Begemand88fc032006-01-14 03:14:10 +00003560 case ISD::BSWAP: {
3561 ExpandOp(Node->getOperand(0), Lo, Hi);
3562 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3563 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3564 Lo = TempLo;
3565 break;
3566 }
3567
Chris Lattneredb1add2005-05-11 04:51:16 +00003568 case ISD::CTPOP:
3569 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003570 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3571 DAG.getNode(ISD::CTPOP, NVT, Lo),
3572 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003573 Hi = DAG.getConstant(0, NVT);
3574 break;
3575
Chris Lattner39a8f332005-05-12 19:05:01 +00003576 case ISD::CTLZ: {
3577 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003578 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003579 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3580 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003581 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3582 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003583 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3584 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3585
3586 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3587 Hi = DAG.getConstant(0, NVT);
3588 break;
3589 }
3590
3591 case ISD::CTTZ: {
3592 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003593 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003594 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3595 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003596 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3597 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003598 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3599 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3600
3601 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3602 Hi = DAG.getConstant(0, NVT);
3603 break;
3604 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003605
Nate Begemanacc398c2006-01-25 18:21:52 +00003606 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003607 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3608 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003609 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3610 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3611
3612 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003613 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003614 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3615 if (!TLI.isLittleEndian())
3616 std::swap(Lo, Hi);
3617 break;
3618 }
3619
Chris Lattner3e928bb2005-01-07 07:47:09 +00003620 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003621 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3622 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003623 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003624
3625 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003626 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003627 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3628 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003629 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003630 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003631
3632 // Build a factor node to remember that this load is independent of the
3633 // other one.
3634 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3635 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003636
Chris Lattner3e928bb2005-01-07 07:47:09 +00003637 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003638 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003639 if (!TLI.isLittleEndian())
3640 std::swap(Lo, Hi);
3641 break;
3642 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003643 case ISD::AND:
3644 case ISD::OR:
3645 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3646 SDOperand LL, LH, RL, RH;
3647 ExpandOp(Node->getOperand(0), LL, LH);
3648 ExpandOp(Node->getOperand(1), RL, RH);
3649 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3650 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3651 break;
3652 }
3653 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003654 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003655 ExpandOp(Node->getOperand(1), LL, LH);
3656 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003657 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3658 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003659 break;
3660 }
Nate Begeman9373a812005-08-10 20:51:12 +00003661 case ISD::SELECT_CC: {
3662 SDOperand TL, TH, FL, FH;
3663 ExpandOp(Node->getOperand(2), TL, TH);
3664 ExpandOp(Node->getOperand(3), FL, FH);
3665 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3666 Node->getOperand(1), TL, FL, Node->getOperand(4));
3667 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3668 Node->getOperand(1), TH, FH, Node->getOperand(4));
3669 break;
3670 }
Nate Begeman144ff662005-10-13 17:15:37 +00003671 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003672 SDOperand Chain = Node->getOperand(0);
3673 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003674 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3675
3676 if (EVT == NVT)
3677 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3678 else
3679 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3680 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003681
3682 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003683 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003684
Nate Begeman144ff662005-10-13 17:15:37 +00003685 // The high part is obtained by SRA'ing all but one of the bits of the lo
3686 // part.
3687 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3688 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3689 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003690 break;
3691 }
3692 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003693 SDOperand Chain = Node->getOperand(0);
3694 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003695 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3696
3697 if (EVT == NVT)
3698 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3699 else
3700 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3701 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003702
3703 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003704 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003705
Nate Begeman144ff662005-10-13 17:15:37 +00003706 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003707 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003708 break;
3709 }
3710 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003711 SDOperand Chain = Node->getOperand(0);
3712 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003713 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3714
3715 if (EVT == NVT)
3716 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3717 else
3718 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3719 EVT);
3720
3721 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003722 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003723
3724 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003725 Hi = DAG.getNode(ISD::UNDEF, NVT);
3726 break;
3727 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003728 case ISD::ANY_EXTEND:
3729 // The low part is any extension of the input (which degenerates to a copy).
3730 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3731 // The high part is undefined.
3732 Hi = DAG.getNode(ISD::UNDEF, NVT);
3733 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003734 case ISD::SIGN_EXTEND: {
3735 // The low part is just a sign extension of the input (which degenerates to
3736 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003737 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003738
Chris Lattner3e928bb2005-01-07 07:47:09 +00003739 // The high part is obtained by SRA'ing all but one of the bits of the lo
3740 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003741 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003742 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3743 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003744 break;
3745 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003746 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003747 // The low part is just a zero extension of the input (which degenerates to
3748 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003749 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003750
Chris Lattner3e928bb2005-01-07 07:47:09 +00003751 // The high part is just a zero.
3752 Hi = DAG.getConstant(0, NVT);
3753 break;
Chris Lattner35481892005-12-23 00:16:34 +00003754
3755 case ISD::BIT_CONVERT: {
3756 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3757 Node->getOperand(0));
3758 ExpandOp(Tmp, Lo, Hi);
3759 break;
3760 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003761
Chris Lattner8137c9e2006-01-28 05:07:51 +00003762 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003763 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3764 TargetLowering::Custom &&
3765 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003766 Lo = TLI.LowerOperation(Op, DAG);
3767 assert(Lo.Val && "Node must be custom expanded!");
3768 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003769 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003770 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003771 break;
3772
Chris Lattner4e6c7462005-01-08 19:27:05 +00003773 // These operators cannot be expanded directly, emit them as calls to
3774 // library functions.
3775 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003776 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003777 SDOperand Op;
3778 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3779 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003780 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3781 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003782 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003783
Chris Lattnerf20d1832005-07-30 01:40:57 +00003784 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3785
Chris Lattner80a3e942005-07-29 00:33:32 +00003786 // Now that the custom expander is done, expand the result, which is still
3787 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003788 if (Op.Val) {
3789 ExpandOp(Op, Lo, Hi);
3790 break;
3791 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003792 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003793
Chris Lattner4e6c7462005-01-08 19:27:05 +00003794 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003795 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003796 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003797 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003798 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003799
Chris Lattner4e6c7462005-01-08 19:27:05 +00003800 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003801 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003802 SDOperand Op;
3803 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3804 case Expand: assert(0 && "cannot expand FP!");
3805 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3806 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3807 }
3808
3809 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3810
3811 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003812 if (Op.Val) {
3813 ExpandOp(Op, Lo, Hi);
3814 break;
3815 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003816 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003817
Chris Lattner4e6c7462005-01-08 19:27:05 +00003818 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003819 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003820 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003821 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003822 break;
3823
Evan Cheng05a2d562006-01-09 18:31:59 +00003824 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003825 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003826 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003827 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003828 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003829 Op = TLI.LowerOperation(Op, DAG);
3830 if (Op.Val) {
3831 // Now that the custom expander is done, expand the result, which is
3832 // still VT.
3833 ExpandOp(Op, Lo, Hi);
3834 break;
3835 }
3836 }
3837
Chris Lattnere34b3962005-01-19 04:19:40 +00003838 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003839 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003840 break;
Chris Lattner47599822005-04-02 03:38:53 +00003841
3842 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003843 TargetLowering::LegalizeAction Action =
3844 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3845 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3846 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003847 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003848 break;
3849 }
3850
Chris Lattnere34b3962005-01-19 04:19:40 +00003851 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003852 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003853 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003854 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003855
Evan Cheng05a2d562006-01-09 18:31:59 +00003856 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003857 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003858 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003859 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003860 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003861 Op = TLI.LowerOperation(Op, DAG);
3862 if (Op.Val) {
3863 // Now that the custom expander is done, expand the result, which is
3864 // still VT.
3865 ExpandOp(Op, Lo, Hi);
3866 break;
3867 }
3868 }
3869
Chris Lattnere34b3962005-01-19 04:19:40 +00003870 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003871 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003872 break;
Chris Lattner47599822005-04-02 03:38:53 +00003873
3874 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003875 TargetLowering::LegalizeAction Action =
3876 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3877 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3878 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003879 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003880 break;
3881 }
3882
Chris Lattnere34b3962005-01-19 04:19:40 +00003883 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003884 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003885 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003886 }
3887
3888 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003889 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003890 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003891 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003892 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003893 Op = TLI.LowerOperation(Op, DAG);
3894 if (Op.Val) {
3895 // Now that the custom expander is done, expand the result, which is
3896 // still VT.
3897 ExpandOp(Op, Lo, Hi);
3898 break;
3899 }
3900 }
3901
Chris Lattnere34b3962005-01-19 04:19:40 +00003902 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003903 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003904 break;
Chris Lattner47599822005-04-02 03:38:53 +00003905
3906 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003907 TargetLowering::LegalizeAction Action =
3908 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3909 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3910 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003911 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003912 break;
3913 }
3914
Chris Lattnere34b3962005-01-19 04:19:40 +00003915 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003916 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003917 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003918 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003919
Misha Brukmanedf128a2005-04-21 22:36:52 +00003920 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003921 case ISD::SUB: {
3922 // If the target wants to custom expand this, let them.
3923 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3924 TargetLowering::Custom) {
3925 Op = TLI.LowerOperation(Op, DAG);
3926 if (Op.Val) {
3927 ExpandOp(Op, Lo, Hi);
3928 break;
3929 }
3930 }
3931
3932 // Expand the subcomponents.
3933 SDOperand LHSL, LHSH, RHSL, RHSH;
3934 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3935 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00003936 std::vector<MVT::ValueType> VTs;
3937 std::vector<SDOperand> LoOps, HiOps;
3938 VTs.push_back(LHSL.getValueType());
3939 VTs.push_back(MVT::Flag);
3940 LoOps.push_back(LHSL);
3941 LoOps.push_back(RHSL);
3942 HiOps.push_back(LHSH);
3943 HiOps.push_back(RHSH);
3944 if (Node->getOpcode() == ISD::ADD) {
3945 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
3946 HiOps.push_back(Lo.getValue(1));
3947 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
3948 } else {
3949 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
3950 HiOps.push_back(Lo.getValue(1));
3951 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
3952 }
Chris Lattner84f67882005-01-20 18:52:28 +00003953 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00003954 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003955 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003956 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003957 SDOperand LL, LH, RL, RH;
3958 ExpandOp(Node->getOperand(0), LL, LH);
3959 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003960 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3961 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3962 // extended the sign bit of the low half through the upper half, and if so
3963 // emit a MULHS instead of the alternate sequence that is valid for any
3964 // i64 x i64 multiply.
3965 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3966 // is RH an extension of the sign bit of RL?
3967 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3968 RH.getOperand(1).getOpcode() == ISD::Constant &&
3969 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3970 // is LH an extension of the sign bit of LL?
3971 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3972 LH.getOperand(1).getOpcode() == ISD::Constant &&
3973 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3974 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3975 } else {
3976 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3977 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3978 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3979 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3980 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3981 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003982 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3983 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00003984 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00003985 }
3986 break;
3987 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003988 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3989 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3990 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3991 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003992 }
3993
Chris Lattner83397362005-12-21 18:02:52 +00003994 // Make sure the resultant values have been legalized themselves, unless this
3995 // is a type that requires multi-step expansion.
3996 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3997 Lo = LegalizeOp(Lo);
3998 Hi = LegalizeOp(Hi);
3999 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004000
4001 // Remember in a map if the values will be reused later.
4002 bool isNew =
4003 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4004 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004005}
4006
Chris Lattnerc7029802006-03-18 01:44:44 +00004007/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4008/// two smaller values of MVT::Vector type.
4009void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4010 SDOperand &Hi) {
4011 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4012 SDNode *Node = Op.Val;
4013 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4014 assert(NumElements > 1 && "Cannot split a single element vector!");
4015 unsigned NewNumElts = NumElements/2;
4016 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4017 SDOperand TypeNode = *(Node->op_end()-1);
4018
4019 // See if we already split it.
4020 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4021 = SplitNodes.find(Op);
4022 if (I != SplitNodes.end()) {
4023 Lo = I->second.first;
4024 Hi = I->second.second;
4025 return;
4026 }
4027
4028 switch (Node->getOpcode()) {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004029 default: assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004030 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004031 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4032 LoOps.push_back(NewNumEltsNode);
4033 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004034 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004035
4036 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4037 HiOps.push_back(NewNumEltsNode);
4038 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004039 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004040 break;
4041 }
4042 case ISD::VADD:
4043 case ISD::VSUB:
4044 case ISD::VMUL:
4045 case ISD::VSDIV:
4046 case ISD::VUDIV:
4047 case ISD::VAND:
4048 case ISD::VOR:
4049 case ISD::VXOR: {
4050 SDOperand LL, LH, RL, RH;
4051 SplitVectorOp(Node->getOperand(0), LL, LH);
4052 SplitVectorOp(Node->getOperand(1), RL, RH);
4053
4054 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4055 NewNumEltsNode, TypeNode);
4056 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4057 NewNumEltsNode, TypeNode);
4058 break;
4059 }
4060 case ISD::VLOAD: {
4061 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4062 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4063 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4064
4065 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4066 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4067 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4068 getIntPtrConstant(IncrementSize));
4069 // FIXME: This creates a bogus srcvalue!
4070 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4071
4072 // Build a factor node to remember that this load is independent of the
4073 // other one.
4074 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4075 Hi.getValue(1));
4076
4077 // Remember that we legalized the chain.
4078 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4079 if (!TLI.isLittleEndian())
4080 std::swap(Lo, Hi);
4081 break;
4082 }
4083 }
4084
4085 // Remember in a map if the values will be reused later.
4086 bool isNew =
4087 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4088 assert(isNew && "Value already expanded?!?");
4089}
4090
4091
4092/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4093/// equivalent operation that returns a scalar (e.g. F32) or packed value
4094/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4095/// type for the result.
4096SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4097 MVT::ValueType NewVT) {
4098 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4099 SDNode *Node = Op.Val;
4100
4101 // See if we already packed it.
4102 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4103 if (I != PackedNodes.end()) return I->second;
4104
4105 SDOperand Result;
4106 switch (Node->getOpcode()) {
4107 default: assert(0 && "Unknown vector operation!");
4108 case ISD::VADD:
4109 case ISD::VSUB:
4110 case ISD::VMUL:
4111 case ISD::VSDIV:
4112 case ISD::VUDIV:
4113 case ISD::VAND:
4114 case ISD::VOR:
4115 case ISD::VXOR:
4116 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4117 NewVT,
4118 PackVectorOp(Node->getOperand(0), NewVT),
4119 PackVectorOp(Node->getOperand(1), NewVT));
4120 break;
4121 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004122 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4123 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004124
Chris Lattner4794a6b2006-03-19 00:07:49 +00004125 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004126
4127 // Remember that we legalized the chain.
4128 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4129 break;
4130 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004131 case ISD::VBUILD_VECTOR:
Chris Lattnerc7029802006-03-18 01:44:44 +00004132 if (!MVT::isVector(NewVT)) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004133 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004134 Result = Node->getOperand(0);
4135 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004136 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004137 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004138 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004139 }
4140 break;
4141 }
4142
4143 if (TLI.isTypeLegal(NewVT))
4144 Result = LegalizeOp(Result);
4145 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4146 assert(isNew && "Value already packed?");
4147 return Result;
4148}
4149
Chris Lattner3e928bb2005-01-07 07:47:09 +00004150
4151// SelectionDAG::Legalize - This is the entry point for the file.
4152//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004153void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004154 /// run - This is the main entry point to this class.
4155 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004156 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004157}
4158