blob: 5de11ff00ceeae7c9e7fcdb6426b594d762fe16a [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;
410 ExpandOp(Op, X, Y);
411 }
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:
455 case ISD::TargetConstantVec:
Chris Lattner948c1b12006-01-28 08:31:04 +0000456 case ISD::TargetConstantPool:
457 case ISD::TargetGlobalAddress:
458 case ISD::TargetExternalSymbol:
459 case ISD::VALUETYPE:
460 case ISD::SRCVALUE:
461 case ISD::STRING:
462 case ISD::CONDCODE:
463 // Primitives must all be legal.
464 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
465 "This must be legal!");
466 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000467 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000468 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
469 // If this is a target node, legalize it by legalizing the operands then
470 // passing it through.
471 std::vector<SDOperand> Ops;
472 bool Changed = false;
473 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
474 Ops.push_back(LegalizeOp(Node->getOperand(i)));
475 Changed = Changed || Node->getOperand(i) != Ops.back();
476 }
477 if (Changed)
478 if (Node->getNumValues() == 1)
479 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
480 else {
481 std::vector<MVT::ValueType> VTs(Node->value_begin(),
482 Node->value_end());
483 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
484 }
485
486 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
487 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
488 return Result.getValue(Op.ResNo);
489 }
490 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000491 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
492 assert(0 && "Do not know how to legalize this operator!");
493 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000494 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000495 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000496 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000497 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
498 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000499 case TargetLowering::Custom:
500 Tmp1 = TLI.LowerOperation(Op, DAG);
501 if (Tmp1.Val) Result = Tmp1;
502 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000503 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000504 break;
505 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000506 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000507 case ISD::AssertSext:
508 case ISD::AssertZext:
509 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000510 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000511 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000512 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000513 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000514 Result = Node->getOperand(Op.ResNo);
515 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000516 case ISD::CopyFromReg:
517 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000518 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000519 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000520 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000521 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000522 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000523 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000524 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000525 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
526 } else {
527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
528 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000529 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
530 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000531 // Since CopyFromReg produces two values, make sure to remember that we
532 // legalized both of them.
533 AddLegalizedOperand(Op.getValue(0), Result);
534 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
535 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000536 case ISD::UNDEF: {
537 MVT::ValueType VT = Op.getValueType();
538 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000539 default: assert(0 && "This action is not supported yet!");
540 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000541 if (MVT::isInteger(VT))
542 Result = DAG.getConstant(0, VT);
543 else if (MVT::isFloatingPoint(VT))
544 Result = DAG.getConstantFP(0, VT);
545 else
546 assert(0 && "Unknown value type!");
547 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000548 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000549 break;
550 }
551 break;
552 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000553
554 case ISD::LOCATION:
555 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
556 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
557
558 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
559 case TargetLowering::Promote:
560 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000561 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000562 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000563 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
564 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
565
566 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
567 const std::string &FName =
568 cast<StringSDNode>(Node->getOperand(3))->getValue();
569 const std::string &DirName =
570 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000571 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000572
Jim Laskeye81aecb2005-12-21 20:51:37 +0000573 std::vector<SDOperand> Ops;
574 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000575 SDOperand LineOp = Node->getOperand(1);
576 SDOperand ColOp = Node->getOperand(2);
577
578 if (useDEBUG_LOC) {
579 Ops.push_back(LineOp); // line #
580 Ops.push_back(ColOp); // col #
581 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
582 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
583 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000584 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
585 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000586 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
587 Ops.push_back(DAG.getConstant(ID, MVT::i32));
588 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
589 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000590 } else {
591 Result = Tmp1; // chain
592 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000593 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000594 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000595 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000596 if (Tmp1 != Node->getOperand(0) ||
597 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000598 std::vector<SDOperand> Ops;
599 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000600 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
601 Ops.push_back(Node->getOperand(1)); // line # must be legal.
602 Ops.push_back(Node->getOperand(2)); // col # must be legal.
603 } else {
604 // Otherwise promote them.
605 Ops.push_back(PromoteOp(Node->getOperand(1)));
606 Ops.push_back(PromoteOp(Node->getOperand(2)));
607 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000608 Ops.push_back(Node->getOperand(3)); // filename must be legal.
609 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000610 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000611 }
612 break;
613 }
614 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000615
616 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000617 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000618 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000619 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000620 case TargetLowering::Legal:
621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
622 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
623 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
624 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000625 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000626 break;
627 }
628 break;
629
630 case ISD::DEBUG_LABEL:
631 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
632 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000633 default: assert(0 && "This action is not supported yet!");
634 case TargetLowering::Legal:
635 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
636 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000638 break;
639 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000640 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000641
Chris Lattner3e928bb2005-01-07 07:47:09 +0000642 case ISD::Constant:
643 // We know we don't need to expand constants here, constants only have one
644 // value and we check that it is fine above.
645
646 // FIXME: Maybe we should handle things like targets that don't support full
647 // 32-bit immediates?
648 break;
649 case ISD::ConstantFP: {
650 // Spill FP immediates to the constant pool if the target cannot directly
651 // codegen them. Targets often have some immediate values that can be
652 // efficiently generated into an FP register without a load. We explicitly
653 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000654 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
655
656 // Check to see if this FP immediate is already legal.
657 bool isLegal = false;
658 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
659 E = TLI.legal_fpimm_end(); I != E; ++I)
660 if (CFP->isExactlyValue(*I)) {
661 isLegal = true;
662 break;
663 }
664
Chris Lattner3181a772006-01-29 06:26:56 +0000665 // If this is a legal constant, turn it into a TargetConstantFP node.
666 if (isLegal) {
667 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
668 break;
669 }
670
671 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
672 default: assert(0 && "This action is not supported yet!");
673 case TargetLowering::Custom:
674 Tmp3 = TLI.LowerOperation(Result, DAG);
675 if (Tmp3.Val) {
676 Result = Tmp3;
677 break;
678 }
679 // FALLTHROUGH
680 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000681 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000682 bool Extend = false;
683
Chris Lattner456a93a2006-01-28 07:39:30 +0000684 // If a FP immediate is precise when represented as a float and if the
685 // target can do an extending load from float to double, we put it into
686 // the constant pool as a float, even if it's is statically typed as a
687 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000688 MVT::ValueType VT = CFP->getValueType(0);
689 bool isDouble = VT == MVT::f64;
690 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
691 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000692 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
693 // Only do this if the target has a native EXTLOAD instruction from
694 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000695 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000696 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
697 VT = MVT::f32;
698 Extend = true;
699 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000700
Chris Lattner456a93a2006-01-28 07:39:30 +0000701 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000702 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000703 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
704 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000705 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000706 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
707 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000708 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000709 }
710 break;
711 }
Chris Lattner8ca05e02006-01-29 06:34:16 +0000712 case ISD::ConstantVec:
713 switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) {
714 default: assert(0 && "This action is not supported yet!");
715 case TargetLowering::Custom:
716 Tmp3 = TLI.LowerOperation(Result, DAG);
717 if (Tmp3.Val) {
718 Result = Tmp3;
719 break;
720 }
721 // FALLTHROUGH
722 case TargetLowering::Expand:
723 // We assume that vector constants are not legal, and will be immediately
724 // spilled to the constant pool.
725 //
726 // Create a ConstantPacked, and put it in the constant pool.
727 MVT::ValueType VT = Node->getValueType(0);
728 const Type *OpNTy =
729 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
730 std::vector<Constant*> CV;
731 if (MVT::isFloatingPoint(VT)) {
732 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
733 double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue();
734 CV.push_back(ConstantFP::get(OpNTy, V));
735 }
736 } else {
737 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
738 uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
739 CV.push_back(ConstantUInt::get(OpNTy, V));
740 }
741 }
742 Constant *CP = ConstantPacked::get(CV);
743 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
744 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
745 DAG.getSrcValue(NULL));
746 break;
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000747 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000748 break;
Chris Lattner040c11c2005-11-09 18:48:57 +0000749 case ISD::TokenFactor:
750 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000751 Tmp1 = LegalizeOp(Node->getOperand(0));
752 Tmp2 = LegalizeOp(Node->getOperand(1));
753 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
754 } else if (Node->getNumOperands() == 3) {
755 Tmp1 = LegalizeOp(Node->getOperand(0));
756 Tmp2 = LegalizeOp(Node->getOperand(1));
757 Tmp3 = LegalizeOp(Node->getOperand(2));
758 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000759 } else {
760 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000761 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000762 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
763 Ops.push_back(LegalizeOp(Node->getOperand(i)));
764 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000765 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000766 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000767
Chris Lattner6831a812006-02-13 09:18:02 +0000768 case ISD::CALLSEQ_START: {
769 SDNode *CallEnd = FindCallEndFromCallStart(Node);
770
771 // Recursively Legalize all of the inputs of the call end that do not lead
772 // to this call start. This ensures that any libcalls that need be inserted
773 // are inserted *before* the CALLSEQ_START.
774 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
775 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
776
777 // Now that we legalized all of the inputs (which may have inserted
778 // libcalls) create the new CALLSEQ_START node.
779 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
780
781 // Merge in the last call, to ensure that this call start after the last
782 // call ended.
783 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
784 Tmp1 = LegalizeOp(Tmp1);
785
786 // Do not try to legalize the target-specific arguments (#1+).
787 if (Tmp1 != Node->getOperand(0)) {
788 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
789 Ops[0] = Tmp1;
790 Result = DAG.UpdateNodeOperands(Result, Ops);
791 }
792
793 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000794 AddLegalizedOperand(Op.getValue(0), Result);
795 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
796 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
797
Chris Lattner6831a812006-02-13 09:18:02 +0000798 // Now that the callseq_start and all of the non-call nodes above this call
799 // sequence have been legalized, legalize the call itself. During this
800 // process, no libcalls can/will be inserted, guaranteeing that no calls
801 // can overlap.
802 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
803 SDOperand InCallSEQ = LastCALLSEQ_END;
804 // Note that we are selecting this call!
805 LastCALLSEQ_END = SDOperand(CallEnd, 0);
806 IsLegalizingCall = true;
807
808 // Legalize the call, starting from the CALLSEQ_END.
809 LegalizeOp(LastCALLSEQ_END);
810 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
811 return Result;
812 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000813 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000814 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
815 // will cause this node to be legalized as well as handling libcalls right.
816 if (LastCALLSEQ_END.Val != Node) {
817 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
818 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
819 assert(I != LegalizedNodes.end() &&
820 "Legalizing the call start should have legalized this node!");
821 return I->second;
822 }
823
824 // Otherwise, the call start has been legalized and everything is going
825 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000826 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000827 // Do not try to legalize the target-specific arguments (#1+), except for
828 // an optional flag input.
829 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
830 if (Tmp1 != Node->getOperand(0)) {
831 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
832 Ops[0] = Tmp1;
833 Result = DAG.UpdateNodeOperands(Result, Ops);
834 }
835 } else {
836 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
837 if (Tmp1 != Node->getOperand(0) ||
838 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
839 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
840 Ops[0] = Tmp1;
841 Ops.back() = Tmp2;
842 Result = DAG.UpdateNodeOperands(Result, Ops);
843 }
Chris Lattner6a542892006-01-24 05:48:21 +0000844 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000845 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000846 // This finishes up call legalization.
847 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000848
849 // If the CALLSEQ_END node has a flag, remember that we legalized it.
850 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
851 if (Node->getNumValues() == 2)
852 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
853 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000854 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
856 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
857 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000858 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000859
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000860 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000861 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000862 switch (TLI.getOperationAction(Node->getOpcode(),
863 Node->getValueType(0))) {
864 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000865 case TargetLowering::Expand: {
866 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
867 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
868 " not tell us which reg is the stack pointer!");
869 SDOperand Chain = Tmp1.getOperand(0);
870 SDOperand Size = Tmp2.getOperand(1);
871 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
872 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
873 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000874 Tmp1 = LegalizeOp(Tmp1);
875 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000876 break;
877 }
878 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000879 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
880 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000881 Tmp1 = LegalizeOp(Tmp3);
882 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000883 }
Chris Lattner903d2782006-01-15 08:54:32 +0000884 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000885 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000886 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000887 }
Chris Lattner903d2782006-01-15 08:54:32 +0000888 // Since this op produce two values, make sure to remember that we
889 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000890 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
891 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000892 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000893 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000894 case ISD::INLINEASM:
895 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
896 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000897 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000898 Tmp2 = Tmp3 = SDOperand(0, 0);
899 else
900 Tmp3 = LegalizeOp(Tmp2);
901
902 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
903 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
904 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000905 if (Tmp3.Val) Ops.back() = Tmp3;
906 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000907 }
908
909 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000910 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000911 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
912 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000913 case ISD::BR:
914 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000915 // Ensure that libcalls are emitted before a branch.
916 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
917 Tmp1 = LegalizeOp(Tmp1);
918 LastCALLSEQ_END = DAG.getEntryNode();
919
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000920 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +0000921 break;
922
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000923 case ISD::BRCOND:
924 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000925 // Ensure that libcalls are emitted before a return.
926 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
927 Tmp1 = LegalizeOp(Tmp1);
928 LastCALLSEQ_END = DAG.getEntryNode();
929
Chris Lattner47e92232005-01-18 19:27:06 +0000930 switch (getTypeAction(Node->getOperand(1).getValueType())) {
931 case Expand: assert(0 && "It's impossible to expand bools");
932 case Legal:
933 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
934 break;
935 case Promote:
936 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
937 break;
938 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000939
940 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000942
943 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
944 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000945 case TargetLowering::Legal: break;
946 case TargetLowering::Custom:
947 Tmp1 = TLI.LowerOperation(Result, DAG);
948 if (Tmp1.Val) Result = Tmp1;
949 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000950 case TargetLowering::Expand:
951 // Expand brcond's setcc into its constituent parts and create a BR_CC
952 // Node.
953 if (Tmp2.getOpcode() == ISD::SETCC) {
954 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
955 Tmp2.getOperand(0), Tmp2.getOperand(1),
956 Node->getOperand(2));
957 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000958 // Make sure the condition is either zero or one. It may have been
959 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +0000960 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
961 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
962 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +0000963
Nate Begeman7cbd5252005-08-16 19:49:35 +0000964 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
965 DAG.getCondCode(ISD::SETNE), Tmp2,
966 DAG.getConstant(0, Tmp2.getValueType()),
967 Node->getOperand(2));
968 }
969 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000970 }
971 break;
972 case ISD::BR_CC:
973 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000974 // Ensure that libcalls are emitted before a branch.
975 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
976 Tmp1 = LegalizeOp(Tmp1);
977 LastCALLSEQ_END = DAG.getEntryNode();
978
Nate Begeman750ac1b2006-02-01 07:19:44 +0000979 Tmp2 = Node->getOperand(2); // LHS
980 Tmp3 = Node->getOperand(3); // RHS
981 Tmp4 = Node->getOperand(1); // CC
982
983 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
984
985 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
986 // the LHS is a legal SETCC itself. In this case, we need to compare
987 // the result against zero to select between true and false values.
988 if (Tmp3.Val == 0) {
989 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
990 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +0000991 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000992
993 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
994 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +0000995
Chris Lattner181b7a32005-12-17 23:46:46 +0000996 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
997 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000998 case TargetLowering::Legal: break;
999 case TargetLowering::Custom:
1000 Tmp4 = TLI.LowerOperation(Result, DAG);
1001 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001002 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001003 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001004 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001005 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001006 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1007 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001008
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001009 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001010 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1011 Tmp2 = Result.getValue(0);
1012 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001013
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001014 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1015 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001016 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001017 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001018 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001019 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001020 Tmp2 = LegalizeOp(Tmp1);
1021 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001022 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001023 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001024 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001025 // Since loads produce two values, make sure to remember that we
1026 // legalized both of them.
1027 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1028 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1029 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001030 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001031 case ISD::EXTLOAD:
1032 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001033 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001034 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1035 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001036
Chris Lattner5f056bf2005-07-10 01:55:33 +00001037 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001038 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001039 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001040 case TargetLowering::Promote:
1041 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1043 DAG.getValueType(MVT::i8));
1044 Tmp1 = Result.getValue(0);
1045 Tmp2 = Result.getValue(1);
1046 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001047 case TargetLowering::Custom:
1048 isCustom = true;
1049 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001050 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001051 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1052 Node->getOperand(3));
1053 Tmp1 = Result.getValue(0);
1054 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001055
1056 if (isCustom) {
1057 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1058 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001059 Tmp1 = LegalizeOp(Tmp3);
1060 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001061 }
1062 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001063 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001064 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001065 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001066 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1067 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001068 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001069 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1070 Tmp2 = LegalizeOp(Load.getValue(1));
1071 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001072 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001073 assert(Node->getOpcode() != ISD::EXTLOAD &&
1074 "EXTLOAD should always be supported!");
1075 // Turn the unsupported load into an EXTLOAD followed by an explicit
1076 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001077 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1078 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001079 SDOperand ValRes;
1080 if (Node->getOpcode() == ISD::SEXTLOAD)
1081 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001082 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001083 else
1084 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001085 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1086 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1087 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001088 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001089 // Since loads produce two values, make sure to remember that we legalized
1090 // both of them.
1091 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1092 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1093 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001094 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001095 case ISD::EXTRACT_ELEMENT: {
1096 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1097 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001098 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001099 case Legal:
1100 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1101 // 1 -> Hi
1102 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1103 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1104 TLI.getShiftAmountTy()));
1105 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1106 } else {
1107 // 0 -> Lo
1108 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1109 Node->getOperand(0));
1110 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001111 break;
1112 case Expand:
1113 // Get both the low and high parts.
1114 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1115 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1116 Result = Tmp2; // 1 -> Hi
1117 else
1118 Result = Tmp1; // 0 -> Lo
1119 break;
1120 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001121 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001122 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001123
1124 case ISD::CopyToReg:
1125 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001126
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001127 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001128 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001129 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001130 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001131 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001132 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001133 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001134 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001135 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001136 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1138 Tmp3);
1139 } else {
1140 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001141 }
1142
1143 // Since this produces two values, make sure to remember that we legalized
1144 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001145 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001146 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001147 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001148 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001149 break;
1150
1151 case ISD::RET:
1152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001153
1154 // Ensure that libcalls are emitted before a return.
1155 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1156 Tmp1 = LegalizeOp(Tmp1);
1157 LastCALLSEQ_END = DAG.getEntryNode();
1158
Chris Lattner3e928bb2005-01-07 07:47:09 +00001159 switch (Node->getNumOperands()) {
1160 case 2: // ret val
1161 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1162 case Legal:
1163 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001164 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001165 break;
1166 case Expand: {
1167 SDOperand Lo, Hi;
1168 ExpandOp(Node->getOperand(1), Lo, Hi);
1169 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001170 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001171 }
1172 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001173 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001174 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1175 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001176 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001177 }
1178 break;
1179 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001180 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001181 break;
1182 default: { // ret <values>
1183 std::vector<SDOperand> NewValues;
1184 NewValues.push_back(Tmp1);
1185 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1186 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1187 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001188 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001189 break;
1190 case Expand: {
1191 SDOperand Lo, Hi;
1192 ExpandOp(Node->getOperand(i), Lo, Hi);
1193 NewValues.push_back(Lo);
1194 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001195 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001196 }
1197 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001198 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001199 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001200
1201 if (NewValues.size() == Node->getNumOperands())
1202 Result = DAG.UpdateNodeOperands(Result, NewValues);
1203 else
1204 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001205 break;
1206 }
1207 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001208
Chris Lattner6862dbc2006-01-29 21:02:23 +00001209 if (Result.getOpcode() == ISD::RET) {
1210 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1211 default: assert(0 && "This action is not supported yet!");
1212 case TargetLowering::Legal: break;
1213 case TargetLowering::Custom:
1214 Tmp1 = TLI.LowerOperation(Result, DAG);
1215 if (Tmp1.Val) Result = Tmp1;
1216 break;
1217 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001218 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001219 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001220 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1222 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1223
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001224 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001225 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001226 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001227 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001228 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001229 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001230 } else {
1231 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001232 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001233 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001234 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1235 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001236 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001237 }
1238
Chris Lattner3e928bb2005-01-07 07:47:09 +00001239 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1240 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001241 Tmp3 = LegalizeOp(Node->getOperand(1));
1242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1243 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001244
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001245 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001246 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1247 default: assert(0 && "This action is not supported yet!");
1248 case TargetLowering::Legal: break;
1249 case TargetLowering::Custom:
1250 Tmp1 = TLI.LowerOperation(Result, DAG);
1251 if (Tmp1.Val) Result = Tmp1;
1252 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001253 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001254 break;
1255 }
1256 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001257 // Truncate the value and store the result.
1258 Tmp3 = PromoteOp(Node->getOperand(1));
1259 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001260 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001261 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001262 break;
1263
Chris Lattner3e928bb2005-01-07 07:47:09 +00001264 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001265 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001266 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001267
1268 // If this is a vector type, then we have to calculate the increment as
1269 // the product of the element size in bytes, and the number of elements
1270 // in the high half of the vector.
1271 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1272 SDNode *InVal = Node->getOperand(1).Val;
1273 unsigned NumElems =
1274 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1275 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1276
1277 // Figure out if there is a Packed type corresponding to this Vector
1278 // type. If so, convert to the packed type.
1279 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1280 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1281 // Turn this into a normal store of the packed type.
1282 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1284 Node->getOperand(3));
1285 break;
1286 } else if (NumElems == 1) {
1287 // Turn this into a normal store of the scalar type.
1288 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1289 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1290 Node->getOperand(3));
1291 break;
1292 } else {
1293 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1294 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1295 }
1296 } else {
1297 ExpandOp(Node->getOperand(1), Lo, Hi);
1298 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1299 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001300
1301 if (!TLI.isLittleEndian())
1302 std::swap(Lo, Hi);
1303
Chris Lattneredb1add2005-05-11 04:51:16 +00001304 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1305 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001306 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1307 getIntPtrConstant(IncrementSize));
1308 assert(isTypeLegal(Tmp2.getValueType()) &&
1309 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001310 // FIXME: This sets the srcvalue of both halves to be the same, which is
1311 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001312 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1313 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001314 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1315 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001316 }
1317 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001318 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001319 case ISD::PCMARKER:
1320 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001322 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001323 case ISD::STACKSAVE:
1324 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001325 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1326 Tmp1 = Result.getValue(0);
1327 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001328
Chris Lattner140d53c2006-01-13 02:50:02 +00001329 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1330 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001331 case TargetLowering::Legal: break;
1332 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001333 Tmp3 = TLI.LowerOperation(Result, DAG);
1334 if (Tmp3.Val) {
1335 Tmp1 = LegalizeOp(Tmp3);
1336 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001337 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001338 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001339 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001340 // Expand to CopyFromReg if the target set
1341 // StackPointerRegisterToSaveRestore.
1342 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001343 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001344 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001345 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001346 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001347 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1348 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001349 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001350 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001351 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001352
1353 // Since stacksave produce two values, make sure to remember that we
1354 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001355 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1356 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1357 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001358
Chris Lattner140d53c2006-01-13 02:50:02 +00001359 case ISD::STACKRESTORE:
1360 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1361 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001363
1364 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1365 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001366 case TargetLowering::Legal: break;
1367 case TargetLowering::Custom:
1368 Tmp1 = TLI.LowerOperation(Result, DAG);
1369 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001370 break;
1371 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001372 // Expand to CopyToReg if the target set
1373 // StackPointerRegisterToSaveRestore.
1374 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1375 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1376 } else {
1377 Result = Tmp1;
1378 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001379 break;
1380 }
1381 break;
1382
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001383 case ISD::READCYCLECOUNTER:
1384 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001385 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001386
1387 // Since rdcc produce two values, make sure to remember that we legalized
1388 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001389 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001390 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001391 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001392
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001393 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001394 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1395 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1396
Chris Lattner456a93a2006-01-28 07:39:30 +00001397 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1398 "Cannot handle illegal TRUNCSTORE yet!");
1399 Tmp2 = LegalizeOp(Node->getOperand(1));
1400
1401 // The only promote case we handle is TRUNCSTORE:i1 X into
1402 // -> TRUNCSTORE:i8 (and X, 1)
1403 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1404 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1405 TargetLowering::Promote) {
1406 // Promote the bool to a mask then store.
1407 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1408 DAG.getConstant(1, Tmp2.getValueType()));
1409 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1410 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001411
Chris Lattner456a93a2006-01-28 07:39:30 +00001412 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1413 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001414 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1415 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001416 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001417
Chris Lattner456a93a2006-01-28 07:39:30 +00001418 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1419 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1420 default: assert(0 && "This action is not supported yet!");
1421 case TargetLowering::Legal: break;
1422 case TargetLowering::Custom:
1423 Tmp1 = TLI.LowerOperation(Result, DAG);
1424 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001425 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001426 }
1427 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001428 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001429 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001430 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1431 case Expand: assert(0 && "It's impossible to expand bools");
1432 case Legal:
1433 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1434 break;
1435 case Promote:
1436 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1437 break;
1438 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001439 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001440 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001441
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001443
Nate Begemanb942a3d2005-08-23 04:29:48 +00001444 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001445 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001446 case TargetLowering::Legal: break;
1447 case TargetLowering::Custom: {
1448 Tmp1 = TLI.LowerOperation(Result, DAG);
1449 if (Tmp1.Val) Result = Tmp1;
1450 break;
1451 }
Nate Begeman9373a812005-08-10 20:51:12 +00001452 case TargetLowering::Expand:
1453 if (Tmp1.getOpcode() == ISD::SETCC) {
1454 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1455 Tmp2, Tmp3,
1456 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1457 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001458 // Make sure the condition is either zero or one. It may have been
1459 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001460 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1461 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1462 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001463 Result = DAG.getSelectCC(Tmp1,
1464 DAG.getConstant(0, Tmp1.getValueType()),
1465 Tmp2, Tmp3, ISD::SETNE);
1466 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001467 break;
1468 case TargetLowering::Promote: {
1469 MVT::ValueType NVT =
1470 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1471 unsigned ExtOp, TruncOp;
1472 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001473 ExtOp = ISD::ANY_EXTEND;
1474 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001475 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001476 ExtOp = ISD::FP_EXTEND;
1477 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001478 }
1479 // Promote each of the values to the new type.
1480 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1481 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1482 // Perform the larger operation, then round down.
1483 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1484 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1485 break;
1486 }
1487 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001488 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001489 case ISD::SELECT_CC: {
1490 Tmp1 = Node->getOperand(0); // LHS
1491 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001492 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1493 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001494 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001495
Nate Begeman750ac1b2006-02-01 07:19:44 +00001496 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1497
1498 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1499 // the LHS is a legal SETCC itself. In this case, we need to compare
1500 // the result against zero to select between true and false values.
1501 if (Tmp2.Val == 0) {
1502 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1503 CC = DAG.getCondCode(ISD::SETNE);
1504 }
1505 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1506
1507 // Everything is legal, see if we should expand this op or something.
1508 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1509 default: assert(0 && "This action is not supported yet!");
1510 case TargetLowering::Legal: break;
1511 case TargetLowering::Custom:
1512 Tmp1 = TLI.LowerOperation(Result, DAG);
1513 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001514 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001515 }
1516 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001517 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001518 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001519 Tmp1 = Node->getOperand(0);
1520 Tmp2 = Node->getOperand(1);
1521 Tmp3 = Node->getOperand(2);
1522 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1523
1524 // If we had to Expand the SetCC operands into a SELECT node, then it may
1525 // not always be possible to return a true LHS & RHS. In this case, just
1526 // return the value we legalized, returned in the LHS
1527 if (Tmp2.Val == 0) {
1528 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001529 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001530 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001531
Chris Lattner73e142f2006-01-30 22:43:50 +00001532 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001533 default: assert(0 && "Cannot handle this action for SETCC yet!");
1534 case TargetLowering::Custom:
1535 isCustom = true;
1536 // FALLTHROUGH.
1537 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001538 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001539 if (isCustom) {
1540 Tmp3 = TLI.LowerOperation(Result, DAG);
1541 if (Tmp3.Val) Result = Tmp3;
1542 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001543 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001544 case TargetLowering::Promote: {
1545 // First step, figure out the appropriate operation to use.
1546 // Allow SETCC to not be supported for all legal data types
1547 // Mostly this targets FP
1548 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1549 MVT::ValueType OldVT = NewInTy;
1550
1551 // Scan for the appropriate larger type to use.
1552 while (1) {
1553 NewInTy = (MVT::ValueType)(NewInTy+1);
1554
1555 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1556 "Fell off of the edge of the integer world");
1557 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1558 "Fell off of the edge of the floating point world");
1559
1560 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001561 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001562 break;
1563 }
1564 if (MVT::isInteger(NewInTy))
1565 assert(0 && "Cannot promote Legal Integer SETCC yet");
1566 else {
1567 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1568 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1569 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001570 Tmp1 = LegalizeOp(Tmp1);
1571 Tmp2 = LegalizeOp(Tmp2);
1572 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001573 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001574 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001575 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001576 case TargetLowering::Expand:
1577 // Expand a setcc node into a select_cc of the same condition, lhs, and
1578 // rhs that selects between const 1 (true) and const 0 (false).
1579 MVT::ValueType VT = Node->getValueType(0);
1580 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1581 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1582 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001583 break;
1584 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001585 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001586 case ISD::MEMSET:
1587 case ISD::MEMCPY:
1588 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001590 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1591
1592 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1593 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1594 case Expand: assert(0 && "Cannot expand a byte!");
1595 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001596 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001597 break;
1598 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001599 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001600 break;
1601 }
1602 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001603 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001604 }
Chris Lattner272455b2005-02-02 03:44:41 +00001605
1606 SDOperand Tmp4;
1607 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001608 case Expand: {
1609 // Length is too big, just take the lo-part of the length.
1610 SDOperand HiPart;
1611 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1612 break;
1613 }
Chris Lattnere5605212005-01-28 22:29:18 +00001614 case Legal:
1615 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001616 break;
1617 case Promote:
1618 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001619 break;
1620 }
1621
1622 SDOperand Tmp5;
1623 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1624 case Expand: assert(0 && "Cannot expand this yet!");
1625 case Legal:
1626 Tmp5 = LegalizeOp(Node->getOperand(4));
1627 break;
1628 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001629 Tmp5 = PromoteOp(Node->getOperand(4));
1630 break;
1631 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001632
1633 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1634 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001635 case TargetLowering::Custom:
1636 isCustom = true;
1637 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001638 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001640 if (isCustom) {
1641 Tmp1 = TLI.LowerOperation(Result, DAG);
1642 if (Tmp1.Val) Result = Tmp1;
1643 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001644 break;
1645 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001646 // Otherwise, the target does not support this operation. Lower the
1647 // operation to an explicit libcall as appropriate.
1648 MVT::ValueType IntPtr = TLI.getPointerTy();
1649 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1650 std::vector<std::pair<SDOperand, const Type*> > Args;
1651
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001652 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001653 if (Node->getOpcode() == ISD::MEMSET) {
1654 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001655 // Extend the (previously legalized) ubyte argument to be an int value
1656 // for the call.
1657 if (Tmp3.getValueType() > MVT::i32)
1658 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1659 else
1660 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001661 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1662 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1663
1664 FnName = "memset";
1665 } else if (Node->getOpcode() == ISD::MEMCPY ||
1666 Node->getOpcode() == ISD::MEMMOVE) {
1667 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1668 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1669 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1670 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1671 } else {
1672 assert(0 && "Unknown op!");
1673 }
Chris Lattner45982da2005-05-12 16:53:42 +00001674
Chris Lattnere1bd8222005-01-11 05:57:22 +00001675 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001676 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001677 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001678 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001679 break;
1680 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001681 }
1682 break;
1683 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001684
Chris Lattner5b359c62005-04-02 04:00:59 +00001685 case ISD::SHL_PARTS:
1686 case ISD::SRA_PARTS:
1687 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001688 std::vector<SDOperand> Ops;
1689 bool Changed = false;
1690 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1691 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1692 Changed |= Ops.back() != Node->getOperand(i);
1693 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001694 if (Changed)
1695 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001696
Evan Cheng05a2d562006-01-09 18:31:59 +00001697 switch (TLI.getOperationAction(Node->getOpcode(),
1698 Node->getValueType(0))) {
1699 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001700 case TargetLowering::Legal: break;
1701 case TargetLowering::Custom:
1702 Tmp1 = TLI.LowerOperation(Result, DAG);
1703 if (Tmp1.Val) {
1704 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001705 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001706 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001707 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1708 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001709 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001710 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001711 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001712 return RetVal;
1713 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001714 break;
1715 }
1716
Chris Lattner2c8086f2005-04-02 05:00:07 +00001717 // Since these produce multiple values, make sure to remember that we
1718 // legalized all of them.
1719 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1720 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1721 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001722 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001723
1724 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001725 case ISD::ADD:
1726 case ISD::SUB:
1727 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001728 case ISD::MULHS:
1729 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001730 case ISD::UDIV:
1731 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001732 case ISD::AND:
1733 case ISD::OR:
1734 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001735 case ISD::SHL:
1736 case ISD::SRL:
1737 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001738 case ISD::FADD:
1739 case ISD::FSUB:
1740 case ISD::FMUL:
1741 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001742 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001743 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1744 case Expand: assert(0 && "Not possible");
1745 case Legal:
1746 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1747 break;
1748 case Promote:
1749 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1750 break;
1751 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001752
1753 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001754
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001755 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001756 default: assert(0 && "Operation not supported");
1757 case TargetLowering::Legal: break;
1758 case TargetLowering::Custom:
1759 Tmp1 = TLI.LowerOperation(Result, DAG);
1760 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001761 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001762 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001763 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001764
1765 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1766 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1767 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1768 case Expand: assert(0 && "Not possible");
1769 case Legal:
1770 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1771 break;
1772 case Promote:
1773 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1774 break;
1775 }
1776
1777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1778
1779 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1780 default: assert(0 && "Operation not supported");
1781 case TargetLowering::Custom:
1782 Tmp1 = TLI.LowerOperation(Result, DAG);
1783 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001784 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001785 case TargetLowering::Legal: break;
1786 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001787 // If this target supports fabs/fneg natively, do this efficiently.
1788 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1789 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1790 // Get the sign bit of the RHS.
1791 MVT::ValueType IVT =
1792 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1793 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1794 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1795 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1796 // Get the absolute value of the result.
1797 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1798 // Select between the nabs and abs value based on the sign bit of
1799 // the input.
1800 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1801 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1802 AbsVal),
1803 AbsVal);
1804 Result = LegalizeOp(Result);
1805 break;
1806 }
1807
1808 // Otherwise, do bitwise ops!
1809
1810 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001811 const char *FnName;
1812 if (Node->getValueType(0) == MVT::f32) {
1813 FnName = "copysignf";
1814 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1815 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1816 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1817 } else {
1818 FnName = "copysign";
1819 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1820 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1821 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1822 }
1823 SDOperand Dummy;
1824 Result = ExpandLibCall(FnName, Node, Dummy);
1825 break;
1826 }
1827 break;
1828
Nate Begeman551bf3f2006-02-17 05:43:56 +00001829 case ISD::ADDC:
1830 case ISD::SUBC:
1831 Tmp1 = LegalizeOp(Node->getOperand(0));
1832 Tmp2 = LegalizeOp(Node->getOperand(1));
1833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1834 // Since this produces two values, make sure to remember that we legalized
1835 // both of them.
1836 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1837 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1838 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001839
Nate Begeman551bf3f2006-02-17 05:43:56 +00001840 case ISD::ADDE:
1841 case ISD::SUBE:
1842 Tmp1 = LegalizeOp(Node->getOperand(0));
1843 Tmp2 = LegalizeOp(Node->getOperand(1));
1844 Tmp3 = LegalizeOp(Node->getOperand(2));
1845 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1846 // Since this produces two values, make sure to remember that we legalized
1847 // both of them.
1848 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1849 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1850 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001851
Nate Begeman419f8b62005-10-18 00:27:41 +00001852 case ISD::BUILD_PAIR: {
1853 MVT::ValueType PairTy = Node->getValueType(0);
1854 // TODO: handle the case where the Lo and Hi operands are not of legal type
1855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1856 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1857 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001858 case TargetLowering::Promote:
1859 case TargetLowering::Custom:
1860 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001861 case TargetLowering::Legal:
1862 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1863 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1864 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001865 case TargetLowering::Expand:
1866 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1867 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1868 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1869 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1870 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001871 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001872 break;
1873 }
1874 break;
1875 }
1876
Nate Begemanc105e192005-04-06 00:23:54 +00001877 case ISD::UREM:
1878 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001879 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001880 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1881 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001882
Nate Begemanc105e192005-04-06 00:23:54 +00001883 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001884 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1885 case TargetLowering::Custom:
1886 isCustom = true;
1887 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001888 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001889 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001890 if (isCustom) {
1891 Tmp1 = TLI.LowerOperation(Result, DAG);
1892 if (Tmp1.Val) Result = Tmp1;
1893 }
Nate Begemanc105e192005-04-06 00:23:54 +00001894 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001895 case TargetLowering::Expand:
1896 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001897 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001898 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001899 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001900 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1901 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1902 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1903 } else {
1904 // Floating point mod -> fmod libcall.
1905 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1906 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001907 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001908 }
1909 break;
1910 }
1911 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001912 case ISD::VAARG: {
1913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1914 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1915
Chris Lattner5c62f332006-01-28 07:42:08 +00001916 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001917 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1918 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001919 case TargetLowering::Custom:
1920 isCustom = true;
1921 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001922 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1924 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001925 Tmp1 = Result.getValue(1);
1926
1927 if (isCustom) {
1928 Tmp2 = TLI.LowerOperation(Result, DAG);
1929 if (Tmp2.Val) {
1930 Result = LegalizeOp(Tmp2);
1931 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1932 }
1933 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001934 break;
1935 case TargetLowering::Expand: {
1936 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1937 Node->getOperand(2));
1938 // Increment the pointer, VAList, to the next vaarg
1939 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1940 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1941 TLI.getPointerTy()));
1942 // Store the incremented VAList to the legalized pointer
1943 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1944 Node->getOperand(2));
1945 // Load the actual argument out of the pointer VAList
1946 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001947 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001948 Result = LegalizeOp(Result);
1949 break;
1950 }
1951 }
1952 // Since VAARG produces two values, make sure to remember that we
1953 // legalized both of them.
1954 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001955 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1956 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00001957 }
1958
1959 case ISD::VACOPY:
1960 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1961 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1962 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1963
1964 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1965 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001966 case TargetLowering::Custom:
1967 isCustom = true;
1968 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001969 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1971 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001972 if (isCustom) {
1973 Tmp1 = TLI.LowerOperation(Result, DAG);
1974 if (Tmp1.Val) Result = Tmp1;
1975 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001976 break;
1977 case TargetLowering::Expand:
1978 // This defaults to loading a pointer from the input and storing it to the
1979 // output, returning the chain.
1980 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1981 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1982 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00001983 break;
1984 }
1985 break;
1986
1987 case ISD::VAEND:
1988 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1989 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1990
1991 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1992 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001993 case TargetLowering::Custom:
1994 isCustom = true;
1995 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001996 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001997 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001998 if (isCustom) {
1999 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2000 if (Tmp1.Val) Result = Tmp1;
2001 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002002 break;
2003 case TargetLowering::Expand:
2004 Result = Tmp1; // Default to a no-op, return the chain
2005 break;
2006 }
2007 break;
2008
2009 case ISD::VASTART:
2010 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2011 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2012
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2014
Nate Begemanacc398c2006-01-25 18:21:52 +00002015 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2016 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002017 case TargetLowering::Legal: break;
2018 case TargetLowering::Custom:
2019 Tmp1 = TLI.LowerOperation(Result, DAG);
2020 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002021 break;
2022 }
2023 break;
2024
Nate Begeman35ef9132006-01-11 21:21:00 +00002025 case ISD::ROTL:
2026 case ISD::ROTR:
2027 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2028 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002029
2030 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2031 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002032 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002033 break;
2034
Nate Begemand88fc032006-01-14 03:14:10 +00002035 case ISD::BSWAP:
2036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2037 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002038 case TargetLowering::Custom:
2039 assert(0 && "Cannot custom legalize this yet!");
2040 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002041 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002042 break;
2043 case TargetLowering::Promote: {
2044 MVT::ValueType OVT = Tmp1.getValueType();
2045 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2046 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002047
Chris Lattner456a93a2006-01-28 07:39:30 +00002048 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2049 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2050 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2051 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2052 break;
2053 }
2054 case TargetLowering::Expand:
2055 Result = ExpandBSWAP(Tmp1);
2056 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002057 }
2058 break;
2059
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002060 case ISD::CTPOP:
2061 case ISD::CTTZ:
2062 case ISD::CTLZ:
2063 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2064 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002065 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002066 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002067 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002068 break;
2069 case TargetLowering::Promote: {
2070 MVT::ValueType OVT = Tmp1.getValueType();
2071 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002072
2073 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002074 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2075 // Perform the larger operation, then subtract if needed.
2076 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002077 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002078 case ISD::CTPOP:
2079 Result = Tmp1;
2080 break;
2081 case ISD::CTTZ:
2082 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002083 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2084 DAG.getConstant(getSizeInBits(NVT), NVT),
2085 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002086 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002087 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2088 break;
2089 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002090 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002091 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2092 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002093 getSizeInBits(OVT), NVT));
2094 break;
2095 }
2096 break;
2097 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002098 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002099 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002100 break;
2101 }
2102 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002103
Chris Lattner2c8086f2005-04-02 05:00:07 +00002104 // Unary operators
2105 case ISD::FABS:
2106 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002107 case ISD::FSQRT:
2108 case ISD::FSIN:
2109 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002110 Tmp1 = LegalizeOp(Node->getOperand(0));
2111 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002112 case TargetLowering::Promote:
2113 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002114 isCustom = true;
2115 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002116 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002117 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002118 if (isCustom) {
2119 Tmp1 = TLI.LowerOperation(Result, DAG);
2120 if (Tmp1.Val) Result = Tmp1;
2121 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002122 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002123 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002124 switch (Node->getOpcode()) {
2125 default: assert(0 && "Unreachable!");
2126 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002127 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2128 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002129 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002130 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002131 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002132 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2133 MVT::ValueType VT = Node->getValueType(0);
2134 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002135 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002136 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2137 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002138 break;
2139 }
2140 case ISD::FSQRT:
2141 case ISD::FSIN:
2142 case ISD::FCOS: {
2143 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002144 const char *FnName = 0;
2145 switch(Node->getOpcode()) {
2146 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2147 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2148 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2149 default: assert(0 && "Unreachable!");
2150 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002151 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002152 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002153 break;
2154 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002155 }
2156 break;
2157 }
2158 break;
Chris Lattner35481892005-12-23 00:16:34 +00002159
2160 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002161 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002162 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002163 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002164 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2165 Node->getOperand(0).getValueType())) {
2166 default: assert(0 && "Unknown operation action!");
2167 case TargetLowering::Expand:
2168 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2169 break;
2170 case TargetLowering::Legal:
2171 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002172 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002173 break;
2174 }
2175 }
2176 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002177 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002178 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002179 case ISD::UINT_TO_FP: {
2180 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002181 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2182 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002183 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002184 Node->getOperand(0).getValueType())) {
2185 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002186 case TargetLowering::Custom:
2187 isCustom = true;
2188 // FALLTHROUGH
2189 case TargetLowering::Legal:
2190 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002191 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002192 if (isCustom) {
2193 Tmp1 = TLI.LowerOperation(Result, DAG);
2194 if (Tmp1.Val) Result = Tmp1;
2195 }
2196 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002197 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002198 Result = ExpandLegalINT_TO_FP(isSigned,
2199 LegalizeOp(Node->getOperand(0)),
2200 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002201 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002202 case TargetLowering::Promote:
2203 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2204 Node->getValueType(0),
2205 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002206 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002207 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002208 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002209 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002210 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2211 Node->getValueType(0), Node->getOperand(0));
2212 break;
2213 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002214 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002215 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002216 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2217 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002218 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002219 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2220 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002221 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002222 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2223 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002224 break;
2225 }
2226 break;
2227 }
2228 case ISD::TRUNCATE:
2229 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2230 case Legal:
2231 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002232 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002233 break;
2234 case Expand:
2235 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2236
2237 // Since the result is legal, we should just be able to truncate the low
2238 // part of the source.
2239 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2240 break;
2241 case Promote:
2242 Result = PromoteOp(Node->getOperand(0));
2243 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2244 break;
2245 }
2246 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002247
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002248 case ISD::FP_TO_SINT:
2249 case ISD::FP_TO_UINT:
2250 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2251 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002252 Tmp1 = LegalizeOp(Node->getOperand(0));
2253
Chris Lattner1618beb2005-07-29 00:11:56 +00002254 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2255 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002256 case TargetLowering::Custom:
2257 isCustom = true;
2258 // FALLTHROUGH
2259 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002260 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002261 if (isCustom) {
2262 Tmp1 = TLI.LowerOperation(Result, DAG);
2263 if (Tmp1.Val) Result = Tmp1;
2264 }
2265 break;
2266 case TargetLowering::Promote:
2267 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2268 Node->getOpcode() == ISD::FP_TO_SINT);
2269 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002270 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002271 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2272 SDOperand True, False;
2273 MVT::ValueType VT = Node->getOperand(0).getValueType();
2274 MVT::ValueType NVT = Node->getValueType(0);
2275 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2276 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2277 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2278 Node->getOperand(0), Tmp2, ISD::SETLT);
2279 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2280 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002281 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002282 Tmp2));
2283 False = DAG.getNode(ISD::XOR, NVT, False,
2284 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002285 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2286 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002287 } else {
2288 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2289 }
2290 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002291 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002292 break;
2293 case Expand:
2294 assert(0 && "Shouldn't need to expand other operators here!");
2295 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002296 Tmp1 = PromoteOp(Node->getOperand(0));
2297 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2298 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002299 break;
2300 }
2301 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002302
Chris Lattner13c78e22005-09-02 00:18:10 +00002303 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002304 case ISD::ZERO_EXTEND:
2305 case ISD::SIGN_EXTEND:
2306 case ISD::FP_EXTEND:
2307 case ISD::FP_ROUND:
2308 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002309 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002310 case Legal:
2311 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002312 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002313 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002314 case Promote:
2315 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002316 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002317 Tmp1 = PromoteOp(Node->getOperand(0));
2318 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002319 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002320 case ISD::ZERO_EXTEND:
2321 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002322 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002323 Result = DAG.getZeroExtendInReg(Result,
2324 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002325 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002326 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002327 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002328 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002329 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002330 Result,
2331 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002332 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002333 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002334 Result = PromoteOp(Node->getOperand(0));
2335 if (Result.getValueType() != Op.getValueType())
2336 // Dynamically dead while we have only 2 FP types.
2337 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2338 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002339 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002340 Result = PromoteOp(Node->getOperand(0));
2341 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2342 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002343 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002344 }
2345 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002346 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002347 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002348 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002349 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002350
2351 // If this operation is not supported, convert it to a shl/shr or load/store
2352 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002353 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2354 default: assert(0 && "This action not supported for this op yet!");
2355 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002356 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002357 break;
2358 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002359 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002360 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002361 // NOTE: we could fall back on load/store here too for targets without
2362 // SAR. However, it is doubtful that any exist.
2363 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2364 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002365 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002366 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2367 Node->getOperand(0), ShiftCst);
2368 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2369 Result, ShiftCst);
2370 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2371 // The only way we can lower this is to turn it into a STORETRUNC,
2372 // EXTLOAD pair, targetting a temporary location (a stack slot).
2373
2374 // NOTE: there is a choice here between constantly creating new stack
2375 // slots and always reusing the same one. We currently always create
2376 // new ones, as reuse may inhibit scheduling.
2377 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2378 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2379 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2380 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002381 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002382 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2383 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2384 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002385 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002386 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002387 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2388 Result, StackSlot, DAG.getSrcValue(NULL),
2389 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002390 } else {
2391 assert(0 && "Unknown op");
2392 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002393 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002394 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002395 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002396 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002397 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002398
2399 // Make sure that the generated code is itself legal.
2400 if (Result != Op)
2401 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002402
Chris Lattner45982da2005-05-12 16:53:42 +00002403 // Note that LegalizeOp may be reentered even from single-use nodes, which
2404 // means that we always must cache transformed nodes.
2405 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002406 return Result;
2407}
2408
Chris Lattner8b6fa222005-01-15 22:16:26 +00002409/// PromoteOp - Given an operation that produces a value in an invalid type,
2410/// promote it to compute the value into a larger type. The produced value will
2411/// have the correct bits for the low portion of the register, but no guarantee
2412/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002413SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2414 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002415 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002416 assert(getTypeAction(VT) == Promote &&
2417 "Caller should expand or legalize operands that are not promotable!");
2418 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2419 "Cannot promote to smaller type!");
2420
Chris Lattner03c85462005-01-15 05:21:40 +00002421 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002422 SDOperand Result;
2423 SDNode *Node = Op.Val;
2424
Chris Lattner6fdcb252005-09-02 20:32:45 +00002425 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2426 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002427
Chris Lattner03c85462005-01-15 05:21:40 +00002428 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002429 case ISD::CopyFromReg:
2430 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002431 default:
2432 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2433 assert(0 && "Do not know how to promote this operator!");
2434 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002435 case ISD::UNDEF:
2436 Result = DAG.getNode(ISD::UNDEF, NVT);
2437 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002438 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002439 if (VT != MVT::i1)
2440 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2441 else
2442 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002443 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2444 break;
2445 case ISD::ConstantFP:
2446 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2447 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2448 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002449
Chris Lattner82fbfb62005-01-18 02:59:52 +00002450 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002451 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002452 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2453 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002454 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002455
2456 case ISD::TRUNCATE:
2457 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2458 case Legal:
2459 Result = LegalizeOp(Node->getOperand(0));
2460 assert(Result.getValueType() >= NVT &&
2461 "This truncation doesn't make sense!");
2462 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2463 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2464 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002465 case Promote:
2466 // The truncation is not required, because we don't guarantee anything
2467 // about high bits anyway.
2468 Result = PromoteOp(Node->getOperand(0));
2469 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002470 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002471 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2472 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002473 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002474 }
2475 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002476 case ISD::SIGN_EXTEND:
2477 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002478 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002479 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2480 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2481 case Legal:
2482 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002483 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002484 break;
2485 case Promote:
2486 // Promote the reg if it's smaller.
2487 Result = PromoteOp(Node->getOperand(0));
2488 // The high bits are not guaranteed to be anything. Insert an extend.
2489 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002490 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002491 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002492 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002493 Result = DAG.getZeroExtendInReg(Result,
2494 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002495 break;
2496 }
2497 break;
Chris Lattner35481892005-12-23 00:16:34 +00002498 case ISD::BIT_CONVERT:
2499 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2500 Result = PromoteOp(Result);
2501 break;
2502
Chris Lattner8b6fa222005-01-15 22:16:26 +00002503 case ISD::FP_EXTEND:
2504 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2505 case ISD::FP_ROUND:
2506 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2507 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2508 case Promote: assert(0 && "Unreachable with 2 FP types!");
2509 case Legal:
2510 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002511 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002512 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002513 break;
2514 }
2515 break;
2516
2517 case ISD::SINT_TO_FP:
2518 case ISD::UINT_TO_FP:
2519 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2520 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002521 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002522 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002523 break;
2524
2525 case Promote:
2526 Result = PromoteOp(Node->getOperand(0));
2527 if (Node->getOpcode() == ISD::SINT_TO_FP)
2528 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002529 Result,
2530 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002531 else
Chris Lattner23993562005-04-13 02:38:47 +00002532 Result = DAG.getZeroExtendInReg(Result,
2533 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002534 // No extra round required here.
2535 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002536 break;
2537 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002538 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2539 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002540 // Round if we cannot tolerate excess precision.
2541 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002542 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2543 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002544 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002545 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002546 break;
2547
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002548 case ISD::SIGN_EXTEND_INREG:
2549 Result = PromoteOp(Node->getOperand(0));
2550 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2551 Node->getOperand(1));
2552 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002553 case ISD::FP_TO_SINT:
2554 case ISD::FP_TO_UINT:
2555 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2556 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002557 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002558 break;
2559 case Promote:
2560 // The input result is prerounded, so we don't have to do anything
2561 // special.
2562 Tmp1 = PromoteOp(Node->getOperand(0));
2563 break;
2564 case Expand:
2565 assert(0 && "not implemented");
2566 }
Nate Begemand2558e32005-08-14 01:20:53 +00002567 // If we're promoting a UINT to a larger size, check to see if the new node
2568 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2569 // we can use that instead. This allows us to generate better code for
2570 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2571 // legal, such as PowerPC.
2572 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002573 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002574 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2575 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002576 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2577 } else {
2578 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2579 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002580 break;
2581
Chris Lattner2c8086f2005-04-02 05:00:07 +00002582 case ISD::FABS:
2583 case ISD::FNEG:
2584 Tmp1 = PromoteOp(Node->getOperand(0));
2585 assert(Tmp1.getValueType() == NVT);
2586 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2587 // NOTE: we do not have to do any extra rounding here for
2588 // NoExcessFPPrecision, because we know the input will have the appropriate
2589 // precision, and these operations don't modify precision at all.
2590 break;
2591
Chris Lattnerda6ba872005-04-28 21:44:33 +00002592 case ISD::FSQRT:
2593 case ISD::FSIN:
2594 case ISD::FCOS:
2595 Tmp1 = PromoteOp(Node->getOperand(0));
2596 assert(Tmp1.getValueType() == NVT);
2597 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002598 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002599 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2600 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002601 break;
2602
Chris Lattner03c85462005-01-15 05:21:40 +00002603 case ISD::AND:
2604 case ISD::OR:
2605 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002606 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002607 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002608 case ISD::MUL:
2609 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002610 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002611 // that too is okay if they are integer operations.
2612 Tmp1 = PromoteOp(Node->getOperand(0));
2613 Tmp2 = PromoteOp(Node->getOperand(1));
2614 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2615 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002616 break;
2617 case ISD::FADD:
2618 case ISD::FSUB:
2619 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002620 Tmp1 = PromoteOp(Node->getOperand(0));
2621 Tmp2 = PromoteOp(Node->getOperand(1));
2622 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2623 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2624
2625 // Floating point operations will give excess precision that we may not be
2626 // able to tolerate. If we DO allow excess precision, just leave it,
2627 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002628 // FIXME: Why would we need to round FP ops more than integer ones?
2629 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002630 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002631 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2632 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002633 break;
2634
Chris Lattner8b6fa222005-01-15 22:16:26 +00002635 case ISD::SDIV:
2636 case ISD::SREM:
2637 // These operators require that their input be sign extended.
2638 Tmp1 = PromoteOp(Node->getOperand(0));
2639 Tmp2 = PromoteOp(Node->getOperand(1));
2640 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002641 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2642 DAG.getValueType(VT));
2643 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2644 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002645 }
2646 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2647
2648 // Perform FP_ROUND: this is probably overly pessimistic.
2649 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002650 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2651 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002652 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002653 case ISD::FDIV:
2654 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002655 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002656 // These operators require that their input be fp extended.
2657 Tmp1 = PromoteOp(Node->getOperand(0));
2658 Tmp2 = PromoteOp(Node->getOperand(1));
2659 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2660
2661 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002662 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002663 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2664 DAG.getValueType(VT));
2665 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002666
2667 case ISD::UDIV:
2668 case ISD::UREM:
2669 // These operators require that their input be zero extended.
2670 Tmp1 = PromoteOp(Node->getOperand(0));
2671 Tmp2 = PromoteOp(Node->getOperand(1));
2672 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002673 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2674 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002675 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2676 break;
2677
2678 case ISD::SHL:
2679 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002680 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002681 break;
2682 case ISD::SRA:
2683 // The input value must be properly sign extended.
2684 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002685 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2686 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002687 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002688 break;
2689 case ISD::SRL:
2690 // The input value must be properly zero extended.
2691 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002692 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002693 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002694 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002695
2696 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002697 Tmp1 = Node->getOperand(0); // Get the chain.
2698 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002699 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2700 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2701 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2702 } else {
2703 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2704 Node->getOperand(2));
2705 // Increment the pointer, VAList, to the next vaarg
2706 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2707 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2708 TLI.getPointerTy()));
2709 // Store the incremented VAList to the legalized pointer
2710 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2711 Node->getOperand(2));
2712 // Load the actual argument out of the pointer VAList
2713 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2714 DAG.getSrcValue(0), VT);
2715 }
2716 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002717 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002718 break;
2719
Chris Lattner03c85462005-01-15 05:21:40 +00002720 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002721 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2722 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002723 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002724 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002725 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002726 case ISD::SEXTLOAD:
2727 case ISD::ZEXTLOAD:
2728 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002729 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2730 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002731 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002732 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002733 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002734 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002735 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002736 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2737 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002738 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002739 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002740 case ISD::SELECT_CC:
2741 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2742 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2743 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002744 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002745 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002746 case ISD::BSWAP:
2747 Tmp1 = Node->getOperand(0);
2748 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2749 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2750 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2751 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2752 TLI.getShiftAmountTy()));
2753 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002754 case ISD::CTPOP:
2755 case ISD::CTTZ:
2756 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002757 // Zero extend the argument
2758 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002759 // Perform the larger operation, then subtract if needed.
2760 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002761 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002762 case ISD::CTPOP:
2763 Result = Tmp1;
2764 break;
2765 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002766 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002767 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002768 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002769 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002770 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002771 break;
2772 case ISD::CTLZ:
2773 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002774 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2775 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002776 getSizeInBits(VT), NVT));
2777 break;
2778 }
2779 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002780 }
2781
2782 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002783
2784 // Make sure the result is itself legal.
2785 Result = LegalizeOp(Result);
2786
2787 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002788 AddPromotedOperand(Op, Result);
2789 return Result;
2790}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002791
Nate Begeman750ac1b2006-02-01 07:19:44 +00002792/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2793/// with condition CC on the current target. This usually involves legalizing
2794/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2795/// there may be no choice but to create a new SetCC node to represent the
2796/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2797/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2798void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2799 SDOperand &RHS,
2800 SDOperand &CC) {
2801 SDOperand Tmp1, Tmp2, Result;
2802
2803 switch (getTypeAction(LHS.getValueType())) {
2804 case Legal:
2805 Tmp1 = LegalizeOp(LHS); // LHS
2806 Tmp2 = LegalizeOp(RHS); // RHS
2807 break;
2808 case Promote:
2809 Tmp1 = PromoteOp(LHS); // LHS
2810 Tmp2 = PromoteOp(RHS); // RHS
2811
2812 // If this is an FP compare, the operands have already been extended.
2813 if (MVT::isInteger(LHS.getValueType())) {
2814 MVT::ValueType VT = LHS.getValueType();
2815 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2816
2817 // Otherwise, we have to insert explicit sign or zero extends. Note
2818 // that we could insert sign extends for ALL conditions, but zero extend
2819 // is cheaper on many machines (an AND instead of two shifts), so prefer
2820 // it.
2821 switch (cast<CondCodeSDNode>(CC)->get()) {
2822 default: assert(0 && "Unknown integer comparison!");
2823 case ISD::SETEQ:
2824 case ISD::SETNE:
2825 case ISD::SETUGE:
2826 case ISD::SETUGT:
2827 case ISD::SETULE:
2828 case ISD::SETULT:
2829 // ALL of these operations will work if we either sign or zero extend
2830 // the operands (including the unsigned comparisons!). Zero extend is
2831 // usually a simpler/cheaper operation, so prefer it.
2832 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2833 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2834 break;
2835 case ISD::SETGE:
2836 case ISD::SETGT:
2837 case ISD::SETLT:
2838 case ISD::SETLE:
2839 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2840 DAG.getValueType(VT));
2841 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2842 DAG.getValueType(VT));
2843 break;
2844 }
2845 }
2846 break;
2847 case Expand:
2848 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2849 ExpandOp(LHS, LHSLo, LHSHi);
2850 ExpandOp(RHS, RHSLo, RHSHi);
2851 switch (cast<CondCodeSDNode>(CC)->get()) {
2852 case ISD::SETEQ:
2853 case ISD::SETNE:
2854 if (RHSLo == RHSHi)
2855 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2856 if (RHSCST->isAllOnesValue()) {
2857 // Comparison to -1.
2858 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2859 Tmp2 = RHSLo;
2860 break;
2861 }
2862
2863 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2864 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2865 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2866 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2867 break;
2868 default:
2869 // If this is a comparison of the sign bit, just look at the top part.
2870 // X > -1, x < 0
2871 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2872 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2873 CST->getValue() == 0) || // X < 0
2874 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2875 CST->isAllOnesValue())) { // X > -1
2876 Tmp1 = LHSHi;
2877 Tmp2 = RHSHi;
2878 break;
2879 }
2880
2881 // FIXME: This generated code sucks.
2882 ISD::CondCode LowCC;
2883 switch (cast<CondCodeSDNode>(CC)->get()) {
2884 default: assert(0 && "Unknown integer setcc!");
2885 case ISD::SETLT:
2886 case ISD::SETULT: LowCC = ISD::SETULT; break;
2887 case ISD::SETGT:
2888 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2889 case ISD::SETLE:
2890 case ISD::SETULE: LowCC = ISD::SETULE; break;
2891 case ISD::SETGE:
2892 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2893 }
2894
2895 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2896 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2897 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2898
2899 // NOTE: on targets without efficient SELECT of bools, we can always use
2900 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2901 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2902 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2903 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2904 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2905 Result, Tmp1, Tmp2));
2906 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002907 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002908 }
2909 }
2910 LHS = Tmp1;
2911 RHS = Tmp2;
2912}
2913
Chris Lattner35481892005-12-23 00:16:34 +00002914/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002915/// The resultant code need not be legal. Note that SrcOp is the input operand
2916/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002917SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2918 SDOperand SrcOp) {
2919 // Create the stack frame object.
2920 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2921 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002922 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002923 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2924
2925 // Emit a store to the stack slot.
2926 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002927 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002928 // Result is a load from the stack slot.
2929 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2930}
2931
Chris Lattner5b359c62005-04-02 04:00:59 +00002932void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2933 SDOperand Op, SDOperand Amt,
2934 SDOperand &Lo, SDOperand &Hi) {
2935 // Expand the subcomponents.
2936 SDOperand LHSL, LHSH;
2937 ExpandOp(Op, LHSL, LHSH);
2938
2939 std::vector<SDOperand> Ops;
2940 Ops.push_back(LHSL);
2941 Ops.push_back(LHSH);
2942 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002943 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002944 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002945 Hi = Lo.getValue(1);
2946}
2947
2948
Chris Lattnere34b3962005-01-19 04:19:40 +00002949/// ExpandShift - Try to find a clever way to expand this shift operation out to
2950/// smaller elements. If we can't find a way that is more efficient than a
2951/// libcall on this target, return false. Otherwise, return true with the
2952/// low-parts expanded into Lo and Hi.
2953bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2954 SDOperand &Lo, SDOperand &Hi) {
2955 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2956 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002957
Chris Lattnere34b3962005-01-19 04:19:40 +00002958 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002959 SDOperand ShAmt = LegalizeOp(Amt);
2960 MVT::ValueType ShTy = ShAmt.getValueType();
2961 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2962 unsigned NVTBits = MVT::getSizeInBits(NVT);
2963
2964 // Handle the case when Amt is an immediate. Other cases are currently broken
2965 // and are disabled.
2966 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2967 unsigned Cst = CN->getValue();
2968 // Expand the incoming operand to be shifted, so that we have its parts
2969 SDOperand InL, InH;
2970 ExpandOp(Op, InL, InH);
2971 switch(Opc) {
2972 case ISD::SHL:
2973 if (Cst > VTBits) {
2974 Lo = DAG.getConstant(0, NVT);
2975 Hi = DAG.getConstant(0, NVT);
2976 } else if (Cst > NVTBits) {
2977 Lo = DAG.getConstant(0, NVT);
2978 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002979 } else if (Cst == NVTBits) {
2980 Lo = DAG.getConstant(0, NVT);
2981 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002982 } else {
2983 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2984 Hi = DAG.getNode(ISD::OR, NVT,
2985 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2986 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2987 }
2988 return true;
2989 case ISD::SRL:
2990 if (Cst > VTBits) {
2991 Lo = DAG.getConstant(0, NVT);
2992 Hi = DAG.getConstant(0, NVT);
2993 } else if (Cst > NVTBits) {
2994 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2995 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002996 } else if (Cst == NVTBits) {
2997 Lo = InH;
2998 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002999 } else {
3000 Lo = DAG.getNode(ISD::OR, NVT,
3001 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3002 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3003 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3004 }
3005 return true;
3006 case ISD::SRA:
3007 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003008 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003009 DAG.getConstant(NVTBits-1, ShTy));
3010 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003011 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003012 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003013 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003014 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003015 } else if (Cst == NVTBits) {
3016 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003017 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003018 DAG.getConstant(NVTBits-1, ShTy));
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::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3024 }
3025 return true;
3026 }
3027 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003028 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003029}
Chris Lattner77e77a62005-01-21 06:05:23 +00003030
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003031
Chris Lattner77e77a62005-01-21 06:05:23 +00003032// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3033// does not fit into a register, return the lo part and set the hi part to the
3034// by-reg argument. If it does fit into a single register, return the result
3035// and leave the Hi part unset.
3036SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3037 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003038 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3039 // The input chain to this libcall is the entry node of the function.
3040 // Legalizing the call will automatically add the previous call to the
3041 // dependence.
3042 SDOperand InChain = DAG.getEntryNode();
3043
Chris Lattner77e77a62005-01-21 06:05:23 +00003044 TargetLowering::ArgListTy Args;
3045 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3046 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3047 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3048 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3049 }
3050 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003051
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003052 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003053 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003054 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003055 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3056 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003057
Chris Lattner6831a812006-02-13 09:18:02 +00003058 // Legalize the call sequence, starting with the chain. This will advance
3059 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3060 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3061 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003062 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003063 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003064 default: assert(0 && "Unknown thing");
3065 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003066 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003067 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003068 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003069 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003070 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003071 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003072 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003073}
3074
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003075
Chris Lattner77e77a62005-01-21 06:05:23 +00003076/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3077/// destination type is legal.
3078SDOperand SelectionDAGLegalize::
3079ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003080 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003081 assert(getTypeAction(Source.getValueType()) == Expand &&
3082 "This is not an expansion!");
3083 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3084
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003085 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003086 assert(Source.getValueType() == MVT::i64 &&
3087 "This only works for 64-bit -> FP");
3088 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3089 // incoming integer is set. To handle this, we dynamically test to see if
3090 // it is set, and, if so, add a fudge factor.
3091 SDOperand Lo, Hi;
3092 ExpandOp(Source, Lo, Hi);
3093
Chris Lattner66de05b2005-05-13 04:45:13 +00003094 // If this is unsigned, and not supported, first perform the conversion to
3095 // signed, then adjust the result if the sign bit is set.
3096 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3097 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3098
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003099 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3100 DAG.getConstant(0, Hi.getValueType()),
3101 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003102 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3103 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3104 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003105 uint64_t FF = 0x5f800000ULL;
3106 if (TLI.isLittleEndian()) FF <<= 32;
3107 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003108
Chris Lattner5839bf22005-08-26 17:15:30 +00003109 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003110 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3111 SDOperand FudgeInReg;
3112 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003113 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3114 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003115 else {
3116 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003117 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3118 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003119 }
Chris Lattner473a9902005-09-29 06:44:39 +00003120 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003121 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003122
Chris Lattnera88a2602005-05-14 05:33:54 +00003123 // Check to see if the target has a custom way to lower this. If so, use it.
3124 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3125 default: assert(0 && "This action not implemented for this operation!");
3126 case TargetLowering::Legal:
3127 case TargetLowering::Expand:
3128 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003129 case TargetLowering::Custom: {
3130 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3131 Source), DAG);
3132 if (NV.Val)
3133 return LegalizeOp(NV);
3134 break; // The target decided this was legal after all
3135 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003136 }
3137
Chris Lattner13689e22005-05-12 07:00:44 +00003138 // Expand the source, then glue it back together for the call. We must expand
3139 // the source in case it is shared (this pass of legalize must traverse it).
3140 SDOperand SrcLo, SrcHi;
3141 ExpandOp(Source, SrcLo, SrcHi);
3142 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3143
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003144 const char *FnName = 0;
3145 if (DestTy == MVT::f32)
3146 FnName = "__floatdisf";
3147 else {
3148 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3149 FnName = "__floatdidf";
3150 }
Chris Lattner6831a812006-02-13 09:18:02 +00003151
3152 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3153 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003154 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003155}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003156
Chris Lattner22cde6a2006-01-28 08:25:58 +00003157/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3158/// INT_TO_FP operation of the specified operand when the target requests that
3159/// we expand it. At this point, we know that the result and operand types are
3160/// legal for the target.
3161SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3162 SDOperand Op0,
3163 MVT::ValueType DestVT) {
3164 if (Op0.getValueType() == MVT::i32) {
3165 // simple 32-bit [signed|unsigned] integer to float/double expansion
3166
3167 // get the stack frame index of a 8 byte buffer
3168 MachineFunction &MF = DAG.getMachineFunction();
3169 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3170 // get address of 8 byte buffer
3171 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3172 // word offset constant for Hi/Lo address computation
3173 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3174 // set up Hi and Lo (into buffer) address based on endian
3175 SDOperand Hi, Lo;
3176 if (TLI.isLittleEndian()) {
3177 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3178 Lo = StackSlot;
3179 } else {
3180 Hi = StackSlot;
3181 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3182 }
3183 // if signed map to unsigned space
3184 SDOperand Op0Mapped;
3185 if (isSigned) {
3186 // constant used to invert sign bit (signed to unsigned mapping)
3187 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3188 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3189 } else {
3190 Op0Mapped = Op0;
3191 }
3192 // store the lo of the constructed double - based on integer input
3193 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3194 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3195 // initial hi portion of constructed double
3196 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3197 // store the hi of the constructed double - biased exponent
3198 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3199 InitialHi, Hi, DAG.getSrcValue(NULL));
3200 // load the constructed double
3201 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3202 DAG.getSrcValue(NULL));
3203 // FP constant to bias correct the final result
3204 SDOperand Bias = DAG.getConstantFP(isSigned ?
3205 BitsToDouble(0x4330000080000000ULL)
3206 : BitsToDouble(0x4330000000000000ULL),
3207 MVT::f64);
3208 // subtract the bias
3209 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3210 // final result
3211 SDOperand Result;
3212 // handle final rounding
3213 if (DestVT == MVT::f64) {
3214 // do nothing
3215 Result = Sub;
3216 } else {
3217 // if f32 then cast to f32
3218 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3219 }
3220 return Result;
3221 }
3222 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3223 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3224
3225 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3226 DAG.getConstant(0, Op0.getValueType()),
3227 ISD::SETLT);
3228 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3229 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3230 SignSet, Four, Zero);
3231
3232 // If the sign bit of the integer is set, the large number will be treated
3233 // as a negative number. To counteract this, the dynamic code adds an
3234 // offset depending on the data type.
3235 uint64_t FF;
3236 switch (Op0.getValueType()) {
3237 default: assert(0 && "Unsupported integer type!");
3238 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3239 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3240 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3241 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3242 }
3243 if (TLI.isLittleEndian()) FF <<= 32;
3244 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3245
3246 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3247 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3248 SDOperand FudgeInReg;
3249 if (DestVT == MVT::f32)
3250 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3251 DAG.getSrcValue(NULL));
3252 else {
3253 assert(DestVT == MVT::f64 && "Unexpected conversion");
3254 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3255 DAG.getEntryNode(), CPIdx,
3256 DAG.getSrcValue(NULL), MVT::f32));
3257 }
3258
3259 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3260}
3261
3262/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3263/// *INT_TO_FP operation of the specified operand when the target requests that
3264/// we promote it. At this point, we know that the result and operand types are
3265/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3266/// operation that takes a larger input.
3267SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3268 MVT::ValueType DestVT,
3269 bool isSigned) {
3270 // First step, figure out the appropriate *INT_TO_FP operation to use.
3271 MVT::ValueType NewInTy = LegalOp.getValueType();
3272
3273 unsigned OpToUse = 0;
3274
3275 // Scan for the appropriate larger type to use.
3276 while (1) {
3277 NewInTy = (MVT::ValueType)(NewInTy+1);
3278 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3279
3280 // If the target supports SINT_TO_FP of this type, use it.
3281 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3282 default: break;
3283 case TargetLowering::Legal:
3284 if (!TLI.isTypeLegal(NewInTy))
3285 break; // Can't use this datatype.
3286 // FALL THROUGH.
3287 case TargetLowering::Custom:
3288 OpToUse = ISD::SINT_TO_FP;
3289 break;
3290 }
3291 if (OpToUse) break;
3292 if (isSigned) continue;
3293
3294 // If the target supports UINT_TO_FP of this type, use it.
3295 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3296 default: break;
3297 case TargetLowering::Legal:
3298 if (!TLI.isTypeLegal(NewInTy))
3299 break; // Can't use this datatype.
3300 // FALL THROUGH.
3301 case TargetLowering::Custom:
3302 OpToUse = ISD::UINT_TO_FP;
3303 break;
3304 }
3305 if (OpToUse) break;
3306
3307 // Otherwise, try a larger type.
3308 }
3309
3310 // Okay, we found the operation and type to use. Zero extend our input to the
3311 // desired type then run the operation on it.
3312 return DAG.getNode(OpToUse, DestVT,
3313 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3314 NewInTy, LegalOp));
3315}
3316
3317/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3318/// FP_TO_*INT operation of the specified operand when the target requests that
3319/// we promote it. At this point, we know that the result and operand types are
3320/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3321/// operation that returns a larger result.
3322SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3323 MVT::ValueType DestVT,
3324 bool isSigned) {
3325 // First step, figure out the appropriate FP_TO*INT operation to use.
3326 MVT::ValueType NewOutTy = DestVT;
3327
3328 unsigned OpToUse = 0;
3329
3330 // Scan for the appropriate larger type to use.
3331 while (1) {
3332 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3333 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3334
3335 // If the target supports FP_TO_SINT returning this type, use it.
3336 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3337 default: break;
3338 case TargetLowering::Legal:
3339 if (!TLI.isTypeLegal(NewOutTy))
3340 break; // Can't use this datatype.
3341 // FALL THROUGH.
3342 case TargetLowering::Custom:
3343 OpToUse = ISD::FP_TO_SINT;
3344 break;
3345 }
3346 if (OpToUse) break;
3347
3348 // If the target supports FP_TO_UINT of this type, use it.
3349 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3350 default: break;
3351 case TargetLowering::Legal:
3352 if (!TLI.isTypeLegal(NewOutTy))
3353 break; // Can't use this datatype.
3354 // FALL THROUGH.
3355 case TargetLowering::Custom:
3356 OpToUse = ISD::FP_TO_UINT;
3357 break;
3358 }
3359 if (OpToUse) break;
3360
3361 // Otherwise, try a larger type.
3362 }
3363
3364 // Okay, we found the operation and type to use. Truncate the result of the
3365 // extended FP_TO_*INT operation to the desired size.
3366 return DAG.getNode(ISD::TRUNCATE, DestVT,
3367 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3368}
3369
3370/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3371///
3372SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3373 MVT::ValueType VT = Op.getValueType();
3374 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3375 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3376 switch (VT) {
3377 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3378 case MVT::i16:
3379 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3380 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3381 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3382 case MVT::i32:
3383 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3384 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3385 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3386 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3387 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3388 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3389 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3390 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3391 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3392 case MVT::i64:
3393 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3394 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3395 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3396 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3397 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3398 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3399 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3400 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3401 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3402 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3403 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3404 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3405 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3406 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3407 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3408 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3409 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3410 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3411 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3412 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3413 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3414 }
3415}
3416
3417/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3418///
3419SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3420 switch (Opc) {
3421 default: assert(0 && "Cannot expand this yet!");
3422 case ISD::CTPOP: {
3423 static const uint64_t mask[6] = {
3424 0x5555555555555555ULL, 0x3333333333333333ULL,
3425 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3426 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3427 };
3428 MVT::ValueType VT = Op.getValueType();
3429 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3430 unsigned len = getSizeInBits(VT);
3431 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3432 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3433 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3434 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3435 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3436 DAG.getNode(ISD::AND, VT,
3437 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3438 }
3439 return Op;
3440 }
3441 case ISD::CTLZ: {
3442 // for now, we do this:
3443 // x = x | (x >> 1);
3444 // x = x | (x >> 2);
3445 // ...
3446 // x = x | (x >>16);
3447 // x = x | (x >>32); // for 64-bit input
3448 // return popcount(~x);
3449 //
3450 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3451 MVT::ValueType VT = Op.getValueType();
3452 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3453 unsigned len = getSizeInBits(VT);
3454 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3455 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3456 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3457 }
3458 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3459 return DAG.getNode(ISD::CTPOP, VT, Op);
3460 }
3461 case ISD::CTTZ: {
3462 // for now, we use: { return popcount(~x & (x - 1)); }
3463 // unless the target has ctlz but not ctpop, in which case we use:
3464 // { return 32 - nlz(~x & (x-1)); }
3465 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3466 MVT::ValueType VT = Op.getValueType();
3467 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3468 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3469 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3470 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3471 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3472 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3473 TLI.isOperationLegal(ISD::CTLZ, VT))
3474 return DAG.getNode(ISD::SUB, VT,
3475 DAG.getConstant(getSizeInBits(VT), VT),
3476 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3477 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3478 }
3479 }
3480}
Chris Lattnere34b3962005-01-19 04:19:40 +00003481
3482
Chris Lattner3e928bb2005-01-07 07:47:09 +00003483/// ExpandOp - Expand the specified SDOperand into its two component pieces
3484/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3485/// LegalizeNodes map is filled in for any results that are not expanded, the
3486/// ExpandedNodes map is filled in for any results that are expanded, and the
3487/// Lo/Hi values are returned.
3488void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3489 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003490 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003491 SDNode *Node = Op.Val;
3492 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003493 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3494 "Cannot expand FP values!");
3495 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003496 "Cannot expand to FP value or to larger int value!");
3497
Chris Lattner6fdcb252005-09-02 20:32:45 +00003498 // See if we already expanded it.
3499 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3500 = ExpandedNodes.find(Op);
3501 if (I != ExpandedNodes.end()) {
3502 Lo = I->second.first;
3503 Hi = I->second.second;
3504 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003505 }
3506
Chris Lattner3e928bb2005-01-07 07:47:09 +00003507 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003508 case ISD::CopyFromReg:
3509 assert(0 && "CopyFromReg must be legal!");
3510 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003511 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3512 assert(0 && "Do not know how to expand this operator!");
3513 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003514 case ISD::UNDEF:
3515 Lo = DAG.getNode(ISD::UNDEF, NVT);
3516 Hi = DAG.getNode(ISD::UNDEF, NVT);
3517 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003518 case ISD::Constant: {
3519 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3520 Lo = DAG.getConstant(Cst, NVT);
3521 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3522 break;
3523 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003524 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003525 // Return the operands.
3526 Lo = Node->getOperand(0);
3527 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003528 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003529
3530 case ISD::SIGN_EXTEND_INREG:
3531 ExpandOp(Node->getOperand(0), Lo, Hi);
3532 // Sign extend the lo-part.
3533 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3534 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3535 TLI.getShiftAmountTy()));
3536 // sext_inreg the low part if needed.
3537 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3538 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003539
Nate Begemand88fc032006-01-14 03:14:10 +00003540 case ISD::BSWAP: {
3541 ExpandOp(Node->getOperand(0), Lo, Hi);
3542 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3543 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3544 Lo = TempLo;
3545 break;
3546 }
3547
Chris Lattneredb1add2005-05-11 04:51:16 +00003548 case ISD::CTPOP:
3549 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003550 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3551 DAG.getNode(ISD::CTPOP, NVT, Lo),
3552 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003553 Hi = DAG.getConstant(0, NVT);
3554 break;
3555
Chris Lattner39a8f332005-05-12 19:05:01 +00003556 case ISD::CTLZ: {
3557 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003558 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003559 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3560 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003561 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3562 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003563 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3564 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3565
3566 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3567 Hi = DAG.getConstant(0, NVT);
3568 break;
3569 }
3570
3571 case ISD::CTTZ: {
3572 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003573 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003574 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3575 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003576 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3577 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003578 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3579 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3580
3581 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3582 Hi = DAG.getConstant(0, NVT);
3583 break;
3584 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003585
Nate Begemanacc398c2006-01-25 18:21:52 +00003586 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003587 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3588 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003589 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3590 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3591
3592 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003593 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003594 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3595 if (!TLI.isLittleEndian())
3596 std::swap(Lo, Hi);
3597 break;
3598 }
3599
Chris Lattner3e928bb2005-01-07 07:47:09 +00003600 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003601 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3602 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003603 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003604
3605 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003606 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003607 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3608 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003609 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003610 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003611
3612 // Build a factor node to remember that this load is independent of the
3613 // other one.
3614 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3615 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003616
Chris Lattner3e928bb2005-01-07 07:47:09 +00003617 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003618 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003619 if (!TLI.isLittleEndian())
3620 std::swap(Lo, Hi);
3621 break;
3622 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003623 case ISD::AND:
3624 case ISD::OR:
3625 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3626 SDOperand LL, LH, RL, RH;
3627 ExpandOp(Node->getOperand(0), LL, LH);
3628 ExpandOp(Node->getOperand(1), RL, RH);
3629 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3630 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3631 break;
3632 }
3633 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003634 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003635 ExpandOp(Node->getOperand(1), LL, LH);
3636 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003637 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3638 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003639 break;
3640 }
Nate Begeman9373a812005-08-10 20:51:12 +00003641 case ISD::SELECT_CC: {
3642 SDOperand TL, TH, FL, FH;
3643 ExpandOp(Node->getOperand(2), TL, TH);
3644 ExpandOp(Node->getOperand(3), FL, FH);
3645 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3646 Node->getOperand(1), TL, FL, Node->getOperand(4));
3647 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3648 Node->getOperand(1), TH, FH, Node->getOperand(4));
3649 break;
3650 }
Nate Begeman144ff662005-10-13 17:15:37 +00003651 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003652 SDOperand Chain = Node->getOperand(0);
3653 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003654 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3655
3656 if (EVT == NVT)
3657 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3658 else
3659 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3660 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003661
3662 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003663 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003664
Nate Begeman144ff662005-10-13 17:15:37 +00003665 // The high part is obtained by SRA'ing all but one of the bits of the lo
3666 // part.
3667 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3668 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3669 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003670 break;
3671 }
3672 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003673 SDOperand Chain = Node->getOperand(0);
3674 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003675 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3676
3677 if (EVT == NVT)
3678 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3679 else
3680 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3681 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003682
3683 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003684 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003685
Nate Begeman144ff662005-10-13 17:15:37 +00003686 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003687 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003688 break;
3689 }
3690 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003691 SDOperand Chain = Node->getOperand(0);
3692 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003693 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3694
3695 if (EVT == NVT)
3696 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3697 else
3698 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3699 EVT);
3700
3701 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003702 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003703
3704 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003705 Hi = DAG.getNode(ISD::UNDEF, NVT);
3706 break;
3707 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003708 case ISD::ANY_EXTEND:
3709 // The low part is any extension of the input (which degenerates to a copy).
3710 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3711 // The high part is undefined.
3712 Hi = DAG.getNode(ISD::UNDEF, NVT);
3713 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003714 case ISD::SIGN_EXTEND: {
3715 // The low part is just a sign extension of the input (which degenerates to
3716 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003717 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003718
Chris Lattner3e928bb2005-01-07 07:47:09 +00003719 // The high part is obtained by SRA'ing all but one of the bits of the lo
3720 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003721 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003722 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3723 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003724 break;
3725 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003726 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003727 // The low part is just a zero extension of the input (which degenerates to
3728 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003729 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003730
Chris Lattner3e928bb2005-01-07 07:47:09 +00003731 // The high part is just a zero.
3732 Hi = DAG.getConstant(0, NVT);
3733 break;
Chris Lattner35481892005-12-23 00:16:34 +00003734
3735 case ISD::BIT_CONVERT: {
3736 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3737 Node->getOperand(0));
3738 ExpandOp(Tmp, Lo, Hi);
3739 break;
3740 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003741
Chris Lattner8137c9e2006-01-28 05:07:51 +00003742 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003743 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3744 TargetLowering::Custom &&
3745 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003746 Lo = TLI.LowerOperation(Op, DAG);
3747 assert(Lo.Val && "Node must be custom expanded!");
3748 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003749 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003750 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003751 break;
3752
Chris Lattner4e6c7462005-01-08 19:27:05 +00003753 // These operators cannot be expanded directly, emit them as calls to
3754 // library functions.
3755 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003756 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003757 SDOperand Op;
3758 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3759 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003760 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3761 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003762 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003763
Chris Lattnerf20d1832005-07-30 01:40:57 +00003764 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3765
Chris Lattner80a3e942005-07-29 00:33:32 +00003766 // Now that the custom expander is done, expand the result, which is still
3767 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003768 if (Op.Val) {
3769 ExpandOp(Op, Lo, Hi);
3770 break;
3771 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003772 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003773
Chris Lattner4e6c7462005-01-08 19:27:05 +00003774 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003775 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003776 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003777 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003778 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003779
Chris Lattner4e6c7462005-01-08 19:27:05 +00003780 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003781 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003782 SDOperand Op;
3783 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3784 case Expand: assert(0 && "cannot expand FP!");
3785 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3786 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3787 }
3788
3789 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3790
3791 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003792 if (Op.Val) {
3793 ExpandOp(Op, Lo, Hi);
3794 break;
3795 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003796 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003797
Chris Lattner4e6c7462005-01-08 19:27:05 +00003798 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003799 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003800 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003801 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003802 break;
3803
Evan Cheng05a2d562006-01-09 18:31:59 +00003804 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003805 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003806 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003807 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003808 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003809 Op = TLI.LowerOperation(Op, DAG);
3810 if (Op.Val) {
3811 // Now that the custom expander is done, expand the result, which is
3812 // still VT.
3813 ExpandOp(Op, Lo, Hi);
3814 break;
3815 }
3816 }
3817
Chris Lattnere34b3962005-01-19 04:19:40 +00003818 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003819 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003820 break;
Chris Lattner47599822005-04-02 03:38:53 +00003821
3822 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003823 TargetLowering::LegalizeAction Action =
3824 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3825 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3826 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003827 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003828 break;
3829 }
3830
Chris Lattnere34b3962005-01-19 04:19:40 +00003831 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003832 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003833 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003834 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003835
Evan Cheng05a2d562006-01-09 18:31:59 +00003836 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003837 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003838 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003839 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003840 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003841 Op = TLI.LowerOperation(Op, DAG);
3842 if (Op.Val) {
3843 // Now that the custom expander is done, expand the result, which is
3844 // still VT.
3845 ExpandOp(Op, Lo, Hi);
3846 break;
3847 }
3848 }
3849
Chris Lattnere34b3962005-01-19 04:19:40 +00003850 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003851 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003852 break;
Chris Lattner47599822005-04-02 03:38:53 +00003853
3854 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003855 TargetLowering::LegalizeAction Action =
3856 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3857 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3858 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003859 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003860 break;
3861 }
3862
Chris Lattnere34b3962005-01-19 04:19:40 +00003863 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003864 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003865 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003866 }
3867
3868 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003869 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003870 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003871 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003872 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003873 Op = TLI.LowerOperation(Op, DAG);
3874 if (Op.Val) {
3875 // Now that the custom expander is done, expand the result, which is
3876 // still VT.
3877 ExpandOp(Op, Lo, Hi);
3878 break;
3879 }
3880 }
3881
Chris Lattnere34b3962005-01-19 04:19:40 +00003882 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003883 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003884 break;
Chris Lattner47599822005-04-02 03:38:53 +00003885
3886 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003887 TargetLowering::LegalizeAction Action =
3888 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3889 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3890 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003891 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003892 break;
3893 }
3894
Chris Lattnere34b3962005-01-19 04:19:40 +00003895 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003896 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003897 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003898 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003899
Misha Brukmanedf128a2005-04-21 22:36:52 +00003900 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003901 case ISD::SUB: {
3902 // If the target wants to custom expand this, let them.
3903 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3904 TargetLowering::Custom) {
3905 Op = TLI.LowerOperation(Op, DAG);
3906 if (Op.Val) {
3907 ExpandOp(Op, Lo, Hi);
3908 break;
3909 }
3910 }
3911
3912 // Expand the subcomponents.
3913 SDOperand LHSL, LHSH, RHSL, RHSH;
3914 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3915 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00003916 std::vector<MVT::ValueType> VTs;
3917 std::vector<SDOperand> LoOps, HiOps;
3918 VTs.push_back(LHSL.getValueType());
3919 VTs.push_back(MVT::Flag);
3920 LoOps.push_back(LHSL);
3921 LoOps.push_back(RHSL);
3922 HiOps.push_back(LHSH);
3923 HiOps.push_back(RHSH);
3924 if (Node->getOpcode() == ISD::ADD) {
3925 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
3926 HiOps.push_back(Lo.getValue(1));
3927 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
3928 } else {
3929 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
3930 HiOps.push_back(Lo.getValue(1));
3931 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
3932 }
Chris Lattner84f67882005-01-20 18:52:28 +00003933 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00003934 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003935 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003936 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003937 SDOperand LL, LH, RL, RH;
3938 ExpandOp(Node->getOperand(0), LL, LH);
3939 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003940 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3941 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3942 // extended the sign bit of the low half through the upper half, and if so
3943 // emit a MULHS instead of the alternate sequence that is valid for any
3944 // i64 x i64 multiply.
3945 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3946 // is RH an extension of the sign bit of RL?
3947 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3948 RH.getOperand(1).getOpcode() == ISD::Constant &&
3949 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3950 // is LH an extension of the sign bit of LL?
3951 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3952 LH.getOperand(1).getOpcode() == ISD::Constant &&
3953 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3954 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3955 } else {
3956 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3957 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3958 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3959 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3960 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3961 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003962 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3963 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00003964 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00003965 }
3966 break;
3967 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003968 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3969 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3970 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3971 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003972 }
3973
Chris Lattner83397362005-12-21 18:02:52 +00003974 // Make sure the resultant values have been legalized themselves, unless this
3975 // is a type that requires multi-step expansion.
3976 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
3977 Lo = LegalizeOp(Lo);
3978 Hi = LegalizeOp(Hi);
3979 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003980
3981 // Remember in a map if the values will be reused later.
3982 bool isNew =
3983 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
3984 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003985}
3986
Chris Lattnerc7029802006-03-18 01:44:44 +00003987/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
3988/// two smaller values of MVT::Vector type.
3989void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
3990 SDOperand &Hi) {
3991 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
3992 SDNode *Node = Op.Val;
3993 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
3994 assert(NumElements > 1 && "Cannot split a single element vector!");
3995 unsigned NewNumElts = NumElements/2;
3996 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
3997 SDOperand TypeNode = *(Node->op_end()-1);
3998
3999 // See if we already split it.
4000 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4001 = SplitNodes.find(Op);
4002 if (I != SplitNodes.end()) {
4003 Lo = I->second.first;
4004 Hi = I->second.second;
4005 return;
4006 }
4007
4008 switch (Node->getOpcode()) {
4009 case ISD::VConstant: {
4010 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4011 LoOps.push_back(NewNumEltsNode);
4012 LoOps.push_back(TypeNode);
4013 Lo = DAG.getNode(ISD::VConstant, MVT::Vector, LoOps);
4014
4015 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4016 HiOps.push_back(NewNumEltsNode);
4017 HiOps.push_back(TypeNode);
4018 Hi = DAG.getNode(ISD::VConstant, MVT::Vector, HiOps);
4019 break;
4020 }
4021 case ISD::VADD:
4022 case ISD::VSUB:
4023 case ISD::VMUL:
4024 case ISD::VSDIV:
4025 case ISD::VUDIV:
4026 case ISD::VAND:
4027 case ISD::VOR:
4028 case ISD::VXOR: {
4029 SDOperand LL, LH, RL, RH;
4030 SplitVectorOp(Node->getOperand(0), LL, LH);
4031 SplitVectorOp(Node->getOperand(1), RL, RH);
4032
4033 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4034 NewNumEltsNode, TypeNode);
4035 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4036 NewNumEltsNode, TypeNode);
4037 break;
4038 }
4039 case ISD::VLOAD: {
4040 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4041 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4042 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4043
4044 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4045 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4046 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4047 getIntPtrConstant(IncrementSize));
4048 // FIXME: This creates a bogus srcvalue!
4049 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4050
4051 // Build a factor node to remember that this load is independent of the
4052 // other one.
4053 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4054 Hi.getValue(1));
4055
4056 // Remember that we legalized the chain.
4057 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4058 if (!TLI.isLittleEndian())
4059 std::swap(Lo, Hi);
4060 break;
4061 }
4062 }
4063
4064 // Remember in a map if the values will be reused later.
4065 bool isNew =
4066 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4067 assert(isNew && "Value already expanded?!?");
4068}
4069
4070
4071/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4072/// equivalent operation that returns a scalar (e.g. F32) or packed value
4073/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4074/// type for the result.
4075SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4076 MVT::ValueType NewVT) {
4077 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4078 SDNode *Node = Op.Val;
4079
4080 // See if we already packed it.
4081 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4082 if (I != PackedNodes.end()) return I->second;
4083
4084 SDOperand Result;
4085 switch (Node->getOpcode()) {
4086 default: assert(0 && "Unknown vector operation!");
4087 case ISD::VADD:
4088 case ISD::VSUB:
4089 case ISD::VMUL:
4090 case ISD::VSDIV:
4091 case ISD::VUDIV:
4092 case ISD::VAND:
4093 case ISD::VOR:
4094 case ISD::VXOR:
4095 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4096 NewVT,
4097 PackVectorOp(Node->getOperand(0), NewVT),
4098 PackVectorOp(Node->getOperand(1), NewVT));
4099 break;
4100 case ISD::VLOAD: {
4101 SDOperand Ch = LegalizeOp(Node->getOperand(2)); // Legalize the chain.
4102 SDOperand Ptr = LegalizeOp(Node->getOperand(3)); // Legalize the pointer.
4103
4104 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(4));
4105
4106 // Remember that we legalized the chain.
4107 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4108 break;
4109 }
4110 case ISD::VConstant:
4111 if (!MVT::isVector(NewVT)) {
4112 Result = Node->getOperand(0);
4113 } else {
4114 // If type of bisected vector is legal, turn it into a ConstantVec (which
4115 // will be lowered to a ConstantPool or something else). Otherwise, bisect
4116 // the VConstant, and return each half as a new VConstant.
4117 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
4118 Result = DAG.getNode(ISD::ConstantVec, NewVT, Ops);
4119 }
4120 break;
4121 }
4122
4123 if (TLI.isTypeLegal(NewVT))
4124 Result = LegalizeOp(Result);
4125 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4126 assert(isNew && "Value already packed?");
4127 return Result;
4128}
4129
Chris Lattner3e928bb2005-01-07 07:47:09 +00004130
4131// SelectionDAG::Legalize - This is the entry point for the file.
4132//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004133void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004134 /// run - This is the main entry point to this class.
4135 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004136 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004137}
4138