blob: 52c43af81111f294fecdcdf7e6ea25403b8c423b [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
Chris Lattner6831a812006-02-13 09:18:02 +000044 // Libcall insertion helpers.
45
46 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
47 /// legalized. We use this to ensure that calls are properly serialized
48 /// against each other, including inserted libcalls.
49 SDOperand LastCALLSEQ_END;
50
51 /// IsLegalizingCall - This member is used *only* for purposes of providing
52 /// helpful assertions that a libcall isn't created while another call is
53 /// being legalized (which could lead to non-serialized call sequences).
54 bool IsLegalizingCall;
55
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000057 Legal, // The target natively supports this operation.
58 Promote, // This operation should be executed in a larger type.
59 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000060 };
Chris Lattner6831a812006-02-13 09:18:02 +000061
Chris Lattner3e928bb2005-01-07 07:47:09 +000062 /// ValueTypeActions - This is a bitvector that contains two bits for each
63 /// value type, where the two bits correspond to the LegalizeAction enum.
64 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000065 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000066
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
70 std::map<SDOperand, SDOperand> LegalizedNodes;
71
Chris Lattner03c85462005-01-15 05:21:40 +000072 /// PromotedNodes - For nodes that are below legal width, and that have more
73 /// than one use, this map indicates what promoted value to use. This allows
74 /// us to avoid promoting the same thing more than once.
75 std::map<SDOperand, SDOperand> PromotedNodes;
76
Chris Lattnerc7029802006-03-18 01:44:44 +000077 /// ExpandedNodes - For nodes that need to be expanded this map indicates
78 /// which which operands are the expanded version of the input. This allows
79 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
81
Chris Lattnerc7029802006-03-18 01:44:44 +000082 /// SplitNodes - For vector nodes that need to be split, this map indicates
83 /// which which operands are the split version of the input. This allows us
84 /// to avoid splitting the same node more than once.
85 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
86
87 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
88 /// concrete packed types, this contains the mapping of ones we have already
89 /// processed to the result.
90 std::map<SDOperand, SDOperand> PackedNodes;
91
Chris Lattner8afc48e2005-01-07 22:28:47 +000092 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000093 LegalizedNodes.insert(std::make_pair(From, To));
94 // If someone requests legalization of the new node, return itself.
95 if (From != To)
96 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000097 }
Chris Lattner03c85462005-01-15 05:21:40 +000098 void AddPromotedOperand(SDOperand From, SDOperand To) {
99 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
100 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000101 // If someone requests legalization of the new node, return itself.
102 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000103 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000104
Chris Lattner3e928bb2005-01-07 07:47:09 +0000105public:
106
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000107 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108
Chris Lattner3e928bb2005-01-07 07:47:09 +0000109 /// getTypeAction - Return how we should legalize values of this type, either
110 /// it is already legal or we need to expand it into multiple registers of
111 /// smaller integer type, or we need to promote it to a larger type.
112 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000113 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000114 }
115
116 /// isTypeLegal - Return true if this type is legal on this target.
117 ///
118 bool isTypeLegal(MVT::ValueType VT) const {
119 return getTypeAction(VT) == Legal;
120 }
121
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122 void LegalizeDAG();
123
Chris Lattner456a93a2006-01-28 07:39:30 +0000124private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000125 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
126 /// appropriate for its type.
127 void HandleOp(SDOperand Op);
128
129 /// LegalizeOp - We know that the specified value has a legal type.
130 /// Recursively ensure that the operands have legal types, then return the
131 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000133
134 /// PromoteOp - Given an operation that produces a value in an invalid type,
135 /// promote it to compute the value into a larger type. The produced value
136 /// will have the correct bits for the low portion of the register, but no
137 /// guarantee is made about the top bits: it may be zero, sign-extended, or
138 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000139 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140
Chris Lattnerc7029802006-03-18 01:44:44 +0000141 /// ExpandOp - Expand the specified SDOperand into its two component pieces
142 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
143 /// the LegalizeNodes map is filled in for any results that are not expanded,
144 /// the ExpandedNodes map is filled in for any results that are expanded, and
145 /// the Lo/Hi values are returned. This applies to integer types and Vector
146 /// types.
147 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
148
149 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
150 /// two smaller values of MVT::Vector type.
151 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
152
153 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
154 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
155 /// this is called, we know that PackedVT is the right type for the result and
156 /// we know that this type is legal for the target.
157 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
158
Chris Lattner6831a812006-02-13 09:18:02 +0000159 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
160
Nate Begeman750ac1b2006-02-01 07:19:44 +0000161 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
162
Chris Lattner77e77a62005-01-21 06:05:23 +0000163 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
164 SDOperand &Hi);
165 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
166 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000167
Chris Lattner35481892005-12-23 00:16:34 +0000168 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000169 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
170 SDOperand LegalOp,
171 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000172 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
173 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000174 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
175 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000176
Chris Lattner456a93a2006-01-28 07:39:30 +0000177 SDOperand ExpandBSWAP(SDOperand Op);
178 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000179 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
180 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000181 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
182 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000183
Chris Lattner3e928bb2005-01-07 07:47:09 +0000184 SDOperand getIntPtrConstant(uint64_t Val) {
185 return DAG.getConstant(Val, TLI.getPointerTy());
186 }
187};
188}
189
Chris Lattnerc7029802006-03-18 01:44:44 +0000190/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
191/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000192static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000193 switch (VecOp) {
194 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000195 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
196 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
197 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
198 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
199 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
200 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
201 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
202 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000203 }
204}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000205
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000206SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
207 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
208 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000209 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000210 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000211}
212
Chris Lattner32fca002005-10-06 01:20:27 +0000213/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
214/// not been visited yet and if all of its operands have already been visited.
215static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
216 std::map<SDNode*, unsigned> &Visited) {
217 if (++Visited[N] != N->getNumOperands())
218 return; // Haven't visited all operands yet
219
220 Order.push_back(N);
221
222 if (N->hasOneUse()) { // Tail recurse in common case.
223 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
224 return;
225 }
226
227 // Now that we have N in, add anything that uses it if all of their operands
228 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000229 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
230 ComputeTopDownOrdering(*UI, Order, Visited);
231}
232
Chris Lattner1618beb2005-07-29 00:11:56 +0000233
Chris Lattner3e928bb2005-01-07 07:47:09 +0000234void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000235 LastCALLSEQ_END = DAG.getEntryNode();
236 IsLegalizingCall = false;
237
Chris Lattnerab510a72005-10-02 17:49:46 +0000238 // The legalize process is inherently a bottom-up recursive process (users
239 // legalize their uses before themselves). Given infinite stack space, we
240 // could just start legalizing on the root and traverse the whole graph. In
241 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000242 // blocks. To avoid this problem, compute an ordering of the nodes where each
243 // node is only legalized after all of its operands are legalized.
244 std::map<SDNode*, unsigned> Visited;
245 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000246
Chris Lattner32fca002005-10-06 01:20:27 +0000247 // Compute ordering from all of the leaves in the graphs, those (like the
248 // entry node) that have no operands.
249 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
250 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000251 if (I->getNumOperands() == 0) {
252 Visited[I] = 0 - 1U;
253 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000254 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000255 }
256
Chris Lattnerde202b32005-11-09 23:47:37 +0000257 assert(Order.size() == Visited.size() &&
258 Order.size() ==
259 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000260 "Error: DAG is cyclic!");
261 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000262
Chris Lattnerc7029802006-03-18 01:44:44 +0000263 for (unsigned i = 0, e = Order.size(); i != e; ++i)
264 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000265
266 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000267 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000268 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
269 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270
271 ExpandedNodes.clear();
272 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000273 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000274 SplitNodes.clear();
275 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000276
277 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000278 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279}
280
Chris Lattner6831a812006-02-13 09:18:02 +0000281
282/// FindCallEndFromCallStart - Given a chained node that is part of a call
283/// sequence, find the CALLSEQ_END node that terminates the call sequence.
284static SDNode *FindCallEndFromCallStart(SDNode *Node) {
285 if (Node->getOpcode() == ISD::CALLSEQ_END)
286 return Node;
287 if (Node->use_empty())
288 return 0; // No CallSeqEnd
289
290 // The chain is usually at the end.
291 SDOperand TheChain(Node, Node->getNumValues()-1);
292 if (TheChain.getValueType() != MVT::Other) {
293 // Sometimes it's at the beginning.
294 TheChain = SDOperand(Node, 0);
295 if (TheChain.getValueType() != MVT::Other) {
296 // Otherwise, hunt for it.
297 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
298 if (Node->getValueType(i) == MVT::Other) {
299 TheChain = SDOperand(Node, i);
300 break;
301 }
302
303 // Otherwise, we walked into a node without a chain.
304 if (TheChain.getValueType() != MVT::Other)
305 return 0;
306 }
307 }
308
309 for (SDNode::use_iterator UI = Node->use_begin(),
310 E = Node->use_end(); UI != E; ++UI) {
311
312 // Make sure to only follow users of our token chain.
313 SDNode *User = *UI;
314 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
315 if (User->getOperand(i) == TheChain)
316 if (SDNode *Result = FindCallEndFromCallStart(User))
317 return Result;
318 }
319 return 0;
320}
321
322/// FindCallStartFromCallEnd - Given a chained node that is part of a call
323/// sequence, find the CALLSEQ_START node that initiates the call sequence.
324static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
325 assert(Node && "Didn't find callseq_start for a call??");
326 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
327
328 assert(Node->getOperand(0).getValueType() == MVT::Other &&
329 "Node doesn't have a token chain argument!");
330 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
331}
332
333/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
334/// see if any uses can reach Dest. If no dest operands can get to dest,
335/// legalize them, legalize ourself, and return false, otherwise, return true.
336bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
337 SDNode *Dest) {
338 if (N == Dest) return true; // N certainly leads to Dest :)
339
340 // If the first result of this node has been already legalized, then it cannot
341 // reach N.
342 switch (getTypeAction(N->getValueType(0))) {
343 case Legal:
344 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
345 break;
346 case Promote:
347 if (PromotedNodes.count(SDOperand(N, 0))) return false;
348 break;
349 case Expand:
350 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
351 break;
352 }
353
354 // Okay, this node has not already been legalized. Check and legalize all
355 // operands. If none lead to Dest, then we can legalize this node.
356 bool OperandsLeadToDest = false;
357 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
358 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
359 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
360
361 if (OperandsLeadToDest) return true;
362
363 // Okay, this node looks safe, legalize it and return false.
364 switch (getTypeAction(N->getValueType(0))) {
365 case Legal:
366 LegalizeOp(SDOperand(N, 0));
367 break;
368 case Promote:
369 PromoteOp(SDOperand(N, 0));
370 break;
371 case Expand: {
372 SDOperand X, Y;
373 ExpandOp(SDOperand(N, 0), X, Y);
374 break;
375 }
376 }
377 return false;
378}
379
Chris Lattnerc7029802006-03-18 01:44:44 +0000380/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
381/// appropriate for its type.
382void SelectionDAGLegalize::HandleOp(SDOperand Op) {
383 switch (getTypeAction(Op.getValueType())) {
384 default: assert(0 && "Bad type action!");
385 case Legal: LegalizeOp(Op); break;
386 case Promote: PromoteOp(Op); break;
387 case Expand:
388 if (Op.getValueType() != MVT::Vector) {
389 SDOperand X, Y;
390 ExpandOp(Op, X, Y);
391 } else {
392 SDNode *N = Op.Val;
393 unsigned NumOps = N->getNumOperands();
394 unsigned NumElements =
395 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
396 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
397 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
398 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
399 // In the common case, this is a legal vector type, convert it to the
400 // packed operation and type now.
401 PackVectorOp(Op, PackedVT);
402 } else if (NumElements == 1) {
403 // Otherwise, if this is a single element vector, convert it to a
404 // scalar operation.
405 PackVectorOp(Op, EVT);
406 } else {
407 // Otherwise, this is a multiple element vector that isn't supported.
408 // Split it in half and legalize both parts.
409 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000410 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000411 }
412 }
413 break;
414 }
415}
Chris Lattner6831a812006-02-13 09:18:02 +0000416
417
Chris Lattnerc7029802006-03-18 01:44:44 +0000418/// LegalizeOp - We know that the specified value has a legal type.
419/// Recursively ensure that the operands have legal types, then return the
420/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000421SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000422 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000423 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000424 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000425
Chris Lattner3e928bb2005-01-07 07:47:09 +0000426 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000427 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000428 if (Node->getNumValues() > 1) {
429 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000430 if (getTypeAction(Node->getValueType(i)) != Legal) {
431 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000432 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000433 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000434 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000435 }
436 }
437
Chris Lattner45982da2005-05-12 16:53:42 +0000438 // Note that LegalizeOp may be reentered even from single-use nodes, which
439 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000440 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
441 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000442
Nate Begeman9373a812005-08-10 20:51:12 +0000443 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000444 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000445 bool isCustom = false;
446
Chris Lattner3e928bb2005-01-07 07:47:09 +0000447 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000448 case ISD::FrameIndex:
449 case ISD::EntryToken:
450 case ISD::Register:
451 case ISD::BasicBlock:
452 case ISD::TargetFrameIndex:
453 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000454 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000455 case ISD::TargetConstantPool:
456 case ISD::TargetGlobalAddress:
457 case ISD::TargetExternalSymbol:
458 case ISD::VALUETYPE:
459 case ISD::SRCVALUE:
460 case ISD::STRING:
461 case ISD::CONDCODE:
462 // Primitives must all be legal.
463 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
464 "This must be legal!");
465 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000467 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
468 // If this is a target node, legalize it by legalizing the operands then
469 // passing it through.
470 std::vector<SDOperand> Ops;
471 bool Changed = false;
472 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
473 Ops.push_back(LegalizeOp(Node->getOperand(i)));
474 Changed = Changed || Node->getOperand(i) != Ops.back();
475 }
476 if (Changed)
477 if (Node->getNumValues() == 1)
478 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
479 else {
480 std::vector<MVT::ValueType> VTs(Node->value_begin(),
481 Node->value_end());
482 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
483 }
484
485 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
486 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
487 return Result.getValue(Op.ResNo);
488 }
489 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
491 assert(0 && "Do not know how to legalize this operator!");
492 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000494 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000495 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000496 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
497 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000498 case TargetLowering::Custom:
499 Tmp1 = TLI.LowerOperation(Op, DAG);
500 if (Tmp1.Val) Result = Tmp1;
501 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000502 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000503 break;
504 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000505 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000506 case ISD::AssertSext:
507 case ISD::AssertZext:
508 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000510 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000511 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000512 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000513 Result = Node->getOperand(Op.ResNo);
514 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000515 case ISD::CopyFromReg:
516 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000517 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000518 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000519 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000520 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000521 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000522 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000523 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
525 } else {
526 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
527 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000528 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
529 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000530 // Since CopyFromReg produces two values, make sure to remember that we
531 // legalized both of them.
532 AddLegalizedOperand(Op.getValue(0), Result);
533 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
534 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000535 case ISD::UNDEF: {
536 MVT::ValueType VT = Op.getValueType();
537 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000538 default: assert(0 && "This action is not supported yet!");
539 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000540 if (MVT::isInteger(VT))
541 Result = DAG.getConstant(0, VT);
542 else if (MVT::isFloatingPoint(VT))
543 Result = DAG.getConstantFP(0, VT);
544 else
545 assert(0 && "Unknown value type!");
546 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000547 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000548 break;
549 }
550 break;
551 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000552
553 case ISD::LOCATION:
554 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
555 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
556
557 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
558 case TargetLowering::Promote:
559 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000560 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000561 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000562 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
563 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
564
565 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
566 const std::string &FName =
567 cast<StringSDNode>(Node->getOperand(3))->getValue();
568 const std::string &DirName =
569 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000570 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000571
Jim Laskeye81aecb2005-12-21 20:51:37 +0000572 std::vector<SDOperand> Ops;
573 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000574 SDOperand LineOp = Node->getOperand(1);
575 SDOperand ColOp = Node->getOperand(2);
576
577 if (useDEBUG_LOC) {
578 Ops.push_back(LineOp); // line #
579 Ops.push_back(ColOp); // col #
580 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
581 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
582 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000583 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
584 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000585 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
586 Ops.push_back(DAG.getConstant(ID, MVT::i32));
587 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
588 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000589 } else {
590 Result = Tmp1; // chain
591 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000592 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000593 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000594 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000595 if (Tmp1 != Node->getOperand(0) ||
596 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000597 std::vector<SDOperand> Ops;
598 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000599 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
600 Ops.push_back(Node->getOperand(1)); // line # must be legal.
601 Ops.push_back(Node->getOperand(2)); // col # must be legal.
602 } else {
603 // Otherwise promote them.
604 Ops.push_back(PromoteOp(Node->getOperand(1)));
605 Ops.push_back(PromoteOp(Node->getOperand(2)));
606 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000607 Ops.push_back(Node->getOperand(3)); // filename must be legal.
608 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000609 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000610 }
611 break;
612 }
613 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000614
615 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000616 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000617 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000618 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000619 case TargetLowering::Legal:
620 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
621 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
622 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
623 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000625 break;
626 }
627 break;
628
629 case ISD::DEBUG_LABEL:
630 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
631 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000632 default: assert(0 && "This action is not supported yet!");
633 case TargetLowering::Legal:
634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
635 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000637 break;
638 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000639 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000640
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 case ISD::Constant:
642 // We know we don't need to expand constants here, constants only have one
643 // value and we check that it is fine above.
644
645 // FIXME: Maybe we should handle things like targets that don't support full
646 // 32-bit immediates?
647 break;
648 case ISD::ConstantFP: {
649 // Spill FP immediates to the constant pool if the target cannot directly
650 // codegen them. Targets often have some immediate values that can be
651 // efficiently generated into an FP register without a load. We explicitly
652 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000653 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
654
655 // Check to see if this FP immediate is already legal.
656 bool isLegal = false;
657 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
658 E = TLI.legal_fpimm_end(); I != E; ++I)
659 if (CFP->isExactlyValue(*I)) {
660 isLegal = true;
661 break;
662 }
663
Chris Lattner3181a772006-01-29 06:26:56 +0000664 // If this is a legal constant, turn it into a TargetConstantFP node.
665 if (isLegal) {
666 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
667 break;
668 }
669
670 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
671 default: assert(0 && "This action is not supported yet!");
672 case TargetLowering::Custom:
673 Tmp3 = TLI.LowerOperation(Result, DAG);
674 if (Tmp3.Val) {
675 Result = Tmp3;
676 break;
677 }
678 // FALLTHROUGH
679 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000680 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000681 bool Extend = false;
682
Chris Lattner456a93a2006-01-28 07:39:30 +0000683 // If a FP immediate is precise when represented as a float and if the
684 // target can do an extending load from float to double, we put it into
685 // the constant pool as a float, even if it's is statically typed as a
686 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000687 MVT::ValueType VT = CFP->getValueType(0);
688 bool isDouble = VT == MVT::f64;
689 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
690 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000691 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
692 // Only do this if the target has a native EXTLOAD instruction from
693 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000694 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000695 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
696 VT = MVT::f32;
697 Extend = true;
698 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000699
Chris Lattner456a93a2006-01-28 07:39:30 +0000700 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000701 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000702 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
703 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000704 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000705 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
706 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000707 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000708 }
709 break;
710 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000711 case ISD::TokenFactor:
712 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000713 Tmp1 = LegalizeOp(Node->getOperand(0));
714 Tmp2 = LegalizeOp(Node->getOperand(1));
715 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
716 } else if (Node->getNumOperands() == 3) {
717 Tmp1 = LegalizeOp(Node->getOperand(0));
718 Tmp2 = LegalizeOp(Node->getOperand(1));
719 Tmp3 = LegalizeOp(Node->getOperand(2));
720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000721 } else {
722 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000723 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000724 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
725 Ops.push_back(LegalizeOp(Node->getOperand(i)));
726 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000727 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000728 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000729
Chris Lattnerb2827b02006-03-19 00:52:58 +0000730 case ISD::BUILD_VECTOR:
731 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000732 default: assert(0 && "This action is not supported yet!");
733 case TargetLowering::Custom:
734 Tmp3 = TLI.LowerOperation(Result, DAG);
735 if (Tmp3.Val) {
736 Result = Tmp3;
737 break;
738 }
739 // FALLTHROUGH
740 case TargetLowering::Expand: {
741 // We assume that built vectors are not legal, and will be immediately
742 // spilled to memory. If the values are all constants, turn this into a
743 // load from the constant pool.
744 bool isConstant = true;
745 for (SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
746 I != E; ++I) {
747 if (!isa<ConstantFPSDNode>(I) && !isa<ConstantSDNode>(I) &&
748 I->getOpcode() != ISD::UNDEF) {
749 isConstant = false;
Chris Lattnerb2827b02006-03-19 00:52:58 +0000750 break;
751 }
752 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000753
754 // Create a ConstantPacked, and put it in the constant pool.
755 if (isConstant) {
756 MVT::ValueType VT = Node->getValueType(0);
757 const Type *OpNTy =
758 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
759 std::vector<Constant*> CV;
760 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
761 if (ConstantFPSDNode *V =
762 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
763 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
764 } else if (ConstantSDNode *V =
765 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
766 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
767 } else {
768 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
769 CV.push_back(UndefValue::get(OpNTy));
770 }
771 }
772 Constant *CP = ConstantPacked::get(CV);
773 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
774 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
775 DAG.getSrcValue(NULL));
776 break;
777 }
778
779 // Otherwise, this isn't a constant entry. Allocate a sufficiently
780 // aligned object on the stack, store each element into it, then load
781 // the result as a vector.
782 assert(0 && "Cannot lower variable BUILD_VECTOR yet!");
783 abort();
Chris Lattnerb2827b02006-03-19 00:52:58 +0000784 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000785 }
786 }
787 break;
788 case ISD::INSERT_VECTOR_ELT:
789 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
790 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
791 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
792 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
793
794 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
795 Node->getValueType(0))) {
796 default: assert(0 && "This action is not supported yet!");
797 case TargetLowering::Legal:
798 break;
799 case TargetLowering::Custom:
800 Tmp3 = TLI.LowerOperation(Result, DAG);
801 if (Tmp3.Val) {
802 Result = Tmp3;
803 break;
804 }
805 // FALLTHROUGH
806 case TargetLowering::Expand: {
807 // If the target doesn't support this, we have to spill the input vector
808 // to a temporary stack slot, update the element, then reload it. This is
809 // badness. We could also load the value into a vector register (either
810 // with a "move to register" or "extload into register" instruction, then
811 // permute it into place, if the idx is a constant and if the idx is
812 // supported by the target.
813 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
814 break;
815 }
816 }
817 break;
Chris Lattner6831a812006-02-13 09:18:02 +0000818 case ISD::CALLSEQ_START: {
819 SDNode *CallEnd = FindCallEndFromCallStart(Node);
820
821 // Recursively Legalize all of the inputs of the call end that do not lead
822 // to this call start. This ensures that any libcalls that need be inserted
823 // are inserted *before* the CALLSEQ_START.
824 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
825 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
826
827 // Now that we legalized all of the inputs (which may have inserted
828 // libcalls) create the new CALLSEQ_START node.
829 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
830
831 // Merge in the last call, to ensure that this call start after the last
832 // call ended.
833 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
834 Tmp1 = LegalizeOp(Tmp1);
835
836 // Do not try to legalize the target-specific arguments (#1+).
837 if (Tmp1 != Node->getOperand(0)) {
838 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
839 Ops[0] = Tmp1;
840 Result = DAG.UpdateNodeOperands(Result, Ops);
841 }
842
843 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000844 AddLegalizedOperand(Op.getValue(0), Result);
845 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
846 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
847
Chris Lattner6831a812006-02-13 09:18:02 +0000848 // Now that the callseq_start and all of the non-call nodes above this call
849 // sequence have been legalized, legalize the call itself. During this
850 // process, no libcalls can/will be inserted, guaranteeing that no calls
851 // can overlap.
852 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
853 SDOperand InCallSEQ = LastCALLSEQ_END;
854 // Note that we are selecting this call!
855 LastCALLSEQ_END = SDOperand(CallEnd, 0);
856 IsLegalizingCall = true;
857
858 // Legalize the call, starting from the CALLSEQ_END.
859 LegalizeOp(LastCALLSEQ_END);
860 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
861 return Result;
862 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000863 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000864 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
865 // will cause this node to be legalized as well as handling libcalls right.
866 if (LastCALLSEQ_END.Val != Node) {
867 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
868 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
869 assert(I != LegalizedNodes.end() &&
870 "Legalizing the call start should have legalized this node!");
871 return I->second;
872 }
873
874 // Otherwise, the call start has been legalized and everything is going
875 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000876 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000877 // Do not try to legalize the target-specific arguments (#1+), except for
878 // an optional flag input.
879 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
880 if (Tmp1 != Node->getOperand(0)) {
881 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
882 Ops[0] = Tmp1;
883 Result = DAG.UpdateNodeOperands(Result, Ops);
884 }
885 } else {
886 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
887 if (Tmp1 != Node->getOperand(0) ||
888 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
889 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
890 Ops[0] = Tmp1;
891 Ops.back() = Tmp2;
892 Result = DAG.UpdateNodeOperands(Result, Ops);
893 }
Chris Lattner6a542892006-01-24 05:48:21 +0000894 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000895 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000896 // This finishes up call legalization.
897 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000898
899 // If the CALLSEQ_END node has a flag, remember that we legalized it.
900 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
901 if (Node->getNumValues() == 2)
902 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
903 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000904 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
906 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
907 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000909
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000910 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000911 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000912 switch (TLI.getOperationAction(Node->getOpcode(),
913 Node->getValueType(0))) {
914 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000915 case TargetLowering::Expand: {
916 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
917 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
918 " not tell us which reg is the stack pointer!");
919 SDOperand Chain = Tmp1.getOperand(0);
920 SDOperand Size = Tmp2.getOperand(1);
921 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
922 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
923 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000924 Tmp1 = LegalizeOp(Tmp1);
925 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000926 break;
927 }
928 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000929 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
930 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000931 Tmp1 = LegalizeOp(Tmp3);
932 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000933 }
Chris Lattner903d2782006-01-15 08:54:32 +0000934 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000935 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000936 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000937 }
Chris Lattner903d2782006-01-15 08:54:32 +0000938 // Since this op produce two values, make sure to remember that we
939 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000940 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
941 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000942 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000943 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000944 case ISD::INLINEASM:
945 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
946 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000947 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000948 Tmp2 = Tmp3 = SDOperand(0, 0);
949 else
950 Tmp3 = LegalizeOp(Tmp2);
951
952 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
953 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
954 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000955 if (Tmp3.Val) Ops.back() = Tmp3;
956 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000957 }
958
959 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000960 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000961 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
962 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000963 case ISD::BR:
964 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000965 // Ensure that libcalls are emitted before a branch.
966 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
967 Tmp1 = LegalizeOp(Tmp1);
968 LastCALLSEQ_END = DAG.getEntryNode();
969
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +0000971 break;
972
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000973 case ISD::BRCOND:
974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000975 // Ensure that libcalls are emitted before a return.
976 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
977 Tmp1 = LegalizeOp(Tmp1);
978 LastCALLSEQ_END = DAG.getEntryNode();
979
Chris Lattner47e92232005-01-18 19:27:06 +0000980 switch (getTypeAction(Node->getOperand(1).getValueType())) {
981 case Expand: assert(0 && "It's impossible to expand bools");
982 case Legal:
983 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
984 break;
985 case Promote:
986 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
987 break;
988 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000989
990 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000992
993 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
994 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000995 case TargetLowering::Legal: break;
996 case TargetLowering::Custom:
997 Tmp1 = TLI.LowerOperation(Result, DAG);
998 if (Tmp1.Val) Result = Tmp1;
999 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001000 case TargetLowering::Expand:
1001 // Expand brcond's setcc into its constituent parts and create a BR_CC
1002 // Node.
1003 if (Tmp2.getOpcode() == ISD::SETCC) {
1004 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1005 Tmp2.getOperand(0), Tmp2.getOperand(1),
1006 Node->getOperand(2));
1007 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001008 // Make sure the condition is either zero or one. It may have been
1009 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001010 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1011 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1012 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001013
Nate Begeman7cbd5252005-08-16 19:49:35 +00001014 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1015 DAG.getCondCode(ISD::SETNE), Tmp2,
1016 DAG.getConstant(0, Tmp2.getValueType()),
1017 Node->getOperand(2));
1018 }
1019 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001020 }
1021 break;
1022 case ISD::BR_CC:
1023 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001024 // Ensure that libcalls are emitted before a branch.
1025 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1026 Tmp1 = LegalizeOp(Tmp1);
1027 LastCALLSEQ_END = DAG.getEntryNode();
1028
Nate Begeman750ac1b2006-02-01 07:19:44 +00001029 Tmp2 = Node->getOperand(2); // LHS
1030 Tmp3 = Node->getOperand(3); // RHS
1031 Tmp4 = Node->getOperand(1); // CC
1032
1033 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1034
1035 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1036 // the LHS is a legal SETCC itself. In this case, we need to compare
1037 // the result against zero to select between true and false values.
1038 if (Tmp3.Val == 0) {
1039 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1040 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001041 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001042
1043 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1044 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001045
Chris Lattner181b7a32005-12-17 23:46:46 +00001046 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1047 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001048 case TargetLowering::Legal: break;
1049 case TargetLowering::Custom:
1050 Tmp4 = TLI.LowerOperation(Result, DAG);
1051 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001052 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001053 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001054 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001055 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001056 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1057 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001058
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001059 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001060 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1061 Tmp2 = Result.getValue(0);
1062 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001063
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001064 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1065 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001066 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001067 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001068 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001069 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001070 Tmp2 = LegalizeOp(Tmp1);
1071 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001072 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001073 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001074 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001075 // Since loads produce two values, make sure to remember that we
1076 // legalized both of them.
1077 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1078 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1079 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001080 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001081 case ISD::EXTLOAD:
1082 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001083 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001084 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1085 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001086
Chris Lattner5f056bf2005-07-10 01:55:33 +00001087 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001088 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001089 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001090 case TargetLowering::Promote:
1091 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1093 DAG.getValueType(MVT::i8));
1094 Tmp1 = Result.getValue(0);
1095 Tmp2 = Result.getValue(1);
1096 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001097 case TargetLowering::Custom:
1098 isCustom = true;
1099 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001100 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1102 Node->getOperand(3));
1103 Tmp1 = Result.getValue(0);
1104 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001105
1106 if (isCustom) {
1107 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1108 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001109 Tmp1 = LegalizeOp(Tmp3);
1110 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001111 }
1112 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001113 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001114 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001115 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001116 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1117 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001118 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001119 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1120 Tmp2 = LegalizeOp(Load.getValue(1));
1121 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001122 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001123 assert(Node->getOpcode() != ISD::EXTLOAD &&
1124 "EXTLOAD should always be supported!");
1125 // Turn the unsupported load into an EXTLOAD followed by an explicit
1126 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001127 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1128 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001129 SDOperand ValRes;
1130 if (Node->getOpcode() == ISD::SEXTLOAD)
1131 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001132 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001133 else
1134 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001135 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1136 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1137 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001138 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001139 // Since loads produce two values, make sure to remember that we legalized
1140 // both of them.
1141 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1142 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1143 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001144 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001145 case ISD::EXTRACT_ELEMENT: {
1146 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1147 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001148 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001149 case Legal:
1150 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1151 // 1 -> Hi
1152 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1153 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1154 TLI.getShiftAmountTy()));
1155 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1156 } else {
1157 // 0 -> Lo
1158 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1159 Node->getOperand(0));
1160 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001161 break;
1162 case Expand:
1163 // Get both the low and high parts.
1164 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1165 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1166 Result = Tmp2; // 1 -> Hi
1167 else
1168 Result = Tmp1; // 0 -> Lo
1169 break;
1170 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001171 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001172 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001173
1174 case ISD::CopyToReg:
1175 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001176
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001177 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001178 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001179 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001180 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001181 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001183 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001184 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001185 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001186 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001187 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1188 Tmp3);
1189 } else {
1190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001191 }
1192
1193 // Since this produces two values, make sure to remember that we legalized
1194 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001195 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001196 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001197 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001198 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001199 break;
1200
1201 case ISD::RET:
1202 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001203
1204 // Ensure that libcalls are emitted before a return.
1205 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1206 Tmp1 = LegalizeOp(Tmp1);
1207 LastCALLSEQ_END = DAG.getEntryNode();
1208
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 switch (Node->getNumOperands()) {
1210 case 2: // ret val
1211 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1212 case Legal:
1213 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001214 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001215 break;
1216 case Expand: {
1217 SDOperand Lo, Hi;
1218 ExpandOp(Node->getOperand(1), Lo, Hi);
1219 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001220 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001221 }
1222 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001223 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001224 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1225 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001226 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001227 }
1228 break;
1229 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001230 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001231 break;
1232 default: { // ret <values>
1233 std::vector<SDOperand> NewValues;
1234 NewValues.push_back(Tmp1);
1235 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1236 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1237 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001238 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001239 break;
1240 case Expand: {
1241 SDOperand Lo, Hi;
1242 ExpandOp(Node->getOperand(i), Lo, Hi);
1243 NewValues.push_back(Lo);
1244 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001245 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001246 }
1247 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001248 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001249 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001250
1251 if (NewValues.size() == Node->getNumOperands())
1252 Result = DAG.UpdateNodeOperands(Result, NewValues);
1253 else
1254 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001255 break;
1256 }
1257 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001258
Chris Lattner6862dbc2006-01-29 21:02:23 +00001259 if (Result.getOpcode() == ISD::RET) {
1260 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1261 default: assert(0 && "This action is not supported yet!");
1262 case TargetLowering::Legal: break;
1263 case TargetLowering::Custom:
1264 Tmp1 = TLI.LowerOperation(Result, DAG);
1265 if (Tmp1.Val) Result = Tmp1;
1266 break;
1267 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001268 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001269 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001270 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001271 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1272 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1273
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001274 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001275 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001276 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001277 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001278 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001279 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001280 } else {
1281 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001282 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001283 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001284 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1285 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001286 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001287 }
1288
Chris Lattner3e928bb2005-01-07 07:47:09 +00001289 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1290 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001291 Tmp3 = LegalizeOp(Node->getOperand(1));
1292 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1293 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001294
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001295 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001296 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1297 default: assert(0 && "This action is not supported yet!");
1298 case TargetLowering::Legal: break;
1299 case TargetLowering::Custom:
1300 Tmp1 = TLI.LowerOperation(Result, DAG);
1301 if (Tmp1.Val) Result = Tmp1;
1302 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001303 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001304 break;
1305 }
1306 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001307 // Truncate the value and store the result.
1308 Tmp3 = PromoteOp(Node->getOperand(1));
1309 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001310 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001311 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001312 break;
1313
Chris Lattner3e928bb2005-01-07 07:47:09 +00001314 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001315 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001316 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001317
1318 // If this is a vector type, then we have to calculate the increment as
1319 // the product of the element size in bytes, and the number of elements
1320 // in the high half of the vector.
1321 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1322 SDNode *InVal = Node->getOperand(1).Val;
1323 unsigned NumElems =
1324 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1325 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1326
1327 // Figure out if there is a Packed type corresponding to this Vector
1328 // type. If so, convert to the packed type.
1329 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1330 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1331 // Turn this into a normal store of the packed type.
1332 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1334 Node->getOperand(3));
1335 break;
1336 } else if (NumElems == 1) {
1337 // Turn this into a normal store of the scalar type.
1338 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1339 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1340 Node->getOperand(3));
1341 break;
1342 } else {
1343 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1344 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1345 }
1346 } else {
1347 ExpandOp(Node->getOperand(1), Lo, Hi);
1348 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1349 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001350
1351 if (!TLI.isLittleEndian())
1352 std::swap(Lo, Hi);
1353
Chris Lattneredb1add2005-05-11 04:51:16 +00001354 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1355 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001356 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1357 getIntPtrConstant(IncrementSize));
1358 assert(isTypeLegal(Tmp2.getValueType()) &&
1359 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001360 // FIXME: This sets the srcvalue of both halves to be the same, which is
1361 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001362 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1363 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001364 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1365 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001366 }
1367 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001368 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001369 case ISD::PCMARKER:
1370 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001371 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001372 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001373 case ISD::STACKSAVE:
1374 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001375 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1376 Tmp1 = Result.getValue(0);
1377 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001378
Chris Lattner140d53c2006-01-13 02:50:02 +00001379 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1380 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001381 case TargetLowering::Legal: break;
1382 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001383 Tmp3 = TLI.LowerOperation(Result, DAG);
1384 if (Tmp3.Val) {
1385 Tmp1 = LegalizeOp(Tmp3);
1386 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001387 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001388 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001389 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001390 // Expand to CopyFromReg if the target set
1391 // StackPointerRegisterToSaveRestore.
1392 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001393 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001394 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001395 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001396 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001397 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1398 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001399 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001400 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001401 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001402
1403 // Since stacksave produce two values, make sure to remember that we
1404 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001405 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1406 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1407 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001408
Chris Lattner140d53c2006-01-13 02:50:02 +00001409 case ISD::STACKRESTORE:
1410 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1411 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001412 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001413
1414 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1415 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001416 case TargetLowering::Legal: break;
1417 case TargetLowering::Custom:
1418 Tmp1 = TLI.LowerOperation(Result, DAG);
1419 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001420 break;
1421 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001422 // Expand to CopyToReg if the target set
1423 // StackPointerRegisterToSaveRestore.
1424 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1425 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1426 } else {
1427 Result = Tmp1;
1428 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001429 break;
1430 }
1431 break;
1432
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001433 case ISD::READCYCLECOUNTER:
1434 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001435 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001436
1437 // Since rdcc produce two values, make sure to remember that we legalized
1438 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001439 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001440 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001441 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001442
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001443 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001444 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1445 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1446
Chris Lattner456a93a2006-01-28 07:39:30 +00001447 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1448 "Cannot handle illegal TRUNCSTORE yet!");
1449 Tmp2 = LegalizeOp(Node->getOperand(1));
1450
1451 // The only promote case we handle is TRUNCSTORE:i1 X into
1452 // -> TRUNCSTORE:i8 (and X, 1)
1453 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1454 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1455 TargetLowering::Promote) {
1456 // Promote the bool to a mask then store.
1457 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1458 DAG.getConstant(1, Tmp2.getValueType()));
1459 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1460 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001461
Chris Lattner456a93a2006-01-28 07:39:30 +00001462 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1463 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001464 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1465 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001466 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001467
Chris Lattner456a93a2006-01-28 07:39:30 +00001468 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1469 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1470 default: assert(0 && "This action is not supported yet!");
1471 case TargetLowering::Legal: break;
1472 case TargetLowering::Custom:
1473 Tmp1 = TLI.LowerOperation(Result, DAG);
1474 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001475 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001476 }
1477 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001478 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001479 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001480 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1481 case Expand: assert(0 && "It's impossible to expand bools");
1482 case Legal:
1483 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1484 break;
1485 case Promote:
1486 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1487 break;
1488 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001489 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001490 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001491
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001492 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001493
Nate Begemanb942a3d2005-08-23 04:29:48 +00001494 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001495 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001496 case TargetLowering::Legal: break;
1497 case TargetLowering::Custom: {
1498 Tmp1 = TLI.LowerOperation(Result, DAG);
1499 if (Tmp1.Val) Result = Tmp1;
1500 break;
1501 }
Nate Begeman9373a812005-08-10 20:51:12 +00001502 case TargetLowering::Expand:
1503 if (Tmp1.getOpcode() == ISD::SETCC) {
1504 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1505 Tmp2, Tmp3,
1506 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1507 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001508 // Make sure the condition is either zero or one. It may have been
1509 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001510 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1511 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1512 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001513 Result = DAG.getSelectCC(Tmp1,
1514 DAG.getConstant(0, Tmp1.getValueType()),
1515 Tmp2, Tmp3, ISD::SETNE);
1516 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001517 break;
1518 case TargetLowering::Promote: {
1519 MVT::ValueType NVT =
1520 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1521 unsigned ExtOp, TruncOp;
1522 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001523 ExtOp = ISD::ANY_EXTEND;
1524 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001525 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001526 ExtOp = ISD::FP_EXTEND;
1527 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001528 }
1529 // Promote each of the values to the new type.
1530 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1531 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1532 // Perform the larger operation, then round down.
1533 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1534 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1535 break;
1536 }
1537 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001538 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001539 case ISD::SELECT_CC: {
1540 Tmp1 = Node->getOperand(0); // LHS
1541 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001542 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1543 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001544 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001545
Nate Begeman750ac1b2006-02-01 07:19:44 +00001546 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1547
1548 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1549 // the LHS is a legal SETCC itself. In this case, we need to compare
1550 // the result against zero to select between true and false values.
1551 if (Tmp2.Val == 0) {
1552 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1553 CC = DAG.getCondCode(ISD::SETNE);
1554 }
1555 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1556
1557 // Everything is legal, see if we should expand this op or something.
1558 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1559 default: assert(0 && "This action is not supported yet!");
1560 case TargetLowering::Legal: break;
1561 case TargetLowering::Custom:
1562 Tmp1 = TLI.LowerOperation(Result, DAG);
1563 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001564 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001565 }
1566 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001567 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001568 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001569 Tmp1 = Node->getOperand(0);
1570 Tmp2 = Node->getOperand(1);
1571 Tmp3 = Node->getOperand(2);
1572 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1573
1574 // If we had to Expand the SetCC operands into a SELECT node, then it may
1575 // not always be possible to return a true LHS & RHS. In this case, just
1576 // return the value we legalized, returned in the LHS
1577 if (Tmp2.Val == 0) {
1578 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001579 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001580 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001581
Chris Lattner73e142f2006-01-30 22:43:50 +00001582 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001583 default: assert(0 && "Cannot handle this action for SETCC yet!");
1584 case TargetLowering::Custom:
1585 isCustom = true;
1586 // FALLTHROUGH.
1587 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001588 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001589 if (isCustom) {
1590 Tmp3 = TLI.LowerOperation(Result, DAG);
1591 if (Tmp3.Val) Result = Tmp3;
1592 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001593 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001594 case TargetLowering::Promote: {
1595 // First step, figure out the appropriate operation to use.
1596 // Allow SETCC to not be supported for all legal data types
1597 // Mostly this targets FP
1598 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1599 MVT::ValueType OldVT = NewInTy;
1600
1601 // Scan for the appropriate larger type to use.
1602 while (1) {
1603 NewInTy = (MVT::ValueType)(NewInTy+1);
1604
1605 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1606 "Fell off of the edge of the integer world");
1607 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1608 "Fell off of the edge of the floating point world");
1609
1610 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001611 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001612 break;
1613 }
1614 if (MVT::isInteger(NewInTy))
1615 assert(0 && "Cannot promote Legal Integer SETCC yet");
1616 else {
1617 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1618 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1619 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001620 Tmp1 = LegalizeOp(Tmp1);
1621 Tmp2 = LegalizeOp(Tmp2);
1622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001623 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001624 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001625 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001626 case TargetLowering::Expand:
1627 // Expand a setcc node into a select_cc of the same condition, lhs, and
1628 // rhs that selects between const 1 (true) and const 0 (false).
1629 MVT::ValueType VT = Node->getValueType(0);
1630 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1631 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1632 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001633 break;
1634 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001635 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001636 case ISD::MEMSET:
1637 case ISD::MEMCPY:
1638 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001640 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1641
1642 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1643 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1644 case Expand: assert(0 && "Cannot expand a byte!");
1645 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001646 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001647 break;
1648 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001649 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001650 break;
1651 }
1652 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001653 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001654 }
Chris Lattner272455b2005-02-02 03:44:41 +00001655
1656 SDOperand Tmp4;
1657 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001658 case Expand: {
1659 // Length is too big, just take the lo-part of the length.
1660 SDOperand HiPart;
1661 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1662 break;
1663 }
Chris Lattnere5605212005-01-28 22:29:18 +00001664 case Legal:
1665 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001666 break;
1667 case Promote:
1668 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001669 break;
1670 }
1671
1672 SDOperand Tmp5;
1673 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1674 case Expand: assert(0 && "Cannot expand this yet!");
1675 case Legal:
1676 Tmp5 = LegalizeOp(Node->getOperand(4));
1677 break;
1678 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001679 Tmp5 = PromoteOp(Node->getOperand(4));
1680 break;
1681 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001682
1683 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1684 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001685 case TargetLowering::Custom:
1686 isCustom = true;
1687 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001688 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001689 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001690 if (isCustom) {
1691 Tmp1 = TLI.LowerOperation(Result, DAG);
1692 if (Tmp1.Val) Result = Tmp1;
1693 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001694 break;
1695 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001696 // Otherwise, the target does not support this operation. Lower the
1697 // operation to an explicit libcall as appropriate.
1698 MVT::ValueType IntPtr = TLI.getPointerTy();
1699 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1700 std::vector<std::pair<SDOperand, const Type*> > Args;
1701
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001702 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001703 if (Node->getOpcode() == ISD::MEMSET) {
1704 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001705 // Extend the (previously legalized) ubyte argument to be an int value
1706 // for the call.
1707 if (Tmp3.getValueType() > MVT::i32)
1708 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1709 else
1710 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001711 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1712 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1713
1714 FnName = "memset";
1715 } else if (Node->getOpcode() == ISD::MEMCPY ||
1716 Node->getOpcode() == ISD::MEMMOVE) {
1717 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1718 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1719 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1720 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1721 } else {
1722 assert(0 && "Unknown op!");
1723 }
Chris Lattner45982da2005-05-12 16:53:42 +00001724
Chris Lattnere1bd8222005-01-11 05:57:22 +00001725 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001726 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001727 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001728 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001729 break;
1730 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001731 }
1732 break;
1733 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001734
Chris Lattner5b359c62005-04-02 04:00:59 +00001735 case ISD::SHL_PARTS:
1736 case ISD::SRA_PARTS:
1737 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001738 std::vector<SDOperand> Ops;
1739 bool Changed = false;
1740 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1741 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1742 Changed |= Ops.back() != Node->getOperand(i);
1743 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001744 if (Changed)
1745 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001746
Evan Cheng05a2d562006-01-09 18:31:59 +00001747 switch (TLI.getOperationAction(Node->getOpcode(),
1748 Node->getValueType(0))) {
1749 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001750 case TargetLowering::Legal: break;
1751 case TargetLowering::Custom:
1752 Tmp1 = TLI.LowerOperation(Result, DAG);
1753 if (Tmp1.Val) {
1754 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001755 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001756 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001757 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1758 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001759 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001760 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001761 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001762 return RetVal;
1763 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001764 break;
1765 }
1766
Chris Lattner2c8086f2005-04-02 05:00:07 +00001767 // Since these produce multiple values, make sure to remember that we
1768 // legalized all of them.
1769 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1770 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1771 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001772 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001773
1774 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001775 case ISD::ADD:
1776 case ISD::SUB:
1777 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001778 case ISD::MULHS:
1779 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001780 case ISD::UDIV:
1781 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001782 case ISD::AND:
1783 case ISD::OR:
1784 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001785 case ISD::SHL:
1786 case ISD::SRL:
1787 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001788 case ISD::FADD:
1789 case ISD::FSUB:
1790 case ISD::FMUL:
1791 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001792 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001793 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1794 case Expand: assert(0 && "Not possible");
1795 case Legal:
1796 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1797 break;
1798 case Promote:
1799 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1800 break;
1801 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001802
1803 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001804
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001805 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001806 default: assert(0 && "Operation not supported");
1807 case TargetLowering::Legal: break;
1808 case TargetLowering::Custom:
1809 Tmp1 = TLI.LowerOperation(Result, DAG);
1810 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001811 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001812 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001813 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001814
1815 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1816 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1817 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1818 case Expand: assert(0 && "Not possible");
1819 case Legal:
1820 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1821 break;
1822 case Promote:
1823 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1824 break;
1825 }
1826
1827 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1828
1829 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1830 default: assert(0 && "Operation not supported");
1831 case TargetLowering::Custom:
1832 Tmp1 = TLI.LowerOperation(Result, DAG);
1833 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001834 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001835 case TargetLowering::Legal: break;
1836 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001837 // If this target supports fabs/fneg natively, do this efficiently.
1838 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1839 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1840 // Get the sign bit of the RHS.
1841 MVT::ValueType IVT =
1842 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1843 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1844 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1845 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1846 // Get the absolute value of the result.
1847 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1848 // Select between the nabs and abs value based on the sign bit of
1849 // the input.
1850 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1851 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1852 AbsVal),
1853 AbsVal);
1854 Result = LegalizeOp(Result);
1855 break;
1856 }
1857
1858 // Otherwise, do bitwise ops!
1859
1860 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001861 const char *FnName;
1862 if (Node->getValueType(0) == MVT::f32) {
1863 FnName = "copysignf";
1864 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1865 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1866 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1867 } else {
1868 FnName = "copysign";
1869 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1870 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1871 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1872 }
1873 SDOperand Dummy;
1874 Result = ExpandLibCall(FnName, Node, Dummy);
1875 break;
1876 }
1877 break;
1878
Nate Begeman551bf3f2006-02-17 05:43:56 +00001879 case ISD::ADDC:
1880 case ISD::SUBC:
1881 Tmp1 = LegalizeOp(Node->getOperand(0));
1882 Tmp2 = LegalizeOp(Node->getOperand(1));
1883 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1884 // Since this produces two values, make sure to remember that we legalized
1885 // both of them.
1886 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1887 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1888 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001889
Nate Begeman551bf3f2006-02-17 05:43:56 +00001890 case ISD::ADDE:
1891 case ISD::SUBE:
1892 Tmp1 = LegalizeOp(Node->getOperand(0));
1893 Tmp2 = LegalizeOp(Node->getOperand(1));
1894 Tmp3 = LegalizeOp(Node->getOperand(2));
1895 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1896 // Since this produces two values, make sure to remember that we legalized
1897 // both of them.
1898 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1899 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1900 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001901
Nate Begeman419f8b62005-10-18 00:27:41 +00001902 case ISD::BUILD_PAIR: {
1903 MVT::ValueType PairTy = Node->getValueType(0);
1904 // TODO: handle the case where the Lo and Hi operands are not of legal type
1905 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1906 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1907 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001908 case TargetLowering::Promote:
1909 case TargetLowering::Custom:
1910 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001911 case TargetLowering::Legal:
1912 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1913 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1914 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001915 case TargetLowering::Expand:
1916 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1917 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1918 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1919 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1920 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001921 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001922 break;
1923 }
1924 break;
1925 }
1926
Nate Begemanc105e192005-04-06 00:23:54 +00001927 case ISD::UREM:
1928 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001929 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001930 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1931 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001932
Nate Begemanc105e192005-04-06 00:23:54 +00001933 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001934 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1935 case TargetLowering::Custom:
1936 isCustom = true;
1937 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001938 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001939 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001940 if (isCustom) {
1941 Tmp1 = TLI.LowerOperation(Result, DAG);
1942 if (Tmp1.Val) Result = Tmp1;
1943 }
Nate Begemanc105e192005-04-06 00:23:54 +00001944 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001945 case TargetLowering::Expand:
1946 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001947 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001948 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001949 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001950 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1951 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1952 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1953 } else {
1954 // Floating point mod -> fmod libcall.
1955 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1956 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001957 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001958 }
1959 break;
1960 }
1961 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001962 case ISD::VAARG: {
1963 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1964 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1965
Chris Lattner5c62f332006-01-28 07:42:08 +00001966 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001967 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1968 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001969 case TargetLowering::Custom:
1970 isCustom = true;
1971 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001972 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1974 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001975 Tmp1 = Result.getValue(1);
1976
1977 if (isCustom) {
1978 Tmp2 = TLI.LowerOperation(Result, DAG);
1979 if (Tmp2.Val) {
1980 Result = LegalizeOp(Tmp2);
1981 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1982 }
1983 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001984 break;
1985 case TargetLowering::Expand: {
1986 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1987 Node->getOperand(2));
1988 // Increment the pointer, VAList, to the next vaarg
1989 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1990 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1991 TLI.getPointerTy()));
1992 // Store the incremented VAList to the legalized pointer
1993 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1994 Node->getOperand(2));
1995 // Load the actual argument out of the pointer VAList
1996 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001997 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001998 Result = LegalizeOp(Result);
1999 break;
2000 }
2001 }
2002 // Since VAARG produces two values, make sure to remember that we
2003 // legalized both of them.
2004 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002005 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2006 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002007 }
2008
2009 case ISD::VACOPY:
2010 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2011 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2012 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2013
2014 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2015 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002016 case TargetLowering::Custom:
2017 isCustom = true;
2018 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002019 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002020 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2021 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002022 if (isCustom) {
2023 Tmp1 = TLI.LowerOperation(Result, DAG);
2024 if (Tmp1.Val) Result = Tmp1;
2025 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002026 break;
2027 case TargetLowering::Expand:
2028 // This defaults to loading a pointer from the input and storing it to the
2029 // output, returning the chain.
2030 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2031 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2032 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002033 break;
2034 }
2035 break;
2036
2037 case ISD::VAEND:
2038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2039 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2040
2041 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2042 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002043 case TargetLowering::Custom:
2044 isCustom = true;
2045 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002046 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002047 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002048 if (isCustom) {
2049 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2050 if (Tmp1.Val) Result = Tmp1;
2051 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002052 break;
2053 case TargetLowering::Expand:
2054 Result = Tmp1; // Default to a no-op, return the chain
2055 break;
2056 }
2057 break;
2058
2059 case ISD::VASTART:
2060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2061 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2062
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002063 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2064
Nate Begemanacc398c2006-01-25 18:21:52 +00002065 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2066 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002067 case TargetLowering::Legal: break;
2068 case TargetLowering::Custom:
2069 Tmp1 = TLI.LowerOperation(Result, DAG);
2070 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002071 break;
2072 }
2073 break;
2074
Nate Begeman35ef9132006-01-11 21:21:00 +00002075 case ISD::ROTL:
2076 case ISD::ROTR:
2077 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2078 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002079
2080 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2081 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002083 break;
2084
Nate Begemand88fc032006-01-14 03:14:10 +00002085 case ISD::BSWAP:
2086 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2087 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002088 case TargetLowering::Custom:
2089 assert(0 && "Cannot custom legalize this yet!");
2090 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002091 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002092 break;
2093 case TargetLowering::Promote: {
2094 MVT::ValueType OVT = Tmp1.getValueType();
2095 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2096 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002097
Chris Lattner456a93a2006-01-28 07:39:30 +00002098 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2099 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2100 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2101 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2102 break;
2103 }
2104 case TargetLowering::Expand:
2105 Result = ExpandBSWAP(Tmp1);
2106 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002107 }
2108 break;
2109
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002110 case ISD::CTPOP:
2111 case ISD::CTTZ:
2112 case ISD::CTLZ:
2113 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2114 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002115 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002116 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002117 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002118 break;
2119 case TargetLowering::Promote: {
2120 MVT::ValueType OVT = Tmp1.getValueType();
2121 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002122
2123 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002124 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2125 // Perform the larger operation, then subtract if needed.
2126 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002127 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002128 case ISD::CTPOP:
2129 Result = Tmp1;
2130 break;
2131 case ISD::CTTZ:
2132 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002133 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2134 DAG.getConstant(getSizeInBits(NVT), NVT),
2135 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002136 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002137 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2138 break;
2139 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002140 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002141 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2142 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002143 getSizeInBits(OVT), NVT));
2144 break;
2145 }
2146 break;
2147 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002148 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002149 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002150 break;
2151 }
2152 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002153
Chris Lattner2c8086f2005-04-02 05:00:07 +00002154 // Unary operators
2155 case ISD::FABS:
2156 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002157 case ISD::FSQRT:
2158 case ISD::FSIN:
2159 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002160 Tmp1 = LegalizeOp(Node->getOperand(0));
2161 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002162 case TargetLowering::Promote:
2163 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002164 isCustom = true;
2165 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002166 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002167 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002168 if (isCustom) {
2169 Tmp1 = TLI.LowerOperation(Result, DAG);
2170 if (Tmp1.Val) Result = Tmp1;
2171 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002172 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002173 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002174 switch (Node->getOpcode()) {
2175 default: assert(0 && "Unreachable!");
2176 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002177 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2178 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002179 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002180 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002181 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002182 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2183 MVT::ValueType VT = Node->getValueType(0);
2184 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002185 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002186 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2187 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002188 break;
2189 }
2190 case ISD::FSQRT:
2191 case ISD::FSIN:
2192 case ISD::FCOS: {
2193 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002194 const char *FnName = 0;
2195 switch(Node->getOpcode()) {
2196 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2197 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2198 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2199 default: assert(0 && "Unreachable!");
2200 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002201 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002202 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002203 break;
2204 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002205 }
2206 break;
2207 }
2208 break;
Chris Lattner35481892005-12-23 00:16:34 +00002209
2210 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002211 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002212 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002213 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002214 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2215 Node->getOperand(0).getValueType())) {
2216 default: assert(0 && "Unknown operation action!");
2217 case TargetLowering::Expand:
2218 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2219 break;
2220 case TargetLowering::Legal:
2221 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002222 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002223 break;
2224 }
2225 }
2226 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002227 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002228 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002229 case ISD::UINT_TO_FP: {
2230 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002231 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2232 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002233 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002234 Node->getOperand(0).getValueType())) {
2235 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002236 case TargetLowering::Custom:
2237 isCustom = true;
2238 // FALLTHROUGH
2239 case TargetLowering::Legal:
2240 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002241 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002242 if (isCustom) {
2243 Tmp1 = TLI.LowerOperation(Result, DAG);
2244 if (Tmp1.Val) Result = Tmp1;
2245 }
2246 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002247 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002248 Result = ExpandLegalINT_TO_FP(isSigned,
2249 LegalizeOp(Node->getOperand(0)),
2250 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002251 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002252 case TargetLowering::Promote:
2253 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2254 Node->getValueType(0),
2255 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002256 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002257 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002258 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002259 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002260 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2261 Node->getValueType(0), Node->getOperand(0));
2262 break;
2263 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002264 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002265 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002266 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2267 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002268 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002269 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2270 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002271 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002272 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2273 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002274 break;
2275 }
2276 break;
2277 }
2278 case ISD::TRUNCATE:
2279 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2280 case Legal:
2281 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002282 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002283 break;
2284 case Expand:
2285 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2286
2287 // Since the result is legal, we should just be able to truncate the low
2288 // part of the source.
2289 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2290 break;
2291 case Promote:
2292 Result = PromoteOp(Node->getOperand(0));
2293 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2294 break;
2295 }
2296 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002297
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002298 case ISD::FP_TO_SINT:
2299 case ISD::FP_TO_UINT:
2300 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2301 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002302 Tmp1 = LegalizeOp(Node->getOperand(0));
2303
Chris Lattner1618beb2005-07-29 00:11:56 +00002304 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2305 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002306 case TargetLowering::Custom:
2307 isCustom = true;
2308 // FALLTHROUGH
2309 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002310 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002311 if (isCustom) {
2312 Tmp1 = TLI.LowerOperation(Result, DAG);
2313 if (Tmp1.Val) Result = Tmp1;
2314 }
2315 break;
2316 case TargetLowering::Promote:
2317 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2318 Node->getOpcode() == ISD::FP_TO_SINT);
2319 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002320 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002321 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2322 SDOperand True, False;
2323 MVT::ValueType VT = Node->getOperand(0).getValueType();
2324 MVT::ValueType NVT = Node->getValueType(0);
2325 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2326 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2327 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2328 Node->getOperand(0), Tmp2, ISD::SETLT);
2329 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2330 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002331 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002332 Tmp2));
2333 False = DAG.getNode(ISD::XOR, NVT, False,
2334 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002335 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2336 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002337 } else {
2338 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2339 }
2340 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002341 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002342 break;
2343 case Expand:
2344 assert(0 && "Shouldn't need to expand other operators here!");
2345 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002346 Tmp1 = PromoteOp(Node->getOperand(0));
2347 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2348 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002349 break;
2350 }
2351 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002352
Chris Lattner13c78e22005-09-02 00:18:10 +00002353 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002354 case ISD::ZERO_EXTEND:
2355 case ISD::SIGN_EXTEND:
2356 case ISD::FP_EXTEND:
2357 case ISD::FP_ROUND:
2358 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002359 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002360 case Legal:
2361 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002362 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002363 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002364 case Promote:
2365 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002366 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002367 Tmp1 = PromoteOp(Node->getOperand(0));
2368 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002369 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002370 case ISD::ZERO_EXTEND:
2371 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002372 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002373 Result = DAG.getZeroExtendInReg(Result,
2374 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002375 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002376 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002377 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002378 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002379 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002380 Result,
2381 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002382 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002383 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002384 Result = PromoteOp(Node->getOperand(0));
2385 if (Result.getValueType() != Op.getValueType())
2386 // Dynamically dead while we have only 2 FP types.
2387 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2388 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002389 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002390 Result = PromoteOp(Node->getOperand(0));
2391 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2392 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002393 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002394 }
2395 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002396 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002397 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002398 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002399 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002400
2401 // If this operation is not supported, convert it to a shl/shr or load/store
2402 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002403 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2404 default: assert(0 && "This action not supported for this op yet!");
2405 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002407 break;
2408 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002409 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002410 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002411 // NOTE: we could fall back on load/store here too for targets without
2412 // SAR. However, it is doubtful that any exist.
2413 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2414 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002415 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002416 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2417 Node->getOperand(0), ShiftCst);
2418 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2419 Result, ShiftCst);
2420 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2421 // The only way we can lower this is to turn it into a STORETRUNC,
2422 // EXTLOAD pair, targetting a temporary location (a stack slot).
2423
2424 // NOTE: there is a choice here between constantly creating new stack
2425 // slots and always reusing the same one. We currently always create
2426 // new ones, as reuse may inhibit scheduling.
2427 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2428 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2429 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2430 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002431 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002432 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2433 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2434 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002435 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002436 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002437 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2438 Result, StackSlot, DAG.getSrcValue(NULL),
2439 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002440 } else {
2441 assert(0 && "Unknown op");
2442 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002443 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002444 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002445 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002446 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002447 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002448
2449 // Make sure that the generated code is itself legal.
2450 if (Result != Op)
2451 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002452
Chris Lattner45982da2005-05-12 16:53:42 +00002453 // Note that LegalizeOp may be reentered even from single-use nodes, which
2454 // means that we always must cache transformed nodes.
2455 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002456 return Result;
2457}
2458
Chris Lattner8b6fa222005-01-15 22:16:26 +00002459/// PromoteOp - Given an operation that produces a value in an invalid type,
2460/// promote it to compute the value into a larger type. The produced value will
2461/// have the correct bits for the low portion of the register, but no guarantee
2462/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002463SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2464 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002465 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002466 assert(getTypeAction(VT) == Promote &&
2467 "Caller should expand or legalize operands that are not promotable!");
2468 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2469 "Cannot promote to smaller type!");
2470
Chris Lattner03c85462005-01-15 05:21:40 +00002471 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002472 SDOperand Result;
2473 SDNode *Node = Op.Val;
2474
Chris Lattner6fdcb252005-09-02 20:32:45 +00002475 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2476 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002477
Chris Lattner03c85462005-01-15 05:21:40 +00002478 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002479 case ISD::CopyFromReg:
2480 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002481 default:
2482 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2483 assert(0 && "Do not know how to promote this operator!");
2484 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002485 case ISD::UNDEF:
2486 Result = DAG.getNode(ISD::UNDEF, NVT);
2487 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002488 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002489 if (VT != MVT::i1)
2490 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2491 else
2492 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002493 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2494 break;
2495 case ISD::ConstantFP:
2496 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2497 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2498 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002499
Chris Lattner82fbfb62005-01-18 02:59:52 +00002500 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002501 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002502 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2503 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002504 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002505
2506 case ISD::TRUNCATE:
2507 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2508 case Legal:
2509 Result = LegalizeOp(Node->getOperand(0));
2510 assert(Result.getValueType() >= NVT &&
2511 "This truncation doesn't make sense!");
2512 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2513 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2514 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002515 case Promote:
2516 // The truncation is not required, because we don't guarantee anything
2517 // about high bits anyway.
2518 Result = PromoteOp(Node->getOperand(0));
2519 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002520 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002521 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2522 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002523 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002524 }
2525 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002526 case ISD::SIGN_EXTEND:
2527 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002528 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002529 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2530 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2531 case Legal:
2532 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002533 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002534 break;
2535 case Promote:
2536 // Promote the reg if it's smaller.
2537 Result = PromoteOp(Node->getOperand(0));
2538 // The high bits are not guaranteed to be anything. Insert an extend.
2539 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002540 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002541 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002542 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002543 Result = DAG.getZeroExtendInReg(Result,
2544 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002545 break;
2546 }
2547 break;
Chris Lattner35481892005-12-23 00:16:34 +00002548 case ISD::BIT_CONVERT:
2549 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2550 Result = PromoteOp(Result);
2551 break;
2552
Chris Lattner8b6fa222005-01-15 22:16:26 +00002553 case ISD::FP_EXTEND:
2554 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2555 case ISD::FP_ROUND:
2556 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2557 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2558 case Promote: assert(0 && "Unreachable with 2 FP types!");
2559 case Legal:
2560 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002561 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002562 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002563 break;
2564 }
2565 break;
2566
2567 case ISD::SINT_TO_FP:
2568 case ISD::UINT_TO_FP:
2569 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2570 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002571 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002572 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002573 break;
2574
2575 case Promote:
2576 Result = PromoteOp(Node->getOperand(0));
2577 if (Node->getOpcode() == ISD::SINT_TO_FP)
2578 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002579 Result,
2580 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002581 else
Chris Lattner23993562005-04-13 02:38:47 +00002582 Result = DAG.getZeroExtendInReg(Result,
2583 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002584 // No extra round required here.
2585 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002586 break;
2587 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002588 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2589 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002590 // Round if we cannot tolerate excess precision.
2591 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002592 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2593 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002594 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002595 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002596 break;
2597
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002598 case ISD::SIGN_EXTEND_INREG:
2599 Result = PromoteOp(Node->getOperand(0));
2600 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2601 Node->getOperand(1));
2602 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002603 case ISD::FP_TO_SINT:
2604 case ISD::FP_TO_UINT:
2605 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2606 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002607 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002608 break;
2609 case Promote:
2610 // The input result is prerounded, so we don't have to do anything
2611 // special.
2612 Tmp1 = PromoteOp(Node->getOperand(0));
2613 break;
2614 case Expand:
2615 assert(0 && "not implemented");
2616 }
Nate Begemand2558e32005-08-14 01:20:53 +00002617 // If we're promoting a UINT to a larger size, check to see if the new node
2618 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2619 // we can use that instead. This allows us to generate better code for
2620 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2621 // legal, such as PowerPC.
2622 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002623 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002624 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2625 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002626 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2627 } else {
2628 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2629 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002630 break;
2631
Chris Lattner2c8086f2005-04-02 05:00:07 +00002632 case ISD::FABS:
2633 case ISD::FNEG:
2634 Tmp1 = PromoteOp(Node->getOperand(0));
2635 assert(Tmp1.getValueType() == NVT);
2636 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2637 // NOTE: we do not have to do any extra rounding here for
2638 // NoExcessFPPrecision, because we know the input will have the appropriate
2639 // precision, and these operations don't modify precision at all.
2640 break;
2641
Chris Lattnerda6ba872005-04-28 21:44:33 +00002642 case ISD::FSQRT:
2643 case ISD::FSIN:
2644 case ISD::FCOS:
2645 Tmp1 = PromoteOp(Node->getOperand(0));
2646 assert(Tmp1.getValueType() == NVT);
2647 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002648 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002649 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2650 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002651 break;
2652
Chris Lattner03c85462005-01-15 05:21:40 +00002653 case ISD::AND:
2654 case ISD::OR:
2655 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002656 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002657 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002658 case ISD::MUL:
2659 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002660 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002661 // that too is okay if they are integer operations.
2662 Tmp1 = PromoteOp(Node->getOperand(0));
2663 Tmp2 = PromoteOp(Node->getOperand(1));
2664 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2665 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002666 break;
2667 case ISD::FADD:
2668 case ISD::FSUB:
2669 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002670 Tmp1 = PromoteOp(Node->getOperand(0));
2671 Tmp2 = PromoteOp(Node->getOperand(1));
2672 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2673 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2674
2675 // Floating point operations will give excess precision that we may not be
2676 // able to tolerate. If we DO allow excess precision, just leave it,
2677 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002678 // FIXME: Why would we need to round FP ops more than integer ones?
2679 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002680 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002681 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2682 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002683 break;
2684
Chris Lattner8b6fa222005-01-15 22:16:26 +00002685 case ISD::SDIV:
2686 case ISD::SREM:
2687 // These operators require that their input be sign extended.
2688 Tmp1 = PromoteOp(Node->getOperand(0));
2689 Tmp2 = PromoteOp(Node->getOperand(1));
2690 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002691 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2692 DAG.getValueType(VT));
2693 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2694 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002695 }
2696 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2697
2698 // Perform FP_ROUND: this is probably overly pessimistic.
2699 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002700 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2701 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002702 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002703 case ISD::FDIV:
2704 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002705 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002706 // These operators require that their input be fp extended.
2707 Tmp1 = PromoteOp(Node->getOperand(0));
2708 Tmp2 = PromoteOp(Node->getOperand(1));
2709 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2710
2711 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002712 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002713 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2714 DAG.getValueType(VT));
2715 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002716
2717 case ISD::UDIV:
2718 case ISD::UREM:
2719 // These operators require that their input be zero extended.
2720 Tmp1 = PromoteOp(Node->getOperand(0));
2721 Tmp2 = PromoteOp(Node->getOperand(1));
2722 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002723 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2724 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002725 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2726 break;
2727
2728 case ISD::SHL:
2729 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002730 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002731 break;
2732 case ISD::SRA:
2733 // The input value must be properly sign extended.
2734 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002735 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2736 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002737 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002738 break;
2739 case ISD::SRL:
2740 // The input value must be properly zero extended.
2741 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002742 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002743 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002744 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002745
2746 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002747 Tmp1 = Node->getOperand(0); // Get the chain.
2748 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002749 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2750 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2751 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2752 } else {
2753 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2754 Node->getOperand(2));
2755 // Increment the pointer, VAList, to the next vaarg
2756 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2757 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2758 TLI.getPointerTy()));
2759 // Store the incremented VAList to the legalized pointer
2760 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2761 Node->getOperand(2));
2762 // Load the actual argument out of the pointer VAList
2763 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2764 DAG.getSrcValue(0), VT);
2765 }
2766 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002767 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002768 break;
2769
Chris Lattner03c85462005-01-15 05:21:40 +00002770 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002771 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2772 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002773 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002774 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002775 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002776 case ISD::SEXTLOAD:
2777 case ISD::ZEXTLOAD:
2778 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002779 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2780 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002781 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002782 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002783 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002784 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002785 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002786 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2787 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002788 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002789 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002790 case ISD::SELECT_CC:
2791 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2792 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2793 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002794 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002795 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002796 case ISD::BSWAP:
2797 Tmp1 = Node->getOperand(0);
2798 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2799 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2800 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2801 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2802 TLI.getShiftAmountTy()));
2803 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002804 case ISD::CTPOP:
2805 case ISD::CTTZ:
2806 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002807 // Zero extend the argument
2808 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002809 // Perform the larger operation, then subtract if needed.
2810 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002811 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002812 case ISD::CTPOP:
2813 Result = Tmp1;
2814 break;
2815 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002816 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002817 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002818 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002819 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002820 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002821 break;
2822 case ISD::CTLZ:
2823 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002824 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2825 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002826 getSizeInBits(VT), NVT));
2827 break;
2828 }
2829 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002830 }
2831
2832 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002833
2834 // Make sure the result is itself legal.
2835 Result = LegalizeOp(Result);
2836
2837 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002838 AddPromotedOperand(Op, Result);
2839 return Result;
2840}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002841
Nate Begeman750ac1b2006-02-01 07:19:44 +00002842/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2843/// with condition CC on the current target. This usually involves legalizing
2844/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2845/// there may be no choice but to create a new SetCC node to represent the
2846/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2847/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2848void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2849 SDOperand &RHS,
2850 SDOperand &CC) {
2851 SDOperand Tmp1, Tmp2, Result;
2852
2853 switch (getTypeAction(LHS.getValueType())) {
2854 case Legal:
2855 Tmp1 = LegalizeOp(LHS); // LHS
2856 Tmp2 = LegalizeOp(RHS); // RHS
2857 break;
2858 case Promote:
2859 Tmp1 = PromoteOp(LHS); // LHS
2860 Tmp2 = PromoteOp(RHS); // RHS
2861
2862 // If this is an FP compare, the operands have already been extended.
2863 if (MVT::isInteger(LHS.getValueType())) {
2864 MVT::ValueType VT = LHS.getValueType();
2865 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2866
2867 // Otherwise, we have to insert explicit sign or zero extends. Note
2868 // that we could insert sign extends for ALL conditions, but zero extend
2869 // is cheaper on many machines (an AND instead of two shifts), so prefer
2870 // it.
2871 switch (cast<CondCodeSDNode>(CC)->get()) {
2872 default: assert(0 && "Unknown integer comparison!");
2873 case ISD::SETEQ:
2874 case ISD::SETNE:
2875 case ISD::SETUGE:
2876 case ISD::SETUGT:
2877 case ISD::SETULE:
2878 case ISD::SETULT:
2879 // ALL of these operations will work if we either sign or zero extend
2880 // the operands (including the unsigned comparisons!). Zero extend is
2881 // usually a simpler/cheaper operation, so prefer it.
2882 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2883 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2884 break;
2885 case ISD::SETGE:
2886 case ISD::SETGT:
2887 case ISD::SETLT:
2888 case ISD::SETLE:
2889 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2890 DAG.getValueType(VT));
2891 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2892 DAG.getValueType(VT));
2893 break;
2894 }
2895 }
2896 break;
2897 case Expand:
2898 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2899 ExpandOp(LHS, LHSLo, LHSHi);
2900 ExpandOp(RHS, RHSLo, RHSHi);
2901 switch (cast<CondCodeSDNode>(CC)->get()) {
2902 case ISD::SETEQ:
2903 case ISD::SETNE:
2904 if (RHSLo == RHSHi)
2905 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2906 if (RHSCST->isAllOnesValue()) {
2907 // Comparison to -1.
2908 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2909 Tmp2 = RHSLo;
2910 break;
2911 }
2912
2913 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2914 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2915 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2916 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2917 break;
2918 default:
2919 // If this is a comparison of the sign bit, just look at the top part.
2920 // X > -1, x < 0
2921 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2922 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2923 CST->getValue() == 0) || // X < 0
2924 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2925 CST->isAllOnesValue())) { // X > -1
2926 Tmp1 = LHSHi;
2927 Tmp2 = RHSHi;
2928 break;
2929 }
2930
2931 // FIXME: This generated code sucks.
2932 ISD::CondCode LowCC;
2933 switch (cast<CondCodeSDNode>(CC)->get()) {
2934 default: assert(0 && "Unknown integer setcc!");
2935 case ISD::SETLT:
2936 case ISD::SETULT: LowCC = ISD::SETULT; break;
2937 case ISD::SETGT:
2938 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2939 case ISD::SETLE:
2940 case ISD::SETULE: LowCC = ISD::SETULE; break;
2941 case ISD::SETGE:
2942 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2943 }
2944
2945 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2946 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2947 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2948
2949 // NOTE: on targets without efficient SELECT of bools, we can always use
2950 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2951 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2952 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2953 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2954 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2955 Result, Tmp1, Tmp2));
2956 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002957 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002958 }
2959 }
2960 LHS = Tmp1;
2961 RHS = Tmp2;
2962}
2963
Chris Lattner35481892005-12-23 00:16:34 +00002964/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002965/// The resultant code need not be legal. Note that SrcOp is the input operand
2966/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002967SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2968 SDOperand SrcOp) {
2969 // Create the stack frame object.
2970 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2971 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002972 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002973 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2974
2975 // Emit a store to the stack slot.
2976 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002977 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002978 // Result is a load from the stack slot.
2979 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2980}
2981
Chris Lattner5b359c62005-04-02 04:00:59 +00002982void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2983 SDOperand Op, SDOperand Amt,
2984 SDOperand &Lo, SDOperand &Hi) {
2985 // Expand the subcomponents.
2986 SDOperand LHSL, LHSH;
2987 ExpandOp(Op, LHSL, LHSH);
2988
2989 std::vector<SDOperand> Ops;
2990 Ops.push_back(LHSL);
2991 Ops.push_back(LHSH);
2992 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002993 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002994 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002995 Hi = Lo.getValue(1);
2996}
2997
2998
Chris Lattnere34b3962005-01-19 04:19:40 +00002999/// ExpandShift - Try to find a clever way to expand this shift operation out to
3000/// smaller elements. If we can't find a way that is more efficient than a
3001/// libcall on this target, return false. Otherwise, return true with the
3002/// low-parts expanded into Lo and Hi.
3003bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3004 SDOperand &Lo, SDOperand &Hi) {
3005 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3006 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003007
Chris Lattnere34b3962005-01-19 04:19:40 +00003008 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003009 SDOperand ShAmt = LegalizeOp(Amt);
3010 MVT::ValueType ShTy = ShAmt.getValueType();
3011 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3012 unsigned NVTBits = MVT::getSizeInBits(NVT);
3013
3014 // Handle the case when Amt is an immediate. Other cases are currently broken
3015 // and are disabled.
3016 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3017 unsigned Cst = CN->getValue();
3018 // Expand the incoming operand to be shifted, so that we have its parts
3019 SDOperand InL, InH;
3020 ExpandOp(Op, InL, InH);
3021 switch(Opc) {
3022 case ISD::SHL:
3023 if (Cst > VTBits) {
3024 Lo = DAG.getConstant(0, NVT);
3025 Hi = DAG.getConstant(0, NVT);
3026 } else if (Cst > NVTBits) {
3027 Lo = DAG.getConstant(0, NVT);
3028 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003029 } else if (Cst == NVTBits) {
3030 Lo = DAG.getConstant(0, NVT);
3031 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003032 } else {
3033 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3034 Hi = DAG.getNode(ISD::OR, NVT,
3035 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3036 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3037 }
3038 return true;
3039 case ISD::SRL:
3040 if (Cst > VTBits) {
3041 Lo = DAG.getConstant(0, NVT);
3042 Hi = DAG.getConstant(0, NVT);
3043 } else if (Cst > NVTBits) {
3044 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3045 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003046 } else if (Cst == NVTBits) {
3047 Lo = InH;
3048 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003049 } else {
3050 Lo = DAG.getNode(ISD::OR, NVT,
3051 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3052 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3053 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3054 }
3055 return true;
3056 case ISD::SRA:
3057 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003058 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003059 DAG.getConstant(NVTBits-1, ShTy));
3060 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003061 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003062 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003063 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003064 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003065 } else if (Cst == NVTBits) {
3066 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003067 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003068 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003069 } else {
3070 Lo = DAG.getNode(ISD::OR, NVT,
3071 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3072 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3073 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3074 }
3075 return true;
3076 }
3077 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003078 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003079}
Chris Lattner77e77a62005-01-21 06:05:23 +00003080
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003081
Chris Lattner77e77a62005-01-21 06:05:23 +00003082// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3083// does not fit into a register, return the lo part and set the hi part to the
3084// by-reg argument. If it does fit into a single register, return the result
3085// and leave the Hi part unset.
3086SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3087 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003088 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3089 // The input chain to this libcall is the entry node of the function.
3090 // Legalizing the call will automatically add the previous call to the
3091 // dependence.
3092 SDOperand InChain = DAG.getEntryNode();
3093
Chris Lattner77e77a62005-01-21 06:05:23 +00003094 TargetLowering::ArgListTy Args;
3095 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3096 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3097 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3098 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3099 }
3100 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003101
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003102 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003103 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003104 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003105 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3106 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003107
Chris Lattner6831a812006-02-13 09:18:02 +00003108 // Legalize the call sequence, starting with the chain. This will advance
3109 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3110 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3111 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003112 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003113 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003114 default: assert(0 && "Unknown thing");
3115 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003116 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003117 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003118 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003119 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003120 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003121 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003122 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003123}
3124
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003125
Chris Lattner77e77a62005-01-21 06:05:23 +00003126/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3127/// destination type is legal.
3128SDOperand SelectionDAGLegalize::
3129ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003130 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003131 assert(getTypeAction(Source.getValueType()) == Expand &&
3132 "This is not an expansion!");
3133 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3134
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003135 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003136 assert(Source.getValueType() == MVT::i64 &&
3137 "This only works for 64-bit -> FP");
3138 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3139 // incoming integer is set. To handle this, we dynamically test to see if
3140 // it is set, and, if so, add a fudge factor.
3141 SDOperand Lo, Hi;
3142 ExpandOp(Source, Lo, Hi);
3143
Chris Lattner66de05b2005-05-13 04:45:13 +00003144 // If this is unsigned, and not supported, first perform the conversion to
3145 // signed, then adjust the result if the sign bit is set.
3146 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3147 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3148
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003149 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3150 DAG.getConstant(0, Hi.getValueType()),
3151 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003152 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3153 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3154 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003155 uint64_t FF = 0x5f800000ULL;
3156 if (TLI.isLittleEndian()) FF <<= 32;
3157 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003158
Chris Lattner5839bf22005-08-26 17:15:30 +00003159 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003160 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3161 SDOperand FudgeInReg;
3162 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003163 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3164 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003165 else {
3166 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003167 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3168 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003169 }
Chris Lattner473a9902005-09-29 06:44:39 +00003170 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003171 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003172
Chris Lattnera88a2602005-05-14 05:33:54 +00003173 // Check to see if the target has a custom way to lower this. If so, use it.
3174 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3175 default: assert(0 && "This action not implemented for this operation!");
3176 case TargetLowering::Legal:
3177 case TargetLowering::Expand:
3178 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003179 case TargetLowering::Custom: {
3180 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3181 Source), DAG);
3182 if (NV.Val)
3183 return LegalizeOp(NV);
3184 break; // The target decided this was legal after all
3185 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003186 }
3187
Chris Lattner13689e22005-05-12 07:00:44 +00003188 // Expand the source, then glue it back together for the call. We must expand
3189 // the source in case it is shared (this pass of legalize must traverse it).
3190 SDOperand SrcLo, SrcHi;
3191 ExpandOp(Source, SrcLo, SrcHi);
3192 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3193
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003194 const char *FnName = 0;
3195 if (DestTy == MVT::f32)
3196 FnName = "__floatdisf";
3197 else {
3198 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3199 FnName = "__floatdidf";
3200 }
Chris Lattner6831a812006-02-13 09:18:02 +00003201
3202 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3203 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003204 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003205}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003206
Chris Lattner22cde6a2006-01-28 08:25:58 +00003207/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3208/// INT_TO_FP operation of the specified operand when the target requests that
3209/// we expand it. At this point, we know that the result and operand types are
3210/// legal for the target.
3211SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3212 SDOperand Op0,
3213 MVT::ValueType DestVT) {
3214 if (Op0.getValueType() == MVT::i32) {
3215 // simple 32-bit [signed|unsigned] integer to float/double expansion
3216
3217 // get the stack frame index of a 8 byte buffer
3218 MachineFunction &MF = DAG.getMachineFunction();
3219 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3220 // get address of 8 byte buffer
3221 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3222 // word offset constant for Hi/Lo address computation
3223 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3224 // set up Hi and Lo (into buffer) address based on endian
3225 SDOperand Hi, Lo;
3226 if (TLI.isLittleEndian()) {
3227 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3228 Lo = StackSlot;
3229 } else {
3230 Hi = StackSlot;
3231 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3232 }
3233 // if signed map to unsigned space
3234 SDOperand Op0Mapped;
3235 if (isSigned) {
3236 // constant used to invert sign bit (signed to unsigned mapping)
3237 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3238 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3239 } else {
3240 Op0Mapped = Op0;
3241 }
3242 // store the lo of the constructed double - based on integer input
3243 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3244 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3245 // initial hi portion of constructed double
3246 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3247 // store the hi of the constructed double - biased exponent
3248 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3249 InitialHi, Hi, DAG.getSrcValue(NULL));
3250 // load the constructed double
3251 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3252 DAG.getSrcValue(NULL));
3253 // FP constant to bias correct the final result
3254 SDOperand Bias = DAG.getConstantFP(isSigned ?
3255 BitsToDouble(0x4330000080000000ULL)
3256 : BitsToDouble(0x4330000000000000ULL),
3257 MVT::f64);
3258 // subtract the bias
3259 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3260 // final result
3261 SDOperand Result;
3262 // handle final rounding
3263 if (DestVT == MVT::f64) {
3264 // do nothing
3265 Result = Sub;
3266 } else {
3267 // if f32 then cast to f32
3268 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3269 }
3270 return Result;
3271 }
3272 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3273 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3274
3275 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3276 DAG.getConstant(0, Op0.getValueType()),
3277 ISD::SETLT);
3278 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3279 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3280 SignSet, Four, Zero);
3281
3282 // If the sign bit of the integer is set, the large number will be treated
3283 // as a negative number. To counteract this, the dynamic code adds an
3284 // offset depending on the data type.
3285 uint64_t FF;
3286 switch (Op0.getValueType()) {
3287 default: assert(0 && "Unsupported integer type!");
3288 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3289 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3290 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3291 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3292 }
3293 if (TLI.isLittleEndian()) FF <<= 32;
3294 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3295
3296 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3297 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3298 SDOperand FudgeInReg;
3299 if (DestVT == MVT::f32)
3300 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3301 DAG.getSrcValue(NULL));
3302 else {
3303 assert(DestVT == MVT::f64 && "Unexpected conversion");
3304 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3305 DAG.getEntryNode(), CPIdx,
3306 DAG.getSrcValue(NULL), MVT::f32));
3307 }
3308
3309 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3310}
3311
3312/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3313/// *INT_TO_FP operation of the specified operand when the target requests that
3314/// we promote it. At this point, we know that the result and operand types are
3315/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3316/// operation that takes a larger input.
3317SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3318 MVT::ValueType DestVT,
3319 bool isSigned) {
3320 // First step, figure out the appropriate *INT_TO_FP operation to use.
3321 MVT::ValueType NewInTy = LegalOp.getValueType();
3322
3323 unsigned OpToUse = 0;
3324
3325 // Scan for the appropriate larger type to use.
3326 while (1) {
3327 NewInTy = (MVT::ValueType)(NewInTy+1);
3328 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3329
3330 // If the target supports SINT_TO_FP of this type, use it.
3331 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3332 default: break;
3333 case TargetLowering::Legal:
3334 if (!TLI.isTypeLegal(NewInTy))
3335 break; // Can't use this datatype.
3336 // FALL THROUGH.
3337 case TargetLowering::Custom:
3338 OpToUse = ISD::SINT_TO_FP;
3339 break;
3340 }
3341 if (OpToUse) break;
3342 if (isSigned) continue;
3343
3344 // If the target supports UINT_TO_FP of this type, use it.
3345 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3346 default: break;
3347 case TargetLowering::Legal:
3348 if (!TLI.isTypeLegal(NewInTy))
3349 break; // Can't use this datatype.
3350 // FALL THROUGH.
3351 case TargetLowering::Custom:
3352 OpToUse = ISD::UINT_TO_FP;
3353 break;
3354 }
3355 if (OpToUse) break;
3356
3357 // Otherwise, try a larger type.
3358 }
3359
3360 // Okay, we found the operation and type to use. Zero extend our input to the
3361 // desired type then run the operation on it.
3362 return DAG.getNode(OpToUse, DestVT,
3363 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3364 NewInTy, LegalOp));
3365}
3366
3367/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3368/// FP_TO_*INT operation of the specified operand when the target requests that
3369/// we promote it. At this point, we know that the result and operand types are
3370/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3371/// operation that returns a larger result.
3372SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3373 MVT::ValueType DestVT,
3374 bool isSigned) {
3375 // First step, figure out the appropriate FP_TO*INT operation to use.
3376 MVT::ValueType NewOutTy = DestVT;
3377
3378 unsigned OpToUse = 0;
3379
3380 // Scan for the appropriate larger type to use.
3381 while (1) {
3382 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3383 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3384
3385 // If the target supports FP_TO_SINT returning this type, use it.
3386 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3387 default: break;
3388 case TargetLowering::Legal:
3389 if (!TLI.isTypeLegal(NewOutTy))
3390 break; // Can't use this datatype.
3391 // FALL THROUGH.
3392 case TargetLowering::Custom:
3393 OpToUse = ISD::FP_TO_SINT;
3394 break;
3395 }
3396 if (OpToUse) break;
3397
3398 // If the target supports FP_TO_UINT of this type, use it.
3399 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3400 default: break;
3401 case TargetLowering::Legal:
3402 if (!TLI.isTypeLegal(NewOutTy))
3403 break; // Can't use this datatype.
3404 // FALL THROUGH.
3405 case TargetLowering::Custom:
3406 OpToUse = ISD::FP_TO_UINT;
3407 break;
3408 }
3409 if (OpToUse) break;
3410
3411 // Otherwise, try a larger type.
3412 }
3413
3414 // Okay, we found the operation and type to use. Truncate the result of the
3415 // extended FP_TO_*INT operation to the desired size.
3416 return DAG.getNode(ISD::TRUNCATE, DestVT,
3417 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3418}
3419
3420/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3421///
3422SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3423 MVT::ValueType VT = Op.getValueType();
3424 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3425 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3426 switch (VT) {
3427 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3428 case MVT::i16:
3429 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3430 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3431 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3432 case MVT::i32:
3433 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3434 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3435 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3436 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3437 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3438 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3439 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3440 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3441 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3442 case MVT::i64:
3443 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3444 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3445 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3446 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3447 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3448 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3449 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3450 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3451 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3452 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3453 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3454 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3455 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3456 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3457 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3458 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3459 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3460 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3461 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3462 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3463 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3464 }
3465}
3466
3467/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3468///
3469SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3470 switch (Opc) {
3471 default: assert(0 && "Cannot expand this yet!");
3472 case ISD::CTPOP: {
3473 static const uint64_t mask[6] = {
3474 0x5555555555555555ULL, 0x3333333333333333ULL,
3475 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3476 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3477 };
3478 MVT::ValueType VT = Op.getValueType();
3479 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3480 unsigned len = getSizeInBits(VT);
3481 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3482 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3483 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3484 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3485 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3486 DAG.getNode(ISD::AND, VT,
3487 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3488 }
3489 return Op;
3490 }
3491 case ISD::CTLZ: {
3492 // for now, we do this:
3493 // x = x | (x >> 1);
3494 // x = x | (x >> 2);
3495 // ...
3496 // x = x | (x >>16);
3497 // x = x | (x >>32); // for 64-bit input
3498 // return popcount(~x);
3499 //
3500 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3501 MVT::ValueType VT = Op.getValueType();
3502 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3503 unsigned len = getSizeInBits(VT);
3504 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3505 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3506 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3507 }
3508 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3509 return DAG.getNode(ISD::CTPOP, VT, Op);
3510 }
3511 case ISD::CTTZ: {
3512 // for now, we use: { return popcount(~x & (x - 1)); }
3513 // unless the target has ctlz but not ctpop, in which case we use:
3514 // { return 32 - nlz(~x & (x-1)); }
3515 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3516 MVT::ValueType VT = Op.getValueType();
3517 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3518 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3519 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3520 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3521 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3522 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3523 TLI.isOperationLegal(ISD::CTLZ, VT))
3524 return DAG.getNode(ISD::SUB, VT,
3525 DAG.getConstant(getSizeInBits(VT), VT),
3526 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3527 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3528 }
3529 }
3530}
Chris Lattnere34b3962005-01-19 04:19:40 +00003531
3532
Chris Lattner3e928bb2005-01-07 07:47:09 +00003533/// ExpandOp - Expand the specified SDOperand into its two component pieces
3534/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3535/// LegalizeNodes map is filled in for any results that are not expanded, the
3536/// ExpandedNodes map is filled in for any results that are expanded, and the
3537/// Lo/Hi values are returned.
3538void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3539 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003540 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003541 SDNode *Node = Op.Val;
3542 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003543 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3544 "Cannot expand FP values!");
3545 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003546 "Cannot expand to FP value or to larger int value!");
3547
Chris Lattner6fdcb252005-09-02 20:32:45 +00003548 // See if we already expanded it.
3549 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3550 = ExpandedNodes.find(Op);
3551 if (I != ExpandedNodes.end()) {
3552 Lo = I->second.first;
3553 Hi = I->second.second;
3554 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003555 }
3556
Chris Lattner3e928bb2005-01-07 07:47:09 +00003557 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003558 case ISD::CopyFromReg:
3559 assert(0 && "CopyFromReg must be legal!");
3560 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003561 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3562 assert(0 && "Do not know how to expand this operator!");
3563 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003564 case ISD::UNDEF:
3565 Lo = DAG.getNode(ISD::UNDEF, NVT);
3566 Hi = DAG.getNode(ISD::UNDEF, NVT);
3567 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003568 case ISD::Constant: {
3569 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3570 Lo = DAG.getConstant(Cst, NVT);
3571 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3572 break;
3573 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003574 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003575 // Return the operands.
3576 Lo = Node->getOperand(0);
3577 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003578 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003579
3580 case ISD::SIGN_EXTEND_INREG:
3581 ExpandOp(Node->getOperand(0), Lo, Hi);
3582 // Sign extend the lo-part.
3583 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3584 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3585 TLI.getShiftAmountTy()));
3586 // sext_inreg the low part if needed.
3587 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3588 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003589
Nate Begemand88fc032006-01-14 03:14:10 +00003590 case ISD::BSWAP: {
3591 ExpandOp(Node->getOperand(0), Lo, Hi);
3592 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3593 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3594 Lo = TempLo;
3595 break;
3596 }
3597
Chris Lattneredb1add2005-05-11 04:51:16 +00003598 case ISD::CTPOP:
3599 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003600 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3601 DAG.getNode(ISD::CTPOP, NVT, Lo),
3602 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003603 Hi = DAG.getConstant(0, NVT);
3604 break;
3605
Chris Lattner39a8f332005-05-12 19:05:01 +00003606 case ISD::CTLZ: {
3607 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003608 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003609 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3610 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003611 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3612 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003613 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3614 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3615
3616 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3617 Hi = DAG.getConstant(0, NVT);
3618 break;
3619 }
3620
3621 case ISD::CTTZ: {
3622 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003623 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003624 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3625 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003626 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3627 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003628 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3629 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3630
3631 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3632 Hi = DAG.getConstant(0, NVT);
3633 break;
3634 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003635
Nate Begemanacc398c2006-01-25 18:21:52 +00003636 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003637 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3638 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003639 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3640 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3641
3642 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003643 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003644 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3645 if (!TLI.isLittleEndian())
3646 std::swap(Lo, Hi);
3647 break;
3648 }
3649
Chris Lattner3e928bb2005-01-07 07:47:09 +00003650 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003651 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3652 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003653 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003654
3655 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003656 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003657 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3658 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003659 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003660 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003661
3662 // Build a factor node to remember that this load is independent of the
3663 // other one.
3664 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3665 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003666
Chris Lattner3e928bb2005-01-07 07:47:09 +00003667 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003668 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003669 if (!TLI.isLittleEndian())
3670 std::swap(Lo, Hi);
3671 break;
3672 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003673 case ISD::AND:
3674 case ISD::OR:
3675 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3676 SDOperand LL, LH, RL, RH;
3677 ExpandOp(Node->getOperand(0), LL, LH);
3678 ExpandOp(Node->getOperand(1), RL, RH);
3679 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3680 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3681 break;
3682 }
3683 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003684 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003685 ExpandOp(Node->getOperand(1), LL, LH);
3686 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003687 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3688 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003689 break;
3690 }
Nate Begeman9373a812005-08-10 20:51:12 +00003691 case ISD::SELECT_CC: {
3692 SDOperand TL, TH, FL, FH;
3693 ExpandOp(Node->getOperand(2), TL, TH);
3694 ExpandOp(Node->getOperand(3), FL, FH);
3695 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3696 Node->getOperand(1), TL, FL, Node->getOperand(4));
3697 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3698 Node->getOperand(1), TH, FH, Node->getOperand(4));
3699 break;
3700 }
Nate Begeman144ff662005-10-13 17:15:37 +00003701 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003702 SDOperand Chain = Node->getOperand(0);
3703 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003704 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3705
3706 if (EVT == NVT)
3707 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3708 else
3709 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3710 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003711
3712 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003713 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003714
Nate Begeman144ff662005-10-13 17:15:37 +00003715 // The high part is obtained by SRA'ing all but one of the bits of the lo
3716 // part.
3717 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3718 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3719 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003720 break;
3721 }
3722 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003723 SDOperand Chain = Node->getOperand(0);
3724 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003725 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3726
3727 if (EVT == NVT)
3728 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3729 else
3730 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3731 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003732
3733 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003734 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003735
Nate Begeman144ff662005-10-13 17:15:37 +00003736 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003737 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003738 break;
3739 }
3740 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003741 SDOperand Chain = Node->getOperand(0);
3742 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003743 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3744
3745 if (EVT == NVT)
3746 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3747 else
3748 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3749 EVT);
3750
3751 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003752 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003753
3754 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003755 Hi = DAG.getNode(ISD::UNDEF, NVT);
3756 break;
3757 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003758 case ISD::ANY_EXTEND:
3759 // The low part is any extension of the input (which degenerates to a copy).
3760 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3761 // The high part is undefined.
3762 Hi = DAG.getNode(ISD::UNDEF, NVT);
3763 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003764 case ISD::SIGN_EXTEND: {
3765 // The low part is just a sign extension of the input (which degenerates to
3766 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003767 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003768
Chris Lattner3e928bb2005-01-07 07:47:09 +00003769 // The high part is obtained by SRA'ing all but one of the bits of the lo
3770 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003771 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003772 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3773 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003774 break;
3775 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003776 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003777 // The low part is just a zero extension of the input (which degenerates to
3778 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003779 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003780
Chris Lattner3e928bb2005-01-07 07:47:09 +00003781 // The high part is just a zero.
3782 Hi = DAG.getConstant(0, NVT);
3783 break;
Chris Lattner35481892005-12-23 00:16:34 +00003784
3785 case ISD::BIT_CONVERT: {
3786 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3787 Node->getOperand(0));
3788 ExpandOp(Tmp, Lo, Hi);
3789 break;
3790 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003791
Chris Lattner8137c9e2006-01-28 05:07:51 +00003792 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003793 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3794 TargetLowering::Custom &&
3795 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003796 Lo = TLI.LowerOperation(Op, DAG);
3797 assert(Lo.Val && "Node must be custom expanded!");
3798 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003799 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003800 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003801 break;
3802
Chris Lattner4e6c7462005-01-08 19:27:05 +00003803 // These operators cannot be expanded directly, emit them as calls to
3804 // library functions.
3805 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003806 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003807 SDOperand Op;
3808 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3809 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003810 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3811 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003812 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003813
Chris Lattnerf20d1832005-07-30 01:40:57 +00003814 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3815
Chris Lattner80a3e942005-07-29 00:33:32 +00003816 // Now that the custom expander is done, expand the result, which is still
3817 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003818 if (Op.Val) {
3819 ExpandOp(Op, Lo, Hi);
3820 break;
3821 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003822 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003823
Chris Lattner4e6c7462005-01-08 19:27:05 +00003824 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003825 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003826 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003827 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003828 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003829
Chris Lattner4e6c7462005-01-08 19:27:05 +00003830 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003831 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003832 SDOperand Op;
3833 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3834 case Expand: assert(0 && "cannot expand FP!");
3835 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3836 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3837 }
3838
3839 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3840
3841 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003842 if (Op.Val) {
3843 ExpandOp(Op, Lo, Hi);
3844 break;
3845 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003846 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003847
Chris Lattner4e6c7462005-01-08 19:27:05 +00003848 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003849 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003850 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003851 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003852 break;
3853
Evan Cheng05a2d562006-01-09 18:31:59 +00003854 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003855 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003856 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003857 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003858 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003859 Op = TLI.LowerOperation(Op, DAG);
3860 if (Op.Val) {
3861 // Now that the custom expander is done, expand the result, which is
3862 // still VT.
3863 ExpandOp(Op, Lo, Hi);
3864 break;
3865 }
3866 }
3867
Chris Lattnere34b3962005-01-19 04:19:40 +00003868 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003869 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003870 break;
Chris Lattner47599822005-04-02 03:38:53 +00003871
3872 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003873 TargetLowering::LegalizeAction Action =
3874 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3875 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3876 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003877 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003878 break;
3879 }
3880
Chris Lattnere34b3962005-01-19 04:19:40 +00003881 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003882 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003883 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003884 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003885
Evan Cheng05a2d562006-01-09 18:31:59 +00003886 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003887 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003888 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003889 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003890 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003891 Op = TLI.LowerOperation(Op, DAG);
3892 if (Op.Val) {
3893 // Now that the custom expander is done, expand the result, which is
3894 // still VT.
3895 ExpandOp(Op, Lo, Hi);
3896 break;
3897 }
3898 }
3899
Chris Lattnere34b3962005-01-19 04:19:40 +00003900 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003901 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003902 break;
Chris Lattner47599822005-04-02 03:38:53 +00003903
3904 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003905 TargetLowering::LegalizeAction Action =
3906 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3907 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3908 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003909 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003910 break;
3911 }
3912
Chris Lattnere34b3962005-01-19 04:19:40 +00003913 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003914 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003915 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003916 }
3917
3918 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003919 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003920 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003921 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003922 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003923 Op = TLI.LowerOperation(Op, DAG);
3924 if (Op.Val) {
3925 // Now that the custom expander is done, expand the result, which is
3926 // still VT.
3927 ExpandOp(Op, Lo, Hi);
3928 break;
3929 }
3930 }
3931
Chris Lattnere34b3962005-01-19 04:19:40 +00003932 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003933 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003934 break;
Chris Lattner47599822005-04-02 03:38:53 +00003935
3936 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003937 TargetLowering::LegalizeAction Action =
3938 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3939 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3940 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003941 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003942 break;
3943 }
3944
Chris Lattnere34b3962005-01-19 04:19:40 +00003945 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003946 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003947 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003948 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003949
Misha Brukmanedf128a2005-04-21 22:36:52 +00003950 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003951 case ISD::SUB: {
3952 // If the target wants to custom expand this, let them.
3953 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3954 TargetLowering::Custom) {
3955 Op = TLI.LowerOperation(Op, DAG);
3956 if (Op.Val) {
3957 ExpandOp(Op, Lo, Hi);
3958 break;
3959 }
3960 }
3961
3962 // Expand the subcomponents.
3963 SDOperand LHSL, LHSH, RHSL, RHSH;
3964 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3965 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00003966 std::vector<MVT::ValueType> VTs;
3967 std::vector<SDOperand> LoOps, HiOps;
3968 VTs.push_back(LHSL.getValueType());
3969 VTs.push_back(MVT::Flag);
3970 LoOps.push_back(LHSL);
3971 LoOps.push_back(RHSL);
3972 HiOps.push_back(LHSH);
3973 HiOps.push_back(RHSH);
3974 if (Node->getOpcode() == ISD::ADD) {
3975 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
3976 HiOps.push_back(Lo.getValue(1));
3977 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
3978 } else {
3979 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
3980 HiOps.push_back(Lo.getValue(1));
3981 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
3982 }
Chris Lattner84f67882005-01-20 18:52:28 +00003983 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00003984 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003985 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003986 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003987 SDOperand LL, LH, RL, RH;
3988 ExpandOp(Node->getOperand(0), LL, LH);
3989 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003990 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3991 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3992 // extended the sign bit of the low half through the upper half, and if so
3993 // emit a MULHS instead of the alternate sequence that is valid for any
3994 // i64 x i64 multiply.
3995 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3996 // is RH an extension of the sign bit of RL?
3997 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3998 RH.getOperand(1).getOpcode() == ISD::Constant &&
3999 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4000 // is LH an extension of the sign bit of LL?
4001 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4002 LH.getOperand(1).getOpcode() == ISD::Constant &&
4003 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4004 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4005 } else {
4006 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4007 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4008 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4009 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4010 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4011 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004012 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4013 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004014 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004015 }
4016 break;
4017 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004018 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4019 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4020 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4021 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004022 }
4023
Chris Lattner83397362005-12-21 18:02:52 +00004024 // Make sure the resultant values have been legalized themselves, unless this
4025 // is a type that requires multi-step expansion.
4026 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4027 Lo = LegalizeOp(Lo);
4028 Hi = LegalizeOp(Hi);
4029 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004030
4031 // Remember in a map if the values will be reused later.
4032 bool isNew =
4033 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4034 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004035}
4036
Chris Lattnerc7029802006-03-18 01:44:44 +00004037/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4038/// two smaller values of MVT::Vector type.
4039void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4040 SDOperand &Hi) {
4041 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4042 SDNode *Node = Op.Val;
4043 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4044 assert(NumElements > 1 && "Cannot split a single element vector!");
4045 unsigned NewNumElts = NumElements/2;
4046 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4047 SDOperand TypeNode = *(Node->op_end()-1);
4048
4049 // See if we already split it.
4050 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4051 = SplitNodes.find(Op);
4052 if (I != SplitNodes.end()) {
4053 Lo = I->second.first;
4054 Hi = I->second.second;
4055 return;
4056 }
4057
4058 switch (Node->getOpcode()) {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004059 default: assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004060 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004061 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4062 LoOps.push_back(NewNumEltsNode);
4063 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004064 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004065
4066 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4067 HiOps.push_back(NewNumEltsNode);
4068 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004069 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004070 break;
4071 }
4072 case ISD::VADD:
4073 case ISD::VSUB:
4074 case ISD::VMUL:
4075 case ISD::VSDIV:
4076 case ISD::VUDIV:
4077 case ISD::VAND:
4078 case ISD::VOR:
4079 case ISD::VXOR: {
4080 SDOperand LL, LH, RL, RH;
4081 SplitVectorOp(Node->getOperand(0), LL, LH);
4082 SplitVectorOp(Node->getOperand(1), RL, RH);
4083
4084 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4085 NewNumEltsNode, TypeNode);
4086 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4087 NewNumEltsNode, TypeNode);
4088 break;
4089 }
4090 case ISD::VLOAD: {
4091 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4092 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4093 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4094
4095 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4096 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4097 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4098 getIntPtrConstant(IncrementSize));
4099 // FIXME: This creates a bogus srcvalue!
4100 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4101
4102 // Build a factor node to remember that this load is independent of the
4103 // other one.
4104 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4105 Hi.getValue(1));
4106
4107 // Remember that we legalized the chain.
4108 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4109 if (!TLI.isLittleEndian())
4110 std::swap(Lo, Hi);
4111 break;
4112 }
4113 }
4114
4115 // Remember in a map if the values will be reused later.
4116 bool isNew =
4117 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4118 assert(isNew && "Value already expanded?!?");
4119}
4120
4121
4122/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4123/// equivalent operation that returns a scalar (e.g. F32) or packed value
4124/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4125/// type for the result.
4126SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4127 MVT::ValueType NewVT) {
4128 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4129 SDNode *Node = Op.Val;
4130
4131 // See if we already packed it.
4132 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4133 if (I != PackedNodes.end()) return I->second;
4134
4135 SDOperand Result;
4136 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004137 default:
4138 Node->dump(); std::cerr << "\n";
4139 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004140 case ISD::VADD:
4141 case ISD::VSUB:
4142 case ISD::VMUL:
4143 case ISD::VSDIV:
4144 case ISD::VUDIV:
4145 case ISD::VAND:
4146 case ISD::VOR:
4147 case ISD::VXOR:
4148 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4149 NewVT,
4150 PackVectorOp(Node->getOperand(0), NewVT),
4151 PackVectorOp(Node->getOperand(1), NewVT));
4152 break;
4153 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004154 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4155 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004156
Chris Lattner4794a6b2006-03-19 00:07:49 +00004157 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004158
4159 // Remember that we legalized the chain.
4160 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4161 break;
4162 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004163 case ISD::VBUILD_VECTOR:
Chris Lattnerc7029802006-03-18 01:44:44 +00004164 if (!MVT::isVector(NewVT)) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004165 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004166 Result = Node->getOperand(0);
4167 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004168 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004169 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004170 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004171 }
4172 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004173 case ISD::VINSERT_VECTOR_ELT:
4174 if (!MVT::isVector(NewVT)) {
4175 // Returning a scalar? Must be the inserted element.
4176 Result = Node->getOperand(1);
4177 } else {
4178 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4179 PackVectorOp(Node->getOperand(0), NewVT),
4180 Node->getOperand(1), Node->getOperand(2));
4181 }
4182 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004183 }
4184
4185 if (TLI.isTypeLegal(NewVT))
4186 Result = LegalizeOp(Result);
4187 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4188 assert(isNew && "Value already packed?");
4189 return Result;
4190}
4191
Chris Lattner3e928bb2005-01-07 07:47:09 +00004192
4193// SelectionDAG::Legalize - This is the entry point for the file.
4194//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004195void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004196 /// run - This is the main entry point to this class.
4197 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004198 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004199}
4200