blob: dc68d7b38a448a3bce63cd1be43899074b27c425 [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>
Evan Cheng033e6812006-03-24 01:17:21 +000024#include <map>
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 Lattnerce872152006-03-19 06:31:19 +0000163 SDOperand CreateStackTemporary(MVT::ValueType VT);
164
Chris Lattner77e77a62005-01-21 06:05:23 +0000165 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
166 SDOperand &Hi);
167 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
168 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000169
Chris Lattner35481892005-12-23 00:16:34 +0000170 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000171 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000172 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand LegalOp,
174 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000175 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
176 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000177 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
178 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000179
Chris Lattner456a93a2006-01-28 07:39:30 +0000180 SDOperand ExpandBSWAP(SDOperand Op);
181 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000182 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
183 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000184 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
185 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000186
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187 SDOperand getIntPtrConstant(uint64_t Val) {
188 return DAG.getConstant(Val, TLI.getPointerTy());
189 }
190};
191}
192
Chris Lattnerc7029802006-03-18 01:44:44 +0000193/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
194/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000195static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000196 switch (VecOp) {
197 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000198 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
199 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
200 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
201 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
202 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
203 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
204 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
205 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000206 }
207}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000208
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000209SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
210 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
211 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000212 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000213 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000214}
215
Chris Lattner32fca002005-10-06 01:20:27 +0000216/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
217/// not been visited yet and if all of its operands have already been visited.
218static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
219 std::map<SDNode*, unsigned> &Visited) {
220 if (++Visited[N] != N->getNumOperands())
221 return; // Haven't visited all operands yet
222
223 Order.push_back(N);
224
225 if (N->hasOneUse()) { // Tail recurse in common case.
226 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
227 return;
228 }
229
230 // Now that we have N in, add anything that uses it if all of their operands
231 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000232 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
233 ComputeTopDownOrdering(*UI, Order, Visited);
234}
235
Chris Lattner1618beb2005-07-29 00:11:56 +0000236
Chris Lattner3e928bb2005-01-07 07:47:09 +0000237void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000238 LastCALLSEQ_END = DAG.getEntryNode();
239 IsLegalizingCall = false;
240
Chris Lattnerab510a72005-10-02 17:49:46 +0000241 // The legalize process is inherently a bottom-up recursive process (users
242 // legalize their uses before themselves). Given infinite stack space, we
243 // could just start legalizing on the root and traverse the whole graph. In
244 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000245 // blocks. To avoid this problem, compute an ordering of the nodes where each
246 // node is only legalized after all of its operands are legalized.
247 std::map<SDNode*, unsigned> Visited;
248 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000249
Chris Lattner32fca002005-10-06 01:20:27 +0000250 // Compute ordering from all of the leaves in the graphs, those (like the
251 // entry node) that have no operands.
252 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
253 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000254 if (I->getNumOperands() == 0) {
255 Visited[I] = 0 - 1U;
256 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000257 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000258 }
259
Chris Lattnerde202b32005-11-09 23:47:37 +0000260 assert(Order.size() == Visited.size() &&
261 Order.size() ==
262 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000263 "Error: DAG is cyclic!");
264 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000265
Chris Lattnerc7029802006-03-18 01:44:44 +0000266 for (unsigned i = 0, e = Order.size(); i != e; ++i)
267 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000268
269 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000271 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
272 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273
274 ExpandedNodes.clear();
275 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000276 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000277 SplitNodes.clear();
278 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279
280 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000281 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000282}
283
Chris Lattner6831a812006-02-13 09:18:02 +0000284
285/// FindCallEndFromCallStart - Given a chained node that is part of a call
286/// sequence, find the CALLSEQ_END node that terminates the call sequence.
287static SDNode *FindCallEndFromCallStart(SDNode *Node) {
288 if (Node->getOpcode() == ISD::CALLSEQ_END)
289 return Node;
290 if (Node->use_empty())
291 return 0; // No CallSeqEnd
292
293 // The chain is usually at the end.
294 SDOperand TheChain(Node, Node->getNumValues()-1);
295 if (TheChain.getValueType() != MVT::Other) {
296 // Sometimes it's at the beginning.
297 TheChain = SDOperand(Node, 0);
298 if (TheChain.getValueType() != MVT::Other) {
299 // Otherwise, hunt for it.
300 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
301 if (Node->getValueType(i) == MVT::Other) {
302 TheChain = SDOperand(Node, i);
303 break;
304 }
305
306 // Otherwise, we walked into a node without a chain.
307 if (TheChain.getValueType() != MVT::Other)
308 return 0;
309 }
310 }
311
312 for (SDNode::use_iterator UI = Node->use_begin(),
313 E = Node->use_end(); UI != E; ++UI) {
314
315 // Make sure to only follow users of our token chain.
316 SDNode *User = *UI;
317 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
318 if (User->getOperand(i) == TheChain)
319 if (SDNode *Result = FindCallEndFromCallStart(User))
320 return Result;
321 }
322 return 0;
323}
324
325/// FindCallStartFromCallEnd - Given a chained node that is part of a call
326/// sequence, find the CALLSEQ_START node that initiates the call sequence.
327static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
328 assert(Node && "Didn't find callseq_start for a call??");
329 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
330
331 assert(Node->getOperand(0).getValueType() == MVT::Other &&
332 "Node doesn't have a token chain argument!");
333 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
334}
335
336/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
337/// see if any uses can reach Dest. If no dest operands can get to dest,
338/// legalize them, legalize ourself, and return false, otherwise, return true.
339bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
340 SDNode *Dest) {
341 if (N == Dest) return true; // N certainly leads to Dest :)
342
343 // If the first result of this node has been already legalized, then it cannot
344 // reach N.
345 switch (getTypeAction(N->getValueType(0))) {
346 case Legal:
347 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
348 break;
349 case Promote:
350 if (PromotedNodes.count(SDOperand(N, 0))) return false;
351 break;
352 case Expand:
353 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
354 break;
355 }
356
357 // Okay, this node has not already been legalized. Check and legalize all
358 // operands. If none lead to Dest, then we can legalize this node.
359 bool OperandsLeadToDest = false;
360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
361 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
362 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
363
364 if (OperandsLeadToDest) return true;
365
366 // Okay, this node looks safe, legalize it and return false.
367 switch (getTypeAction(N->getValueType(0))) {
368 case Legal:
369 LegalizeOp(SDOperand(N, 0));
370 break;
371 case Promote:
372 PromoteOp(SDOperand(N, 0));
373 break;
374 case Expand: {
375 SDOperand X, Y;
376 ExpandOp(SDOperand(N, 0), X, Y);
377 break;
378 }
379 }
380 return false;
381}
382
Chris Lattnerc7029802006-03-18 01:44:44 +0000383/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
384/// appropriate for its type.
385void SelectionDAGLegalize::HandleOp(SDOperand Op) {
386 switch (getTypeAction(Op.getValueType())) {
387 default: assert(0 && "Bad type action!");
388 case Legal: LegalizeOp(Op); break;
389 case Promote: PromoteOp(Op); break;
390 case Expand:
391 if (Op.getValueType() != MVT::Vector) {
392 SDOperand X, Y;
393 ExpandOp(Op, X, Y);
394 } else {
395 SDNode *N = Op.Val;
396 unsigned NumOps = N->getNumOperands();
397 unsigned NumElements =
398 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
399 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
400 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
401 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
402 // In the common case, this is a legal vector type, convert it to the
403 // packed operation and type now.
404 PackVectorOp(Op, PackedVT);
405 } else if (NumElements == 1) {
406 // Otherwise, if this is a single element vector, convert it to a
407 // scalar operation.
408 PackVectorOp(Op, EVT);
409 } else {
410 // Otherwise, this is a multiple element vector that isn't supported.
411 // Split it in half and legalize both parts.
412 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000413 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000414 }
415 }
416 break;
417 }
418}
Chris Lattner6831a812006-02-13 09:18:02 +0000419
420
Chris Lattnerc7029802006-03-18 01:44:44 +0000421/// LegalizeOp - We know that the specified value has a legal type.
422/// Recursively ensure that the operands have legal types, then return the
423/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000424SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000425 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000426 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000427 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000428
Chris Lattner3e928bb2005-01-07 07:47:09 +0000429 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000430 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000431 if (Node->getNumValues() > 1) {
432 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000433 if (getTypeAction(Node->getValueType(i)) != Legal) {
434 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000435 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000436 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000437 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000438 }
439 }
440
Chris Lattner45982da2005-05-12 16:53:42 +0000441 // Note that LegalizeOp may be reentered even from single-use nodes, which
442 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000443 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
444 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000445
Nate Begeman9373a812005-08-10 20:51:12 +0000446 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000447 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000448 bool isCustom = false;
449
Chris Lattner3e928bb2005-01-07 07:47:09 +0000450 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000451 case ISD::FrameIndex:
452 case ISD::EntryToken:
453 case ISD::Register:
454 case ISD::BasicBlock:
455 case ISD::TargetFrameIndex:
456 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000457 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000458 case ISD::TargetConstantPool:
459 case ISD::TargetGlobalAddress:
460 case ISD::TargetExternalSymbol:
461 case ISD::VALUETYPE:
462 case ISD::SRCVALUE:
463 case ISD::STRING:
464 case ISD::CONDCODE:
465 // Primitives must all be legal.
466 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
467 "This must be legal!");
468 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000469 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000470 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
471 // If this is a target node, legalize it by legalizing the operands then
472 // passing it through.
473 std::vector<SDOperand> Ops;
474 bool Changed = false;
475 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
476 Ops.push_back(LegalizeOp(Node->getOperand(i)));
477 Changed = Changed || Node->getOperand(i) != Ops.back();
478 }
479 if (Changed)
480 if (Node->getNumValues() == 1)
481 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
482 else {
483 std::vector<MVT::ValueType> VTs(Node->value_begin(),
484 Node->value_end());
485 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
486 }
487
488 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
489 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
490 return Result.getValue(Op.ResNo);
491 }
492 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
494 assert(0 && "Do not know how to legalize this operator!");
495 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000496 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000497 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000498 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000499 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
500 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000501 case TargetLowering::Custom:
502 Tmp1 = TLI.LowerOperation(Op, DAG);
503 if (Tmp1.Val) Result = Tmp1;
504 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000505 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000506 break;
507 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000508 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000509 case ISD::AssertSext:
510 case ISD::AssertZext:
511 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000513 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000514 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000515 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000516 Result = Node->getOperand(Op.ResNo);
517 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000518 case ISD::CopyFromReg:
519 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000520 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000521 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000522 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000523 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000524 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000525 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000526 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
528 } else {
529 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
530 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000531 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
532 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000533 // Since CopyFromReg produces two values, make sure to remember that we
534 // legalized both of them.
535 AddLegalizedOperand(Op.getValue(0), Result);
536 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
537 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000538 case ISD::UNDEF: {
539 MVT::ValueType VT = Op.getValueType();
540 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000541 default: assert(0 && "This action is not supported yet!");
542 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000543 if (MVT::isInteger(VT))
544 Result = DAG.getConstant(0, VT);
545 else if (MVT::isFloatingPoint(VT))
546 Result = DAG.getConstantFP(0, VT);
547 else
548 assert(0 && "Unknown value type!");
549 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000550 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000551 break;
552 }
553 break;
554 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000555
556 case ISD::LOCATION:
557 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
558 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
559
560 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
561 case TargetLowering::Promote:
562 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000563 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000564 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000565 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
566 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
567
568 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
569 const std::string &FName =
570 cast<StringSDNode>(Node->getOperand(3))->getValue();
571 const std::string &DirName =
572 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000573 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000574
Jim Laskeye81aecb2005-12-21 20:51:37 +0000575 std::vector<SDOperand> Ops;
576 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000577 SDOperand LineOp = Node->getOperand(1);
578 SDOperand ColOp = Node->getOperand(2);
579
580 if (useDEBUG_LOC) {
581 Ops.push_back(LineOp); // line #
582 Ops.push_back(ColOp); // col #
583 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
584 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
585 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000586 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
587 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000588 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
589 Ops.push_back(DAG.getConstant(ID, MVT::i32));
590 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
591 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000592 } else {
593 Result = Tmp1; // chain
594 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000595 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000596 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000597 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000598 if (Tmp1 != Node->getOperand(0) ||
599 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000600 std::vector<SDOperand> Ops;
601 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000602 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
603 Ops.push_back(Node->getOperand(1)); // line # must be legal.
604 Ops.push_back(Node->getOperand(2)); // col # must be legal.
605 } else {
606 // Otherwise promote them.
607 Ops.push_back(PromoteOp(Node->getOperand(1)));
608 Ops.push_back(PromoteOp(Node->getOperand(2)));
609 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000610 Ops.push_back(Node->getOperand(3)); // filename must be legal.
611 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000612 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000613 }
614 break;
615 }
616 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000617
618 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000619 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000620 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000621 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000622 case TargetLowering::Legal:
623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
624 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
625 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
626 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000628 break;
629 }
630 break;
631
632 case ISD::DEBUG_LABEL:
633 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
634 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000635 default: assert(0 && "This action is not supported yet!");
636 case TargetLowering::Legal:
637 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
638 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000640 break;
641 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000642 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000643
Chris Lattner3e928bb2005-01-07 07:47:09 +0000644 case ISD::Constant:
645 // We know we don't need to expand constants here, constants only have one
646 // value and we check that it is fine above.
647
648 // FIXME: Maybe we should handle things like targets that don't support full
649 // 32-bit immediates?
650 break;
651 case ISD::ConstantFP: {
652 // Spill FP immediates to the constant pool if the target cannot directly
653 // codegen them. Targets often have some immediate values that can be
654 // efficiently generated into an FP register without a load. We explicitly
655 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000656 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
657
658 // Check to see if this FP immediate is already legal.
659 bool isLegal = false;
660 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
661 E = TLI.legal_fpimm_end(); I != E; ++I)
662 if (CFP->isExactlyValue(*I)) {
663 isLegal = true;
664 break;
665 }
666
Chris Lattner3181a772006-01-29 06:26:56 +0000667 // If this is a legal constant, turn it into a TargetConstantFP node.
668 if (isLegal) {
669 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
670 break;
671 }
672
673 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
674 default: assert(0 && "This action is not supported yet!");
675 case TargetLowering::Custom:
676 Tmp3 = TLI.LowerOperation(Result, DAG);
677 if (Tmp3.Val) {
678 Result = Tmp3;
679 break;
680 }
681 // FALLTHROUGH
682 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000683 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000684 bool Extend = false;
685
Chris Lattner456a93a2006-01-28 07:39:30 +0000686 // If a FP immediate is precise when represented as a float and if the
687 // target can do an extending load from float to double, we put it into
688 // the constant pool as a float, even if it's is statically typed as a
689 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000690 MVT::ValueType VT = CFP->getValueType(0);
691 bool isDouble = VT == MVT::f64;
692 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
693 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000694 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
695 // Only do this if the target has a native EXTLOAD instruction from
696 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000697 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000698 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
699 VT = MVT::f32;
700 Extend = true;
701 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000702
Chris Lattner456a93a2006-01-28 07:39:30 +0000703 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000704 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000705 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
706 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000707 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000708 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
709 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000710 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000711 }
712 break;
713 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000714 case ISD::TokenFactor:
715 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000716 Tmp1 = LegalizeOp(Node->getOperand(0));
717 Tmp2 = LegalizeOp(Node->getOperand(1));
718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
719 } else if (Node->getNumOperands() == 3) {
720 Tmp1 = LegalizeOp(Node->getOperand(0));
721 Tmp2 = LegalizeOp(Node->getOperand(1));
722 Tmp3 = LegalizeOp(Node->getOperand(2));
723 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000724 } else {
725 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000726 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000727 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
728 Ops.push_back(LegalizeOp(Node->getOperand(i)));
729 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000730 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000731 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000732
Chris Lattnerb2827b02006-03-19 00:52:58 +0000733 case ISD::BUILD_VECTOR:
734 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000735 default: assert(0 && "This action is not supported yet!");
736 case TargetLowering::Custom:
737 Tmp3 = TLI.LowerOperation(Result, DAG);
738 if (Tmp3.Val) {
739 Result = Tmp3;
740 break;
741 }
742 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000743 case TargetLowering::Expand:
744 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000745 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000746 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000747 break;
748 case ISD::INSERT_VECTOR_ELT:
749 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
750 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
751 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
752 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
753
754 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
755 Node->getValueType(0))) {
756 default: assert(0 && "This action is not supported yet!");
757 case TargetLowering::Legal:
758 break;
759 case TargetLowering::Custom:
760 Tmp3 = TLI.LowerOperation(Result, DAG);
761 if (Tmp3.Val) {
762 Result = Tmp3;
763 break;
764 }
765 // FALLTHROUGH
766 case TargetLowering::Expand: {
767 // If the target doesn't support this, we have to spill the input vector
768 // to a temporary stack slot, update the element, then reload it. This is
769 // badness. We could also load the value into a vector register (either
770 // with a "move to register" or "extload into register" instruction, then
771 // permute it into place, if the idx is a constant and if the idx is
772 // supported by the target.
773 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
774 break;
775 }
776 }
777 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000778 case ISD::SCALAR_TO_VECTOR:
779 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
780 Result = DAG.UpdateNodeOperands(Result, Tmp1);
781 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
782 Node->getValueType(0))) {
783 default: assert(0 && "This action is not supported yet!");
784 case TargetLowering::Legal:
785 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000786 case TargetLowering::Custom:
787 Tmp3 = TLI.LowerOperation(Result, DAG);
788 if (Tmp3.Val) {
789 Result = Tmp3;
790 break;
791 }
792 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000793 case TargetLowering::Expand: {
794 // If the target doesn't support this, store the value to a temporary
795 // stack slot, then EXTLOAD the vector back out.
Chris Lattner4d3abee2006-03-19 06:47:21 +0000796 // TODO: If a target doesn't support this, create a stack slot for the
797 // whole vector, then store into it, then load the whole vector.
Chris Lattnerce872152006-03-19 06:31:19 +0000798 SDOperand StackPtr =
799 CreateStackTemporary(Node->getOperand(0).getValueType());
800 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
801 Node->getOperand(0), StackPtr,
802 DAG.getSrcValue(NULL));
803 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
804 DAG.getSrcValue(NULL),
805 Node->getOperand(0).getValueType());
806 break;
807 }
808 }
809 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000810 case ISD::VECTOR_SHUFFLE:
811 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
812 "vector shuffle should not be created if not legal!");
813 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
814 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
816
817 // Allow targets to custom lower the SHUFFLEs they support.
818 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
819 == TargetLowering::Custom) {
820 Tmp1 = TLI.LowerOperation(Result, DAG);
821 if (Tmp1.Val) Result = Tmp1;
822 }
823 break;
Chris Lattner384504c2006-03-21 20:44:12 +0000824
825 case ISD::EXTRACT_VECTOR_ELT:
826 Tmp1 = LegalizeOp(Node->getOperand(0));
827 Tmp2 = LegalizeOp(Node->getOperand(1));
828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +0000829
830 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
831 Tmp1.getValueType())) {
832 default: assert(0 && "This action is not supported yet!");
833 case TargetLowering::Legal:
834 break;
835 case TargetLowering::Custom:
836 Tmp3 = TLI.LowerOperation(Result, DAG);
837 if (Tmp3.Val) {
838 Result = Tmp3;
839 break;
840 }
841 // FALLTHROUGH
842 case TargetLowering::Expand: {
843 // If the target doesn't support this, store the value to a temporary
844 // stack slot, then LOAD the scalar element back out.
845 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
846 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
847 Tmp1, StackPtr, DAG.getSrcValue(NULL));
848
849 // Add the offset to the index.
850 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
851 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
852 DAG.getConstant(EltSize, Tmp2.getValueType()));
853 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
854
855 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
856 DAG.getSrcValue(NULL));
857 break;
858 }
859 }
Chris Lattner384504c2006-03-21 20:44:12 +0000860 break;
861
Chris Lattner3b9fa892006-03-22 00:12:37 +0000862 case ISD::VEXTRACT_VECTOR_ELT: {
Chris Lattner384504c2006-03-21 20:44:12 +0000863 // We know that operand #0 is the Vec vector. If the index is a constant
864 // or if the invec is a supported hardware type, we can use it. Otherwise,
865 // lower to a store then an indexed load.
866 Tmp1 = Node->getOperand(0);
867 Tmp2 = LegalizeOp(Node->getOperand(1));
868
869 SDNode *InVal = Tmp1.Val;
870 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
871 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
872
873 // Figure out if there is a Packed type corresponding to this Vector
874 // type. If so, convert to the packed type.
875 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
876 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
877 // Turn this into a packed extract_vector_elt operation.
878 Tmp1 = PackVectorOp(Tmp1, TVT);
879 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Node->getValueType(0),
880 Tmp1, Tmp2);
881 break;
882 } else if (NumElems == 1) {
883 // This must be an access of the only element.
884 Result = PackVectorOp(Tmp1, EVT);
885 break;
886 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Tmp2)) {
887 SDOperand Lo, Hi;
888 SplitVectorOp(Tmp1, Lo, Hi);
889 if (CIdx->getValue() < NumElems/2) {
890 Tmp1 = Lo;
891 } else {
892 Tmp1 = Hi;
893 Tmp2 = DAG.getConstant(CIdx->getValue() - NumElems/2,
894 Tmp2.getValueType());
895 }
896
897 // It's now an extract from the appropriate high or low part.
898 Result = LegalizeOp(DAG.UpdateNodeOperands(Result, Tmp1, Tmp2));
899 } else {
Chris Lattner3b9fa892006-03-22 00:12:37 +0000900 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
Chris Lattner384504c2006-03-21 20:44:12 +0000901 assert(0 && "unimp!");
902 }
903 break;
Chris Lattner3b9fa892006-03-22 00:12:37 +0000904 }
Chris Lattner87100e02006-03-20 01:52:29 +0000905
Chris Lattner6831a812006-02-13 09:18:02 +0000906 case ISD::CALLSEQ_START: {
907 SDNode *CallEnd = FindCallEndFromCallStart(Node);
908
909 // Recursively Legalize all of the inputs of the call end that do not lead
910 // to this call start. This ensures that any libcalls that need be inserted
911 // are inserted *before* the CALLSEQ_START.
912 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
913 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
914
915 // Now that we legalized all of the inputs (which may have inserted
916 // libcalls) create the new CALLSEQ_START node.
917 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
918
919 // Merge in the last call, to ensure that this call start after the last
920 // call ended.
921 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
922 Tmp1 = LegalizeOp(Tmp1);
923
924 // Do not try to legalize the target-specific arguments (#1+).
925 if (Tmp1 != Node->getOperand(0)) {
926 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
927 Ops[0] = Tmp1;
928 Result = DAG.UpdateNodeOperands(Result, Ops);
929 }
930
931 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000932 AddLegalizedOperand(Op.getValue(0), Result);
933 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
934 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
935
Chris Lattner6831a812006-02-13 09:18:02 +0000936 // Now that the callseq_start and all of the non-call nodes above this call
937 // sequence have been legalized, legalize the call itself. During this
938 // process, no libcalls can/will be inserted, guaranteeing that no calls
939 // can overlap.
940 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
941 SDOperand InCallSEQ = LastCALLSEQ_END;
942 // Note that we are selecting this call!
943 LastCALLSEQ_END = SDOperand(CallEnd, 0);
944 IsLegalizingCall = true;
945
946 // Legalize the call, starting from the CALLSEQ_END.
947 LegalizeOp(LastCALLSEQ_END);
948 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
949 return Result;
950 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000951 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000952 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
953 // will cause this node to be legalized as well as handling libcalls right.
954 if (LastCALLSEQ_END.Val != Node) {
955 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
956 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
957 assert(I != LegalizedNodes.end() &&
958 "Legalizing the call start should have legalized this node!");
959 return I->second;
960 }
961
962 // Otherwise, the call start has been legalized and everything is going
963 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000964 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000965 // Do not try to legalize the target-specific arguments (#1+), except for
966 // an optional flag input.
967 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
968 if (Tmp1 != Node->getOperand(0)) {
969 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
970 Ops[0] = Tmp1;
971 Result = DAG.UpdateNodeOperands(Result, Ops);
972 }
973 } else {
974 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
975 if (Tmp1 != Node->getOperand(0) ||
976 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
977 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
978 Ops[0] = Tmp1;
979 Ops.back() = Tmp2;
980 Result = DAG.UpdateNodeOperands(Result, Ops);
981 }
Chris Lattner6a542892006-01-24 05:48:21 +0000982 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000983 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000984 // This finishes up call legalization.
985 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000986
987 // If the CALLSEQ_END node has a flag, remember that we legalized it.
988 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
989 if (Node->getNumValues() == 2)
990 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
991 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000992 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
994 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
995 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000997
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000998 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000999 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001000 switch (TLI.getOperationAction(Node->getOpcode(),
1001 Node->getValueType(0))) {
1002 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001003 case TargetLowering::Expand: {
1004 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1005 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1006 " not tell us which reg is the stack pointer!");
1007 SDOperand Chain = Tmp1.getOperand(0);
1008 SDOperand Size = Tmp2.getOperand(1);
1009 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1010 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1011 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001012 Tmp1 = LegalizeOp(Tmp1);
1013 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001014 break;
1015 }
1016 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001017 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1018 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001019 Tmp1 = LegalizeOp(Tmp3);
1020 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001021 }
Chris Lattner903d2782006-01-15 08:54:32 +00001022 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001023 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001024 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001025 }
Chris Lattner903d2782006-01-15 08:54:32 +00001026 // Since this op produce two values, make sure to remember that we
1027 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001028 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1029 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001030 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001031 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001032 case ISD::INLINEASM:
1033 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1034 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001035 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +00001036 Tmp2 = Tmp3 = SDOperand(0, 0);
1037 else
1038 Tmp3 = LegalizeOp(Tmp2);
1039
1040 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1041 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1042 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001043 if (Tmp3.Val) Ops.back() = Tmp3;
1044 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001045 }
1046
1047 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001048 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001049 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1050 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +00001051 case ISD::BR:
1052 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001053 // Ensure that libcalls are emitted before a branch.
1054 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1055 Tmp1 = LegalizeOp(Tmp1);
1056 LastCALLSEQ_END = DAG.getEntryNode();
1057
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001058 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001059 break;
1060
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001061 case ISD::BRCOND:
1062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001063 // Ensure that libcalls are emitted before a return.
1064 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1065 Tmp1 = LegalizeOp(Tmp1);
1066 LastCALLSEQ_END = DAG.getEntryNode();
1067
Chris Lattner47e92232005-01-18 19:27:06 +00001068 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1069 case Expand: assert(0 && "It's impossible to expand bools");
1070 case Legal:
1071 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1072 break;
1073 case Promote:
1074 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1075 break;
1076 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001077
1078 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001079 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001080
1081 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1082 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001083 case TargetLowering::Legal: break;
1084 case TargetLowering::Custom:
1085 Tmp1 = TLI.LowerOperation(Result, DAG);
1086 if (Tmp1.Val) Result = Tmp1;
1087 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001088 case TargetLowering::Expand:
1089 // Expand brcond's setcc into its constituent parts and create a BR_CC
1090 // Node.
1091 if (Tmp2.getOpcode() == ISD::SETCC) {
1092 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1093 Tmp2.getOperand(0), Tmp2.getOperand(1),
1094 Node->getOperand(2));
1095 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001096 // Make sure the condition is either zero or one. It may have been
1097 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001098 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1099 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1100 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001101
Nate Begeman7cbd5252005-08-16 19:49:35 +00001102 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1103 DAG.getCondCode(ISD::SETNE), Tmp2,
1104 DAG.getConstant(0, Tmp2.getValueType()),
1105 Node->getOperand(2));
1106 }
1107 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001108 }
1109 break;
1110 case ISD::BR_CC:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001112 // Ensure that libcalls are emitted before a branch.
1113 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1114 Tmp1 = LegalizeOp(Tmp1);
1115 LastCALLSEQ_END = DAG.getEntryNode();
1116
Nate Begeman750ac1b2006-02-01 07:19:44 +00001117 Tmp2 = Node->getOperand(2); // LHS
1118 Tmp3 = Node->getOperand(3); // RHS
1119 Tmp4 = Node->getOperand(1); // CC
1120
1121 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1122
1123 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1124 // the LHS is a legal SETCC itself. In this case, we need to compare
1125 // the result against zero to select between true and false values.
1126 if (Tmp3.Val == 0) {
1127 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1128 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001129 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001130
1131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1132 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001133
Chris Lattner181b7a32005-12-17 23:46:46 +00001134 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1135 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001136 case TargetLowering::Legal: break;
1137 case TargetLowering::Custom:
1138 Tmp4 = TLI.LowerOperation(Result, DAG);
1139 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001140 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001141 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001142 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001143 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001144 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1145 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001146
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001147 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1149 Tmp2 = Result.getValue(0);
1150 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001151
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001152 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1153 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001154 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001155 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001156 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001157 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001158 Tmp2 = LegalizeOp(Tmp1);
1159 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001160 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001161 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001162 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001163 // Since loads produce two values, make sure to remember that we
1164 // legalized both of them.
1165 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1166 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1167 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001168 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001169 case ISD::EXTLOAD:
1170 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001171 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001172 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1173 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001174
Chris Lattner5f056bf2005-07-10 01:55:33 +00001175 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001176 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001177 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001178 case TargetLowering::Promote:
1179 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001180 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1181 DAG.getValueType(MVT::i8));
1182 Tmp1 = Result.getValue(0);
1183 Tmp2 = Result.getValue(1);
1184 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001185 case TargetLowering::Custom:
1186 isCustom = true;
1187 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001188 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1190 Node->getOperand(3));
1191 Tmp1 = Result.getValue(0);
1192 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001193
1194 if (isCustom) {
1195 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1196 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001197 Tmp1 = LegalizeOp(Tmp3);
1198 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001199 }
1200 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001201 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001202 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001203 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001204 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1205 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001206 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001207 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1208 Tmp2 = LegalizeOp(Load.getValue(1));
1209 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001210 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001211 assert(Node->getOpcode() != ISD::EXTLOAD &&
1212 "EXTLOAD should always be supported!");
1213 // Turn the unsupported load into an EXTLOAD followed by an explicit
1214 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001215 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1216 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001217 SDOperand ValRes;
1218 if (Node->getOpcode() == ISD::SEXTLOAD)
1219 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001220 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001221 else
1222 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001223 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1224 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1225 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001226 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001227 // Since loads produce two values, make sure to remember that we legalized
1228 // both of them.
1229 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1230 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1231 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001232 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001233 case ISD::EXTRACT_ELEMENT: {
1234 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1235 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001236 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001237 case Legal:
1238 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1239 // 1 -> Hi
1240 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1241 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1242 TLI.getShiftAmountTy()));
1243 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1244 } else {
1245 // 0 -> Lo
1246 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1247 Node->getOperand(0));
1248 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001249 break;
1250 case Expand:
1251 // Get both the low and high parts.
1252 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1253 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1254 Result = Tmp2; // 1 -> Hi
1255 else
1256 Result = Tmp1; // 0 -> Lo
1257 break;
1258 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001259 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001260 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001261
1262 case ISD::CopyToReg:
1263 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001264
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001265 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001266 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001267 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001268 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001269 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001270 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001271 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001272 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001273 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001274 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001275 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1276 Tmp3);
1277 } else {
1278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001279 }
1280
1281 // Since this produces two values, make sure to remember that we legalized
1282 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001283 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001284 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001285 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001286 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001287 break;
1288
1289 case ISD::RET:
1290 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001291
1292 // Ensure that libcalls are emitted before a return.
1293 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1294 Tmp1 = LegalizeOp(Tmp1);
1295 LastCALLSEQ_END = DAG.getEntryNode();
1296
Chris Lattner3e928bb2005-01-07 07:47:09 +00001297 switch (Node->getNumOperands()) {
1298 case 2: // ret val
1299 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1300 case Legal:
1301 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001302 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001303 break;
1304 case Expand: {
1305 SDOperand Lo, Hi;
1306 ExpandOp(Node->getOperand(1), Lo, Hi);
1307 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001308 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001309 }
1310 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001311 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001312 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1313 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001314 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001315 }
1316 break;
1317 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001318 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001319 break;
1320 default: { // ret <values>
1321 std::vector<SDOperand> NewValues;
1322 NewValues.push_back(Tmp1);
1323 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1324 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1325 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001326 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001327 break;
1328 case Expand: {
1329 SDOperand Lo, Hi;
1330 ExpandOp(Node->getOperand(i), Lo, Hi);
1331 NewValues.push_back(Lo);
1332 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001333 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001334 }
1335 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001336 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001337 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001338
1339 if (NewValues.size() == Node->getNumOperands())
1340 Result = DAG.UpdateNodeOperands(Result, NewValues);
1341 else
1342 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001343 break;
1344 }
1345 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001346
Chris Lattner6862dbc2006-01-29 21:02:23 +00001347 if (Result.getOpcode() == ISD::RET) {
1348 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1349 default: assert(0 && "This action is not supported yet!");
1350 case TargetLowering::Legal: break;
1351 case TargetLowering::Custom:
1352 Tmp1 = TLI.LowerOperation(Result, DAG);
1353 if (Tmp1.Val) Result = Tmp1;
1354 break;
1355 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001356 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001357 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001358 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001359 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1360 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1361
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001362 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001363 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001364 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001365 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001366 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001367 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001368 } else {
1369 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001370 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001371 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001372 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1373 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001374 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001375 }
1376
Chris Lattner3e928bb2005-01-07 07:47:09 +00001377 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1378 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001379 Tmp3 = LegalizeOp(Node->getOperand(1));
1380 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1381 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001382
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001383 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001384 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1385 default: assert(0 && "This action is not supported yet!");
1386 case TargetLowering::Legal: break;
1387 case TargetLowering::Custom:
1388 Tmp1 = TLI.LowerOperation(Result, DAG);
1389 if (Tmp1.Val) Result = Tmp1;
1390 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001391 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001392 break;
1393 }
1394 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001395 // Truncate the value and store the result.
1396 Tmp3 = PromoteOp(Node->getOperand(1));
1397 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001398 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001399 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001400 break;
1401
Chris Lattner3e928bb2005-01-07 07:47:09 +00001402 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001403 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001404 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001405
1406 // If this is a vector type, then we have to calculate the increment as
1407 // the product of the element size in bytes, and the number of elements
1408 // in the high half of the vector.
1409 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1410 SDNode *InVal = Node->getOperand(1).Val;
1411 unsigned NumElems =
1412 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1413 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1414
1415 // Figure out if there is a Packed type corresponding to this Vector
1416 // type. If so, convert to the packed type.
1417 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1418 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1419 // Turn this into a normal store of the packed type.
1420 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1422 Node->getOperand(3));
1423 break;
1424 } else if (NumElems == 1) {
1425 // Turn this into a normal store of the scalar type.
1426 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1427 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1428 Node->getOperand(3));
1429 break;
1430 } else {
1431 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1432 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1433 }
1434 } else {
1435 ExpandOp(Node->getOperand(1), Lo, Hi);
1436 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1437 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001438
1439 if (!TLI.isLittleEndian())
1440 std::swap(Lo, Hi);
1441
Chris Lattneredb1add2005-05-11 04:51:16 +00001442 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1443 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001444 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1445 getIntPtrConstant(IncrementSize));
1446 assert(isTypeLegal(Tmp2.getValueType()) &&
1447 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001448 // FIXME: This sets the srcvalue of both halves to be the same, which is
1449 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001450 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1451 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001452 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1453 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001454 }
1455 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001456 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001457 case ISD::PCMARKER:
1458 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001459 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001460 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001461 case ISD::STACKSAVE:
1462 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001463 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1464 Tmp1 = Result.getValue(0);
1465 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001466
Chris Lattner140d53c2006-01-13 02:50:02 +00001467 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1468 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001469 case TargetLowering::Legal: break;
1470 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001471 Tmp3 = TLI.LowerOperation(Result, DAG);
1472 if (Tmp3.Val) {
1473 Tmp1 = LegalizeOp(Tmp3);
1474 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001475 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001476 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001477 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001478 // Expand to CopyFromReg if the target set
1479 // StackPointerRegisterToSaveRestore.
1480 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001481 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001482 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001483 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001484 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001485 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1486 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001487 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001488 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001489 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001490
1491 // Since stacksave produce two values, make sure to remember that we
1492 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001493 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1494 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1495 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001496
Chris Lattner140d53c2006-01-13 02:50:02 +00001497 case ISD::STACKRESTORE:
1498 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1499 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001500 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001501
1502 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1503 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001504 case TargetLowering::Legal: break;
1505 case TargetLowering::Custom:
1506 Tmp1 = TLI.LowerOperation(Result, DAG);
1507 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001508 break;
1509 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001510 // Expand to CopyToReg if the target set
1511 // StackPointerRegisterToSaveRestore.
1512 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1513 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1514 } else {
1515 Result = Tmp1;
1516 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001517 break;
1518 }
1519 break;
1520
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001521 case ISD::READCYCLECOUNTER:
1522 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001523 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001524
1525 // Since rdcc produce two values, make sure to remember that we legalized
1526 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001527 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001528 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001529 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001530
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001531 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001532 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1533 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1534
Chris Lattner456a93a2006-01-28 07:39:30 +00001535 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1536 "Cannot handle illegal TRUNCSTORE yet!");
1537 Tmp2 = LegalizeOp(Node->getOperand(1));
1538
1539 // The only promote case we handle is TRUNCSTORE:i1 X into
1540 // -> TRUNCSTORE:i8 (and X, 1)
1541 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1542 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1543 TargetLowering::Promote) {
1544 // Promote the bool to a mask then store.
1545 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1546 DAG.getConstant(1, Tmp2.getValueType()));
1547 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1548 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001549
Chris Lattner456a93a2006-01-28 07:39:30 +00001550 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1551 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001552 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1553 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001554 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001555
Chris Lattner456a93a2006-01-28 07:39:30 +00001556 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1557 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1558 default: assert(0 && "This action is not supported yet!");
1559 case TargetLowering::Legal: break;
1560 case TargetLowering::Custom:
1561 Tmp1 = TLI.LowerOperation(Result, DAG);
1562 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001563 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001564 }
1565 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001566 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001567 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001568 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1569 case Expand: assert(0 && "It's impossible to expand bools");
1570 case Legal:
1571 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1572 break;
1573 case Promote:
1574 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1575 break;
1576 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001577 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001578 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001579
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001580 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001581
Nate Begemanb942a3d2005-08-23 04:29:48 +00001582 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001583 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001584 case TargetLowering::Legal: break;
1585 case TargetLowering::Custom: {
1586 Tmp1 = TLI.LowerOperation(Result, DAG);
1587 if (Tmp1.Val) Result = Tmp1;
1588 break;
1589 }
Nate Begeman9373a812005-08-10 20:51:12 +00001590 case TargetLowering::Expand:
1591 if (Tmp1.getOpcode() == ISD::SETCC) {
1592 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1593 Tmp2, Tmp3,
1594 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1595 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001596 // Make sure the condition is either zero or one. It may have been
1597 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001598 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1599 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1600 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001601 Result = DAG.getSelectCC(Tmp1,
1602 DAG.getConstant(0, Tmp1.getValueType()),
1603 Tmp2, Tmp3, ISD::SETNE);
1604 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001605 break;
1606 case TargetLowering::Promote: {
1607 MVT::ValueType NVT =
1608 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1609 unsigned ExtOp, TruncOp;
1610 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001611 ExtOp = ISD::ANY_EXTEND;
1612 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001613 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001614 ExtOp = ISD::FP_EXTEND;
1615 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001616 }
1617 // Promote each of the values to the new type.
1618 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1619 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1620 // Perform the larger operation, then round down.
1621 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1622 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1623 break;
1624 }
1625 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001626 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001627 case ISD::SELECT_CC: {
1628 Tmp1 = Node->getOperand(0); // LHS
1629 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001630 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1631 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001632 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001633
Nate Begeman750ac1b2006-02-01 07:19:44 +00001634 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1635
1636 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1637 // the LHS is a legal SETCC itself. In this case, we need to compare
1638 // the result against zero to select between true and false values.
1639 if (Tmp2.Val == 0) {
1640 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1641 CC = DAG.getCondCode(ISD::SETNE);
1642 }
1643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1644
1645 // Everything is legal, see if we should expand this op or something.
1646 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1647 default: assert(0 && "This action is not supported yet!");
1648 case TargetLowering::Legal: break;
1649 case TargetLowering::Custom:
1650 Tmp1 = TLI.LowerOperation(Result, DAG);
1651 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001652 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001653 }
1654 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001655 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001656 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001657 Tmp1 = Node->getOperand(0);
1658 Tmp2 = Node->getOperand(1);
1659 Tmp3 = Node->getOperand(2);
1660 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1661
1662 // If we had to Expand the SetCC operands into a SELECT node, then it may
1663 // not always be possible to return a true LHS & RHS. In this case, just
1664 // return the value we legalized, returned in the LHS
1665 if (Tmp2.Val == 0) {
1666 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001667 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001668 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001669
Chris Lattner73e142f2006-01-30 22:43:50 +00001670 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001671 default: assert(0 && "Cannot handle this action for SETCC yet!");
1672 case TargetLowering::Custom:
1673 isCustom = true;
1674 // FALLTHROUGH.
1675 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001676 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001677 if (isCustom) {
1678 Tmp3 = TLI.LowerOperation(Result, DAG);
1679 if (Tmp3.Val) Result = Tmp3;
1680 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001681 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001682 case TargetLowering::Promote: {
1683 // First step, figure out the appropriate operation to use.
1684 // Allow SETCC to not be supported for all legal data types
1685 // Mostly this targets FP
1686 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1687 MVT::ValueType OldVT = NewInTy;
1688
1689 // Scan for the appropriate larger type to use.
1690 while (1) {
1691 NewInTy = (MVT::ValueType)(NewInTy+1);
1692
1693 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1694 "Fell off of the edge of the integer world");
1695 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1696 "Fell off of the edge of the floating point world");
1697
1698 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001699 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001700 break;
1701 }
1702 if (MVT::isInteger(NewInTy))
1703 assert(0 && "Cannot promote Legal Integer SETCC yet");
1704 else {
1705 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1706 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1707 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001708 Tmp1 = LegalizeOp(Tmp1);
1709 Tmp2 = LegalizeOp(Tmp2);
1710 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001711 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001712 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001713 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001714 case TargetLowering::Expand:
1715 // Expand a setcc node into a select_cc of the same condition, lhs, and
1716 // rhs that selects between const 1 (true) and const 0 (false).
1717 MVT::ValueType VT = Node->getValueType(0);
1718 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1719 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1720 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001721 break;
1722 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001723 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001724 case ISD::MEMSET:
1725 case ISD::MEMCPY:
1726 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001727 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001728 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1729
1730 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1731 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1732 case Expand: assert(0 && "Cannot expand a byte!");
1733 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001734 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001735 break;
1736 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001737 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001738 break;
1739 }
1740 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001741 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001742 }
Chris Lattner272455b2005-02-02 03:44:41 +00001743
1744 SDOperand Tmp4;
1745 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001746 case Expand: {
1747 // Length is too big, just take the lo-part of the length.
1748 SDOperand HiPart;
1749 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1750 break;
1751 }
Chris Lattnere5605212005-01-28 22:29:18 +00001752 case Legal:
1753 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001754 break;
1755 case Promote:
1756 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001757 break;
1758 }
1759
1760 SDOperand Tmp5;
1761 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1762 case Expand: assert(0 && "Cannot expand this yet!");
1763 case Legal:
1764 Tmp5 = LegalizeOp(Node->getOperand(4));
1765 break;
1766 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001767 Tmp5 = PromoteOp(Node->getOperand(4));
1768 break;
1769 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001770
1771 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1772 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001773 case TargetLowering::Custom:
1774 isCustom = true;
1775 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001776 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001778 if (isCustom) {
1779 Tmp1 = TLI.LowerOperation(Result, DAG);
1780 if (Tmp1.Val) Result = Tmp1;
1781 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001782 break;
1783 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001784 // Otherwise, the target does not support this operation. Lower the
1785 // operation to an explicit libcall as appropriate.
1786 MVT::ValueType IntPtr = TLI.getPointerTy();
1787 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1788 std::vector<std::pair<SDOperand, const Type*> > Args;
1789
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001790 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001791 if (Node->getOpcode() == ISD::MEMSET) {
1792 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001793 // Extend the (previously legalized) ubyte argument to be an int value
1794 // for the call.
1795 if (Tmp3.getValueType() > MVT::i32)
1796 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1797 else
1798 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001799 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1800 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1801
1802 FnName = "memset";
1803 } else if (Node->getOpcode() == ISD::MEMCPY ||
1804 Node->getOpcode() == ISD::MEMMOVE) {
1805 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1806 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1807 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1808 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1809 } else {
1810 assert(0 && "Unknown op!");
1811 }
Chris Lattner45982da2005-05-12 16:53:42 +00001812
Chris Lattnere1bd8222005-01-11 05:57:22 +00001813 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001814 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001815 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001816 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001817 break;
1818 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001819 }
1820 break;
1821 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001822
Chris Lattner5b359c62005-04-02 04:00:59 +00001823 case ISD::SHL_PARTS:
1824 case ISD::SRA_PARTS:
1825 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001826 std::vector<SDOperand> Ops;
1827 bool Changed = false;
1828 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1829 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1830 Changed |= Ops.back() != Node->getOperand(i);
1831 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001832 if (Changed)
1833 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001834
Evan Cheng05a2d562006-01-09 18:31:59 +00001835 switch (TLI.getOperationAction(Node->getOpcode(),
1836 Node->getValueType(0))) {
1837 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001838 case TargetLowering::Legal: break;
1839 case TargetLowering::Custom:
1840 Tmp1 = TLI.LowerOperation(Result, DAG);
1841 if (Tmp1.Val) {
1842 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001843 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001844 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001845 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1846 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001847 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001848 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001849 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001850 return RetVal;
1851 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001852 break;
1853 }
1854
Chris Lattner2c8086f2005-04-02 05:00:07 +00001855 // Since these produce multiple values, make sure to remember that we
1856 // legalized all of them.
1857 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1858 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1859 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001860 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001861
1862 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001863 case ISD::ADD:
1864 case ISD::SUB:
1865 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001866 case ISD::MULHS:
1867 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001868 case ISD::UDIV:
1869 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001870 case ISD::AND:
1871 case ISD::OR:
1872 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001873 case ISD::SHL:
1874 case ISD::SRL:
1875 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001876 case ISD::FADD:
1877 case ISD::FSUB:
1878 case ISD::FMUL:
1879 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001880 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001881 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1882 case Expand: assert(0 && "Not possible");
1883 case Legal:
1884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1885 break;
1886 case Promote:
1887 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1888 break;
1889 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001890
1891 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001892
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001893 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001894 default: assert(0 && "Operation not supported");
1895 case TargetLowering::Legal: break;
1896 case TargetLowering::Custom:
1897 Tmp1 = TLI.LowerOperation(Result, DAG);
1898 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001899 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001900 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001901 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001902
1903 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1904 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1905 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1906 case Expand: assert(0 && "Not possible");
1907 case Legal:
1908 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1909 break;
1910 case Promote:
1911 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1912 break;
1913 }
1914
1915 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1916
1917 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1918 default: assert(0 && "Operation not supported");
1919 case TargetLowering::Custom:
1920 Tmp1 = TLI.LowerOperation(Result, DAG);
1921 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001922 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001923 case TargetLowering::Legal: break;
1924 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001925 // If this target supports fabs/fneg natively, do this efficiently.
1926 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1927 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1928 // Get the sign bit of the RHS.
1929 MVT::ValueType IVT =
1930 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1931 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1932 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1933 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1934 // Get the absolute value of the result.
1935 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1936 // Select between the nabs and abs value based on the sign bit of
1937 // the input.
1938 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1939 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1940 AbsVal),
1941 AbsVal);
1942 Result = LegalizeOp(Result);
1943 break;
1944 }
1945
1946 // Otherwise, do bitwise ops!
1947
1948 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001949 const char *FnName;
1950 if (Node->getValueType(0) == MVT::f32) {
1951 FnName = "copysignf";
1952 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1953 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1954 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1955 } else {
1956 FnName = "copysign";
1957 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1958 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1959 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1960 }
1961 SDOperand Dummy;
1962 Result = ExpandLibCall(FnName, Node, Dummy);
1963 break;
1964 }
1965 break;
1966
Nate Begeman551bf3f2006-02-17 05:43:56 +00001967 case ISD::ADDC:
1968 case ISD::SUBC:
1969 Tmp1 = LegalizeOp(Node->getOperand(0));
1970 Tmp2 = LegalizeOp(Node->getOperand(1));
1971 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1972 // Since this produces two values, make sure to remember that we legalized
1973 // both of them.
1974 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1975 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1976 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001977
Nate Begeman551bf3f2006-02-17 05:43:56 +00001978 case ISD::ADDE:
1979 case ISD::SUBE:
1980 Tmp1 = LegalizeOp(Node->getOperand(0));
1981 Tmp2 = LegalizeOp(Node->getOperand(1));
1982 Tmp3 = LegalizeOp(Node->getOperand(2));
1983 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1984 // Since this produces two values, make sure to remember that we legalized
1985 // both of them.
1986 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1987 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1988 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001989
Nate Begeman419f8b62005-10-18 00:27:41 +00001990 case ISD::BUILD_PAIR: {
1991 MVT::ValueType PairTy = Node->getValueType(0);
1992 // TODO: handle the case where the Lo and Hi operands are not of legal type
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1994 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1995 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001996 case TargetLowering::Promote:
1997 case TargetLowering::Custom:
1998 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001999 case TargetLowering::Legal:
2000 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2001 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2002 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002003 case TargetLowering::Expand:
2004 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2005 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2006 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2007 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2008 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002009 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002010 break;
2011 }
2012 break;
2013 }
2014
Nate Begemanc105e192005-04-06 00:23:54 +00002015 case ISD::UREM:
2016 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002017 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002018 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2019 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002020
Nate Begemanc105e192005-04-06 00:23:54 +00002021 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002022 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2023 case TargetLowering::Custom:
2024 isCustom = true;
2025 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002026 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002027 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002028 if (isCustom) {
2029 Tmp1 = TLI.LowerOperation(Result, DAG);
2030 if (Tmp1.Val) Result = Tmp1;
2031 }
Nate Begemanc105e192005-04-06 00:23:54 +00002032 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002033 case TargetLowering::Expand:
2034 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002035 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002036 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002037 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002038 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2039 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2040 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2041 } else {
2042 // Floating point mod -> fmod libcall.
2043 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2044 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002045 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002046 }
2047 break;
2048 }
2049 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002050 case ISD::VAARG: {
2051 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2052 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2053
Chris Lattner5c62f332006-01-28 07:42:08 +00002054 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002055 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2056 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002057 case TargetLowering::Custom:
2058 isCustom = true;
2059 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002060 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2062 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002063 Tmp1 = Result.getValue(1);
2064
2065 if (isCustom) {
2066 Tmp2 = TLI.LowerOperation(Result, DAG);
2067 if (Tmp2.Val) {
2068 Result = LegalizeOp(Tmp2);
2069 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2070 }
2071 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002072 break;
2073 case TargetLowering::Expand: {
2074 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2075 Node->getOperand(2));
2076 // Increment the pointer, VAList, to the next vaarg
2077 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2078 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2079 TLI.getPointerTy()));
2080 // Store the incremented VAList to the legalized pointer
2081 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2082 Node->getOperand(2));
2083 // Load the actual argument out of the pointer VAList
2084 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002085 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002086 Result = LegalizeOp(Result);
2087 break;
2088 }
2089 }
2090 // Since VAARG produces two values, make sure to remember that we
2091 // legalized both of them.
2092 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002093 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2094 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002095 }
2096
2097 case ISD::VACOPY:
2098 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2099 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2100 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2101
2102 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2103 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002104 case TargetLowering::Custom:
2105 isCustom = true;
2106 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002107 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002108 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2109 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002110 if (isCustom) {
2111 Tmp1 = TLI.LowerOperation(Result, DAG);
2112 if (Tmp1.Val) Result = Tmp1;
2113 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002114 break;
2115 case TargetLowering::Expand:
2116 // This defaults to loading a pointer from the input and storing it to the
2117 // output, returning the chain.
2118 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2119 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2120 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002121 break;
2122 }
2123 break;
2124
2125 case ISD::VAEND:
2126 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2127 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2128
2129 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2130 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002131 case TargetLowering::Custom:
2132 isCustom = true;
2133 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002134 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002135 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002136 if (isCustom) {
2137 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2138 if (Tmp1.Val) Result = Tmp1;
2139 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002140 break;
2141 case TargetLowering::Expand:
2142 Result = Tmp1; // Default to a no-op, return the chain
2143 break;
2144 }
2145 break;
2146
2147 case ISD::VASTART:
2148 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2149 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2150
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002151 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2152
Nate Begemanacc398c2006-01-25 18:21:52 +00002153 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2154 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002155 case TargetLowering::Legal: break;
2156 case TargetLowering::Custom:
2157 Tmp1 = TLI.LowerOperation(Result, DAG);
2158 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002159 break;
2160 }
2161 break;
2162
Nate Begeman35ef9132006-01-11 21:21:00 +00002163 case ISD::ROTL:
2164 case ISD::ROTR:
2165 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2166 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002167
2168 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2169 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002170 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002171 break;
2172
Nate Begemand88fc032006-01-14 03:14:10 +00002173 case ISD::BSWAP:
2174 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2175 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002176 case TargetLowering::Custom:
2177 assert(0 && "Cannot custom legalize this yet!");
2178 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002179 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002180 break;
2181 case TargetLowering::Promote: {
2182 MVT::ValueType OVT = Tmp1.getValueType();
2183 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2184 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002185
Chris Lattner456a93a2006-01-28 07:39:30 +00002186 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2187 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2188 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2189 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2190 break;
2191 }
2192 case TargetLowering::Expand:
2193 Result = ExpandBSWAP(Tmp1);
2194 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002195 }
2196 break;
2197
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002198 case ISD::CTPOP:
2199 case ISD::CTTZ:
2200 case ISD::CTLZ:
2201 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2202 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002203 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002204 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002205 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002206 break;
2207 case TargetLowering::Promote: {
2208 MVT::ValueType OVT = Tmp1.getValueType();
2209 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002210
2211 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002212 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2213 // Perform the larger operation, then subtract if needed.
2214 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002215 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002216 case ISD::CTPOP:
2217 Result = Tmp1;
2218 break;
2219 case ISD::CTTZ:
2220 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002221 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2222 DAG.getConstant(getSizeInBits(NVT), NVT),
2223 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002224 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002225 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2226 break;
2227 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002228 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002229 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2230 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002231 getSizeInBits(OVT), NVT));
2232 break;
2233 }
2234 break;
2235 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002236 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002237 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002238 break;
2239 }
2240 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002241
Chris Lattner2c8086f2005-04-02 05:00:07 +00002242 // Unary operators
2243 case ISD::FABS:
2244 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002245 case ISD::FSQRT:
2246 case ISD::FSIN:
2247 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002248 Tmp1 = LegalizeOp(Node->getOperand(0));
2249 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002250 case TargetLowering::Promote:
2251 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002252 isCustom = true;
2253 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002254 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002255 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002256 if (isCustom) {
2257 Tmp1 = TLI.LowerOperation(Result, DAG);
2258 if (Tmp1.Val) Result = Tmp1;
2259 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002260 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002261 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002262 switch (Node->getOpcode()) {
2263 default: assert(0 && "Unreachable!");
2264 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002265 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2266 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002267 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002268 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002269 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002270 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2271 MVT::ValueType VT = Node->getValueType(0);
2272 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002273 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002274 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2275 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002276 break;
2277 }
2278 case ISD::FSQRT:
2279 case ISD::FSIN:
2280 case ISD::FCOS: {
2281 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002282 const char *FnName = 0;
2283 switch(Node->getOpcode()) {
2284 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2285 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2286 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2287 default: assert(0 && "Unreachable!");
2288 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002289 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002290 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002291 break;
2292 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002293 }
2294 break;
2295 }
2296 break;
Chris Lattner35481892005-12-23 00:16:34 +00002297
2298 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002299 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002300 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002301 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002302 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2303 Node->getOperand(0).getValueType())) {
2304 default: assert(0 && "Unknown operation action!");
2305 case TargetLowering::Expand:
2306 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2307 break;
2308 case TargetLowering::Legal:
2309 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002310 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002311 break;
2312 }
2313 }
2314 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002315 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002316 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002317 case ISD::UINT_TO_FP: {
2318 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002319 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2320 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002321 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002322 Node->getOperand(0).getValueType())) {
2323 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002324 case TargetLowering::Custom:
2325 isCustom = true;
2326 // FALLTHROUGH
2327 case TargetLowering::Legal:
2328 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002329 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002330 if (isCustom) {
2331 Tmp1 = TLI.LowerOperation(Result, DAG);
2332 if (Tmp1.Val) Result = Tmp1;
2333 }
2334 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002335 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002336 Result = ExpandLegalINT_TO_FP(isSigned,
2337 LegalizeOp(Node->getOperand(0)),
2338 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002339 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002340 case TargetLowering::Promote:
2341 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2342 Node->getValueType(0),
2343 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002344 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002345 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002346 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002347 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002348 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2349 Node->getValueType(0), Node->getOperand(0));
2350 break;
2351 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002352 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002353 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002354 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2355 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002356 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002357 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2358 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002359 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002360 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2361 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002362 break;
2363 }
2364 break;
2365 }
2366 case ISD::TRUNCATE:
2367 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2368 case Legal:
2369 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002370 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002371 break;
2372 case Expand:
2373 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2374
2375 // Since the result is legal, we should just be able to truncate the low
2376 // part of the source.
2377 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2378 break;
2379 case Promote:
2380 Result = PromoteOp(Node->getOperand(0));
2381 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2382 break;
2383 }
2384 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002385
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002386 case ISD::FP_TO_SINT:
2387 case ISD::FP_TO_UINT:
2388 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2389 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002390 Tmp1 = LegalizeOp(Node->getOperand(0));
2391
Chris Lattner1618beb2005-07-29 00:11:56 +00002392 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2393 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002394 case TargetLowering::Custom:
2395 isCustom = true;
2396 // FALLTHROUGH
2397 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002398 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002399 if (isCustom) {
2400 Tmp1 = TLI.LowerOperation(Result, DAG);
2401 if (Tmp1.Val) Result = Tmp1;
2402 }
2403 break;
2404 case TargetLowering::Promote:
2405 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2406 Node->getOpcode() == ISD::FP_TO_SINT);
2407 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002408 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002409 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2410 SDOperand True, False;
2411 MVT::ValueType VT = Node->getOperand(0).getValueType();
2412 MVT::ValueType NVT = Node->getValueType(0);
2413 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2414 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2415 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2416 Node->getOperand(0), Tmp2, ISD::SETLT);
2417 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2418 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002419 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002420 Tmp2));
2421 False = DAG.getNode(ISD::XOR, NVT, False,
2422 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002423 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2424 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002425 } else {
2426 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2427 }
2428 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002429 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002430 break;
2431 case Expand:
2432 assert(0 && "Shouldn't need to expand other operators here!");
2433 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002434 Tmp1 = PromoteOp(Node->getOperand(0));
2435 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2436 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002437 break;
2438 }
2439 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002440
Chris Lattner13c78e22005-09-02 00:18:10 +00002441 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002442 case ISD::ZERO_EXTEND:
2443 case ISD::SIGN_EXTEND:
2444 case ISD::FP_EXTEND:
2445 case ISD::FP_ROUND:
2446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002447 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002448 case Legal:
2449 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002450 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002451 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002452 case Promote:
2453 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002454 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002455 Tmp1 = PromoteOp(Node->getOperand(0));
2456 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002457 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002458 case ISD::ZERO_EXTEND:
2459 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002460 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002461 Result = DAG.getZeroExtendInReg(Result,
2462 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002463 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002464 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002465 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002466 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002467 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002468 Result,
2469 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002470 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002471 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002472 Result = PromoteOp(Node->getOperand(0));
2473 if (Result.getValueType() != Op.getValueType())
2474 // Dynamically dead while we have only 2 FP types.
2475 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2476 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002477 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002478 Result = PromoteOp(Node->getOperand(0));
2479 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2480 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002481 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002482 }
2483 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002484 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002485 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002486 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002487 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002488
2489 // If this operation is not supported, convert it to a shl/shr or load/store
2490 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002491 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2492 default: assert(0 && "This action not supported for this op yet!");
2493 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002494 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002495 break;
2496 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002497 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002498 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002499 // NOTE: we could fall back on load/store here too for targets without
2500 // SAR. However, it is doubtful that any exist.
2501 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2502 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002503 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002504 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2505 Node->getOperand(0), ShiftCst);
2506 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2507 Result, ShiftCst);
2508 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2509 // The only way we can lower this is to turn it into a STORETRUNC,
2510 // EXTLOAD pair, targetting a temporary location (a stack slot).
2511
2512 // NOTE: there is a choice here between constantly creating new stack
2513 // slots and always reusing the same one. We currently always create
2514 // new ones, as reuse may inhibit scheduling.
2515 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2516 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2517 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2518 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002519 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002520 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2521 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2522 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002523 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002524 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002525 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2526 Result, StackSlot, DAG.getSrcValue(NULL),
2527 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002528 } else {
2529 assert(0 && "Unknown op");
2530 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002531 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002532 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002533 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002534 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002535 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002536
2537 // Make sure that the generated code is itself legal.
2538 if (Result != Op)
2539 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002540
Chris Lattner45982da2005-05-12 16:53:42 +00002541 // Note that LegalizeOp may be reentered even from single-use nodes, which
2542 // means that we always must cache transformed nodes.
2543 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002544 return Result;
2545}
2546
Chris Lattner8b6fa222005-01-15 22:16:26 +00002547/// PromoteOp - Given an operation that produces a value in an invalid type,
2548/// promote it to compute the value into a larger type. The produced value will
2549/// have the correct bits for the low portion of the register, but no guarantee
2550/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002551SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2552 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002553 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002554 assert(getTypeAction(VT) == Promote &&
2555 "Caller should expand or legalize operands that are not promotable!");
2556 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2557 "Cannot promote to smaller type!");
2558
Chris Lattner03c85462005-01-15 05:21:40 +00002559 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002560 SDOperand Result;
2561 SDNode *Node = Op.Val;
2562
Chris Lattner6fdcb252005-09-02 20:32:45 +00002563 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2564 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002565
Chris Lattner03c85462005-01-15 05:21:40 +00002566 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002567 case ISD::CopyFromReg:
2568 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002569 default:
2570 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2571 assert(0 && "Do not know how to promote this operator!");
2572 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002573 case ISD::UNDEF:
2574 Result = DAG.getNode(ISD::UNDEF, NVT);
2575 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002576 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002577 if (VT != MVT::i1)
2578 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2579 else
2580 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002581 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2582 break;
2583 case ISD::ConstantFP:
2584 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2585 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2586 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002587
Chris Lattner82fbfb62005-01-18 02:59:52 +00002588 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002589 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002590 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2591 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002592 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002593
2594 case ISD::TRUNCATE:
2595 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2596 case Legal:
2597 Result = LegalizeOp(Node->getOperand(0));
2598 assert(Result.getValueType() >= NVT &&
2599 "This truncation doesn't make sense!");
2600 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2601 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2602 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002603 case Promote:
2604 // The truncation is not required, because we don't guarantee anything
2605 // about high bits anyway.
2606 Result = PromoteOp(Node->getOperand(0));
2607 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002608 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002609 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2610 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002611 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002612 }
2613 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002614 case ISD::SIGN_EXTEND:
2615 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002616 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002617 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2618 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2619 case Legal:
2620 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002621 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002622 break;
2623 case Promote:
2624 // Promote the reg if it's smaller.
2625 Result = PromoteOp(Node->getOperand(0));
2626 // The high bits are not guaranteed to be anything. Insert an extend.
2627 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002628 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002629 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002630 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002631 Result = DAG.getZeroExtendInReg(Result,
2632 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002633 break;
2634 }
2635 break;
Chris Lattner35481892005-12-23 00:16:34 +00002636 case ISD::BIT_CONVERT:
2637 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2638 Result = PromoteOp(Result);
2639 break;
2640
Chris Lattner8b6fa222005-01-15 22:16:26 +00002641 case ISD::FP_EXTEND:
2642 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2643 case ISD::FP_ROUND:
2644 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2645 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2646 case Promote: assert(0 && "Unreachable with 2 FP types!");
2647 case Legal:
2648 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002649 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002650 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002651 break;
2652 }
2653 break;
2654
2655 case ISD::SINT_TO_FP:
2656 case ISD::UINT_TO_FP:
2657 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2658 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002659 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002660 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002661 break;
2662
2663 case Promote:
2664 Result = PromoteOp(Node->getOperand(0));
2665 if (Node->getOpcode() == ISD::SINT_TO_FP)
2666 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002667 Result,
2668 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002669 else
Chris Lattner23993562005-04-13 02:38:47 +00002670 Result = DAG.getZeroExtendInReg(Result,
2671 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002672 // No extra round required here.
2673 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002674 break;
2675 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002676 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2677 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002678 // Round if we cannot tolerate excess precision.
2679 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002680 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2681 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002682 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002683 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002684 break;
2685
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002686 case ISD::SIGN_EXTEND_INREG:
2687 Result = PromoteOp(Node->getOperand(0));
2688 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2689 Node->getOperand(1));
2690 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002691 case ISD::FP_TO_SINT:
2692 case ISD::FP_TO_UINT:
2693 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2694 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002695 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002696 break;
2697 case Promote:
2698 // The input result is prerounded, so we don't have to do anything
2699 // special.
2700 Tmp1 = PromoteOp(Node->getOperand(0));
2701 break;
2702 case Expand:
2703 assert(0 && "not implemented");
2704 }
Nate Begemand2558e32005-08-14 01:20:53 +00002705 // If we're promoting a UINT to a larger size, check to see if the new node
2706 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2707 // we can use that instead. This allows us to generate better code for
2708 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2709 // legal, such as PowerPC.
2710 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002711 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002712 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2713 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002714 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2715 } else {
2716 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2717 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002718 break;
2719
Chris Lattner2c8086f2005-04-02 05:00:07 +00002720 case ISD::FABS:
2721 case ISD::FNEG:
2722 Tmp1 = PromoteOp(Node->getOperand(0));
2723 assert(Tmp1.getValueType() == NVT);
2724 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2725 // NOTE: we do not have to do any extra rounding here for
2726 // NoExcessFPPrecision, because we know the input will have the appropriate
2727 // precision, and these operations don't modify precision at all.
2728 break;
2729
Chris Lattnerda6ba872005-04-28 21:44:33 +00002730 case ISD::FSQRT:
2731 case ISD::FSIN:
2732 case ISD::FCOS:
2733 Tmp1 = PromoteOp(Node->getOperand(0));
2734 assert(Tmp1.getValueType() == NVT);
2735 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002736 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002737 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2738 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002739 break;
2740
Chris Lattner03c85462005-01-15 05:21:40 +00002741 case ISD::AND:
2742 case ISD::OR:
2743 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002744 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002745 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002746 case ISD::MUL:
2747 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002748 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002749 // that too is okay if they are integer operations.
2750 Tmp1 = PromoteOp(Node->getOperand(0));
2751 Tmp2 = PromoteOp(Node->getOperand(1));
2752 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2753 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002754 break;
2755 case ISD::FADD:
2756 case ISD::FSUB:
2757 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002758 Tmp1 = PromoteOp(Node->getOperand(0));
2759 Tmp2 = PromoteOp(Node->getOperand(1));
2760 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2761 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2762
2763 // Floating point operations will give excess precision that we may not be
2764 // able to tolerate. If we DO allow excess precision, just leave it,
2765 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002766 // FIXME: Why would we need to round FP ops more than integer ones?
2767 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002768 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002769 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2770 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002771 break;
2772
Chris Lattner8b6fa222005-01-15 22:16:26 +00002773 case ISD::SDIV:
2774 case ISD::SREM:
2775 // These operators require that their input be sign extended.
2776 Tmp1 = PromoteOp(Node->getOperand(0));
2777 Tmp2 = PromoteOp(Node->getOperand(1));
2778 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002779 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2780 DAG.getValueType(VT));
2781 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2782 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002783 }
2784 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2785
2786 // Perform FP_ROUND: this is probably overly pessimistic.
2787 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002788 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2789 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002790 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002791 case ISD::FDIV:
2792 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002793 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002794 // These operators require that their input be fp extended.
2795 Tmp1 = PromoteOp(Node->getOperand(0));
2796 Tmp2 = PromoteOp(Node->getOperand(1));
2797 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2798
2799 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002800 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002801 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2802 DAG.getValueType(VT));
2803 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002804
2805 case ISD::UDIV:
2806 case ISD::UREM:
2807 // These operators require that their input be zero extended.
2808 Tmp1 = PromoteOp(Node->getOperand(0));
2809 Tmp2 = PromoteOp(Node->getOperand(1));
2810 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002811 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2812 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002813 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2814 break;
2815
2816 case ISD::SHL:
2817 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002818 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002819 break;
2820 case ISD::SRA:
2821 // The input value must be properly sign extended.
2822 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002823 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2824 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002825 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002826 break;
2827 case ISD::SRL:
2828 // The input value must be properly zero extended.
2829 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002830 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002831 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002832 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002833
2834 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002835 Tmp1 = Node->getOperand(0); // Get the chain.
2836 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002837 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2838 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2839 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2840 } else {
2841 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2842 Node->getOperand(2));
2843 // Increment the pointer, VAList, to the next vaarg
2844 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2845 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2846 TLI.getPointerTy()));
2847 // Store the incremented VAList to the legalized pointer
2848 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2849 Node->getOperand(2));
2850 // Load the actual argument out of the pointer VAList
2851 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2852 DAG.getSrcValue(0), VT);
2853 }
2854 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002855 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002856 break;
2857
Chris Lattner03c85462005-01-15 05:21:40 +00002858 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002859 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2860 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002861 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002862 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002863 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002864 case ISD::SEXTLOAD:
2865 case ISD::ZEXTLOAD:
2866 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002867 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2868 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002869 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002870 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002871 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002872 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002873 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002874 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2875 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002876 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002877 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002878 case ISD::SELECT_CC:
2879 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2880 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2881 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002882 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002883 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002884 case ISD::BSWAP:
2885 Tmp1 = Node->getOperand(0);
2886 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2887 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2888 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2889 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2890 TLI.getShiftAmountTy()));
2891 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002892 case ISD::CTPOP:
2893 case ISD::CTTZ:
2894 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002895 // Zero extend the argument
2896 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002897 // Perform the larger operation, then subtract if needed.
2898 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002899 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002900 case ISD::CTPOP:
2901 Result = Tmp1;
2902 break;
2903 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002904 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002905 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002906 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002907 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002908 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002909 break;
2910 case ISD::CTLZ:
2911 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002912 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2913 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002914 getSizeInBits(VT), NVT));
2915 break;
2916 }
2917 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002918 }
2919
2920 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002921
2922 // Make sure the result is itself legal.
2923 Result = LegalizeOp(Result);
2924
2925 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002926 AddPromotedOperand(Op, Result);
2927 return Result;
2928}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002929
Nate Begeman750ac1b2006-02-01 07:19:44 +00002930/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2931/// with condition CC on the current target. This usually involves legalizing
2932/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2933/// there may be no choice but to create a new SetCC node to represent the
2934/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2935/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2936void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2937 SDOperand &RHS,
2938 SDOperand &CC) {
2939 SDOperand Tmp1, Tmp2, Result;
2940
2941 switch (getTypeAction(LHS.getValueType())) {
2942 case Legal:
2943 Tmp1 = LegalizeOp(LHS); // LHS
2944 Tmp2 = LegalizeOp(RHS); // RHS
2945 break;
2946 case Promote:
2947 Tmp1 = PromoteOp(LHS); // LHS
2948 Tmp2 = PromoteOp(RHS); // RHS
2949
2950 // If this is an FP compare, the operands have already been extended.
2951 if (MVT::isInteger(LHS.getValueType())) {
2952 MVT::ValueType VT = LHS.getValueType();
2953 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2954
2955 // Otherwise, we have to insert explicit sign or zero extends. Note
2956 // that we could insert sign extends for ALL conditions, but zero extend
2957 // is cheaper on many machines (an AND instead of two shifts), so prefer
2958 // it.
2959 switch (cast<CondCodeSDNode>(CC)->get()) {
2960 default: assert(0 && "Unknown integer comparison!");
2961 case ISD::SETEQ:
2962 case ISD::SETNE:
2963 case ISD::SETUGE:
2964 case ISD::SETUGT:
2965 case ISD::SETULE:
2966 case ISD::SETULT:
2967 // ALL of these operations will work if we either sign or zero extend
2968 // the operands (including the unsigned comparisons!). Zero extend is
2969 // usually a simpler/cheaper operation, so prefer it.
2970 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2971 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2972 break;
2973 case ISD::SETGE:
2974 case ISD::SETGT:
2975 case ISD::SETLT:
2976 case ISD::SETLE:
2977 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2978 DAG.getValueType(VT));
2979 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2980 DAG.getValueType(VT));
2981 break;
2982 }
2983 }
2984 break;
2985 case Expand:
2986 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2987 ExpandOp(LHS, LHSLo, LHSHi);
2988 ExpandOp(RHS, RHSLo, RHSHi);
2989 switch (cast<CondCodeSDNode>(CC)->get()) {
2990 case ISD::SETEQ:
2991 case ISD::SETNE:
2992 if (RHSLo == RHSHi)
2993 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2994 if (RHSCST->isAllOnesValue()) {
2995 // Comparison to -1.
2996 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2997 Tmp2 = RHSLo;
2998 break;
2999 }
3000
3001 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3002 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3003 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3004 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3005 break;
3006 default:
3007 // If this is a comparison of the sign bit, just look at the top part.
3008 // X > -1, x < 0
3009 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3010 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3011 CST->getValue() == 0) || // X < 0
3012 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3013 CST->isAllOnesValue())) { // X > -1
3014 Tmp1 = LHSHi;
3015 Tmp2 = RHSHi;
3016 break;
3017 }
3018
3019 // FIXME: This generated code sucks.
3020 ISD::CondCode LowCC;
3021 switch (cast<CondCodeSDNode>(CC)->get()) {
3022 default: assert(0 && "Unknown integer setcc!");
3023 case ISD::SETLT:
3024 case ISD::SETULT: LowCC = ISD::SETULT; break;
3025 case ISD::SETGT:
3026 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3027 case ISD::SETLE:
3028 case ISD::SETULE: LowCC = ISD::SETULE; break;
3029 case ISD::SETGE:
3030 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3031 }
3032
3033 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3034 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3035 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3036
3037 // NOTE: on targets without efficient SELECT of bools, we can always use
3038 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3039 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3040 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3041 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3042 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3043 Result, Tmp1, Tmp2));
3044 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003045 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003046 }
3047 }
3048 LHS = Tmp1;
3049 RHS = Tmp2;
3050}
3051
Chris Lattner35481892005-12-23 00:16:34 +00003052/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003053/// The resultant code need not be legal. Note that SrcOp is the input operand
3054/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003055SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3056 SDOperand SrcOp) {
3057 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003058 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003059
3060 // Emit a store to the stack slot.
3061 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003062 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003063 // Result is a load from the stack slot.
3064 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3065}
3066
Chris Lattnerce872152006-03-19 06:31:19 +00003067/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3068/// support the operation, but do support the resultant packed vector type.
3069SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3070
3071 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003072 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003073 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003074 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003075 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003076 std::map<SDOperand, std::vector<unsigned> > Values;
3077 Values[SplatValue].push_back(0);
3078 for (unsigned i = 1; i < NumElems; ++i) {
3079 SDOperand V = Node->getOperand(i);
3080 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3081 if (I != Values.end())
3082 I->second.push_back(i);
3083 else
3084 Values[V].push_back(i);
3085 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003086 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003087 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003088 SplatValue = SDOperand(0,0);
Chris Lattnerce872152006-03-19 06:31:19 +00003089 }
3090
3091 if (isOnlyLowElement) {
3092 // If the low element is an undef too, then this whole things is an undef.
3093 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3094 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3095 // Otherwise, turn this into a scalar_to_vector node.
3096 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3097 Node->getOperand(0));
3098 }
3099
Chris Lattner87100e02006-03-20 01:52:29 +00003100 if (SplatValue.Val) { // Splat of one value?
3101 // Build the shuffle constant vector: <0, 0, 0, 0>
3102 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003103 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003104 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003105 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner87100e02006-03-20 01:52:29 +00003106 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3107
3108 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3109 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3110 // Get the splatted value into the low element of a vector register.
3111 SDOperand LowValVec =
3112 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3113
3114 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3115 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3116 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3117 SplatMask);
3118 }
3119 }
3120
Chris Lattnerce872152006-03-19 06:31:19 +00003121 // If the elements are all constants, turn this into a load from the constant
3122 // pool.
3123 bool isConstant = true;
3124 for (SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
3125 I != E; ++I) {
3126 if (!isa<ConstantFPSDNode>(I) && !isa<ConstantSDNode>(I) &&
3127 I->getOpcode() != ISD::UNDEF) {
3128 isConstant = false;
3129 break;
3130 }
3131 }
3132
3133 // Create a ConstantPacked, and put it in the constant pool.
3134 if (isConstant) {
3135 MVT::ValueType VT = Node->getValueType(0);
3136 const Type *OpNTy =
3137 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3138 std::vector<Constant*> CV;
Evan Cheng033e6812006-03-24 01:17:21 +00003139 for (unsigned i = 0, e = NumElems; i != e; ++i) {
Chris Lattnerce872152006-03-19 06:31:19 +00003140 if (ConstantFPSDNode *V =
3141 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3142 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3143 } else if (ConstantSDNode *V =
3144 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3145 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3146 } else {
3147 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3148 CV.push_back(UndefValue::get(OpNTy));
3149 }
3150 }
3151 Constant *CP = ConstantPacked::get(CV);
3152 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3153 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3154 DAG.getSrcValue(NULL));
3155 }
Evan Cheng033e6812006-03-24 01:17:21 +00003156
3157 // If there are only two unique elements, we may be able to turn this into a
3158 // vector shuffle.
3159 if (Values.size() == 2) {
3160 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3161 MVT::ValueType MaskVT =
3162 MVT::getIntVectorWithNumElements(NumElems);
3163 std::vector<SDOperand> MaskVec(NumElems);
3164 unsigned i = 0;
3165 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3166 E = Values.end(); I != E; ++I) {
3167 for (std::vector<unsigned>::iterator II = I->second.begin(),
3168 EE = I->second.end(); II != EE; ++II)
3169 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3170 i += NumElems;
3171 }
3172 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3173
3174 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3175 if (TLI.isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
3176 std::vector<SDOperand> Ops;
3177 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3178 E = Values.end(); I != E; ++I) {
3179 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3180 I->first);
3181 Ops.push_back(Op);
3182 }
3183 Ops.push_back(ShuffleMask);
3184
3185 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3186 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3187 }
3188 }
Chris Lattnerce872152006-03-19 06:31:19 +00003189
3190 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3191 // aligned object on the stack, store each element into it, then load
3192 // the result as a vector.
3193 MVT::ValueType VT = Node->getValueType(0);
3194 // Create the stack frame object.
3195 SDOperand FIPtr = CreateStackTemporary(VT);
3196
3197 // Emit a store of each element to the stack slot.
3198 std::vector<SDOperand> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003199 unsigned TypeByteSize =
3200 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3201 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3202 // Store (in the right endianness) the elements to memory.
3203 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3204 // Ignore undef elements.
3205 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3206
Chris Lattner841c8822006-03-22 01:46:54 +00003207 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003208
3209 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3210 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3211
3212 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3213 Node->getOperand(i), Idx,
3214 DAG.getSrcValue(NULL)));
3215 }
3216
3217 SDOperand StoreChain;
3218 if (!Stores.empty()) // Not all undef elements?
3219 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3220 else
3221 StoreChain = DAG.getEntryNode();
3222
3223 // Result is a load from the stack slot.
3224 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3225}
3226
3227/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3228/// specified value type.
3229SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3230 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3231 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3232 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3233 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3234}
3235
Chris Lattner5b359c62005-04-02 04:00:59 +00003236void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3237 SDOperand Op, SDOperand Amt,
3238 SDOperand &Lo, SDOperand &Hi) {
3239 // Expand the subcomponents.
3240 SDOperand LHSL, LHSH;
3241 ExpandOp(Op, LHSL, LHSH);
3242
3243 std::vector<SDOperand> Ops;
3244 Ops.push_back(LHSL);
3245 Ops.push_back(LHSH);
3246 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003247 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003248 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003249 Hi = Lo.getValue(1);
3250}
3251
3252
Chris Lattnere34b3962005-01-19 04:19:40 +00003253/// ExpandShift - Try to find a clever way to expand this shift operation out to
3254/// smaller elements. If we can't find a way that is more efficient than a
3255/// libcall on this target, return false. Otherwise, return true with the
3256/// low-parts expanded into Lo and Hi.
3257bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3258 SDOperand &Lo, SDOperand &Hi) {
3259 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3260 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003261
Chris Lattnere34b3962005-01-19 04:19:40 +00003262 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003263 SDOperand ShAmt = LegalizeOp(Amt);
3264 MVT::ValueType ShTy = ShAmt.getValueType();
3265 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3266 unsigned NVTBits = MVT::getSizeInBits(NVT);
3267
3268 // Handle the case when Amt is an immediate. Other cases are currently broken
3269 // and are disabled.
3270 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3271 unsigned Cst = CN->getValue();
3272 // Expand the incoming operand to be shifted, so that we have its parts
3273 SDOperand InL, InH;
3274 ExpandOp(Op, InL, InH);
3275 switch(Opc) {
3276 case ISD::SHL:
3277 if (Cst > VTBits) {
3278 Lo = DAG.getConstant(0, NVT);
3279 Hi = DAG.getConstant(0, NVT);
3280 } else if (Cst > NVTBits) {
3281 Lo = DAG.getConstant(0, NVT);
3282 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003283 } else if (Cst == NVTBits) {
3284 Lo = DAG.getConstant(0, NVT);
3285 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003286 } else {
3287 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3288 Hi = DAG.getNode(ISD::OR, NVT,
3289 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3290 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3291 }
3292 return true;
3293 case ISD::SRL:
3294 if (Cst > VTBits) {
3295 Lo = DAG.getConstant(0, NVT);
3296 Hi = DAG.getConstant(0, NVT);
3297 } else if (Cst > NVTBits) {
3298 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3299 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003300 } else if (Cst == NVTBits) {
3301 Lo = InH;
3302 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003303 } else {
3304 Lo = DAG.getNode(ISD::OR, NVT,
3305 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3306 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3307 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3308 }
3309 return true;
3310 case ISD::SRA:
3311 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003312 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003313 DAG.getConstant(NVTBits-1, ShTy));
3314 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003315 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003316 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003317 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003318 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003319 } else if (Cst == NVTBits) {
3320 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003321 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003322 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003323 } else {
3324 Lo = DAG.getNode(ISD::OR, NVT,
3325 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3326 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3327 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3328 }
3329 return true;
3330 }
3331 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003332 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003333}
Chris Lattner77e77a62005-01-21 06:05:23 +00003334
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003335
Chris Lattner77e77a62005-01-21 06:05:23 +00003336// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3337// does not fit into a register, return the lo part and set the hi part to the
3338// by-reg argument. If it does fit into a single register, return the result
3339// and leave the Hi part unset.
3340SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3341 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003342 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3343 // The input chain to this libcall is the entry node of the function.
3344 // Legalizing the call will automatically add the previous call to the
3345 // dependence.
3346 SDOperand InChain = DAG.getEntryNode();
3347
Chris Lattner77e77a62005-01-21 06:05:23 +00003348 TargetLowering::ArgListTy Args;
3349 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3350 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3351 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3352 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3353 }
3354 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003355
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003356 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003357 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003358 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003359 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3360 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003361
Chris Lattner6831a812006-02-13 09:18:02 +00003362 // Legalize the call sequence, starting with the chain. This will advance
3363 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3364 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3365 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003366 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003367 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003368 default: assert(0 && "Unknown thing");
3369 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003370 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003371 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003372 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003373 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003374 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003375 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003376 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003377}
3378
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003379
Chris Lattner77e77a62005-01-21 06:05:23 +00003380/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3381/// destination type is legal.
3382SDOperand SelectionDAGLegalize::
3383ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003384 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003385 assert(getTypeAction(Source.getValueType()) == Expand &&
3386 "This is not an expansion!");
3387 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3388
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003389 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003390 assert(Source.getValueType() == MVT::i64 &&
3391 "This only works for 64-bit -> FP");
3392 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3393 // incoming integer is set. To handle this, we dynamically test to see if
3394 // it is set, and, if so, add a fudge factor.
3395 SDOperand Lo, Hi;
3396 ExpandOp(Source, Lo, Hi);
3397
Chris Lattner66de05b2005-05-13 04:45:13 +00003398 // If this is unsigned, and not supported, first perform the conversion to
3399 // signed, then adjust the result if the sign bit is set.
3400 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3401 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3402
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003403 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3404 DAG.getConstant(0, Hi.getValueType()),
3405 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003406 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3407 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3408 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003409 uint64_t FF = 0x5f800000ULL;
3410 if (TLI.isLittleEndian()) FF <<= 32;
3411 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003412
Chris Lattner5839bf22005-08-26 17:15:30 +00003413 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003414 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3415 SDOperand FudgeInReg;
3416 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003417 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3418 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003419 else {
3420 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003421 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3422 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003423 }
Chris Lattner473a9902005-09-29 06:44:39 +00003424 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003425 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003426
Chris Lattnera88a2602005-05-14 05:33:54 +00003427 // Check to see if the target has a custom way to lower this. If so, use it.
3428 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3429 default: assert(0 && "This action not implemented for this operation!");
3430 case TargetLowering::Legal:
3431 case TargetLowering::Expand:
3432 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003433 case TargetLowering::Custom: {
3434 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3435 Source), DAG);
3436 if (NV.Val)
3437 return LegalizeOp(NV);
3438 break; // The target decided this was legal after all
3439 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003440 }
3441
Chris Lattner13689e22005-05-12 07:00:44 +00003442 // Expand the source, then glue it back together for the call. We must expand
3443 // the source in case it is shared (this pass of legalize must traverse it).
3444 SDOperand SrcLo, SrcHi;
3445 ExpandOp(Source, SrcLo, SrcHi);
3446 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3447
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003448 const char *FnName = 0;
3449 if (DestTy == MVT::f32)
3450 FnName = "__floatdisf";
3451 else {
3452 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3453 FnName = "__floatdidf";
3454 }
Chris Lattner6831a812006-02-13 09:18:02 +00003455
3456 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3457 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003458 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003459}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003460
Chris Lattner22cde6a2006-01-28 08:25:58 +00003461/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3462/// INT_TO_FP operation of the specified operand when the target requests that
3463/// we expand it. At this point, we know that the result and operand types are
3464/// legal for the target.
3465SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3466 SDOperand Op0,
3467 MVT::ValueType DestVT) {
3468 if (Op0.getValueType() == MVT::i32) {
3469 // simple 32-bit [signed|unsigned] integer to float/double expansion
3470
3471 // get the stack frame index of a 8 byte buffer
3472 MachineFunction &MF = DAG.getMachineFunction();
3473 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3474 // get address of 8 byte buffer
3475 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3476 // word offset constant for Hi/Lo address computation
3477 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3478 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00003479 SDOperand Hi = StackSlot;
3480 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3481 if (TLI.isLittleEndian())
3482 std::swap(Hi, Lo);
3483
Chris Lattner22cde6a2006-01-28 08:25:58 +00003484 // if signed map to unsigned space
3485 SDOperand Op0Mapped;
3486 if (isSigned) {
3487 // constant used to invert sign bit (signed to unsigned mapping)
3488 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3489 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3490 } else {
3491 Op0Mapped = Op0;
3492 }
3493 // store the lo of the constructed double - based on integer input
3494 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3495 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3496 // initial hi portion of constructed double
3497 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3498 // store the hi of the constructed double - biased exponent
3499 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3500 InitialHi, Hi, DAG.getSrcValue(NULL));
3501 // load the constructed double
3502 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3503 DAG.getSrcValue(NULL));
3504 // FP constant to bias correct the final result
3505 SDOperand Bias = DAG.getConstantFP(isSigned ?
3506 BitsToDouble(0x4330000080000000ULL)
3507 : BitsToDouble(0x4330000000000000ULL),
3508 MVT::f64);
3509 // subtract the bias
3510 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3511 // final result
3512 SDOperand Result;
3513 // handle final rounding
3514 if (DestVT == MVT::f64) {
3515 // do nothing
3516 Result = Sub;
3517 } else {
3518 // if f32 then cast to f32
3519 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3520 }
3521 return Result;
3522 }
3523 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3524 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3525
3526 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3527 DAG.getConstant(0, Op0.getValueType()),
3528 ISD::SETLT);
3529 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3530 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3531 SignSet, Four, Zero);
3532
3533 // If the sign bit of the integer is set, the large number will be treated
3534 // as a negative number. To counteract this, the dynamic code adds an
3535 // offset depending on the data type.
3536 uint64_t FF;
3537 switch (Op0.getValueType()) {
3538 default: assert(0 && "Unsupported integer type!");
3539 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3540 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3541 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3542 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3543 }
3544 if (TLI.isLittleEndian()) FF <<= 32;
3545 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3546
3547 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3548 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3549 SDOperand FudgeInReg;
3550 if (DestVT == MVT::f32)
3551 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3552 DAG.getSrcValue(NULL));
3553 else {
3554 assert(DestVT == MVT::f64 && "Unexpected conversion");
3555 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3556 DAG.getEntryNode(), CPIdx,
3557 DAG.getSrcValue(NULL), MVT::f32));
3558 }
3559
3560 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3561}
3562
3563/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3564/// *INT_TO_FP operation of the specified operand when the target requests that
3565/// we promote it. At this point, we know that the result and operand types are
3566/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3567/// operation that takes a larger input.
3568SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3569 MVT::ValueType DestVT,
3570 bool isSigned) {
3571 // First step, figure out the appropriate *INT_TO_FP operation to use.
3572 MVT::ValueType NewInTy = LegalOp.getValueType();
3573
3574 unsigned OpToUse = 0;
3575
3576 // Scan for the appropriate larger type to use.
3577 while (1) {
3578 NewInTy = (MVT::ValueType)(NewInTy+1);
3579 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3580
3581 // If the target supports SINT_TO_FP of this type, use it.
3582 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3583 default: break;
3584 case TargetLowering::Legal:
3585 if (!TLI.isTypeLegal(NewInTy))
3586 break; // Can't use this datatype.
3587 // FALL THROUGH.
3588 case TargetLowering::Custom:
3589 OpToUse = ISD::SINT_TO_FP;
3590 break;
3591 }
3592 if (OpToUse) break;
3593 if (isSigned) continue;
3594
3595 // If the target supports UINT_TO_FP of this type, use it.
3596 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3597 default: break;
3598 case TargetLowering::Legal:
3599 if (!TLI.isTypeLegal(NewInTy))
3600 break; // Can't use this datatype.
3601 // FALL THROUGH.
3602 case TargetLowering::Custom:
3603 OpToUse = ISD::UINT_TO_FP;
3604 break;
3605 }
3606 if (OpToUse) break;
3607
3608 // Otherwise, try a larger type.
3609 }
3610
3611 // Okay, we found the operation and type to use. Zero extend our input to the
3612 // desired type then run the operation on it.
3613 return DAG.getNode(OpToUse, DestVT,
3614 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3615 NewInTy, LegalOp));
3616}
3617
3618/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3619/// FP_TO_*INT operation of the specified operand when the target requests that
3620/// we promote it. At this point, we know that the result and operand types are
3621/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3622/// operation that returns a larger result.
3623SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3624 MVT::ValueType DestVT,
3625 bool isSigned) {
3626 // First step, figure out the appropriate FP_TO*INT operation to use.
3627 MVT::ValueType NewOutTy = DestVT;
3628
3629 unsigned OpToUse = 0;
3630
3631 // Scan for the appropriate larger type to use.
3632 while (1) {
3633 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3634 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3635
3636 // If the target supports FP_TO_SINT returning this type, use it.
3637 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3638 default: break;
3639 case TargetLowering::Legal:
3640 if (!TLI.isTypeLegal(NewOutTy))
3641 break; // Can't use this datatype.
3642 // FALL THROUGH.
3643 case TargetLowering::Custom:
3644 OpToUse = ISD::FP_TO_SINT;
3645 break;
3646 }
3647 if (OpToUse) break;
3648
3649 // If the target supports FP_TO_UINT of this type, use it.
3650 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3651 default: break;
3652 case TargetLowering::Legal:
3653 if (!TLI.isTypeLegal(NewOutTy))
3654 break; // Can't use this datatype.
3655 // FALL THROUGH.
3656 case TargetLowering::Custom:
3657 OpToUse = ISD::FP_TO_UINT;
3658 break;
3659 }
3660 if (OpToUse) break;
3661
3662 // Otherwise, try a larger type.
3663 }
3664
3665 // Okay, we found the operation and type to use. Truncate the result of the
3666 // extended FP_TO_*INT operation to the desired size.
3667 return DAG.getNode(ISD::TRUNCATE, DestVT,
3668 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3669}
3670
3671/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3672///
3673SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3674 MVT::ValueType VT = Op.getValueType();
3675 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3676 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3677 switch (VT) {
3678 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3679 case MVT::i16:
3680 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3681 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3682 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3683 case MVT::i32:
3684 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3685 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3686 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3687 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3688 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3689 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3690 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3691 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3692 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3693 case MVT::i64:
3694 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3695 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3696 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3697 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3698 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3699 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3700 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3701 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3702 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3703 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3704 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3705 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3706 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3707 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3708 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3709 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3710 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3711 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3712 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3713 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3714 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3715 }
3716}
3717
3718/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3719///
3720SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3721 switch (Opc) {
3722 default: assert(0 && "Cannot expand this yet!");
3723 case ISD::CTPOP: {
3724 static const uint64_t mask[6] = {
3725 0x5555555555555555ULL, 0x3333333333333333ULL,
3726 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3727 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3728 };
3729 MVT::ValueType VT = Op.getValueType();
3730 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3731 unsigned len = getSizeInBits(VT);
3732 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3733 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3734 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3735 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3736 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3737 DAG.getNode(ISD::AND, VT,
3738 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3739 }
3740 return Op;
3741 }
3742 case ISD::CTLZ: {
3743 // for now, we do this:
3744 // x = x | (x >> 1);
3745 // x = x | (x >> 2);
3746 // ...
3747 // x = x | (x >>16);
3748 // x = x | (x >>32); // for 64-bit input
3749 // return popcount(~x);
3750 //
3751 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3752 MVT::ValueType VT = Op.getValueType();
3753 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3754 unsigned len = getSizeInBits(VT);
3755 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3756 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3757 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3758 }
3759 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3760 return DAG.getNode(ISD::CTPOP, VT, Op);
3761 }
3762 case ISD::CTTZ: {
3763 // for now, we use: { return popcount(~x & (x - 1)); }
3764 // unless the target has ctlz but not ctpop, in which case we use:
3765 // { return 32 - nlz(~x & (x-1)); }
3766 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3767 MVT::ValueType VT = Op.getValueType();
3768 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3769 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3770 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3771 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3772 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3773 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3774 TLI.isOperationLegal(ISD::CTLZ, VT))
3775 return DAG.getNode(ISD::SUB, VT,
3776 DAG.getConstant(getSizeInBits(VT), VT),
3777 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3778 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3779 }
3780 }
3781}
Chris Lattnere34b3962005-01-19 04:19:40 +00003782
3783
Chris Lattner3e928bb2005-01-07 07:47:09 +00003784/// ExpandOp - Expand the specified SDOperand into its two component pieces
3785/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3786/// LegalizeNodes map is filled in for any results that are not expanded, the
3787/// ExpandedNodes map is filled in for any results that are expanded, and the
3788/// Lo/Hi values are returned.
3789void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3790 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003791 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003792 SDNode *Node = Op.Val;
3793 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003794 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3795 "Cannot expand FP values!");
3796 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003797 "Cannot expand to FP value or to larger int value!");
3798
Chris Lattner6fdcb252005-09-02 20:32:45 +00003799 // See if we already expanded it.
3800 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3801 = ExpandedNodes.find(Op);
3802 if (I != ExpandedNodes.end()) {
3803 Lo = I->second.first;
3804 Hi = I->second.second;
3805 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003806 }
3807
Chris Lattner3e928bb2005-01-07 07:47:09 +00003808 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003809 case ISD::CopyFromReg:
3810 assert(0 && "CopyFromReg must be legal!");
3811 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003812 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3813 assert(0 && "Do not know how to expand this operator!");
3814 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003815 case ISD::UNDEF:
3816 Lo = DAG.getNode(ISD::UNDEF, NVT);
3817 Hi = DAG.getNode(ISD::UNDEF, NVT);
3818 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003819 case ISD::Constant: {
3820 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3821 Lo = DAG.getConstant(Cst, NVT);
3822 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3823 break;
3824 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003825 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003826 // Return the operands.
3827 Lo = Node->getOperand(0);
3828 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003829 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003830
3831 case ISD::SIGN_EXTEND_INREG:
3832 ExpandOp(Node->getOperand(0), Lo, Hi);
3833 // Sign extend the lo-part.
3834 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3835 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3836 TLI.getShiftAmountTy()));
3837 // sext_inreg the low part if needed.
3838 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3839 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003840
Nate Begemand88fc032006-01-14 03:14:10 +00003841 case ISD::BSWAP: {
3842 ExpandOp(Node->getOperand(0), Lo, Hi);
3843 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3844 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3845 Lo = TempLo;
3846 break;
3847 }
3848
Chris Lattneredb1add2005-05-11 04:51:16 +00003849 case ISD::CTPOP:
3850 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003851 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3852 DAG.getNode(ISD::CTPOP, NVT, Lo),
3853 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003854 Hi = DAG.getConstant(0, NVT);
3855 break;
3856
Chris Lattner39a8f332005-05-12 19:05:01 +00003857 case ISD::CTLZ: {
3858 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003859 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003860 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3861 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003862 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3863 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003864 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3865 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3866
3867 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3868 Hi = DAG.getConstant(0, NVT);
3869 break;
3870 }
3871
3872 case ISD::CTTZ: {
3873 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003874 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003875 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3876 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003877 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3878 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003879 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3880 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3881
3882 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3883 Hi = DAG.getConstant(0, NVT);
3884 break;
3885 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003886
Nate Begemanacc398c2006-01-25 18:21:52 +00003887 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003888 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3889 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003890 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3891 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3892
3893 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003894 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003895 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3896 if (!TLI.isLittleEndian())
3897 std::swap(Lo, Hi);
3898 break;
3899 }
3900
Chris Lattner3e928bb2005-01-07 07:47:09 +00003901 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003902 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3903 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003904 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003905
3906 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003907 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003908 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3909 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003910 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003911 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003912
3913 // Build a factor node to remember that this load is independent of the
3914 // other one.
3915 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3916 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003917
Chris Lattner3e928bb2005-01-07 07:47:09 +00003918 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003919 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003920 if (!TLI.isLittleEndian())
3921 std::swap(Lo, Hi);
3922 break;
3923 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003924 case ISD::AND:
3925 case ISD::OR:
3926 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3927 SDOperand LL, LH, RL, RH;
3928 ExpandOp(Node->getOperand(0), LL, LH);
3929 ExpandOp(Node->getOperand(1), RL, RH);
3930 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3931 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3932 break;
3933 }
3934 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003935 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003936 ExpandOp(Node->getOperand(1), LL, LH);
3937 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003938 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3939 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003940 break;
3941 }
Nate Begeman9373a812005-08-10 20:51:12 +00003942 case ISD::SELECT_CC: {
3943 SDOperand TL, TH, FL, FH;
3944 ExpandOp(Node->getOperand(2), TL, TH);
3945 ExpandOp(Node->getOperand(3), FL, FH);
3946 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3947 Node->getOperand(1), TL, FL, Node->getOperand(4));
3948 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3949 Node->getOperand(1), TH, FH, Node->getOperand(4));
3950 break;
3951 }
Nate Begeman144ff662005-10-13 17:15:37 +00003952 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003953 SDOperand Chain = Node->getOperand(0);
3954 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003955 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3956
3957 if (EVT == NVT)
3958 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3959 else
3960 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3961 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003962
3963 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003964 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003965
Nate Begeman144ff662005-10-13 17:15:37 +00003966 // The high part is obtained by SRA'ing all but one of the bits of the lo
3967 // part.
3968 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3969 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3970 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003971 break;
3972 }
3973 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003974 SDOperand Chain = Node->getOperand(0);
3975 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003976 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3977
3978 if (EVT == NVT)
3979 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3980 else
3981 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3982 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003983
3984 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003985 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003986
Nate Begeman144ff662005-10-13 17:15:37 +00003987 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003988 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003989 break;
3990 }
3991 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003992 SDOperand Chain = Node->getOperand(0);
3993 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003994 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3995
3996 if (EVT == NVT)
3997 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3998 else
3999 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4000 EVT);
4001
4002 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004003 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004004
4005 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00004006 Hi = DAG.getNode(ISD::UNDEF, NVT);
4007 break;
4008 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004009 case ISD::ANY_EXTEND:
4010 // The low part is any extension of the input (which degenerates to a copy).
4011 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4012 // The high part is undefined.
4013 Hi = DAG.getNode(ISD::UNDEF, NVT);
4014 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004015 case ISD::SIGN_EXTEND: {
4016 // The low part is just a sign extension of the input (which degenerates to
4017 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004018 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004019
Chris Lattner3e928bb2005-01-07 07:47:09 +00004020 // The high part is obtained by SRA'ing all but one of the bits of the lo
4021 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004022 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004023 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4024 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004025 break;
4026 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004027 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004028 // The low part is just a zero extension of the input (which degenerates to
4029 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004030 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004031
Chris Lattner3e928bb2005-01-07 07:47:09 +00004032 // The high part is just a zero.
4033 Hi = DAG.getConstant(0, NVT);
4034 break;
Chris Lattner35481892005-12-23 00:16:34 +00004035
4036 case ISD::BIT_CONVERT: {
4037 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4038 Node->getOperand(0));
4039 ExpandOp(Tmp, Lo, Hi);
4040 break;
4041 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004042
Chris Lattner8137c9e2006-01-28 05:07:51 +00004043 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004044 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4045 TargetLowering::Custom &&
4046 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004047 Lo = TLI.LowerOperation(Op, DAG);
4048 assert(Lo.Val && "Node must be custom expanded!");
4049 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004050 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004051 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004052 break;
4053
Chris Lattner4e6c7462005-01-08 19:27:05 +00004054 // These operators cannot be expanded directly, emit them as calls to
4055 // library functions.
4056 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004057 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004058 SDOperand Op;
4059 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4060 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004061 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4062 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004063 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004064
Chris Lattnerf20d1832005-07-30 01:40:57 +00004065 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4066
Chris Lattner80a3e942005-07-29 00:33:32 +00004067 // Now that the custom expander is done, expand the result, which is still
4068 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004069 if (Op.Val) {
4070 ExpandOp(Op, Lo, Hi);
4071 break;
4072 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004073 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004074
Chris Lattner4e6c7462005-01-08 19:27:05 +00004075 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004076 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004077 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004078 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004079 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004080
Chris Lattner4e6c7462005-01-08 19:27:05 +00004081 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004082 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004083 SDOperand Op;
4084 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4085 case Expand: assert(0 && "cannot expand FP!");
4086 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4087 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4088 }
4089
4090 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4091
4092 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004093 if (Op.Val) {
4094 ExpandOp(Op, Lo, Hi);
4095 break;
4096 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004097 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004098
Chris Lattner4e6c7462005-01-08 19:27:05 +00004099 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004100 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004101 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004102 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004103 break;
4104
Evan Cheng05a2d562006-01-09 18:31:59 +00004105 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004106 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004107 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004108 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004109 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004110 Op = TLI.LowerOperation(Op, DAG);
4111 if (Op.Val) {
4112 // Now that the custom expander is done, expand the result, which is
4113 // still VT.
4114 ExpandOp(Op, Lo, Hi);
4115 break;
4116 }
4117 }
4118
Chris Lattnere34b3962005-01-19 04:19:40 +00004119 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004120 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004121 break;
Chris Lattner47599822005-04-02 03:38:53 +00004122
4123 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004124 TargetLowering::LegalizeAction Action =
4125 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4126 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4127 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004128 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004129 break;
4130 }
4131
Chris Lattnere34b3962005-01-19 04:19:40 +00004132 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004133 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004134 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004135 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004136
Evan Cheng05a2d562006-01-09 18:31:59 +00004137 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004138 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004139 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004140 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004141 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004142 Op = TLI.LowerOperation(Op, DAG);
4143 if (Op.Val) {
4144 // Now that the custom expander is done, expand the result, which is
4145 // still VT.
4146 ExpandOp(Op, Lo, Hi);
4147 break;
4148 }
4149 }
4150
Chris Lattnere34b3962005-01-19 04:19:40 +00004151 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004152 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004153 break;
Chris Lattner47599822005-04-02 03:38:53 +00004154
4155 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004156 TargetLowering::LegalizeAction Action =
4157 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4158 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4159 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004160 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004161 break;
4162 }
4163
Chris Lattnere34b3962005-01-19 04:19:40 +00004164 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004165 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004166 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004167 }
4168
4169 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004170 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004171 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004172 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004173 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004174 Op = TLI.LowerOperation(Op, DAG);
4175 if (Op.Val) {
4176 // Now that the custom expander is done, expand the result, which is
4177 // still VT.
4178 ExpandOp(Op, Lo, Hi);
4179 break;
4180 }
4181 }
4182
Chris Lattnere34b3962005-01-19 04:19:40 +00004183 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004184 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004185 break;
Chris Lattner47599822005-04-02 03:38:53 +00004186
4187 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004188 TargetLowering::LegalizeAction Action =
4189 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4190 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4191 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004192 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004193 break;
4194 }
4195
Chris Lattnere34b3962005-01-19 04:19:40 +00004196 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004197 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004198 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004199 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004200
Misha Brukmanedf128a2005-04-21 22:36:52 +00004201 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004202 case ISD::SUB: {
4203 // If the target wants to custom expand this, let them.
4204 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4205 TargetLowering::Custom) {
4206 Op = TLI.LowerOperation(Op, DAG);
4207 if (Op.Val) {
4208 ExpandOp(Op, Lo, Hi);
4209 break;
4210 }
4211 }
4212
4213 // Expand the subcomponents.
4214 SDOperand LHSL, LHSH, RHSL, RHSH;
4215 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4216 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004217 std::vector<MVT::ValueType> VTs;
4218 std::vector<SDOperand> LoOps, HiOps;
4219 VTs.push_back(LHSL.getValueType());
4220 VTs.push_back(MVT::Flag);
4221 LoOps.push_back(LHSL);
4222 LoOps.push_back(RHSL);
4223 HiOps.push_back(LHSH);
4224 HiOps.push_back(RHSH);
4225 if (Node->getOpcode() == ISD::ADD) {
4226 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4227 HiOps.push_back(Lo.getValue(1));
4228 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4229 } else {
4230 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4231 HiOps.push_back(Lo.getValue(1));
4232 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4233 }
Chris Lattner84f67882005-01-20 18:52:28 +00004234 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004235 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004236 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004237 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004238 SDOperand LL, LH, RL, RH;
4239 ExpandOp(Node->getOperand(0), LL, LH);
4240 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004241 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4242 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4243 // extended the sign bit of the low half through the upper half, and if so
4244 // emit a MULHS instead of the alternate sequence that is valid for any
4245 // i64 x i64 multiply.
4246 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4247 // is RH an extension of the sign bit of RL?
4248 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4249 RH.getOperand(1).getOpcode() == ISD::Constant &&
4250 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4251 // is LH an extension of the sign bit of LL?
4252 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4253 LH.getOperand(1).getOpcode() == ISD::Constant &&
4254 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4255 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4256 } else {
4257 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4258 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4259 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4260 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4261 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4262 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004263 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4264 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004265 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004266 }
4267 break;
4268 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004269 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4270 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4271 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4272 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004273 }
4274
Chris Lattner83397362005-12-21 18:02:52 +00004275 // Make sure the resultant values have been legalized themselves, unless this
4276 // is a type that requires multi-step expansion.
4277 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4278 Lo = LegalizeOp(Lo);
4279 Hi = LegalizeOp(Hi);
4280 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004281
4282 // Remember in a map if the values will be reused later.
4283 bool isNew =
4284 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4285 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004286}
4287
Chris Lattnerc7029802006-03-18 01:44:44 +00004288/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4289/// two smaller values of MVT::Vector type.
4290void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4291 SDOperand &Hi) {
4292 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4293 SDNode *Node = Op.Val;
4294 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4295 assert(NumElements > 1 && "Cannot split a single element vector!");
4296 unsigned NewNumElts = NumElements/2;
4297 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4298 SDOperand TypeNode = *(Node->op_end()-1);
4299
4300 // See if we already split it.
4301 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4302 = SplitNodes.find(Op);
4303 if (I != SplitNodes.end()) {
4304 Lo = I->second.first;
4305 Hi = I->second.second;
4306 return;
4307 }
4308
4309 switch (Node->getOpcode()) {
Chris Lattner7692eb42006-03-23 21:16:34 +00004310 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004311 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004312 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4313 LoOps.push_back(NewNumEltsNode);
4314 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004315 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004316
4317 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4318 HiOps.push_back(NewNumEltsNode);
4319 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004320 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004321 break;
4322 }
4323 case ISD::VADD:
4324 case ISD::VSUB:
4325 case ISD::VMUL:
4326 case ISD::VSDIV:
4327 case ISD::VUDIV:
4328 case ISD::VAND:
4329 case ISD::VOR:
4330 case ISD::VXOR: {
4331 SDOperand LL, LH, RL, RH;
4332 SplitVectorOp(Node->getOperand(0), LL, LH);
4333 SplitVectorOp(Node->getOperand(1), RL, RH);
4334
4335 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4336 NewNumEltsNode, TypeNode);
4337 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4338 NewNumEltsNode, TypeNode);
4339 break;
4340 }
4341 case ISD::VLOAD: {
4342 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4343 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4344 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4345
4346 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4347 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4348 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4349 getIntPtrConstant(IncrementSize));
4350 // FIXME: This creates a bogus srcvalue!
4351 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4352
4353 // Build a factor node to remember that this load is independent of the
4354 // other one.
4355 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4356 Hi.getValue(1));
4357
4358 // Remember that we legalized the chain.
4359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4360 if (!TLI.isLittleEndian())
4361 std::swap(Lo, Hi);
4362 break;
4363 }
Chris Lattner7692eb42006-03-23 21:16:34 +00004364 case ISD::VBIT_CONVERT: {
4365 // We know the result is a vector. The input may be either a vector or a
4366 // scalar value.
4367 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4368 // Lower to a store/load. FIXME: this could be improved probably.
4369 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4370
4371 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4372 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4373 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4374 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4375 SplitVectorOp(St, Lo, Hi);
4376 } else {
4377 // If the input is a vector type, we have to either scalarize it, pack it
4378 // or convert it based on whether the input vector type is legal.
4379 SDNode *InVal = Node->getOperand(0).Val;
4380 unsigned NumElems =
4381 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4382 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4383
4384 // If the input is from a single element vector, scalarize the vector,
4385 // then treat like a scalar.
4386 if (NumElems == 1) {
4387 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4388 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4389 Op.getOperand(1), Op.getOperand(2));
4390 SplitVectorOp(Scalar, Lo, Hi);
4391 } else {
4392 // Split the input vector.
4393 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4394
4395 // Convert each of the pieces now.
4396 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4397 NewNumEltsNode, TypeNode);
4398 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4399 NewNumEltsNode, TypeNode);
4400 }
4401 break;
4402 }
4403 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004404 }
4405
4406 // Remember in a map if the values will be reused later.
4407 bool isNew =
4408 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4409 assert(isNew && "Value already expanded?!?");
4410}
4411
4412
4413/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4414/// equivalent operation that returns a scalar (e.g. F32) or packed value
4415/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4416/// type for the result.
4417SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4418 MVT::ValueType NewVT) {
Chris Lattner1c6191f2006-03-21 19:20:37 +00004419 // FIXME: THIS IS A TEMPORARY HACK
4420 if (Op.getValueType() == NewVT) return Op;
Chris Lattner384504c2006-03-21 20:44:12 +00004421
Chris Lattnerc7029802006-03-18 01:44:44 +00004422 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4423 SDNode *Node = Op.Val;
4424
4425 // See if we already packed it.
4426 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4427 if (I != PackedNodes.end()) return I->second;
4428
4429 SDOperand Result;
4430 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004431 default:
4432 Node->dump(); std::cerr << "\n";
4433 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004434 case ISD::VADD:
4435 case ISD::VSUB:
4436 case ISD::VMUL:
4437 case ISD::VSDIV:
4438 case ISD::VUDIV:
4439 case ISD::VAND:
4440 case ISD::VOR:
4441 case ISD::VXOR:
4442 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4443 NewVT,
4444 PackVectorOp(Node->getOperand(0), NewVT),
4445 PackVectorOp(Node->getOperand(1), NewVT));
4446 break;
4447 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004448 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4449 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004450
Chris Lattner4794a6b2006-03-19 00:07:49 +00004451 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004452
4453 // Remember that we legalized the chain.
4454 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4455 break;
4456 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004457 case ISD::VBUILD_VECTOR:
Chris Lattnerc7029802006-03-18 01:44:44 +00004458 if (!MVT::isVector(NewVT)) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004459 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004460 Result = Node->getOperand(0);
4461 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004462 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004463 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004464 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004465 }
4466 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004467 case ISD::VINSERT_VECTOR_ELT:
4468 if (!MVT::isVector(NewVT)) {
4469 // Returning a scalar? Must be the inserted element.
4470 Result = Node->getOperand(1);
4471 } else {
4472 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4473 PackVectorOp(Node->getOperand(0), NewVT),
4474 Node->getOperand(1), Node->getOperand(2));
4475 }
4476 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00004477 case ISD::VBIT_CONVERT:
4478 if (Op.getOperand(0).getValueType() != MVT::Vector)
4479 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4480 else {
4481 // If the input is a vector type, we have to either scalarize it, pack it
4482 // or convert it based on whether the input vector type is legal.
4483 SDNode *InVal = Node->getOperand(0).Val;
4484 unsigned NumElems =
4485 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4486 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4487
4488 // Figure out if there is a Packed type corresponding to this Vector
4489 // type. If so, convert to the packed type.
4490 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4491 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4492 // Turn this into a bit convert of the packed input.
4493 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4494 PackVectorOp(Node->getOperand(0), TVT));
4495 break;
4496 } else if (NumElems == 1) {
4497 // Turn this into a bit convert of the scalar input.
4498 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4499 PackVectorOp(Node->getOperand(0), EVT));
4500 break;
4501 } else {
4502 // FIXME: UNIMP!
4503 assert(0 && "Cast from unsupported vector type not implemented yet!");
4504 }
4505 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004506 }
4507
4508 if (TLI.isTypeLegal(NewVT))
4509 Result = LegalizeOp(Result);
4510 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4511 assert(isNew && "Value already packed?");
4512 return Result;
4513}
4514
Chris Lattner3e928bb2005-01-07 07:47:09 +00004515
4516// SelectionDAG::Legalize - This is the entry point for the file.
4517//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004518void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004519 /// run - This is the main entry point to this class.
4520 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004521 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004522}
4523