blob: dfafb70585f218a67fe554d02f21332ab42707ec [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 Lattnerd1f04d42006-03-24 02:26:29 +0000555
556 case ISD::INTRINSIC: {
557 std::vector<SDOperand> Ops;
558 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
559 Ops.push_back(LegalizeOp(Node->getOperand(i)));
560 Result = DAG.UpdateNodeOperands(Result, Ops);
561 break;
562 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000563
564 case ISD::LOCATION:
565 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
566 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
567
568 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
569 case TargetLowering::Promote:
570 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000571 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000572 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000573 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
574 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
575
576 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
577 const std::string &FName =
578 cast<StringSDNode>(Node->getOperand(3))->getValue();
579 const std::string &DirName =
580 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000581 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000582
Jim Laskeye81aecb2005-12-21 20:51:37 +0000583 std::vector<SDOperand> Ops;
584 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000585 SDOperand LineOp = Node->getOperand(1);
586 SDOperand ColOp = Node->getOperand(2);
587
588 if (useDEBUG_LOC) {
589 Ops.push_back(LineOp); // line #
590 Ops.push_back(ColOp); // col #
591 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
592 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
593 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000594 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
595 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000596 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
597 Ops.push_back(DAG.getConstant(ID, MVT::i32));
598 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
599 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000600 } else {
601 Result = Tmp1; // chain
602 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000603 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000604 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000605 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000606 if (Tmp1 != Node->getOperand(0) ||
607 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000608 std::vector<SDOperand> Ops;
609 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000610 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
611 Ops.push_back(Node->getOperand(1)); // line # must be legal.
612 Ops.push_back(Node->getOperand(2)); // col # must be legal.
613 } else {
614 // Otherwise promote them.
615 Ops.push_back(PromoteOp(Node->getOperand(1)));
616 Ops.push_back(PromoteOp(Node->getOperand(2)));
617 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000618 Ops.push_back(Node->getOperand(3)); // filename must be legal.
619 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000620 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000621 }
622 break;
623 }
624 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000625
626 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000627 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000628 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000629 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000630 case TargetLowering::Legal:
631 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
632 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
633 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
634 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000636 break;
637 }
638 break;
639
640 case ISD::DEBUG_LABEL:
641 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
642 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000643 default: assert(0 && "This action is not supported yet!");
644 case TargetLowering::Legal:
645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000648 break;
649 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000650 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000651
Chris Lattner3e928bb2005-01-07 07:47:09 +0000652 case ISD::Constant:
653 // We know we don't need to expand constants here, constants only have one
654 // value and we check that it is fine above.
655
656 // FIXME: Maybe we should handle things like targets that don't support full
657 // 32-bit immediates?
658 break;
659 case ISD::ConstantFP: {
660 // Spill FP immediates to the constant pool if the target cannot directly
661 // codegen them. Targets often have some immediate values that can be
662 // efficiently generated into an FP register without a load. We explicitly
663 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000664 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
665
666 // Check to see if this FP immediate is already legal.
667 bool isLegal = false;
668 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
669 E = TLI.legal_fpimm_end(); I != E; ++I)
670 if (CFP->isExactlyValue(*I)) {
671 isLegal = true;
672 break;
673 }
674
Chris Lattner3181a772006-01-29 06:26:56 +0000675 // If this is a legal constant, turn it into a TargetConstantFP node.
676 if (isLegal) {
677 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
678 break;
679 }
680
681 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
682 default: assert(0 && "This action is not supported yet!");
683 case TargetLowering::Custom:
684 Tmp3 = TLI.LowerOperation(Result, DAG);
685 if (Tmp3.Val) {
686 Result = Tmp3;
687 break;
688 }
689 // FALLTHROUGH
690 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000691 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000692 bool Extend = false;
693
Chris Lattner456a93a2006-01-28 07:39:30 +0000694 // If a FP immediate is precise when represented as a float and if the
695 // target can do an extending load from float to double, we put it into
696 // the constant pool as a float, even if it's is statically typed as a
697 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000698 MVT::ValueType VT = CFP->getValueType(0);
699 bool isDouble = VT == MVT::f64;
700 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
701 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000702 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
703 // Only do this if the target has a native EXTLOAD instruction from
704 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000705 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000706 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
707 VT = MVT::f32;
708 Extend = true;
709 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000710
Chris Lattner456a93a2006-01-28 07:39:30 +0000711 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000712 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000713 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
714 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000715 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000716 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
717 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000718 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000719 }
720 break;
721 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000722 case ISD::TokenFactor:
723 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000724 Tmp1 = LegalizeOp(Node->getOperand(0));
725 Tmp2 = LegalizeOp(Node->getOperand(1));
726 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
727 } else if (Node->getNumOperands() == 3) {
728 Tmp1 = LegalizeOp(Node->getOperand(0));
729 Tmp2 = LegalizeOp(Node->getOperand(1));
730 Tmp3 = LegalizeOp(Node->getOperand(2));
731 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000732 } else {
733 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000734 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000735 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
736 Ops.push_back(LegalizeOp(Node->getOperand(i)));
737 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000738 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000739 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000740
Chris Lattnerb2827b02006-03-19 00:52:58 +0000741 case ISD::BUILD_VECTOR:
742 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000743 default: assert(0 && "This action is not supported yet!");
744 case TargetLowering::Custom:
745 Tmp3 = TLI.LowerOperation(Result, DAG);
746 if (Tmp3.Val) {
747 Result = Tmp3;
748 break;
749 }
750 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000751 case TargetLowering::Expand:
752 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000753 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000754 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000755 break;
756 case ISD::INSERT_VECTOR_ELT:
757 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
758 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
759 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
760 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
761
762 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
763 Node->getValueType(0))) {
764 default: assert(0 && "This action is not supported yet!");
765 case TargetLowering::Legal:
766 break;
767 case TargetLowering::Custom:
768 Tmp3 = TLI.LowerOperation(Result, DAG);
769 if (Tmp3.Val) {
770 Result = Tmp3;
771 break;
772 }
773 // FALLTHROUGH
774 case TargetLowering::Expand: {
775 // If the target doesn't support this, we have to spill the input vector
776 // to a temporary stack slot, update the element, then reload it. This is
777 // badness. We could also load the value into a vector register (either
778 // with a "move to register" or "extload into register" instruction, then
779 // permute it into place, if the idx is a constant and if the idx is
780 // supported by the target.
781 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
782 break;
783 }
784 }
785 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000786 case ISD::SCALAR_TO_VECTOR:
787 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
788 Result = DAG.UpdateNodeOperands(Result, Tmp1);
789 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
790 Node->getValueType(0))) {
791 default: assert(0 && "This action is not supported yet!");
792 case TargetLowering::Legal:
793 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +0000794 case TargetLowering::Custom:
795 Tmp3 = TLI.LowerOperation(Result, DAG);
796 if (Tmp3.Val) {
797 Result = Tmp3;
798 break;
799 }
800 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000801 case TargetLowering::Expand: {
802 // If the target doesn't support this, store the value to a temporary
803 // stack slot, then EXTLOAD the vector back out.
Chris Lattner4d3abee2006-03-19 06:47:21 +0000804 // TODO: If a target doesn't support this, create a stack slot for the
805 // whole vector, then store into it, then load the whole vector.
Chris Lattnerce872152006-03-19 06:31:19 +0000806 SDOperand StackPtr =
807 CreateStackTemporary(Node->getOperand(0).getValueType());
808 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
809 Node->getOperand(0), StackPtr,
810 DAG.getSrcValue(NULL));
811 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
812 DAG.getSrcValue(NULL),
813 Node->getOperand(0).getValueType());
814 break;
815 }
816 }
817 break;
Chris Lattner87100e02006-03-20 01:52:29 +0000818 case ISD::VECTOR_SHUFFLE:
819 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
820 "vector shuffle should not be created if not legal!");
821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
822 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
823 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
824
825 // Allow targets to custom lower the SHUFFLEs they support.
826 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
827 == TargetLowering::Custom) {
828 Tmp1 = TLI.LowerOperation(Result, DAG);
829 if (Tmp1.Val) Result = Tmp1;
830 }
831 break;
Chris Lattner384504c2006-03-21 20:44:12 +0000832
833 case ISD::EXTRACT_VECTOR_ELT:
834 Tmp1 = LegalizeOp(Node->getOperand(0));
835 Tmp2 = LegalizeOp(Node->getOperand(1));
836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +0000837
838 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
839 Tmp1.getValueType())) {
840 default: assert(0 && "This action is not supported yet!");
841 case TargetLowering::Legal:
842 break;
843 case TargetLowering::Custom:
844 Tmp3 = TLI.LowerOperation(Result, DAG);
845 if (Tmp3.Val) {
846 Result = Tmp3;
847 break;
848 }
849 // FALLTHROUGH
850 case TargetLowering::Expand: {
851 // If the target doesn't support this, store the value to a temporary
852 // stack slot, then LOAD the scalar element back out.
853 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
854 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
855 Tmp1, StackPtr, DAG.getSrcValue(NULL));
856
857 // Add the offset to the index.
858 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
859 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
860 DAG.getConstant(EltSize, Tmp2.getValueType()));
861 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
862
863 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
864 DAG.getSrcValue(NULL));
865 break;
866 }
867 }
Chris Lattner384504c2006-03-21 20:44:12 +0000868 break;
869
Chris Lattner3b9fa892006-03-22 00:12:37 +0000870 case ISD::VEXTRACT_VECTOR_ELT: {
Chris Lattner384504c2006-03-21 20:44:12 +0000871 // We know that operand #0 is the Vec vector. If the index is a constant
872 // or if the invec is a supported hardware type, we can use it. Otherwise,
873 // lower to a store then an indexed load.
874 Tmp1 = Node->getOperand(0);
875 Tmp2 = LegalizeOp(Node->getOperand(1));
876
877 SDNode *InVal = Tmp1.Val;
878 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
879 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
880
881 // Figure out if there is a Packed type corresponding to this Vector
882 // type. If so, convert to the packed type.
883 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
884 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
885 // Turn this into a packed extract_vector_elt operation.
886 Tmp1 = PackVectorOp(Tmp1, TVT);
887 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Node->getValueType(0),
888 Tmp1, Tmp2);
889 break;
890 } else if (NumElems == 1) {
891 // This must be an access of the only element.
892 Result = PackVectorOp(Tmp1, EVT);
893 break;
894 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Tmp2)) {
895 SDOperand Lo, Hi;
896 SplitVectorOp(Tmp1, Lo, Hi);
897 if (CIdx->getValue() < NumElems/2) {
898 Tmp1 = Lo;
899 } else {
900 Tmp1 = Hi;
901 Tmp2 = DAG.getConstant(CIdx->getValue() - NumElems/2,
902 Tmp2.getValueType());
903 }
904
905 // It's now an extract from the appropriate high or low part.
906 Result = LegalizeOp(DAG.UpdateNodeOperands(Result, Tmp1, Tmp2));
907 } else {
Chris Lattner3b9fa892006-03-22 00:12:37 +0000908 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
Chris Lattner384504c2006-03-21 20:44:12 +0000909 assert(0 && "unimp!");
910 }
911 break;
Chris Lattner3b9fa892006-03-22 00:12:37 +0000912 }
Chris Lattner87100e02006-03-20 01:52:29 +0000913
Chris Lattner6831a812006-02-13 09:18:02 +0000914 case ISD::CALLSEQ_START: {
915 SDNode *CallEnd = FindCallEndFromCallStart(Node);
916
917 // Recursively Legalize all of the inputs of the call end that do not lead
918 // to this call start. This ensures that any libcalls that need be inserted
919 // are inserted *before* the CALLSEQ_START.
920 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
921 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
922
923 // Now that we legalized all of the inputs (which may have inserted
924 // libcalls) create the new CALLSEQ_START node.
925 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
926
927 // Merge in the last call, to ensure that this call start after the last
928 // call ended.
929 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
930 Tmp1 = LegalizeOp(Tmp1);
931
932 // Do not try to legalize the target-specific arguments (#1+).
933 if (Tmp1 != Node->getOperand(0)) {
934 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
935 Ops[0] = Tmp1;
936 Result = DAG.UpdateNodeOperands(Result, Ops);
937 }
938
939 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000940 AddLegalizedOperand(Op.getValue(0), Result);
941 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
942 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
943
Chris Lattner6831a812006-02-13 09:18:02 +0000944 // Now that the callseq_start and all of the non-call nodes above this call
945 // sequence have been legalized, legalize the call itself. During this
946 // process, no libcalls can/will be inserted, guaranteeing that no calls
947 // can overlap.
948 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
949 SDOperand InCallSEQ = LastCALLSEQ_END;
950 // Note that we are selecting this call!
951 LastCALLSEQ_END = SDOperand(CallEnd, 0);
952 IsLegalizingCall = true;
953
954 // Legalize the call, starting from the CALLSEQ_END.
955 LegalizeOp(LastCALLSEQ_END);
956 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
957 return Result;
958 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000959 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000960 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
961 // will cause this node to be legalized as well as handling libcalls right.
962 if (LastCALLSEQ_END.Val != Node) {
963 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
964 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
965 assert(I != LegalizedNodes.end() &&
966 "Legalizing the call start should have legalized this node!");
967 return I->second;
968 }
969
970 // Otherwise, the call start has been legalized and everything is going
971 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000972 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000973 // Do not try to legalize the target-specific arguments (#1+), except for
974 // an optional flag input.
975 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
976 if (Tmp1 != Node->getOperand(0)) {
977 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
978 Ops[0] = Tmp1;
979 Result = DAG.UpdateNodeOperands(Result, Ops);
980 }
981 } else {
982 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
983 if (Tmp1 != Node->getOperand(0) ||
984 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
985 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
986 Ops[0] = Tmp1;
987 Ops.back() = Tmp2;
988 Result = DAG.UpdateNodeOperands(Result, Ops);
989 }
Chris Lattner6a542892006-01-24 05:48:21 +0000990 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000991 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000992 // This finishes up call legalization.
993 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000994
995 // If the CALLSEQ_END node has a flag, remember that we legalized it.
996 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
997 if (Node->getNumValues() == 2)
998 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
999 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001000 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1002 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1003 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001005
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001006 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001007 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001008 switch (TLI.getOperationAction(Node->getOpcode(),
1009 Node->getValueType(0))) {
1010 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001011 case TargetLowering::Expand: {
1012 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1013 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1014 " not tell us which reg is the stack pointer!");
1015 SDOperand Chain = Tmp1.getOperand(0);
1016 SDOperand Size = Tmp2.getOperand(1);
1017 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1018 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1019 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001020 Tmp1 = LegalizeOp(Tmp1);
1021 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001022 break;
1023 }
1024 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001025 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1026 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001027 Tmp1 = LegalizeOp(Tmp3);
1028 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001029 }
Chris Lattner903d2782006-01-15 08:54:32 +00001030 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001031 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001032 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001033 }
Chris Lattner903d2782006-01-15 08:54:32 +00001034 // Since this op produce two values, make sure to remember that we
1035 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001036 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1037 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001038 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001039 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001040 case ISD::INLINEASM:
1041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1042 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001043 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +00001044 Tmp2 = Tmp3 = SDOperand(0, 0);
1045 else
1046 Tmp3 = LegalizeOp(Tmp2);
1047
1048 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1049 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1050 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001051 if (Tmp3.Val) Ops.back() = Tmp3;
1052 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001053 }
1054
1055 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001056 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001057 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1058 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +00001059 case ISD::BR:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001061 // Ensure that libcalls are emitted before a branch.
1062 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1063 Tmp1 = LegalizeOp(Tmp1);
1064 LastCALLSEQ_END = DAG.getEntryNode();
1065
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001067 break;
1068
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001069 case ISD::BRCOND:
1070 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001071 // Ensure that libcalls are emitted before a return.
1072 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1073 Tmp1 = LegalizeOp(Tmp1);
1074 LastCALLSEQ_END = DAG.getEntryNode();
1075
Chris Lattner47e92232005-01-18 19:27:06 +00001076 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1077 case Expand: assert(0 && "It's impossible to expand bools");
1078 case Legal:
1079 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1080 break;
1081 case Promote:
1082 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1083 break;
1084 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001085
1086 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001088
1089 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1090 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001091 case TargetLowering::Legal: break;
1092 case TargetLowering::Custom:
1093 Tmp1 = TLI.LowerOperation(Result, DAG);
1094 if (Tmp1.Val) Result = Tmp1;
1095 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001096 case TargetLowering::Expand:
1097 // Expand brcond's setcc into its constituent parts and create a BR_CC
1098 // Node.
1099 if (Tmp2.getOpcode() == ISD::SETCC) {
1100 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1101 Tmp2.getOperand(0), Tmp2.getOperand(1),
1102 Node->getOperand(2));
1103 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001104 // Make sure the condition is either zero or one. It may have been
1105 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001106 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1107 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1108 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001109
Nate Begeman7cbd5252005-08-16 19:49:35 +00001110 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1111 DAG.getCondCode(ISD::SETNE), Tmp2,
1112 DAG.getConstant(0, Tmp2.getValueType()),
1113 Node->getOperand(2));
1114 }
1115 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001116 }
1117 break;
1118 case ISD::BR_CC:
1119 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001120 // Ensure that libcalls are emitted before a branch.
1121 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1122 Tmp1 = LegalizeOp(Tmp1);
1123 LastCALLSEQ_END = DAG.getEntryNode();
1124
Nate Begeman750ac1b2006-02-01 07:19:44 +00001125 Tmp2 = Node->getOperand(2); // LHS
1126 Tmp3 = Node->getOperand(3); // RHS
1127 Tmp4 = Node->getOperand(1); // CC
1128
1129 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1130
1131 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1132 // the LHS is a legal SETCC itself. In this case, we need to compare
1133 // the result against zero to select between true and false values.
1134 if (Tmp3.Val == 0) {
1135 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1136 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001137 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001138
1139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1140 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001141
Chris Lattner181b7a32005-12-17 23:46:46 +00001142 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1143 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001144 case TargetLowering::Legal: break;
1145 case TargetLowering::Custom:
1146 Tmp4 = TLI.LowerOperation(Result, DAG);
1147 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001148 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001149 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001150 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001151 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1153 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001154
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001155 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001156 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1157 Tmp2 = Result.getValue(0);
1158 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001159
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001160 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1161 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001162 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001163 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001164 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001165 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001166 Tmp2 = LegalizeOp(Tmp1);
1167 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001168 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001169 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001170 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001171 // Since loads produce two values, make sure to remember that we
1172 // legalized both of them.
1173 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1174 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1175 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001176 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001177 case ISD::EXTLOAD:
1178 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001179 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001180 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1181 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001182
Chris Lattner5f056bf2005-07-10 01:55:33 +00001183 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001184 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001185 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001186 case TargetLowering::Promote:
1187 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1189 DAG.getValueType(MVT::i8));
1190 Tmp1 = Result.getValue(0);
1191 Tmp2 = Result.getValue(1);
1192 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001193 case TargetLowering::Custom:
1194 isCustom = true;
1195 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001196 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1198 Node->getOperand(3));
1199 Tmp1 = Result.getValue(0);
1200 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001201
1202 if (isCustom) {
1203 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1204 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001205 Tmp1 = LegalizeOp(Tmp3);
1206 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001207 }
1208 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001209 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001210 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001211 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001212 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1213 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001214 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001215 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1216 Tmp2 = LegalizeOp(Load.getValue(1));
1217 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001218 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001219 assert(Node->getOpcode() != ISD::EXTLOAD &&
1220 "EXTLOAD should always be supported!");
1221 // Turn the unsupported load into an EXTLOAD followed by an explicit
1222 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001223 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1224 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001225 SDOperand ValRes;
1226 if (Node->getOpcode() == ISD::SEXTLOAD)
1227 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001228 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001229 else
1230 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001231 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1232 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1233 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001234 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001235 // Since loads produce two values, make sure to remember that we legalized
1236 // both of them.
1237 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1238 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1239 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001240 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001241 case ISD::EXTRACT_ELEMENT: {
1242 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1243 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001244 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001245 case Legal:
1246 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1247 // 1 -> Hi
1248 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1249 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1250 TLI.getShiftAmountTy()));
1251 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1252 } else {
1253 // 0 -> Lo
1254 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1255 Node->getOperand(0));
1256 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001257 break;
1258 case Expand:
1259 // Get both the low and high parts.
1260 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1261 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1262 Result = Tmp2; // 1 -> Hi
1263 else
1264 Result = Tmp1; // 0 -> Lo
1265 break;
1266 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001267 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001268 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001269
1270 case ISD::CopyToReg:
1271 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001272
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001273 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001274 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001275 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001276 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001277 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001279 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001280 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001281 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001282 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1284 Tmp3);
1285 } else {
1286 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001287 }
1288
1289 // Since this produces two values, make sure to remember that we legalized
1290 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001291 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001292 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001293 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001294 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001295 break;
1296
1297 case ISD::RET:
1298 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001299
1300 // Ensure that libcalls are emitted before a return.
1301 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1302 Tmp1 = LegalizeOp(Tmp1);
1303 LastCALLSEQ_END = DAG.getEntryNode();
1304
Chris Lattner3e928bb2005-01-07 07:47:09 +00001305 switch (Node->getNumOperands()) {
1306 case 2: // ret val
1307 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1308 case Legal:
1309 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001310 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001311 break;
1312 case Expand: {
1313 SDOperand Lo, Hi;
1314 ExpandOp(Node->getOperand(1), Lo, Hi);
1315 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001316 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001317 }
1318 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001319 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1321 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001322 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001323 }
1324 break;
1325 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001326 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001327 break;
1328 default: { // ret <values>
1329 std::vector<SDOperand> NewValues;
1330 NewValues.push_back(Tmp1);
1331 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1332 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1333 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001334 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001335 break;
1336 case Expand: {
1337 SDOperand Lo, Hi;
1338 ExpandOp(Node->getOperand(i), Lo, Hi);
1339 NewValues.push_back(Lo);
1340 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001341 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001342 }
1343 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001344 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001345 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001346
1347 if (NewValues.size() == Node->getNumOperands())
1348 Result = DAG.UpdateNodeOperands(Result, NewValues);
1349 else
1350 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001351 break;
1352 }
1353 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001354
Chris Lattner6862dbc2006-01-29 21:02:23 +00001355 if (Result.getOpcode() == ISD::RET) {
1356 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1357 default: assert(0 && "This action is not supported yet!");
1358 case TargetLowering::Legal: break;
1359 case TargetLowering::Custom:
1360 Tmp1 = TLI.LowerOperation(Result, DAG);
1361 if (Tmp1.Val) Result = Tmp1;
1362 break;
1363 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001364 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001365 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001366 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1368 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1369
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001370 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001371 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001372 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001373 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001374 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001375 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001376 } else {
1377 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001378 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001379 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001380 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1381 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001382 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001383 }
1384
Chris Lattner3e928bb2005-01-07 07:47:09 +00001385 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1386 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001387 Tmp3 = LegalizeOp(Node->getOperand(1));
1388 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1389 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001390
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001391 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001392 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1393 default: assert(0 && "This action is not supported yet!");
1394 case TargetLowering::Legal: break;
1395 case TargetLowering::Custom:
1396 Tmp1 = TLI.LowerOperation(Result, DAG);
1397 if (Tmp1.Val) Result = Tmp1;
1398 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001399 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001400 break;
1401 }
1402 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001403 // Truncate the value and store the result.
1404 Tmp3 = PromoteOp(Node->getOperand(1));
1405 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001406 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001407 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001408 break;
1409
Chris Lattner3e928bb2005-01-07 07:47:09 +00001410 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001411 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001412 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001413
1414 // If this is a vector type, then we have to calculate the increment as
1415 // the product of the element size in bytes, and the number of elements
1416 // in the high half of the vector.
1417 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1418 SDNode *InVal = Node->getOperand(1).Val;
1419 unsigned NumElems =
1420 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1421 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1422
1423 // Figure out if there is a Packed type corresponding to this Vector
1424 // type. If so, convert to the packed type.
1425 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1426 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1427 // Turn this into a normal store of the packed type.
1428 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1429 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1430 Node->getOperand(3));
1431 break;
1432 } else if (NumElems == 1) {
1433 // Turn this into a normal store of the scalar type.
1434 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1435 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1436 Node->getOperand(3));
1437 break;
1438 } else {
1439 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1440 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1441 }
1442 } else {
1443 ExpandOp(Node->getOperand(1), Lo, Hi);
1444 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1445 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001446
1447 if (!TLI.isLittleEndian())
1448 std::swap(Lo, Hi);
1449
Chris Lattneredb1add2005-05-11 04:51:16 +00001450 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1451 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001452 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1453 getIntPtrConstant(IncrementSize));
1454 assert(isTypeLegal(Tmp2.getValueType()) &&
1455 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001456 // FIXME: This sets the srcvalue of both halves to be the same, which is
1457 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001458 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1459 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001460 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1461 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001462 }
1463 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001464 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001465 case ISD::PCMARKER:
1466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001467 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001468 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001469 case ISD::STACKSAVE:
1470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001471 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1472 Tmp1 = Result.getValue(0);
1473 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001474
Chris Lattner140d53c2006-01-13 02:50:02 +00001475 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1476 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001477 case TargetLowering::Legal: break;
1478 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001479 Tmp3 = TLI.LowerOperation(Result, DAG);
1480 if (Tmp3.Val) {
1481 Tmp1 = LegalizeOp(Tmp3);
1482 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001483 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001484 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001485 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001486 // Expand to CopyFromReg if the target set
1487 // StackPointerRegisterToSaveRestore.
1488 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001489 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001490 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001491 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001492 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001493 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1494 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001495 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001496 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001497 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001498
1499 // Since stacksave produce two values, make sure to remember that we
1500 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001501 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1502 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1503 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001504
Chris Lattner140d53c2006-01-13 02:50:02 +00001505 case ISD::STACKRESTORE:
1506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1507 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001508 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001509
1510 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1511 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001512 case TargetLowering::Legal: break;
1513 case TargetLowering::Custom:
1514 Tmp1 = TLI.LowerOperation(Result, DAG);
1515 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001516 break;
1517 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001518 // Expand to CopyToReg if the target set
1519 // StackPointerRegisterToSaveRestore.
1520 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1521 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1522 } else {
1523 Result = Tmp1;
1524 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001525 break;
1526 }
1527 break;
1528
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001529 case ISD::READCYCLECOUNTER:
1530 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001531 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001532
1533 // Since rdcc produce two values, make sure to remember that we legalized
1534 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001535 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001536 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001537 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001538
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001539 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001540 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1541 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1542
Chris Lattner456a93a2006-01-28 07:39:30 +00001543 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1544 "Cannot handle illegal TRUNCSTORE yet!");
1545 Tmp2 = LegalizeOp(Node->getOperand(1));
1546
1547 // The only promote case we handle is TRUNCSTORE:i1 X into
1548 // -> TRUNCSTORE:i8 (and X, 1)
1549 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1550 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1551 TargetLowering::Promote) {
1552 // Promote the bool to a mask then store.
1553 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1554 DAG.getConstant(1, Tmp2.getValueType()));
1555 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1556 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001557
Chris Lattner456a93a2006-01-28 07:39:30 +00001558 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1559 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001560 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1561 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001562 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001563
Chris Lattner456a93a2006-01-28 07:39:30 +00001564 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1565 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1566 default: assert(0 && "This action is not supported yet!");
1567 case TargetLowering::Legal: break;
1568 case TargetLowering::Custom:
1569 Tmp1 = TLI.LowerOperation(Result, DAG);
1570 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001571 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001572 }
1573 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001574 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001575 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001576 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1577 case Expand: assert(0 && "It's impossible to expand bools");
1578 case Legal:
1579 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1580 break;
1581 case Promote:
1582 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1583 break;
1584 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001585 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001586 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001587
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001588 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001589
Nate Begemanb942a3d2005-08-23 04:29:48 +00001590 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001591 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001592 case TargetLowering::Legal: break;
1593 case TargetLowering::Custom: {
1594 Tmp1 = TLI.LowerOperation(Result, DAG);
1595 if (Tmp1.Val) Result = Tmp1;
1596 break;
1597 }
Nate Begeman9373a812005-08-10 20:51:12 +00001598 case TargetLowering::Expand:
1599 if (Tmp1.getOpcode() == ISD::SETCC) {
1600 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1601 Tmp2, Tmp3,
1602 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1603 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001604 // Make sure the condition is either zero or one. It may have been
1605 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001606 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1607 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1608 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001609 Result = DAG.getSelectCC(Tmp1,
1610 DAG.getConstant(0, Tmp1.getValueType()),
1611 Tmp2, Tmp3, ISD::SETNE);
1612 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001613 break;
1614 case TargetLowering::Promote: {
1615 MVT::ValueType NVT =
1616 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1617 unsigned ExtOp, TruncOp;
1618 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001619 ExtOp = ISD::ANY_EXTEND;
1620 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001621 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001622 ExtOp = ISD::FP_EXTEND;
1623 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001624 }
1625 // Promote each of the values to the new type.
1626 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1627 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1628 // Perform the larger operation, then round down.
1629 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1630 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1631 break;
1632 }
1633 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001634 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001635 case ISD::SELECT_CC: {
1636 Tmp1 = Node->getOperand(0); // LHS
1637 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001638 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1639 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001640 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001641
Nate Begeman750ac1b2006-02-01 07:19:44 +00001642 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1643
1644 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1645 // the LHS is a legal SETCC itself. In this case, we need to compare
1646 // the result against zero to select between true and false values.
1647 if (Tmp2.Val == 0) {
1648 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1649 CC = DAG.getCondCode(ISD::SETNE);
1650 }
1651 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1652
1653 // Everything is legal, see if we should expand this op or something.
1654 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1655 default: assert(0 && "This action is not supported yet!");
1656 case TargetLowering::Legal: break;
1657 case TargetLowering::Custom:
1658 Tmp1 = TLI.LowerOperation(Result, DAG);
1659 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001660 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001661 }
1662 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001663 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001664 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001665 Tmp1 = Node->getOperand(0);
1666 Tmp2 = Node->getOperand(1);
1667 Tmp3 = Node->getOperand(2);
1668 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1669
1670 // If we had to Expand the SetCC operands into a SELECT node, then it may
1671 // not always be possible to return a true LHS & RHS. In this case, just
1672 // return the value we legalized, returned in the LHS
1673 if (Tmp2.Val == 0) {
1674 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001675 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001676 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001677
Chris Lattner73e142f2006-01-30 22:43:50 +00001678 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001679 default: assert(0 && "Cannot handle this action for SETCC yet!");
1680 case TargetLowering::Custom:
1681 isCustom = true;
1682 // FALLTHROUGH.
1683 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001684 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001685 if (isCustom) {
1686 Tmp3 = TLI.LowerOperation(Result, DAG);
1687 if (Tmp3.Val) Result = Tmp3;
1688 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001689 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001690 case TargetLowering::Promote: {
1691 // First step, figure out the appropriate operation to use.
1692 // Allow SETCC to not be supported for all legal data types
1693 // Mostly this targets FP
1694 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1695 MVT::ValueType OldVT = NewInTy;
1696
1697 // Scan for the appropriate larger type to use.
1698 while (1) {
1699 NewInTy = (MVT::ValueType)(NewInTy+1);
1700
1701 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1702 "Fell off of the edge of the integer world");
1703 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1704 "Fell off of the edge of the floating point world");
1705
1706 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001707 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001708 break;
1709 }
1710 if (MVT::isInteger(NewInTy))
1711 assert(0 && "Cannot promote Legal Integer SETCC yet");
1712 else {
1713 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1714 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1715 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001716 Tmp1 = LegalizeOp(Tmp1);
1717 Tmp2 = LegalizeOp(Tmp2);
1718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001719 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001720 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001721 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001722 case TargetLowering::Expand:
1723 // Expand a setcc node into a select_cc of the same condition, lhs, and
1724 // rhs that selects between const 1 (true) and const 0 (false).
1725 MVT::ValueType VT = Node->getValueType(0);
1726 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1727 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1728 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001729 break;
1730 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001731 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001732 case ISD::MEMSET:
1733 case ISD::MEMCPY:
1734 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001736 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1737
1738 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1739 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1740 case Expand: assert(0 && "Cannot expand a byte!");
1741 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001742 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001743 break;
1744 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001745 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001746 break;
1747 }
1748 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001749 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001750 }
Chris Lattner272455b2005-02-02 03:44:41 +00001751
1752 SDOperand Tmp4;
1753 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001754 case Expand: {
1755 // Length is too big, just take the lo-part of the length.
1756 SDOperand HiPart;
1757 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1758 break;
1759 }
Chris Lattnere5605212005-01-28 22:29:18 +00001760 case Legal:
1761 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001762 break;
1763 case Promote:
1764 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001765 break;
1766 }
1767
1768 SDOperand Tmp5;
1769 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1770 case Expand: assert(0 && "Cannot expand this yet!");
1771 case Legal:
1772 Tmp5 = LegalizeOp(Node->getOperand(4));
1773 break;
1774 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001775 Tmp5 = PromoteOp(Node->getOperand(4));
1776 break;
1777 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001778
1779 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1780 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001781 case TargetLowering::Custom:
1782 isCustom = true;
1783 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001784 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001785 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001786 if (isCustom) {
1787 Tmp1 = TLI.LowerOperation(Result, DAG);
1788 if (Tmp1.Val) Result = Tmp1;
1789 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001790 break;
1791 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001792 // Otherwise, the target does not support this operation. Lower the
1793 // operation to an explicit libcall as appropriate.
1794 MVT::ValueType IntPtr = TLI.getPointerTy();
1795 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1796 std::vector<std::pair<SDOperand, const Type*> > Args;
1797
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001798 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001799 if (Node->getOpcode() == ISD::MEMSET) {
1800 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001801 // Extend the (previously legalized) ubyte argument to be an int value
1802 // for the call.
1803 if (Tmp3.getValueType() > MVT::i32)
1804 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1805 else
1806 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001807 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1808 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1809
1810 FnName = "memset";
1811 } else if (Node->getOpcode() == ISD::MEMCPY ||
1812 Node->getOpcode() == ISD::MEMMOVE) {
1813 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1814 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1815 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1816 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1817 } else {
1818 assert(0 && "Unknown op!");
1819 }
Chris Lattner45982da2005-05-12 16:53:42 +00001820
Chris Lattnere1bd8222005-01-11 05:57:22 +00001821 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001822 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001823 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001824 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001825 break;
1826 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001827 }
1828 break;
1829 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001830
Chris Lattner5b359c62005-04-02 04:00:59 +00001831 case ISD::SHL_PARTS:
1832 case ISD::SRA_PARTS:
1833 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001834 std::vector<SDOperand> Ops;
1835 bool Changed = false;
1836 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1837 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1838 Changed |= Ops.back() != Node->getOperand(i);
1839 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001840 if (Changed)
1841 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001842
Evan Cheng05a2d562006-01-09 18:31:59 +00001843 switch (TLI.getOperationAction(Node->getOpcode(),
1844 Node->getValueType(0))) {
1845 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001846 case TargetLowering::Legal: break;
1847 case TargetLowering::Custom:
1848 Tmp1 = TLI.LowerOperation(Result, DAG);
1849 if (Tmp1.Val) {
1850 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001851 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001852 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001853 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1854 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001855 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001856 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001857 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001858 return RetVal;
1859 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001860 break;
1861 }
1862
Chris Lattner2c8086f2005-04-02 05:00:07 +00001863 // Since these produce multiple values, make sure to remember that we
1864 // legalized all of them.
1865 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1866 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1867 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001868 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001869
1870 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001871 case ISD::ADD:
1872 case ISD::SUB:
1873 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001874 case ISD::MULHS:
1875 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001876 case ISD::UDIV:
1877 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001878 case ISD::AND:
1879 case ISD::OR:
1880 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001881 case ISD::SHL:
1882 case ISD::SRL:
1883 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001884 case ISD::FADD:
1885 case ISD::FSUB:
1886 case ISD::FMUL:
1887 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001888 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001889 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1890 case Expand: assert(0 && "Not possible");
1891 case Legal:
1892 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1893 break;
1894 case Promote:
1895 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1896 break;
1897 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001898
1899 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001900
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001901 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001902 default: assert(0 && "Operation not supported");
1903 case TargetLowering::Legal: break;
1904 case TargetLowering::Custom:
1905 Tmp1 = TLI.LowerOperation(Result, DAG);
1906 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001907 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001908 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001909 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001910
1911 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1912 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1913 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1914 case Expand: assert(0 && "Not possible");
1915 case Legal:
1916 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1917 break;
1918 case Promote:
1919 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1920 break;
1921 }
1922
1923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1924
1925 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1926 default: assert(0 && "Operation not supported");
1927 case TargetLowering::Custom:
1928 Tmp1 = TLI.LowerOperation(Result, DAG);
1929 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001930 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001931 case TargetLowering::Legal: break;
1932 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001933 // If this target supports fabs/fneg natively, do this efficiently.
1934 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1935 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1936 // Get the sign bit of the RHS.
1937 MVT::ValueType IVT =
1938 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1939 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1940 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1941 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1942 // Get the absolute value of the result.
1943 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1944 // Select between the nabs and abs value based on the sign bit of
1945 // the input.
1946 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1947 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1948 AbsVal),
1949 AbsVal);
1950 Result = LegalizeOp(Result);
1951 break;
1952 }
1953
1954 // Otherwise, do bitwise ops!
1955
1956 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001957 const char *FnName;
1958 if (Node->getValueType(0) == MVT::f32) {
1959 FnName = "copysignf";
1960 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1961 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1962 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1963 } else {
1964 FnName = "copysign";
1965 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1966 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1967 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1968 }
1969 SDOperand Dummy;
1970 Result = ExpandLibCall(FnName, Node, Dummy);
1971 break;
1972 }
1973 break;
1974
Nate Begeman551bf3f2006-02-17 05:43:56 +00001975 case ISD::ADDC:
1976 case ISD::SUBC:
1977 Tmp1 = LegalizeOp(Node->getOperand(0));
1978 Tmp2 = LegalizeOp(Node->getOperand(1));
1979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1980 // Since this produces two values, make sure to remember that we legalized
1981 // both of them.
1982 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1983 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1984 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001985
Nate Begeman551bf3f2006-02-17 05:43:56 +00001986 case ISD::ADDE:
1987 case ISD::SUBE:
1988 Tmp1 = LegalizeOp(Node->getOperand(0));
1989 Tmp2 = LegalizeOp(Node->getOperand(1));
1990 Tmp3 = LegalizeOp(Node->getOperand(2));
1991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1992 // Since this produces two values, make sure to remember that we legalized
1993 // both of them.
1994 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1995 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1996 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001997
Nate Begeman419f8b62005-10-18 00:27:41 +00001998 case ISD::BUILD_PAIR: {
1999 MVT::ValueType PairTy = Node->getValueType(0);
2000 // TODO: handle the case where the Lo and Hi operands are not of legal type
2001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2002 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2003 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002004 case TargetLowering::Promote:
2005 case TargetLowering::Custom:
2006 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002007 case TargetLowering::Legal:
2008 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2009 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2010 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002011 case TargetLowering::Expand:
2012 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2013 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2014 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2015 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2016 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002017 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002018 break;
2019 }
2020 break;
2021 }
2022
Nate Begemanc105e192005-04-06 00:23:54 +00002023 case ISD::UREM:
2024 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002025 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002026 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2027 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002028
Nate Begemanc105e192005-04-06 00:23:54 +00002029 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002030 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2031 case TargetLowering::Custom:
2032 isCustom = true;
2033 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002034 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002035 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002036 if (isCustom) {
2037 Tmp1 = TLI.LowerOperation(Result, DAG);
2038 if (Tmp1.Val) Result = Tmp1;
2039 }
Nate Begemanc105e192005-04-06 00:23:54 +00002040 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002041 case TargetLowering::Expand:
2042 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002043 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00002044 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002045 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002046 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2047 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2048 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2049 } else {
2050 // Floating point mod -> fmod libcall.
2051 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2052 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002053 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002054 }
2055 break;
2056 }
2057 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002058 case ISD::VAARG: {
2059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2060 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2061
Chris Lattner5c62f332006-01-28 07:42:08 +00002062 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002063 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2064 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002065 case TargetLowering::Custom:
2066 isCustom = true;
2067 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002068 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2070 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002071 Tmp1 = Result.getValue(1);
2072
2073 if (isCustom) {
2074 Tmp2 = TLI.LowerOperation(Result, DAG);
2075 if (Tmp2.Val) {
2076 Result = LegalizeOp(Tmp2);
2077 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2078 }
2079 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002080 break;
2081 case TargetLowering::Expand: {
2082 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2083 Node->getOperand(2));
2084 // Increment the pointer, VAList, to the next vaarg
2085 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2086 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2087 TLI.getPointerTy()));
2088 // Store the incremented VAList to the legalized pointer
2089 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2090 Node->getOperand(2));
2091 // Load the actual argument out of the pointer VAList
2092 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002093 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002094 Result = LegalizeOp(Result);
2095 break;
2096 }
2097 }
2098 // Since VAARG produces two values, make sure to remember that we
2099 // legalized both of them.
2100 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002101 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2102 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002103 }
2104
2105 case ISD::VACOPY:
2106 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2107 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2108 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2109
2110 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2111 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002112 case TargetLowering::Custom:
2113 isCustom = true;
2114 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002115 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002116 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2117 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002118 if (isCustom) {
2119 Tmp1 = TLI.LowerOperation(Result, DAG);
2120 if (Tmp1.Val) Result = Tmp1;
2121 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002122 break;
2123 case TargetLowering::Expand:
2124 // This defaults to loading a pointer from the input and storing it to the
2125 // output, returning the chain.
2126 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2127 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2128 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002129 break;
2130 }
2131 break;
2132
2133 case ISD::VAEND:
2134 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2135 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2136
2137 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2138 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002139 case TargetLowering::Custom:
2140 isCustom = true;
2141 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002142 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002144 if (isCustom) {
2145 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2146 if (Tmp1.Val) Result = Tmp1;
2147 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002148 break;
2149 case TargetLowering::Expand:
2150 Result = Tmp1; // Default to a no-op, return the chain
2151 break;
2152 }
2153 break;
2154
2155 case ISD::VASTART:
2156 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2157 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2158
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002159 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2160
Nate Begemanacc398c2006-01-25 18:21:52 +00002161 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2162 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002163 case TargetLowering::Legal: break;
2164 case TargetLowering::Custom:
2165 Tmp1 = TLI.LowerOperation(Result, DAG);
2166 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002167 break;
2168 }
2169 break;
2170
Nate Begeman35ef9132006-01-11 21:21:00 +00002171 case ISD::ROTL:
2172 case ISD::ROTR:
2173 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2174 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002175
2176 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2177 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002179 break;
2180
Nate Begemand88fc032006-01-14 03:14:10 +00002181 case ISD::BSWAP:
2182 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2183 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002184 case TargetLowering::Custom:
2185 assert(0 && "Cannot custom legalize this yet!");
2186 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002187 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002188 break;
2189 case TargetLowering::Promote: {
2190 MVT::ValueType OVT = Tmp1.getValueType();
2191 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2192 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002193
Chris Lattner456a93a2006-01-28 07:39:30 +00002194 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2195 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2196 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2197 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2198 break;
2199 }
2200 case TargetLowering::Expand:
2201 Result = ExpandBSWAP(Tmp1);
2202 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002203 }
2204 break;
2205
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002206 case ISD::CTPOP:
2207 case ISD::CTTZ:
2208 case ISD::CTLZ:
2209 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2210 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002211 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002212 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002213 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002214 break;
2215 case TargetLowering::Promote: {
2216 MVT::ValueType OVT = Tmp1.getValueType();
2217 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002218
2219 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002220 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2221 // Perform the larger operation, then subtract if needed.
2222 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002223 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002224 case ISD::CTPOP:
2225 Result = Tmp1;
2226 break;
2227 case ISD::CTTZ:
2228 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002229 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2230 DAG.getConstant(getSizeInBits(NVT), NVT),
2231 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002232 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002233 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2234 break;
2235 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002236 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002237 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2238 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002239 getSizeInBits(OVT), NVT));
2240 break;
2241 }
2242 break;
2243 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002244 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002245 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002246 break;
2247 }
2248 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002249
Chris Lattner2c8086f2005-04-02 05:00:07 +00002250 // Unary operators
2251 case ISD::FABS:
2252 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002253 case ISD::FSQRT:
2254 case ISD::FSIN:
2255 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002256 Tmp1 = LegalizeOp(Node->getOperand(0));
2257 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002258 case TargetLowering::Promote:
2259 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002260 isCustom = true;
2261 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002262 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002263 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002264 if (isCustom) {
2265 Tmp1 = TLI.LowerOperation(Result, DAG);
2266 if (Tmp1.Val) Result = Tmp1;
2267 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002268 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002269 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002270 switch (Node->getOpcode()) {
2271 default: assert(0 && "Unreachable!");
2272 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002273 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2274 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002275 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002276 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002277 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002278 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2279 MVT::ValueType VT = Node->getValueType(0);
2280 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002281 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002282 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2283 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002284 break;
2285 }
2286 case ISD::FSQRT:
2287 case ISD::FSIN:
2288 case ISD::FCOS: {
2289 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002290 const char *FnName = 0;
2291 switch(Node->getOpcode()) {
2292 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2293 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2294 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2295 default: assert(0 && "Unreachable!");
2296 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002297 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002298 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002299 break;
2300 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002301 }
2302 break;
2303 }
2304 break;
Chris Lattner35481892005-12-23 00:16:34 +00002305
2306 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002307 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002308 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002309 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002310 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2311 Node->getOperand(0).getValueType())) {
2312 default: assert(0 && "Unknown operation action!");
2313 case TargetLowering::Expand:
2314 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2315 break;
2316 case TargetLowering::Legal:
2317 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002318 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002319 break;
2320 }
2321 }
2322 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002323 case ISD::VBIT_CONVERT: {
2324 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2325 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2326
2327 // The input has to be a vector type, we have to either scalarize it, pack
2328 // it, or convert it based on whether the input vector type is legal.
2329 SDNode *InVal = Node->getOperand(0).Val;
2330 unsigned NumElems =
2331 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2332 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2333
2334 // Figure out if there is a Packed type corresponding to this Vector
2335 // type. If so, convert to the packed type.
2336 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2337 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2338 // Turn this into a bit convert of the packed input.
2339 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2340 PackVectorOp(Node->getOperand(0), TVT));
2341 break;
2342 } else if (NumElems == 1) {
2343 // Turn this into a bit convert of the scalar input.
2344 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2345 PackVectorOp(Node->getOperand(0), EVT));
2346 break;
2347 } else {
2348 // FIXME: UNIMP! Store then reload
2349 assert(0 && "Cast from unsupported vector type not implemented yet!");
2350 }
2351 }
2352
Chris Lattner2c8086f2005-04-02 05:00:07 +00002353 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002354 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002355 case ISD::UINT_TO_FP: {
2356 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002357 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2358 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002359 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002360 Node->getOperand(0).getValueType())) {
2361 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002362 case TargetLowering::Custom:
2363 isCustom = true;
2364 // FALLTHROUGH
2365 case TargetLowering::Legal:
2366 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002367 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002368 if (isCustom) {
2369 Tmp1 = TLI.LowerOperation(Result, DAG);
2370 if (Tmp1.Val) Result = Tmp1;
2371 }
2372 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002373 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002374 Result = ExpandLegalINT_TO_FP(isSigned,
2375 LegalizeOp(Node->getOperand(0)),
2376 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002377 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002378 case TargetLowering::Promote:
2379 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2380 Node->getValueType(0),
2381 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002382 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002383 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002384 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002385 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002386 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2387 Node->getValueType(0), Node->getOperand(0));
2388 break;
2389 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002390 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002391 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002392 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2393 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002394 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002395 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2396 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002397 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002398 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2399 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002400 break;
2401 }
2402 break;
2403 }
2404 case ISD::TRUNCATE:
2405 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2406 case Legal:
2407 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002408 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002409 break;
2410 case Expand:
2411 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2412
2413 // Since the result is legal, we should just be able to truncate the low
2414 // part of the source.
2415 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2416 break;
2417 case Promote:
2418 Result = PromoteOp(Node->getOperand(0));
2419 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2420 break;
2421 }
2422 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002423
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002424 case ISD::FP_TO_SINT:
2425 case ISD::FP_TO_UINT:
2426 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2427 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002428 Tmp1 = LegalizeOp(Node->getOperand(0));
2429
Chris Lattner1618beb2005-07-29 00:11:56 +00002430 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2431 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002432 case TargetLowering::Custom:
2433 isCustom = true;
2434 // FALLTHROUGH
2435 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002436 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002437 if (isCustom) {
2438 Tmp1 = TLI.LowerOperation(Result, DAG);
2439 if (Tmp1.Val) Result = Tmp1;
2440 }
2441 break;
2442 case TargetLowering::Promote:
2443 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2444 Node->getOpcode() == ISD::FP_TO_SINT);
2445 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002446 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002447 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2448 SDOperand True, False;
2449 MVT::ValueType VT = Node->getOperand(0).getValueType();
2450 MVT::ValueType NVT = Node->getValueType(0);
2451 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2452 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2453 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2454 Node->getOperand(0), Tmp2, ISD::SETLT);
2455 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2456 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002457 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002458 Tmp2));
2459 False = DAG.getNode(ISD::XOR, NVT, False,
2460 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002461 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2462 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002463 } else {
2464 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2465 }
2466 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002467 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002468 break;
2469 case Expand:
2470 assert(0 && "Shouldn't need to expand other operators here!");
2471 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002472 Tmp1 = PromoteOp(Node->getOperand(0));
2473 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2474 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002475 break;
2476 }
2477 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002478
Chris Lattner13c78e22005-09-02 00:18:10 +00002479 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002480 case ISD::ZERO_EXTEND:
2481 case ISD::SIGN_EXTEND:
2482 case ISD::FP_EXTEND:
2483 case ISD::FP_ROUND:
2484 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002485 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002486 case Legal:
2487 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002488 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002489 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002490 case Promote:
2491 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002492 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002493 Tmp1 = PromoteOp(Node->getOperand(0));
2494 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002495 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002496 case ISD::ZERO_EXTEND:
2497 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002498 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002499 Result = DAG.getZeroExtendInReg(Result,
2500 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002501 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002502 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002503 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002504 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002505 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002506 Result,
2507 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002508 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002509 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002510 Result = PromoteOp(Node->getOperand(0));
2511 if (Result.getValueType() != Op.getValueType())
2512 // Dynamically dead while we have only 2 FP types.
2513 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2514 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002515 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002516 Result = PromoteOp(Node->getOperand(0));
2517 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2518 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002519 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002520 }
2521 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002522 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002523 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002524 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002525 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002526
2527 // If this operation is not supported, convert it to a shl/shr or load/store
2528 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002529 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2530 default: assert(0 && "This action not supported for this op yet!");
2531 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002532 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002533 break;
2534 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002535 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002536 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002537 // NOTE: we could fall back on load/store here too for targets without
2538 // SAR. However, it is doubtful that any exist.
2539 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2540 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002541 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002542 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2543 Node->getOperand(0), ShiftCst);
2544 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2545 Result, ShiftCst);
2546 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2547 // The only way we can lower this is to turn it into a STORETRUNC,
2548 // EXTLOAD pair, targetting a temporary location (a stack slot).
2549
2550 // NOTE: there is a choice here between constantly creating new stack
2551 // slots and always reusing the same one. We currently always create
2552 // new ones, as reuse may inhibit scheduling.
2553 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2554 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2555 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2556 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002557 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002558 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2559 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2560 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002561 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002562 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002563 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2564 Result, StackSlot, DAG.getSrcValue(NULL),
2565 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002566 } else {
2567 assert(0 && "Unknown op");
2568 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002569 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002570 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002571 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002572 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002573 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002574
2575 // Make sure that the generated code is itself legal.
2576 if (Result != Op)
2577 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002578
Chris Lattner45982da2005-05-12 16:53:42 +00002579 // Note that LegalizeOp may be reentered even from single-use nodes, which
2580 // means that we always must cache transformed nodes.
2581 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002582 return Result;
2583}
2584
Chris Lattner8b6fa222005-01-15 22:16:26 +00002585/// PromoteOp - Given an operation that produces a value in an invalid type,
2586/// promote it to compute the value into a larger type. The produced value will
2587/// have the correct bits for the low portion of the register, but no guarantee
2588/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002589SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2590 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002591 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002592 assert(getTypeAction(VT) == Promote &&
2593 "Caller should expand or legalize operands that are not promotable!");
2594 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2595 "Cannot promote to smaller type!");
2596
Chris Lattner03c85462005-01-15 05:21:40 +00002597 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002598 SDOperand Result;
2599 SDNode *Node = Op.Val;
2600
Chris Lattner6fdcb252005-09-02 20:32:45 +00002601 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2602 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002603
Chris Lattner03c85462005-01-15 05:21:40 +00002604 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002605 case ISD::CopyFromReg:
2606 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002607 default:
2608 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2609 assert(0 && "Do not know how to promote this operator!");
2610 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002611 case ISD::UNDEF:
2612 Result = DAG.getNode(ISD::UNDEF, NVT);
2613 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002614 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002615 if (VT != MVT::i1)
2616 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2617 else
2618 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002619 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2620 break;
2621 case ISD::ConstantFP:
2622 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2623 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2624 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002625
Chris Lattner82fbfb62005-01-18 02:59:52 +00002626 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002627 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002628 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2629 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002630 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002631
2632 case ISD::TRUNCATE:
2633 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2634 case Legal:
2635 Result = LegalizeOp(Node->getOperand(0));
2636 assert(Result.getValueType() >= NVT &&
2637 "This truncation doesn't make sense!");
2638 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2639 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2640 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002641 case Promote:
2642 // The truncation is not required, because we don't guarantee anything
2643 // about high bits anyway.
2644 Result = PromoteOp(Node->getOperand(0));
2645 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002646 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002647 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2648 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002649 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002650 }
2651 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002652 case ISD::SIGN_EXTEND:
2653 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002654 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002655 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2656 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2657 case Legal:
2658 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002659 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002660 break;
2661 case Promote:
2662 // Promote the reg if it's smaller.
2663 Result = PromoteOp(Node->getOperand(0));
2664 // The high bits are not guaranteed to be anything. Insert an extend.
2665 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002666 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002667 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002668 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002669 Result = DAG.getZeroExtendInReg(Result,
2670 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002671 break;
2672 }
2673 break;
Chris Lattner35481892005-12-23 00:16:34 +00002674 case ISD::BIT_CONVERT:
2675 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2676 Result = PromoteOp(Result);
2677 break;
2678
Chris Lattner8b6fa222005-01-15 22:16:26 +00002679 case ISD::FP_EXTEND:
2680 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2681 case ISD::FP_ROUND:
2682 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2683 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2684 case Promote: assert(0 && "Unreachable with 2 FP types!");
2685 case Legal:
2686 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002687 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002688 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002689 break;
2690 }
2691 break;
2692
2693 case ISD::SINT_TO_FP:
2694 case ISD::UINT_TO_FP:
2695 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2696 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002697 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002698 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002699 break;
2700
2701 case Promote:
2702 Result = PromoteOp(Node->getOperand(0));
2703 if (Node->getOpcode() == ISD::SINT_TO_FP)
2704 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002705 Result,
2706 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002707 else
Chris Lattner23993562005-04-13 02:38:47 +00002708 Result = DAG.getZeroExtendInReg(Result,
2709 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002710 // No extra round required here.
2711 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002712 break;
2713 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002714 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2715 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002716 // Round if we cannot tolerate excess precision.
2717 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002718 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2719 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002720 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002721 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002722 break;
2723
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002724 case ISD::SIGN_EXTEND_INREG:
2725 Result = PromoteOp(Node->getOperand(0));
2726 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2727 Node->getOperand(1));
2728 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002729 case ISD::FP_TO_SINT:
2730 case ISD::FP_TO_UINT:
2731 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2732 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002733 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002734 break;
2735 case Promote:
2736 // The input result is prerounded, so we don't have to do anything
2737 // special.
2738 Tmp1 = PromoteOp(Node->getOperand(0));
2739 break;
2740 case Expand:
2741 assert(0 && "not implemented");
2742 }
Nate Begemand2558e32005-08-14 01:20:53 +00002743 // If we're promoting a UINT to a larger size, check to see if the new node
2744 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2745 // we can use that instead. This allows us to generate better code for
2746 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2747 // legal, such as PowerPC.
2748 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002749 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002750 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2751 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002752 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2753 } else {
2754 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2755 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002756 break;
2757
Chris Lattner2c8086f2005-04-02 05:00:07 +00002758 case ISD::FABS:
2759 case ISD::FNEG:
2760 Tmp1 = PromoteOp(Node->getOperand(0));
2761 assert(Tmp1.getValueType() == NVT);
2762 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2763 // NOTE: we do not have to do any extra rounding here for
2764 // NoExcessFPPrecision, because we know the input will have the appropriate
2765 // precision, and these operations don't modify precision at all.
2766 break;
2767
Chris Lattnerda6ba872005-04-28 21:44:33 +00002768 case ISD::FSQRT:
2769 case ISD::FSIN:
2770 case ISD::FCOS:
2771 Tmp1 = PromoteOp(Node->getOperand(0));
2772 assert(Tmp1.getValueType() == NVT);
2773 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002774 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002775 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2776 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002777 break;
2778
Chris Lattner03c85462005-01-15 05:21:40 +00002779 case ISD::AND:
2780 case ISD::OR:
2781 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002782 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002783 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002784 case ISD::MUL:
2785 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002786 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002787 // that too is okay if they are integer operations.
2788 Tmp1 = PromoteOp(Node->getOperand(0));
2789 Tmp2 = PromoteOp(Node->getOperand(1));
2790 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2791 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002792 break;
2793 case ISD::FADD:
2794 case ISD::FSUB:
2795 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002796 Tmp1 = PromoteOp(Node->getOperand(0));
2797 Tmp2 = PromoteOp(Node->getOperand(1));
2798 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2799 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2800
2801 // Floating point operations will give excess precision that we may not be
2802 // able to tolerate. If we DO allow excess precision, just leave it,
2803 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002804 // FIXME: Why would we need to round FP ops more than integer ones?
2805 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002806 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002807 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2808 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002809 break;
2810
Chris Lattner8b6fa222005-01-15 22:16:26 +00002811 case ISD::SDIV:
2812 case ISD::SREM:
2813 // These operators require that their input be sign extended.
2814 Tmp1 = PromoteOp(Node->getOperand(0));
2815 Tmp2 = PromoteOp(Node->getOperand(1));
2816 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002817 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2818 DAG.getValueType(VT));
2819 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2820 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002821 }
2822 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2823
2824 // Perform FP_ROUND: this is probably overly pessimistic.
2825 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002826 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2827 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002828 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002829 case ISD::FDIV:
2830 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002831 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002832 // These operators require that their input be fp extended.
2833 Tmp1 = PromoteOp(Node->getOperand(0));
2834 Tmp2 = PromoteOp(Node->getOperand(1));
2835 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2836
2837 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002838 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002839 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2840 DAG.getValueType(VT));
2841 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002842
2843 case ISD::UDIV:
2844 case ISD::UREM:
2845 // These operators require that their input be zero extended.
2846 Tmp1 = PromoteOp(Node->getOperand(0));
2847 Tmp2 = PromoteOp(Node->getOperand(1));
2848 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002849 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2850 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002851 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2852 break;
2853
2854 case ISD::SHL:
2855 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002856 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002857 break;
2858 case ISD::SRA:
2859 // The input value must be properly sign extended.
2860 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002861 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2862 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002863 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002864 break;
2865 case ISD::SRL:
2866 // The input value must be properly zero extended.
2867 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002868 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002869 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002870 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002871
2872 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002873 Tmp1 = Node->getOperand(0); // Get the chain.
2874 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002875 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2876 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2877 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2878 } else {
2879 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2880 Node->getOperand(2));
2881 // Increment the pointer, VAList, to the next vaarg
2882 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2883 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2884 TLI.getPointerTy()));
2885 // Store the incremented VAList to the legalized pointer
2886 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2887 Node->getOperand(2));
2888 // Load the actual argument out of the pointer VAList
2889 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2890 DAG.getSrcValue(0), VT);
2891 }
2892 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002893 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002894 break;
2895
Chris Lattner03c85462005-01-15 05:21:40 +00002896 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002897 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2898 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002899 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002900 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002901 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002902 case ISD::SEXTLOAD:
2903 case ISD::ZEXTLOAD:
2904 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002905 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2906 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002907 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002908 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002909 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002910 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002911 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002912 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2913 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002914 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002915 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002916 case ISD::SELECT_CC:
2917 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2918 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2919 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002920 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002921 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002922 case ISD::BSWAP:
2923 Tmp1 = Node->getOperand(0);
2924 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2925 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2926 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2927 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2928 TLI.getShiftAmountTy()));
2929 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002930 case ISD::CTPOP:
2931 case ISD::CTTZ:
2932 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002933 // Zero extend the argument
2934 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002935 // Perform the larger operation, then subtract if needed.
2936 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002937 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002938 case ISD::CTPOP:
2939 Result = Tmp1;
2940 break;
2941 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002942 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002943 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002944 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002945 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002946 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002947 break;
2948 case ISD::CTLZ:
2949 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002950 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2951 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002952 getSizeInBits(VT), NVT));
2953 break;
2954 }
2955 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002956 }
2957
2958 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002959
2960 // Make sure the result is itself legal.
2961 Result = LegalizeOp(Result);
2962
2963 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002964 AddPromotedOperand(Op, Result);
2965 return Result;
2966}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002967
Nate Begeman750ac1b2006-02-01 07:19:44 +00002968/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2969/// with condition CC on the current target. This usually involves legalizing
2970/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2971/// there may be no choice but to create a new SetCC node to represent the
2972/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2973/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2974void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2975 SDOperand &RHS,
2976 SDOperand &CC) {
2977 SDOperand Tmp1, Tmp2, Result;
2978
2979 switch (getTypeAction(LHS.getValueType())) {
2980 case Legal:
2981 Tmp1 = LegalizeOp(LHS); // LHS
2982 Tmp2 = LegalizeOp(RHS); // RHS
2983 break;
2984 case Promote:
2985 Tmp1 = PromoteOp(LHS); // LHS
2986 Tmp2 = PromoteOp(RHS); // RHS
2987
2988 // If this is an FP compare, the operands have already been extended.
2989 if (MVT::isInteger(LHS.getValueType())) {
2990 MVT::ValueType VT = LHS.getValueType();
2991 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2992
2993 // Otherwise, we have to insert explicit sign or zero extends. Note
2994 // that we could insert sign extends for ALL conditions, but zero extend
2995 // is cheaper on many machines (an AND instead of two shifts), so prefer
2996 // it.
2997 switch (cast<CondCodeSDNode>(CC)->get()) {
2998 default: assert(0 && "Unknown integer comparison!");
2999 case ISD::SETEQ:
3000 case ISD::SETNE:
3001 case ISD::SETUGE:
3002 case ISD::SETUGT:
3003 case ISD::SETULE:
3004 case ISD::SETULT:
3005 // ALL of these operations will work if we either sign or zero extend
3006 // the operands (including the unsigned comparisons!). Zero extend is
3007 // usually a simpler/cheaper operation, so prefer it.
3008 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3009 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3010 break;
3011 case ISD::SETGE:
3012 case ISD::SETGT:
3013 case ISD::SETLT:
3014 case ISD::SETLE:
3015 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3016 DAG.getValueType(VT));
3017 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3018 DAG.getValueType(VT));
3019 break;
3020 }
3021 }
3022 break;
3023 case Expand:
3024 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3025 ExpandOp(LHS, LHSLo, LHSHi);
3026 ExpandOp(RHS, RHSLo, RHSHi);
3027 switch (cast<CondCodeSDNode>(CC)->get()) {
3028 case ISD::SETEQ:
3029 case ISD::SETNE:
3030 if (RHSLo == RHSHi)
3031 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3032 if (RHSCST->isAllOnesValue()) {
3033 // Comparison to -1.
3034 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3035 Tmp2 = RHSLo;
3036 break;
3037 }
3038
3039 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3040 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3041 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3042 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3043 break;
3044 default:
3045 // If this is a comparison of the sign bit, just look at the top part.
3046 // X > -1, x < 0
3047 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3048 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3049 CST->getValue() == 0) || // X < 0
3050 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3051 CST->isAllOnesValue())) { // X > -1
3052 Tmp1 = LHSHi;
3053 Tmp2 = RHSHi;
3054 break;
3055 }
3056
3057 // FIXME: This generated code sucks.
3058 ISD::CondCode LowCC;
3059 switch (cast<CondCodeSDNode>(CC)->get()) {
3060 default: assert(0 && "Unknown integer setcc!");
3061 case ISD::SETLT:
3062 case ISD::SETULT: LowCC = ISD::SETULT; break;
3063 case ISD::SETGT:
3064 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3065 case ISD::SETLE:
3066 case ISD::SETULE: LowCC = ISD::SETULE; break;
3067 case ISD::SETGE:
3068 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3069 }
3070
3071 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3072 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3073 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3074
3075 // NOTE: on targets without efficient SELECT of bools, we can always use
3076 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3077 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3078 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3079 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3080 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3081 Result, Tmp1, Tmp2));
3082 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003083 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003084 }
3085 }
3086 LHS = Tmp1;
3087 RHS = Tmp2;
3088}
3089
Chris Lattner35481892005-12-23 00:16:34 +00003090/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003091/// The resultant code need not be legal. Note that SrcOp is the input operand
3092/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003093SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3094 SDOperand SrcOp) {
3095 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003096 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003097
3098 // Emit a store to the stack slot.
3099 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003100 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003101 // Result is a load from the stack slot.
3102 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3103}
3104
Chris Lattnerce872152006-03-19 06:31:19 +00003105/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3106/// support the operation, but do support the resultant packed vector type.
3107SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3108
3109 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003110 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003111 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003112 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003113 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003114 std::map<SDOperand, std::vector<unsigned> > Values;
3115 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003116 bool isConstant = true;
3117 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3118 SplatValue.getOpcode() != ISD::UNDEF)
3119 isConstant = false;
3120
Evan Cheng033e6812006-03-24 01:17:21 +00003121 for (unsigned i = 1; i < NumElems; ++i) {
3122 SDOperand V = Node->getOperand(i);
3123 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3124 if (I != Values.end())
3125 I->second.push_back(i);
3126 else
3127 Values[V].push_back(i);
3128 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003129 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003130 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003131 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003132
3133 // If this isn't a constant element or an undef, we can't use a constant
3134 // pool load.
3135 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3136 V.getOpcode() != ISD::UNDEF)
3137 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003138 }
3139
3140 if (isOnlyLowElement) {
3141 // If the low element is an undef too, then this whole things is an undef.
3142 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3143 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3144 // Otherwise, turn this into a scalar_to_vector node.
3145 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3146 Node->getOperand(0));
3147 }
3148
Chris Lattner2eb86532006-03-24 07:29:17 +00003149 // If all elements are constants, create a load from the constant pool.
3150 if (isConstant) {
3151 MVT::ValueType VT = Node->getValueType(0);
3152 const Type *OpNTy =
3153 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3154 std::vector<Constant*> CV;
3155 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3156 if (ConstantFPSDNode *V =
3157 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3158 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3159 } else if (ConstantSDNode *V =
3160 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3161 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3162 } else {
3163 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3164 CV.push_back(UndefValue::get(OpNTy));
3165 }
3166 }
3167 Constant *CP = ConstantPacked::get(CV);
3168 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3169 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3170 DAG.getSrcValue(NULL));
3171 }
3172
Chris Lattner87100e02006-03-20 01:52:29 +00003173 if (SplatValue.Val) { // Splat of one value?
3174 // Build the shuffle constant vector: <0, 0, 0, 0>
3175 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003176 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003177 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003178 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner87100e02006-03-20 01:52:29 +00003179 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3180
3181 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3182 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3183 // Get the splatted value into the low element of a vector register.
3184 SDOperand LowValVec =
3185 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3186
3187 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3188 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3189 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3190 SplatMask);
3191 }
3192 }
3193
Evan Cheng033e6812006-03-24 01:17:21 +00003194 // If there are only two unique elements, we may be able to turn this into a
3195 // vector shuffle.
3196 if (Values.size() == 2) {
3197 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3198 MVT::ValueType MaskVT =
3199 MVT::getIntVectorWithNumElements(NumElems);
3200 std::vector<SDOperand> MaskVec(NumElems);
3201 unsigned i = 0;
3202 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3203 E = Values.end(); I != E; ++I) {
3204 for (std::vector<unsigned>::iterator II = I->second.begin(),
3205 EE = I->second.end(); II != EE; ++II)
3206 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3207 i += NumElems;
3208 }
3209 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3210
3211 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3212 if (TLI.isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
3213 std::vector<SDOperand> Ops;
3214 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3215 E = Values.end(); I != E; ++I) {
3216 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3217 I->first);
3218 Ops.push_back(Op);
3219 }
3220 Ops.push_back(ShuffleMask);
3221
3222 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3223 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3224 }
3225 }
Chris Lattnerce872152006-03-19 06:31:19 +00003226
3227 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3228 // aligned object on the stack, store each element into it, then load
3229 // the result as a vector.
3230 MVT::ValueType VT = Node->getValueType(0);
3231 // Create the stack frame object.
3232 SDOperand FIPtr = CreateStackTemporary(VT);
3233
3234 // Emit a store of each element to the stack slot.
3235 std::vector<SDOperand> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003236 unsigned TypeByteSize =
3237 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3238 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3239 // Store (in the right endianness) the elements to memory.
3240 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3241 // Ignore undef elements.
3242 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3243
Chris Lattner841c8822006-03-22 01:46:54 +00003244 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003245
3246 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3247 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3248
3249 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3250 Node->getOperand(i), Idx,
3251 DAG.getSrcValue(NULL)));
3252 }
3253
3254 SDOperand StoreChain;
3255 if (!Stores.empty()) // Not all undef elements?
3256 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3257 else
3258 StoreChain = DAG.getEntryNode();
3259
3260 // Result is a load from the stack slot.
3261 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3262}
3263
3264/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3265/// specified value type.
3266SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3267 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3268 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3269 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3270 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3271}
3272
Chris Lattner5b359c62005-04-02 04:00:59 +00003273void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3274 SDOperand Op, SDOperand Amt,
3275 SDOperand &Lo, SDOperand &Hi) {
3276 // Expand the subcomponents.
3277 SDOperand LHSL, LHSH;
3278 ExpandOp(Op, LHSL, LHSH);
3279
3280 std::vector<SDOperand> Ops;
3281 Ops.push_back(LHSL);
3282 Ops.push_back(LHSH);
3283 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003284 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003285 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003286 Hi = Lo.getValue(1);
3287}
3288
3289
Chris Lattnere34b3962005-01-19 04:19:40 +00003290/// ExpandShift - Try to find a clever way to expand this shift operation out to
3291/// smaller elements. If we can't find a way that is more efficient than a
3292/// libcall on this target, return false. Otherwise, return true with the
3293/// low-parts expanded into Lo and Hi.
3294bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3295 SDOperand &Lo, SDOperand &Hi) {
3296 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3297 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003298
Chris Lattnere34b3962005-01-19 04:19:40 +00003299 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003300 SDOperand ShAmt = LegalizeOp(Amt);
3301 MVT::ValueType ShTy = ShAmt.getValueType();
3302 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3303 unsigned NVTBits = MVT::getSizeInBits(NVT);
3304
3305 // Handle the case when Amt is an immediate. Other cases are currently broken
3306 // and are disabled.
3307 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3308 unsigned Cst = CN->getValue();
3309 // Expand the incoming operand to be shifted, so that we have its parts
3310 SDOperand InL, InH;
3311 ExpandOp(Op, InL, InH);
3312 switch(Opc) {
3313 case ISD::SHL:
3314 if (Cst > VTBits) {
3315 Lo = DAG.getConstant(0, NVT);
3316 Hi = DAG.getConstant(0, NVT);
3317 } else if (Cst > NVTBits) {
3318 Lo = DAG.getConstant(0, NVT);
3319 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003320 } else if (Cst == NVTBits) {
3321 Lo = DAG.getConstant(0, NVT);
3322 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003323 } else {
3324 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3325 Hi = DAG.getNode(ISD::OR, NVT,
3326 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3327 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3328 }
3329 return true;
3330 case ISD::SRL:
3331 if (Cst > VTBits) {
3332 Lo = DAG.getConstant(0, NVT);
3333 Hi = DAG.getConstant(0, NVT);
3334 } else if (Cst > NVTBits) {
3335 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3336 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003337 } else if (Cst == NVTBits) {
3338 Lo = InH;
3339 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003340 } else {
3341 Lo = DAG.getNode(ISD::OR, NVT,
3342 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3343 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3344 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3345 }
3346 return true;
3347 case ISD::SRA:
3348 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003349 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003350 DAG.getConstant(NVTBits-1, ShTy));
3351 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003352 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003353 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003354 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003355 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003356 } else if (Cst == NVTBits) {
3357 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003358 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003359 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003360 } else {
3361 Lo = DAG.getNode(ISD::OR, NVT,
3362 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3363 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3364 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3365 }
3366 return true;
3367 }
3368 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003369 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003370}
Chris Lattner77e77a62005-01-21 06:05:23 +00003371
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003372
Chris Lattner77e77a62005-01-21 06:05:23 +00003373// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3374// does not fit into a register, return the lo part and set the hi part to the
3375// by-reg argument. If it does fit into a single register, return the result
3376// and leave the Hi part unset.
3377SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3378 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003379 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3380 // The input chain to this libcall is the entry node of the function.
3381 // Legalizing the call will automatically add the previous call to the
3382 // dependence.
3383 SDOperand InChain = DAG.getEntryNode();
3384
Chris Lattner77e77a62005-01-21 06:05:23 +00003385 TargetLowering::ArgListTy Args;
3386 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3387 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3388 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3389 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3390 }
3391 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003392
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003393 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003394 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003395 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003396 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3397 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003398
Chris Lattner6831a812006-02-13 09:18:02 +00003399 // Legalize the call sequence, starting with the chain. This will advance
3400 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3401 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3402 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003403 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003404 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003405 default: assert(0 && "Unknown thing");
3406 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003407 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003408 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003409 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003410 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003411 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003412 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003413 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003414}
3415
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003416
Chris Lattner77e77a62005-01-21 06:05:23 +00003417/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3418/// destination type is legal.
3419SDOperand SelectionDAGLegalize::
3420ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003421 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003422 assert(getTypeAction(Source.getValueType()) == Expand &&
3423 "This is not an expansion!");
3424 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3425
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003426 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003427 assert(Source.getValueType() == MVT::i64 &&
3428 "This only works for 64-bit -> FP");
3429 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3430 // incoming integer is set. To handle this, we dynamically test to see if
3431 // it is set, and, if so, add a fudge factor.
3432 SDOperand Lo, Hi;
3433 ExpandOp(Source, Lo, Hi);
3434
Chris Lattner66de05b2005-05-13 04:45:13 +00003435 // If this is unsigned, and not supported, first perform the conversion to
3436 // signed, then adjust the result if the sign bit is set.
3437 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3438 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3439
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003440 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3441 DAG.getConstant(0, Hi.getValueType()),
3442 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003443 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3444 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3445 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003446 uint64_t FF = 0x5f800000ULL;
3447 if (TLI.isLittleEndian()) FF <<= 32;
3448 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003449
Chris Lattner5839bf22005-08-26 17:15:30 +00003450 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003451 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3452 SDOperand FudgeInReg;
3453 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003454 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3455 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003456 else {
3457 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003458 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3459 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003460 }
Chris Lattner473a9902005-09-29 06:44:39 +00003461 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003462 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003463
Chris Lattnera88a2602005-05-14 05:33:54 +00003464 // Check to see if the target has a custom way to lower this. If so, use it.
3465 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3466 default: assert(0 && "This action not implemented for this operation!");
3467 case TargetLowering::Legal:
3468 case TargetLowering::Expand:
3469 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003470 case TargetLowering::Custom: {
3471 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3472 Source), DAG);
3473 if (NV.Val)
3474 return LegalizeOp(NV);
3475 break; // The target decided this was legal after all
3476 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003477 }
3478
Chris Lattner13689e22005-05-12 07:00:44 +00003479 // Expand the source, then glue it back together for the call. We must expand
3480 // the source in case it is shared (this pass of legalize must traverse it).
3481 SDOperand SrcLo, SrcHi;
3482 ExpandOp(Source, SrcLo, SrcHi);
3483 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3484
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003485 const char *FnName = 0;
3486 if (DestTy == MVT::f32)
3487 FnName = "__floatdisf";
3488 else {
3489 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3490 FnName = "__floatdidf";
3491 }
Chris Lattner6831a812006-02-13 09:18:02 +00003492
3493 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3494 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003495 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003496}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003497
Chris Lattner22cde6a2006-01-28 08:25:58 +00003498/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3499/// INT_TO_FP operation of the specified operand when the target requests that
3500/// we expand it. At this point, we know that the result and operand types are
3501/// legal for the target.
3502SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3503 SDOperand Op0,
3504 MVT::ValueType DestVT) {
3505 if (Op0.getValueType() == MVT::i32) {
3506 // simple 32-bit [signed|unsigned] integer to float/double expansion
3507
3508 // get the stack frame index of a 8 byte buffer
3509 MachineFunction &MF = DAG.getMachineFunction();
3510 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3511 // get address of 8 byte buffer
3512 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3513 // word offset constant for Hi/Lo address computation
3514 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3515 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00003516 SDOperand Hi = StackSlot;
3517 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3518 if (TLI.isLittleEndian())
3519 std::swap(Hi, Lo);
3520
Chris Lattner22cde6a2006-01-28 08:25:58 +00003521 // if signed map to unsigned space
3522 SDOperand Op0Mapped;
3523 if (isSigned) {
3524 // constant used to invert sign bit (signed to unsigned mapping)
3525 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3526 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3527 } else {
3528 Op0Mapped = Op0;
3529 }
3530 // store the lo of the constructed double - based on integer input
3531 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3532 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3533 // initial hi portion of constructed double
3534 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3535 // store the hi of the constructed double - biased exponent
3536 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3537 InitialHi, Hi, DAG.getSrcValue(NULL));
3538 // load the constructed double
3539 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3540 DAG.getSrcValue(NULL));
3541 // FP constant to bias correct the final result
3542 SDOperand Bias = DAG.getConstantFP(isSigned ?
3543 BitsToDouble(0x4330000080000000ULL)
3544 : BitsToDouble(0x4330000000000000ULL),
3545 MVT::f64);
3546 // subtract the bias
3547 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3548 // final result
3549 SDOperand Result;
3550 // handle final rounding
3551 if (DestVT == MVT::f64) {
3552 // do nothing
3553 Result = Sub;
3554 } else {
3555 // if f32 then cast to f32
3556 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3557 }
3558 return Result;
3559 }
3560 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3561 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3562
3563 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3564 DAG.getConstant(0, Op0.getValueType()),
3565 ISD::SETLT);
3566 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3567 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3568 SignSet, Four, Zero);
3569
3570 // If the sign bit of the integer is set, the large number will be treated
3571 // as a negative number. To counteract this, the dynamic code adds an
3572 // offset depending on the data type.
3573 uint64_t FF;
3574 switch (Op0.getValueType()) {
3575 default: assert(0 && "Unsupported integer type!");
3576 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3577 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3578 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3579 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3580 }
3581 if (TLI.isLittleEndian()) FF <<= 32;
3582 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3583
3584 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3585 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3586 SDOperand FudgeInReg;
3587 if (DestVT == MVT::f32)
3588 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3589 DAG.getSrcValue(NULL));
3590 else {
3591 assert(DestVT == MVT::f64 && "Unexpected conversion");
3592 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3593 DAG.getEntryNode(), CPIdx,
3594 DAG.getSrcValue(NULL), MVT::f32));
3595 }
3596
3597 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3598}
3599
3600/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3601/// *INT_TO_FP operation of the specified operand when the target requests that
3602/// we promote it. At this point, we know that the result and operand types are
3603/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3604/// operation that takes a larger input.
3605SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3606 MVT::ValueType DestVT,
3607 bool isSigned) {
3608 // First step, figure out the appropriate *INT_TO_FP operation to use.
3609 MVT::ValueType NewInTy = LegalOp.getValueType();
3610
3611 unsigned OpToUse = 0;
3612
3613 // Scan for the appropriate larger type to use.
3614 while (1) {
3615 NewInTy = (MVT::ValueType)(NewInTy+1);
3616 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3617
3618 // If the target supports SINT_TO_FP of this type, use it.
3619 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3620 default: break;
3621 case TargetLowering::Legal:
3622 if (!TLI.isTypeLegal(NewInTy))
3623 break; // Can't use this datatype.
3624 // FALL THROUGH.
3625 case TargetLowering::Custom:
3626 OpToUse = ISD::SINT_TO_FP;
3627 break;
3628 }
3629 if (OpToUse) break;
3630 if (isSigned) continue;
3631
3632 // If the target supports UINT_TO_FP of this type, use it.
3633 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3634 default: break;
3635 case TargetLowering::Legal:
3636 if (!TLI.isTypeLegal(NewInTy))
3637 break; // Can't use this datatype.
3638 // FALL THROUGH.
3639 case TargetLowering::Custom:
3640 OpToUse = ISD::UINT_TO_FP;
3641 break;
3642 }
3643 if (OpToUse) break;
3644
3645 // Otherwise, try a larger type.
3646 }
3647
3648 // Okay, we found the operation and type to use. Zero extend our input to the
3649 // desired type then run the operation on it.
3650 return DAG.getNode(OpToUse, DestVT,
3651 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3652 NewInTy, LegalOp));
3653}
3654
3655/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3656/// FP_TO_*INT operation of the specified operand when the target requests that
3657/// we promote it. At this point, we know that the result and operand types are
3658/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3659/// operation that returns a larger result.
3660SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3661 MVT::ValueType DestVT,
3662 bool isSigned) {
3663 // First step, figure out the appropriate FP_TO*INT operation to use.
3664 MVT::ValueType NewOutTy = DestVT;
3665
3666 unsigned OpToUse = 0;
3667
3668 // Scan for the appropriate larger type to use.
3669 while (1) {
3670 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3671 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3672
3673 // If the target supports FP_TO_SINT returning this type, use it.
3674 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3675 default: break;
3676 case TargetLowering::Legal:
3677 if (!TLI.isTypeLegal(NewOutTy))
3678 break; // Can't use this datatype.
3679 // FALL THROUGH.
3680 case TargetLowering::Custom:
3681 OpToUse = ISD::FP_TO_SINT;
3682 break;
3683 }
3684 if (OpToUse) break;
3685
3686 // If the target supports FP_TO_UINT of this type, use it.
3687 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3688 default: break;
3689 case TargetLowering::Legal:
3690 if (!TLI.isTypeLegal(NewOutTy))
3691 break; // Can't use this datatype.
3692 // FALL THROUGH.
3693 case TargetLowering::Custom:
3694 OpToUse = ISD::FP_TO_UINT;
3695 break;
3696 }
3697 if (OpToUse) break;
3698
3699 // Otherwise, try a larger type.
3700 }
3701
3702 // Okay, we found the operation and type to use. Truncate the result of the
3703 // extended FP_TO_*INT operation to the desired size.
3704 return DAG.getNode(ISD::TRUNCATE, DestVT,
3705 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3706}
3707
3708/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3709///
3710SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3711 MVT::ValueType VT = Op.getValueType();
3712 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3713 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3714 switch (VT) {
3715 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3716 case MVT::i16:
3717 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3718 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3719 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3720 case MVT::i32:
3721 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3722 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3723 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3724 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3725 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3726 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3727 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3728 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3729 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3730 case MVT::i64:
3731 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3732 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3733 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3734 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3735 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3736 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3737 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3738 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3739 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3740 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3741 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3742 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3743 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3744 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3745 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3746 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3747 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3748 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3749 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3750 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3751 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3752 }
3753}
3754
3755/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3756///
3757SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3758 switch (Opc) {
3759 default: assert(0 && "Cannot expand this yet!");
3760 case ISD::CTPOP: {
3761 static const uint64_t mask[6] = {
3762 0x5555555555555555ULL, 0x3333333333333333ULL,
3763 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3764 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3765 };
3766 MVT::ValueType VT = Op.getValueType();
3767 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3768 unsigned len = getSizeInBits(VT);
3769 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3770 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3771 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3772 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3773 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3774 DAG.getNode(ISD::AND, VT,
3775 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3776 }
3777 return Op;
3778 }
3779 case ISD::CTLZ: {
3780 // for now, we do this:
3781 // x = x | (x >> 1);
3782 // x = x | (x >> 2);
3783 // ...
3784 // x = x | (x >>16);
3785 // x = x | (x >>32); // for 64-bit input
3786 // return popcount(~x);
3787 //
3788 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3789 MVT::ValueType VT = Op.getValueType();
3790 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3791 unsigned len = getSizeInBits(VT);
3792 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3793 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3794 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3795 }
3796 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3797 return DAG.getNode(ISD::CTPOP, VT, Op);
3798 }
3799 case ISD::CTTZ: {
3800 // for now, we use: { return popcount(~x & (x - 1)); }
3801 // unless the target has ctlz but not ctpop, in which case we use:
3802 // { return 32 - nlz(~x & (x-1)); }
3803 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3804 MVT::ValueType VT = Op.getValueType();
3805 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3806 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3807 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3808 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3809 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3810 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3811 TLI.isOperationLegal(ISD::CTLZ, VT))
3812 return DAG.getNode(ISD::SUB, VT,
3813 DAG.getConstant(getSizeInBits(VT), VT),
3814 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3815 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3816 }
3817 }
3818}
Chris Lattnere34b3962005-01-19 04:19:40 +00003819
3820
Chris Lattner3e928bb2005-01-07 07:47:09 +00003821/// ExpandOp - Expand the specified SDOperand into its two component pieces
3822/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3823/// LegalizeNodes map is filled in for any results that are not expanded, the
3824/// ExpandedNodes map is filled in for any results that are expanded, and the
3825/// Lo/Hi values are returned.
3826void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3827 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003828 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003829 SDNode *Node = Op.Val;
3830 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003831 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3832 "Cannot expand FP values!");
3833 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003834 "Cannot expand to FP value or to larger int value!");
3835
Chris Lattner6fdcb252005-09-02 20:32:45 +00003836 // See if we already expanded it.
3837 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3838 = ExpandedNodes.find(Op);
3839 if (I != ExpandedNodes.end()) {
3840 Lo = I->second.first;
3841 Hi = I->second.second;
3842 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003843 }
3844
Chris Lattner3e928bb2005-01-07 07:47:09 +00003845 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003846 case ISD::CopyFromReg:
3847 assert(0 && "CopyFromReg must be legal!");
3848 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003849 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3850 assert(0 && "Do not know how to expand this operator!");
3851 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003852 case ISD::UNDEF:
3853 Lo = DAG.getNode(ISD::UNDEF, NVT);
3854 Hi = DAG.getNode(ISD::UNDEF, NVT);
3855 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003856 case ISD::Constant: {
3857 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3858 Lo = DAG.getConstant(Cst, NVT);
3859 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3860 break;
3861 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003862 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003863 // Return the operands.
3864 Lo = Node->getOperand(0);
3865 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003866 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003867
3868 case ISD::SIGN_EXTEND_INREG:
3869 ExpandOp(Node->getOperand(0), Lo, Hi);
3870 // Sign extend the lo-part.
3871 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3872 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3873 TLI.getShiftAmountTy()));
3874 // sext_inreg the low part if needed.
3875 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3876 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003877
Nate Begemand88fc032006-01-14 03:14:10 +00003878 case ISD::BSWAP: {
3879 ExpandOp(Node->getOperand(0), Lo, Hi);
3880 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3881 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3882 Lo = TempLo;
3883 break;
3884 }
3885
Chris Lattneredb1add2005-05-11 04:51:16 +00003886 case ISD::CTPOP:
3887 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003888 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3889 DAG.getNode(ISD::CTPOP, NVT, Lo),
3890 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003891 Hi = DAG.getConstant(0, NVT);
3892 break;
3893
Chris Lattner39a8f332005-05-12 19:05:01 +00003894 case ISD::CTLZ: {
3895 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003896 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003897 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3898 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003899 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3900 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003901 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3902 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3903
3904 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3905 Hi = DAG.getConstant(0, NVT);
3906 break;
3907 }
3908
3909 case ISD::CTTZ: {
3910 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003911 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003912 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3913 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003914 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3915 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003916 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3917 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3918
3919 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3920 Hi = DAG.getConstant(0, NVT);
3921 break;
3922 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003923
Nate Begemanacc398c2006-01-25 18:21:52 +00003924 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003925 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3926 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003927 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3928 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3929
3930 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003931 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003932 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3933 if (!TLI.isLittleEndian())
3934 std::swap(Lo, Hi);
3935 break;
3936 }
3937
Chris Lattner3e928bb2005-01-07 07:47:09 +00003938 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003939 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3940 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003941 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003942
3943 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003944 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003945 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3946 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003947 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003948 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003949
3950 // Build a factor node to remember that this load is independent of the
3951 // other one.
3952 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3953 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003954
Chris Lattner3e928bb2005-01-07 07:47:09 +00003955 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003956 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003957 if (!TLI.isLittleEndian())
3958 std::swap(Lo, Hi);
3959 break;
3960 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003961 case ISD::AND:
3962 case ISD::OR:
3963 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3964 SDOperand LL, LH, RL, RH;
3965 ExpandOp(Node->getOperand(0), LL, LH);
3966 ExpandOp(Node->getOperand(1), RL, RH);
3967 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3968 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3969 break;
3970 }
3971 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003972 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003973 ExpandOp(Node->getOperand(1), LL, LH);
3974 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003975 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3976 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003977 break;
3978 }
Nate Begeman9373a812005-08-10 20:51:12 +00003979 case ISD::SELECT_CC: {
3980 SDOperand TL, TH, FL, FH;
3981 ExpandOp(Node->getOperand(2), TL, TH);
3982 ExpandOp(Node->getOperand(3), FL, FH);
3983 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3984 Node->getOperand(1), TL, FL, Node->getOperand(4));
3985 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3986 Node->getOperand(1), TH, FH, Node->getOperand(4));
3987 break;
3988 }
Nate Begeman144ff662005-10-13 17:15:37 +00003989 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003990 SDOperand Chain = Node->getOperand(0);
3991 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003992 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3993
3994 if (EVT == NVT)
3995 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3996 else
3997 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3998 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003999
4000 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004001 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004002
Nate Begeman144ff662005-10-13 17:15:37 +00004003 // The high part is obtained by SRA'ing all but one of the bits of the lo
4004 // part.
4005 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4006 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4007 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00004008 break;
4009 }
4010 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004011 SDOperand Chain = Node->getOperand(0);
4012 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00004013 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4014
4015 if (EVT == NVT)
4016 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4017 else
4018 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4019 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004020
4021 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004022 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004023
Nate Begeman144ff662005-10-13 17:15:37 +00004024 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004025 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00004026 break;
4027 }
4028 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004029 SDOperand Chain = Node->getOperand(0);
4030 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00004031 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4032
4033 if (EVT == NVT)
4034 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4035 else
4036 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4037 EVT);
4038
4039 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004040 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00004041
4042 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00004043 Hi = DAG.getNode(ISD::UNDEF, NVT);
4044 break;
4045 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004046 case ISD::ANY_EXTEND:
4047 // The low part is any extension of the input (which degenerates to a copy).
4048 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4049 // The high part is undefined.
4050 Hi = DAG.getNode(ISD::UNDEF, NVT);
4051 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004052 case ISD::SIGN_EXTEND: {
4053 // The low part is just a sign extension of the input (which degenerates to
4054 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004055 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004056
Chris Lattner3e928bb2005-01-07 07:47:09 +00004057 // The high part is obtained by SRA'ing all but one of the bits of the lo
4058 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004059 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004060 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4061 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004062 break;
4063 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004064 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004065 // The low part is just a zero extension of the input (which degenerates to
4066 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004067 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004068
Chris Lattner3e928bb2005-01-07 07:47:09 +00004069 // The high part is just a zero.
4070 Hi = DAG.getConstant(0, NVT);
4071 break;
Chris Lattner35481892005-12-23 00:16:34 +00004072
4073 case ISD::BIT_CONVERT: {
4074 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4075 Node->getOperand(0));
4076 ExpandOp(Tmp, Lo, Hi);
4077 break;
4078 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004079
Chris Lattner8137c9e2006-01-28 05:07:51 +00004080 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004081 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4082 TargetLowering::Custom &&
4083 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004084 Lo = TLI.LowerOperation(Op, DAG);
4085 assert(Lo.Val && "Node must be custom expanded!");
4086 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004087 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004088 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004089 break;
4090
Chris Lattner4e6c7462005-01-08 19:27:05 +00004091 // These operators cannot be expanded directly, emit them as calls to
4092 // library functions.
4093 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004094 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004095 SDOperand Op;
4096 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4097 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004098 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4099 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004100 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004101
Chris Lattnerf20d1832005-07-30 01:40:57 +00004102 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4103
Chris Lattner80a3e942005-07-29 00:33:32 +00004104 // Now that the custom expander is done, expand the result, which is still
4105 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004106 if (Op.Val) {
4107 ExpandOp(Op, Lo, Hi);
4108 break;
4109 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004110 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004111
Chris Lattner4e6c7462005-01-08 19:27:05 +00004112 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004113 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004114 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004115 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004116 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004117
Chris Lattner4e6c7462005-01-08 19:27:05 +00004118 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00004119 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004120 SDOperand Op;
4121 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4122 case Expand: assert(0 && "cannot expand FP!");
4123 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4124 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4125 }
4126
4127 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4128
4129 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004130 if (Op.Val) {
4131 ExpandOp(Op, Lo, Hi);
4132 break;
4133 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004134 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004135
Chris Lattner4e6c7462005-01-08 19:27:05 +00004136 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00004137 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004138 else
Chris Lattner77e77a62005-01-21 06:05:23 +00004139 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004140 break;
4141
Evan Cheng05a2d562006-01-09 18:31:59 +00004142 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004143 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004144 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004145 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004146 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004147 Op = TLI.LowerOperation(Op, DAG);
4148 if (Op.Val) {
4149 // Now that the custom expander is done, expand the result, which is
4150 // still VT.
4151 ExpandOp(Op, Lo, Hi);
4152 break;
4153 }
4154 }
4155
Chris Lattnere34b3962005-01-19 04:19:40 +00004156 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004157 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004158 break;
Chris Lattner47599822005-04-02 03:38:53 +00004159
4160 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004161 TargetLowering::LegalizeAction Action =
4162 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4163 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4164 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004165 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004166 break;
4167 }
4168
Chris Lattnere34b3962005-01-19 04:19:40 +00004169 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004170 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004171 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004172 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004173
Evan Cheng05a2d562006-01-09 18:31:59 +00004174 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004175 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004176 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004177 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004178 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004179 Op = TLI.LowerOperation(Op, DAG);
4180 if (Op.Val) {
4181 // Now that the custom expander is done, expand the result, which is
4182 // still VT.
4183 ExpandOp(Op, Lo, Hi);
4184 break;
4185 }
4186 }
4187
Chris Lattnere34b3962005-01-19 04:19:40 +00004188 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004189 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004190 break;
Chris Lattner47599822005-04-02 03:38:53 +00004191
4192 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004193 TargetLowering::LegalizeAction Action =
4194 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4195 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4196 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004197 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004198 break;
4199 }
4200
Chris Lattnere34b3962005-01-19 04:19:40 +00004201 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004202 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004203 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004204 }
4205
4206 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004207 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004208 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004209 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004210 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004211 Op = TLI.LowerOperation(Op, DAG);
4212 if (Op.Val) {
4213 // Now that the custom expander is done, expand the result, which is
4214 // still VT.
4215 ExpandOp(Op, Lo, Hi);
4216 break;
4217 }
4218 }
4219
Chris Lattnere34b3962005-01-19 04:19:40 +00004220 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004221 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004222 break;
Chris Lattner47599822005-04-02 03:38:53 +00004223
4224 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004225 TargetLowering::LegalizeAction Action =
4226 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4227 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4228 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004229 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004230 break;
4231 }
4232
Chris Lattnere34b3962005-01-19 04:19:40 +00004233 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004234 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004235 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004236 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004237
Misha Brukmanedf128a2005-04-21 22:36:52 +00004238 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004239 case ISD::SUB: {
4240 // If the target wants to custom expand this, let them.
4241 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4242 TargetLowering::Custom) {
4243 Op = TLI.LowerOperation(Op, DAG);
4244 if (Op.Val) {
4245 ExpandOp(Op, Lo, Hi);
4246 break;
4247 }
4248 }
4249
4250 // Expand the subcomponents.
4251 SDOperand LHSL, LHSH, RHSL, RHSH;
4252 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4253 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004254 std::vector<MVT::ValueType> VTs;
4255 std::vector<SDOperand> LoOps, HiOps;
4256 VTs.push_back(LHSL.getValueType());
4257 VTs.push_back(MVT::Flag);
4258 LoOps.push_back(LHSL);
4259 LoOps.push_back(RHSL);
4260 HiOps.push_back(LHSH);
4261 HiOps.push_back(RHSH);
4262 if (Node->getOpcode() == ISD::ADD) {
4263 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4264 HiOps.push_back(Lo.getValue(1));
4265 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4266 } else {
4267 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4268 HiOps.push_back(Lo.getValue(1));
4269 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4270 }
Chris Lattner84f67882005-01-20 18:52:28 +00004271 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004272 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004273 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004274 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004275 SDOperand LL, LH, RL, RH;
4276 ExpandOp(Node->getOperand(0), LL, LH);
4277 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004278 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4279 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4280 // extended the sign bit of the low half through the upper half, and if so
4281 // emit a MULHS instead of the alternate sequence that is valid for any
4282 // i64 x i64 multiply.
4283 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4284 // is RH an extension of the sign bit of RL?
4285 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4286 RH.getOperand(1).getOpcode() == ISD::Constant &&
4287 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4288 // is LH an extension of the sign bit of LL?
4289 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4290 LH.getOperand(1).getOpcode() == ISD::Constant &&
4291 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4292 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4293 } else {
4294 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4295 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4296 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4297 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4298 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4299 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004300 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4301 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004302 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004303 }
4304 break;
4305 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004306 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4307 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4308 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4309 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004310 }
4311
Chris Lattner83397362005-12-21 18:02:52 +00004312 // Make sure the resultant values have been legalized themselves, unless this
4313 // is a type that requires multi-step expansion.
4314 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4315 Lo = LegalizeOp(Lo);
4316 Hi = LegalizeOp(Hi);
4317 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004318
4319 // Remember in a map if the values will be reused later.
4320 bool isNew =
4321 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4322 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004323}
4324
Chris Lattnerc7029802006-03-18 01:44:44 +00004325/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4326/// two smaller values of MVT::Vector type.
4327void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4328 SDOperand &Hi) {
4329 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4330 SDNode *Node = Op.Val;
4331 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4332 assert(NumElements > 1 && "Cannot split a single element vector!");
4333 unsigned NewNumElts = NumElements/2;
4334 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4335 SDOperand TypeNode = *(Node->op_end()-1);
4336
4337 // See if we already split it.
4338 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4339 = SplitNodes.find(Op);
4340 if (I != SplitNodes.end()) {
4341 Lo = I->second.first;
4342 Hi = I->second.second;
4343 return;
4344 }
4345
4346 switch (Node->getOpcode()) {
Chris Lattner7692eb42006-03-23 21:16:34 +00004347 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004348 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004349 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4350 LoOps.push_back(NewNumEltsNode);
4351 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004352 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004353
4354 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4355 HiOps.push_back(NewNumEltsNode);
4356 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004357 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004358 break;
4359 }
4360 case ISD::VADD:
4361 case ISD::VSUB:
4362 case ISD::VMUL:
4363 case ISD::VSDIV:
4364 case ISD::VUDIV:
4365 case ISD::VAND:
4366 case ISD::VOR:
4367 case ISD::VXOR: {
4368 SDOperand LL, LH, RL, RH;
4369 SplitVectorOp(Node->getOperand(0), LL, LH);
4370 SplitVectorOp(Node->getOperand(1), RL, RH);
4371
4372 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4373 NewNumEltsNode, TypeNode);
4374 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4375 NewNumEltsNode, TypeNode);
4376 break;
4377 }
4378 case ISD::VLOAD: {
4379 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4380 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4381 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4382
4383 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4384 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4385 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4386 getIntPtrConstant(IncrementSize));
4387 // FIXME: This creates a bogus srcvalue!
4388 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4389
4390 // Build a factor node to remember that this load is independent of the
4391 // other one.
4392 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4393 Hi.getValue(1));
4394
4395 // Remember that we legalized the chain.
4396 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4397 if (!TLI.isLittleEndian())
4398 std::swap(Lo, Hi);
4399 break;
4400 }
Chris Lattner7692eb42006-03-23 21:16:34 +00004401 case ISD::VBIT_CONVERT: {
4402 // We know the result is a vector. The input may be either a vector or a
4403 // scalar value.
4404 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4405 // Lower to a store/load. FIXME: this could be improved probably.
4406 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4407
4408 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4409 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4410 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4411 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4412 SplitVectorOp(St, Lo, Hi);
4413 } else {
4414 // If the input is a vector type, we have to either scalarize it, pack it
4415 // or convert it based on whether the input vector type is legal.
4416 SDNode *InVal = Node->getOperand(0).Val;
4417 unsigned NumElems =
4418 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4419 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4420
4421 // If the input is from a single element vector, scalarize the vector,
4422 // then treat like a scalar.
4423 if (NumElems == 1) {
4424 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4425 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4426 Op.getOperand(1), Op.getOperand(2));
4427 SplitVectorOp(Scalar, Lo, Hi);
4428 } else {
4429 // Split the input vector.
4430 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4431
4432 // Convert each of the pieces now.
4433 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4434 NewNumEltsNode, TypeNode);
4435 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4436 NewNumEltsNode, TypeNode);
4437 }
4438 break;
4439 }
4440 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004441 }
4442
4443 // Remember in a map if the values will be reused later.
4444 bool isNew =
4445 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4446 assert(isNew && "Value already expanded?!?");
4447}
4448
4449
4450/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4451/// equivalent operation that returns a scalar (e.g. F32) or packed value
4452/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4453/// type for the result.
4454SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4455 MVT::ValueType NewVT) {
Chris Lattner1c6191f2006-03-21 19:20:37 +00004456 // FIXME: THIS IS A TEMPORARY HACK
4457 if (Op.getValueType() == NewVT) return Op;
Chris Lattner384504c2006-03-21 20:44:12 +00004458
Chris Lattnerc7029802006-03-18 01:44:44 +00004459 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4460 SDNode *Node = Op.Val;
4461
4462 // See if we already packed it.
4463 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4464 if (I != PackedNodes.end()) return I->second;
4465
4466 SDOperand Result;
4467 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004468 default:
4469 Node->dump(); std::cerr << "\n";
4470 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004471 case ISD::VADD:
4472 case ISD::VSUB:
4473 case ISD::VMUL:
4474 case ISD::VSDIV:
4475 case ISD::VUDIV:
4476 case ISD::VAND:
4477 case ISD::VOR:
4478 case ISD::VXOR:
4479 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4480 NewVT,
4481 PackVectorOp(Node->getOperand(0), NewVT),
4482 PackVectorOp(Node->getOperand(1), NewVT));
4483 break;
4484 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004485 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4486 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004487
Chris Lattner4794a6b2006-03-19 00:07:49 +00004488 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004489
4490 // Remember that we legalized the chain.
4491 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4492 break;
4493 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004494 case ISD::VBUILD_VECTOR:
Chris Lattnerc7029802006-03-18 01:44:44 +00004495 if (!MVT::isVector(NewVT)) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004496 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004497 Result = Node->getOperand(0);
4498 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004499 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004500 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004501 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004502 }
4503 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004504 case ISD::VINSERT_VECTOR_ELT:
4505 if (!MVT::isVector(NewVT)) {
4506 // Returning a scalar? Must be the inserted element.
4507 Result = Node->getOperand(1);
4508 } else {
4509 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4510 PackVectorOp(Node->getOperand(0), NewVT),
4511 Node->getOperand(1), Node->getOperand(2));
4512 }
4513 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00004514 case ISD::VBIT_CONVERT:
4515 if (Op.getOperand(0).getValueType() != MVT::Vector)
4516 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4517 else {
4518 // If the input is a vector type, we have to either scalarize it, pack it
4519 // or convert it based on whether the input vector type is legal.
4520 SDNode *InVal = Node->getOperand(0).Val;
4521 unsigned NumElems =
4522 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4523 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4524
4525 // Figure out if there is a Packed type corresponding to this Vector
4526 // type. If so, convert to the packed type.
4527 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4528 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4529 // Turn this into a bit convert of the packed input.
4530 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4531 PackVectorOp(Node->getOperand(0), TVT));
4532 break;
4533 } else if (NumElems == 1) {
4534 // Turn this into a bit convert of the scalar input.
4535 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4536 PackVectorOp(Node->getOperand(0), EVT));
4537 break;
4538 } else {
4539 // FIXME: UNIMP!
4540 assert(0 && "Cast from unsupported vector type not implemented yet!");
4541 }
4542 }
Chris Lattnerc7029802006-03-18 01:44:44 +00004543 }
4544
4545 if (TLI.isTypeLegal(NewVT))
4546 Result = LegalizeOp(Result);
4547 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4548 assert(isNew && "Value already packed?");
4549 return Result;
4550}
4551
Chris Lattner3e928bb2005-01-07 07:47:09 +00004552
4553// SelectionDAG::Legalize - This is the entry point for the file.
4554//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004555void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004556 /// run - This is the main entry point to this class.
4557 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004558 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004559}
4560