blob: a00e47e4b33f694fff2aef028edbcabb2462d1da [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
Chris Lattner6831a812006-02-13 09:18:02 +000044 // Libcall insertion helpers.
45
46 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
47 /// legalized. We use this to ensure that calls are properly serialized
48 /// against each other, including inserted libcalls.
49 SDOperand LastCALLSEQ_END;
50
51 /// IsLegalizingCall - This member is used *only* for purposes of providing
52 /// helpful assertions that a libcall isn't created while another call is
53 /// being legalized (which could lead to non-serialized call sequences).
54 bool IsLegalizingCall;
55
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000057 Legal, // The target natively supports this operation.
58 Promote, // This operation should be executed in a larger type.
59 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000060 };
Chris Lattner6831a812006-02-13 09:18:02 +000061
Chris Lattner3e928bb2005-01-07 07:47:09 +000062 /// ValueTypeActions - This is a bitvector that contains two bits for each
63 /// value type, where the two bits correspond to the LegalizeAction enum.
64 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000065 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000066
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
70 std::map<SDOperand, SDOperand> LegalizedNodes;
71
Chris Lattner03c85462005-01-15 05:21:40 +000072 /// PromotedNodes - For nodes that are below legal width, and that have more
73 /// than one use, this map indicates what promoted value to use. This allows
74 /// us to avoid promoting the same thing more than once.
75 std::map<SDOperand, SDOperand> PromotedNodes;
76
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 /// ExpandedNodes - For nodes that need to be expanded, and which have more
78 /// than one use, this map indicates which which operands are the expanded
79 /// version of the input. This allows us to avoid expanding the same node
80 /// more than once.
81 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
82
Chris Lattner8afc48e2005-01-07 22:28:47 +000083 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000084 LegalizedNodes.insert(std::make_pair(From, To));
85 // If someone requests legalization of the new node, return itself.
86 if (From != To)
87 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000088 }
Chris Lattner03c85462005-01-15 05:21:40 +000089 void AddPromotedOperand(SDOperand From, SDOperand To) {
90 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
91 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +000092 // If someone requests legalization of the new node, return itself.
93 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +000094 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000095
Chris Lattner3e928bb2005-01-07 07:47:09 +000096public:
97
Chris Lattner9c32d3b2005-01-23 04:42:50 +000098 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000099
Chris Lattner3e928bb2005-01-07 07:47:09 +0000100 /// getTypeAction - Return how we should legalize values of this type, either
101 /// it is already legal or we need to expand it into multiple registers of
102 /// smaller integer type, or we need to promote it to a larger type.
103 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000104 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000105 }
106
107 /// isTypeLegal - Return true if this type is legal on this target.
108 ///
109 bool isTypeLegal(MVT::ValueType VT) const {
110 return getTypeAction(VT) == Legal;
111 }
112
Chris Lattner3e928bb2005-01-07 07:47:09 +0000113 void LegalizeDAG();
114
Chris Lattner456a93a2006-01-28 07:39:30 +0000115private:
116
Chris Lattner3e928bb2005-01-07 07:47:09 +0000117 SDOperand LegalizeOp(SDOperand O);
118 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000119 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000120
Chris Lattner6831a812006-02-13 09:18:02 +0000121 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
122
Nate Begeman750ac1b2006-02-01 07:19:44 +0000123 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
124
Chris Lattner77e77a62005-01-21 06:05:23 +0000125 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
126 SDOperand &Hi);
127 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
128 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000129
Chris Lattner35481892005-12-23 00:16:34 +0000130 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000131 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
132 SDOperand LegalOp,
133 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000134 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000136 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
137 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000138
Chris Lattner456a93a2006-01-28 07:39:30 +0000139 SDOperand ExpandBSWAP(SDOperand Op);
140 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000141 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000143 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
144 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000145
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattnerb89175f2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000160
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000164 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000165 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000166}
167
Chris Lattner32fca002005-10-06 01:20:27 +0000168/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
169/// not been visited yet and if all of its operands have already been visited.
170static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
171 std::map<SDNode*, unsigned> &Visited) {
172 if (++Visited[N] != N->getNumOperands())
173 return; // Haven't visited all operands yet
174
175 Order.push_back(N);
176
177 if (N->hasOneUse()) { // Tail recurse in common case.
178 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
179 return;
180 }
181
182 // Now that we have N in, add anything that uses it if all of their operands
183 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000184 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
185 ComputeTopDownOrdering(*UI, Order, Visited);
186}
187
Chris Lattner1618beb2005-07-29 00:11:56 +0000188
Chris Lattner3e928bb2005-01-07 07:47:09 +0000189void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000190 LastCALLSEQ_END = DAG.getEntryNode();
191 IsLegalizingCall = false;
192
Chris Lattnerab510a72005-10-02 17:49:46 +0000193 // The legalize process is inherently a bottom-up recursive process (users
194 // legalize their uses before themselves). Given infinite stack space, we
195 // could just start legalizing on the root and traverse the whole graph. In
196 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000197 // blocks. To avoid this problem, compute an ordering of the nodes where each
198 // node is only legalized after all of its operands are legalized.
199 std::map<SDNode*, unsigned> Visited;
200 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000201
Chris Lattner32fca002005-10-06 01:20:27 +0000202 // Compute ordering from all of the leaves in the graphs, those (like the
203 // entry node) that have no operands.
204 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
205 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000206 if (I->getNumOperands() == 0) {
207 Visited[I] = 0 - 1U;
208 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000209 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000210 }
211
Chris Lattnerde202b32005-11-09 23:47:37 +0000212 assert(Order.size() == Visited.size() &&
213 Order.size() ==
214 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000215 "Error: DAG is cyclic!");
216 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000217
Chris Lattner32fca002005-10-06 01:20:27 +0000218 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
219 SDNode *N = Order[i];
220 switch (getTypeAction(N->getValueType(0))) {
221 default: assert(0 && "Bad type action!");
222 case Legal:
223 LegalizeOp(SDOperand(N, 0));
224 break;
225 case Promote:
226 PromoteOp(SDOperand(N, 0));
227 break;
228 case Expand: {
229 SDOperand X, Y;
230 ExpandOp(SDOperand(N, 0), X, Y);
231 break;
232 }
233 }
234 }
235
236 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000237 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000238 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
239 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000240
241 ExpandedNodes.clear();
242 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000243 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000244
245 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000246 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000247}
248
Chris Lattner6831a812006-02-13 09:18:02 +0000249
250/// FindCallEndFromCallStart - Given a chained node that is part of a call
251/// sequence, find the CALLSEQ_END node that terminates the call sequence.
252static SDNode *FindCallEndFromCallStart(SDNode *Node) {
253 if (Node->getOpcode() == ISD::CALLSEQ_END)
254 return Node;
255 if (Node->use_empty())
256 return 0; // No CallSeqEnd
257
258 // The chain is usually at the end.
259 SDOperand TheChain(Node, Node->getNumValues()-1);
260 if (TheChain.getValueType() != MVT::Other) {
261 // Sometimes it's at the beginning.
262 TheChain = SDOperand(Node, 0);
263 if (TheChain.getValueType() != MVT::Other) {
264 // Otherwise, hunt for it.
265 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
266 if (Node->getValueType(i) == MVT::Other) {
267 TheChain = SDOperand(Node, i);
268 break;
269 }
270
271 // Otherwise, we walked into a node without a chain.
272 if (TheChain.getValueType() != MVT::Other)
273 return 0;
274 }
275 }
276
277 for (SDNode::use_iterator UI = Node->use_begin(),
278 E = Node->use_end(); UI != E; ++UI) {
279
280 // Make sure to only follow users of our token chain.
281 SDNode *User = *UI;
282 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
283 if (User->getOperand(i) == TheChain)
284 if (SDNode *Result = FindCallEndFromCallStart(User))
285 return Result;
286 }
287 return 0;
288}
289
290/// FindCallStartFromCallEnd - Given a chained node that is part of a call
291/// sequence, find the CALLSEQ_START node that initiates the call sequence.
292static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
293 assert(Node && "Didn't find callseq_start for a call??");
294 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
295
296 assert(Node->getOperand(0).getValueType() == MVT::Other &&
297 "Node doesn't have a token chain argument!");
298 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
299}
300
301/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
302/// see if any uses can reach Dest. If no dest operands can get to dest,
303/// legalize them, legalize ourself, and return false, otherwise, return true.
304bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
305 SDNode *Dest) {
306 if (N == Dest) return true; // N certainly leads to Dest :)
307
308 // If the first result of this node has been already legalized, then it cannot
309 // reach N.
310 switch (getTypeAction(N->getValueType(0))) {
311 case Legal:
312 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
313 break;
314 case Promote:
315 if (PromotedNodes.count(SDOperand(N, 0))) return false;
316 break;
317 case Expand:
318 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
319 break;
320 }
321
322 // Okay, this node has not already been legalized. Check and legalize all
323 // operands. If none lead to Dest, then we can legalize this node.
324 bool OperandsLeadToDest = false;
325 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
326 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
327 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
328
329 if (OperandsLeadToDest) return true;
330
331 // Okay, this node looks safe, legalize it and return false.
332 switch (getTypeAction(N->getValueType(0))) {
333 case Legal:
334 LegalizeOp(SDOperand(N, 0));
335 break;
336 case Promote:
337 PromoteOp(SDOperand(N, 0));
338 break;
339 case Expand: {
340 SDOperand X, Y;
341 ExpandOp(SDOperand(N, 0), X, Y);
342 break;
343 }
344 }
345 return false;
346}
347
348
349
Chris Lattner3e928bb2005-01-07 07:47:09 +0000350SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000351 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000352 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000353 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000354
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000356 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000357 if (Node->getNumValues() > 1) {
358 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
359 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000360 case Legal: break; // Nothing to do.
361 case Expand: {
362 SDOperand T1, T2;
363 ExpandOp(Op.getValue(i), T1, T2);
364 assert(LegalizedNodes.count(Op) &&
365 "Expansion didn't add legal operands!");
366 return LegalizedNodes[Op];
367 }
368 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000369 PromoteOp(Op.getValue(i));
370 assert(LegalizedNodes.count(Op) &&
Chris Lattner456a93a2006-01-28 07:39:30 +0000371 "Promotion didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000372 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000373 }
374 }
375
Chris Lattner45982da2005-05-12 16:53:42 +0000376 // Note that LegalizeOp may be reentered even from single-use nodes, which
377 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000378 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
379 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000380
Nate Begeman9373a812005-08-10 20:51:12 +0000381 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000382 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000383 bool isCustom = false;
384
Chris Lattner3e928bb2005-01-07 07:47:09 +0000385 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000386 case ISD::FrameIndex:
387 case ISD::EntryToken:
388 case ISD::Register:
389 case ISD::BasicBlock:
390 case ISD::TargetFrameIndex:
391 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000392 case ISD::TargetConstantFP:
393 case ISD::TargetConstantVec:
Chris Lattner948c1b12006-01-28 08:31:04 +0000394 case ISD::TargetConstantPool:
395 case ISD::TargetGlobalAddress:
396 case ISD::TargetExternalSymbol:
397 case ISD::VALUETYPE:
398 case ISD::SRCVALUE:
399 case ISD::STRING:
400 case ISD::CONDCODE:
401 // Primitives must all be legal.
402 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
403 "This must be legal!");
404 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000405 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000406 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
407 // If this is a target node, legalize it by legalizing the operands then
408 // passing it through.
409 std::vector<SDOperand> Ops;
410 bool Changed = false;
411 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
412 Ops.push_back(LegalizeOp(Node->getOperand(i)));
413 Changed = Changed || Node->getOperand(i) != Ops.back();
414 }
415 if (Changed)
416 if (Node->getNumValues() == 1)
417 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
418 else {
419 std::vector<MVT::ValueType> VTs(Node->value_begin(),
420 Node->value_end());
421 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
422 }
423
424 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
425 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
426 return Result.getValue(Op.ResNo);
427 }
428 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000429 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
430 assert(0 && "Do not know how to legalize this operator!");
431 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000432 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000433 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000434 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000435 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
436 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000437 case TargetLowering::Custom:
438 Tmp1 = TLI.LowerOperation(Op, DAG);
439 if (Tmp1.Val) Result = Tmp1;
440 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000441 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000442 break;
443 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000444 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000445 case ISD::AssertSext:
446 case ISD::AssertZext:
447 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000449 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000450 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000451 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000452 Result = Node->getOperand(Op.ResNo);
453 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000454 case ISD::CopyFromReg:
455 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000456 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000457 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000458 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000459 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000460 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000461 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000462 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
464 } else {
465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
466 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000467 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
468 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000469 // Since CopyFromReg produces two values, make sure to remember that we
470 // legalized both of them.
471 AddLegalizedOperand(Op.getValue(0), Result);
472 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
473 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000474 case ISD::UNDEF: {
475 MVT::ValueType VT = Op.getValueType();
476 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000477 default: assert(0 && "This action is not supported yet!");
478 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000479 if (MVT::isInteger(VT))
480 Result = DAG.getConstant(0, VT);
481 else if (MVT::isFloatingPoint(VT))
482 Result = DAG.getConstantFP(0, VT);
483 else
484 assert(0 && "Unknown value type!");
485 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000486 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000487 break;
488 }
489 break;
490 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000491
492 case ISD::LOCATION:
493 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
494 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
495
496 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
497 case TargetLowering::Promote:
498 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000499 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000500 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000501 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
502 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
503
504 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
505 const std::string &FName =
506 cast<StringSDNode>(Node->getOperand(3))->getValue();
507 const std::string &DirName =
508 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000509 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000510
Jim Laskeye81aecb2005-12-21 20:51:37 +0000511 std::vector<SDOperand> Ops;
512 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000513 SDOperand LineOp = Node->getOperand(1);
514 SDOperand ColOp = Node->getOperand(2);
515
516 if (useDEBUG_LOC) {
517 Ops.push_back(LineOp); // line #
518 Ops.push_back(ColOp); // col #
519 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
520 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
521 } else {
522 unsigned Line = dyn_cast<ConstantSDNode>(LineOp)->getValue();
523 unsigned Col = dyn_cast<ConstantSDNode>(ColOp)->getValue();
524 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
525 Ops.push_back(DAG.getConstant(ID, MVT::i32));
526 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
527 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000528 } else {
529 Result = Tmp1; // chain
530 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000531 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000532 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000533 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000534 if (Tmp1 != Node->getOperand(0) ||
535 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000536 std::vector<SDOperand> Ops;
537 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000538 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
539 Ops.push_back(Node->getOperand(1)); // line # must be legal.
540 Ops.push_back(Node->getOperand(2)); // col # must be legal.
541 } else {
542 // Otherwise promote them.
543 Ops.push_back(PromoteOp(Node->getOperand(1)));
544 Ops.push_back(PromoteOp(Node->getOperand(2)));
545 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000546 Ops.push_back(Node->getOperand(3)); // filename must be legal.
547 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000548 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000549 }
550 break;
551 }
552 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000553
554 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000555 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000556 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000557 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000558 case TargetLowering::Legal:
559 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
560 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
561 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
562 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000564 break;
565 }
566 break;
567
568 case ISD::DEBUG_LABEL:
569 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
570 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000571 default: assert(0 && "This action is not supported yet!");
572 case TargetLowering::Legal:
573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
574 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000575 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000576 break;
577 }
578 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000579
Chris Lattner3e928bb2005-01-07 07:47:09 +0000580 case ISD::Constant:
581 // We know we don't need to expand constants here, constants only have one
582 // value and we check that it is fine above.
583
584 // FIXME: Maybe we should handle things like targets that don't support full
585 // 32-bit immediates?
586 break;
587 case ISD::ConstantFP: {
588 // Spill FP immediates to the constant pool if the target cannot directly
589 // codegen them. Targets often have some immediate values that can be
590 // efficiently generated into an FP register without a load. We explicitly
591 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000592 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
593
594 // Check to see if this FP immediate is already legal.
595 bool isLegal = false;
596 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
597 E = TLI.legal_fpimm_end(); I != E; ++I)
598 if (CFP->isExactlyValue(*I)) {
599 isLegal = true;
600 break;
601 }
602
Chris Lattner3181a772006-01-29 06:26:56 +0000603 // If this is a legal constant, turn it into a TargetConstantFP node.
604 if (isLegal) {
605 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
606 break;
607 }
608
609 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
610 default: assert(0 && "This action is not supported yet!");
611 case TargetLowering::Custom:
612 Tmp3 = TLI.LowerOperation(Result, DAG);
613 if (Tmp3.Val) {
614 Result = Tmp3;
615 break;
616 }
617 // FALLTHROUGH
618 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000619 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000620 bool Extend = false;
621
Chris Lattner456a93a2006-01-28 07:39:30 +0000622 // If a FP immediate is precise when represented as a float and if the
623 // target can do an extending load from float to double, we put it into
624 // the constant pool as a float, even if it's is statically typed as a
625 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000626 MVT::ValueType VT = CFP->getValueType(0);
627 bool isDouble = VT == MVT::f64;
628 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
629 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000630 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
631 // Only do this if the target has a native EXTLOAD instruction from
632 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000633 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000634 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
635 VT = MVT::f32;
636 Extend = true;
637 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000638
Chris Lattner456a93a2006-01-28 07:39:30 +0000639 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000640 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000641 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
642 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000643 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000644 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
645 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000646 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000647 }
648 break;
649 }
Chris Lattner8ca05e02006-01-29 06:34:16 +0000650 case ISD::ConstantVec:
651 switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) {
652 default: assert(0 && "This action is not supported yet!");
653 case TargetLowering::Custom:
654 Tmp3 = TLI.LowerOperation(Result, DAG);
655 if (Tmp3.Val) {
656 Result = Tmp3;
657 break;
658 }
659 // FALLTHROUGH
660 case TargetLowering::Expand:
661 // We assume that vector constants are not legal, and will be immediately
662 // spilled to the constant pool.
663 //
664 // Create a ConstantPacked, and put it in the constant pool.
665 MVT::ValueType VT = Node->getValueType(0);
666 const Type *OpNTy =
667 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
668 std::vector<Constant*> CV;
669 if (MVT::isFloatingPoint(VT)) {
670 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
671 double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue();
672 CV.push_back(ConstantFP::get(OpNTy, V));
673 }
674 } else {
675 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
676 uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
677 CV.push_back(ConstantUInt::get(OpNTy, V));
678 }
679 }
680 Constant *CP = ConstantPacked::get(CV);
681 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
682 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
683 DAG.getSrcValue(NULL));
684 break;
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000685 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000686 break;
Chris Lattner040c11c2005-11-09 18:48:57 +0000687 case ISD::TokenFactor:
688 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000689 Tmp1 = LegalizeOp(Node->getOperand(0));
690 Tmp2 = LegalizeOp(Node->getOperand(1));
691 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
692 } else if (Node->getNumOperands() == 3) {
693 Tmp1 = LegalizeOp(Node->getOperand(0));
694 Tmp2 = LegalizeOp(Node->getOperand(1));
695 Tmp3 = LegalizeOp(Node->getOperand(2));
696 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000697 } else {
698 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000699 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000700 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
701 Ops.push_back(LegalizeOp(Node->getOperand(i)));
702 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000703 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000704 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000705
Chris Lattner6831a812006-02-13 09:18:02 +0000706 case ISD::CALLSEQ_START: {
707 SDNode *CallEnd = FindCallEndFromCallStart(Node);
708
709 // Recursively Legalize all of the inputs of the call end that do not lead
710 // to this call start. This ensures that any libcalls that need be inserted
711 // are inserted *before* the CALLSEQ_START.
712 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
713 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
714
715 // Now that we legalized all of the inputs (which may have inserted
716 // libcalls) create the new CALLSEQ_START node.
717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
718
719 // Merge in the last call, to ensure that this call start after the last
720 // call ended.
721 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
722 Tmp1 = LegalizeOp(Tmp1);
723
724 // Do not try to legalize the target-specific arguments (#1+).
725 if (Tmp1 != Node->getOperand(0)) {
726 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
727 Ops[0] = Tmp1;
728 Result = DAG.UpdateNodeOperands(Result, Ops);
729 }
730
731 // Remember that the CALLSEQ_START is legalized.
732 AddLegalizedOperand(Op, Result);
733
734 // Now that the callseq_start and all of the non-call nodes above this call
735 // sequence have been legalized, legalize the call itself. During this
736 // process, no libcalls can/will be inserted, guaranteeing that no calls
737 // can overlap.
738 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
739 SDOperand InCallSEQ = LastCALLSEQ_END;
740 // Note that we are selecting this call!
741 LastCALLSEQ_END = SDOperand(CallEnd, 0);
742 IsLegalizingCall = true;
743
744 // Legalize the call, starting from the CALLSEQ_END.
745 LegalizeOp(LastCALLSEQ_END);
746 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
747 return Result;
748 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000749 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000750 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
751 // will cause this node to be legalized as well as handling libcalls right.
752 if (LastCALLSEQ_END.Val != Node) {
753 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
754 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
755 assert(I != LegalizedNodes.end() &&
756 "Legalizing the call start should have legalized this node!");
757 return I->second;
758 }
759
760 // Otherwise, the call start has been legalized and everything is going
761 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000762 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000763 // Do not try to legalize the target-specific arguments (#1+), except for
764 // an optional flag input.
765 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
766 if (Tmp1 != Node->getOperand(0)) {
767 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
768 Ops[0] = Tmp1;
769 Result = DAG.UpdateNodeOperands(Result, Ops);
770 }
771 } else {
772 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
773 if (Tmp1 != Node->getOperand(0) ||
774 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
775 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
776 Ops[0] = Tmp1;
777 Ops.back() = Tmp2;
778 Result = DAG.UpdateNodeOperands(Result, Ops);
779 }
Chris Lattner6a542892006-01-24 05:48:21 +0000780 }
Chris Lattner6831a812006-02-13 09:18:02 +0000781 assert(IsLegalizingCall && "imbalance between START/END?");
782 // This finishes up call legalization.
783 IsLegalizingCall = false;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000784 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000785 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000786 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
787 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
788 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000789 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000790
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000791 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000792 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000793 switch (TLI.getOperationAction(Node->getOpcode(),
794 Node->getValueType(0))) {
795 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000796 case TargetLowering::Expand: {
797 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
798 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
799 " not tell us which reg is the stack pointer!");
800 SDOperand Chain = Tmp1.getOperand(0);
801 SDOperand Size = Tmp2.getOperand(1);
802 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
803 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
804 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000805 Tmp1 = LegalizeOp(Tmp1);
806 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000807 break;
808 }
809 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000810 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
811 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000812 Tmp1 = LegalizeOp(Tmp3);
813 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000814 }
Chris Lattner903d2782006-01-15 08:54:32 +0000815 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000816 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000817 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000818 }
Chris Lattner903d2782006-01-15 08:54:32 +0000819 // Since this op produce two values, make sure to remember that we
820 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000821 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
822 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000823 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000824 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000825 case ISD::INLINEASM:
826 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
827 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000828 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000829 Tmp2 = Tmp3 = SDOperand(0, 0);
830 else
831 Tmp3 = LegalizeOp(Tmp2);
832
833 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
834 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
835 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000836 if (Tmp3.Val) Ops.back() = Tmp3;
837 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000838 }
839
840 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000841 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000842 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
843 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000844 case ISD::BR:
845 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000846 // Ensure that libcalls are emitted before a branch.
847 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
848 Tmp1 = LegalizeOp(Tmp1);
849 LastCALLSEQ_END = DAG.getEntryNode();
850
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000851 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +0000852 break;
853
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000854 case ISD::BRCOND:
855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000856 // Ensure that libcalls are emitted before a return.
857 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
858 Tmp1 = LegalizeOp(Tmp1);
859 LastCALLSEQ_END = DAG.getEntryNode();
860
Chris Lattner47e92232005-01-18 19:27:06 +0000861 switch (getTypeAction(Node->getOperand(1).getValueType())) {
862 case Expand: assert(0 && "It's impossible to expand bools");
863 case Legal:
864 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
865 break;
866 case Promote:
867 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
868 break;
869 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000870
871 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000873
874 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
875 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000876 case TargetLowering::Legal: break;
877 case TargetLowering::Custom:
878 Tmp1 = TLI.LowerOperation(Result, DAG);
879 if (Tmp1.Val) Result = Tmp1;
880 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000881 case TargetLowering::Expand:
882 // Expand brcond's setcc into its constituent parts and create a BR_CC
883 // Node.
884 if (Tmp2.getOpcode() == ISD::SETCC) {
885 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
886 Tmp2.getOperand(0), Tmp2.getOperand(1),
887 Node->getOperand(2));
888 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000889 // Make sure the condition is either zero or one. It may have been
890 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +0000891 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
892 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
893 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +0000894
Nate Begeman7cbd5252005-08-16 19:49:35 +0000895 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
896 DAG.getCondCode(ISD::SETNE), Tmp2,
897 DAG.getConstant(0, Tmp2.getValueType()),
898 Node->getOperand(2));
899 }
900 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000901 }
902 break;
903 case ISD::BR_CC:
904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000905 // Ensure that libcalls are emitted before a branch.
906 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
907 Tmp1 = LegalizeOp(Tmp1);
908 LastCALLSEQ_END = DAG.getEntryNode();
909
Nate Begeman750ac1b2006-02-01 07:19:44 +0000910 Tmp2 = Node->getOperand(2); // LHS
911 Tmp3 = Node->getOperand(3); // RHS
912 Tmp4 = Node->getOperand(1); // CC
913
914 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
915
916 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
917 // the LHS is a legal SETCC itself. In this case, we need to compare
918 // the result against zero to select between true and false values.
919 if (Tmp3.Val == 0) {
920 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
921 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +0000922 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000923
924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
925 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +0000926
Chris Lattner181b7a32005-12-17 23:46:46 +0000927 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
928 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000929 case TargetLowering::Legal: break;
930 case TargetLowering::Custom:
931 Tmp4 = TLI.LowerOperation(Result, DAG);
932 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +0000933 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000934 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000935 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000936 case ISD::BRCONDTWOWAY:
937 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
938 switch (getTypeAction(Node->getOperand(1).getValueType())) {
939 case Expand: assert(0 && "It's impossible to expand bools");
940 case Legal:
941 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
942 break;
943 case Promote:
944 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
945 break;
946 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000947
Chris Lattner411e8882005-04-09 03:30:19 +0000948 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
949 // pair.
950 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
951 case TargetLowering::Promote:
952 default: assert(0 && "This action is not supported yet!");
953 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000954 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
955 Node->getOperand(3));
Chris Lattner411e8882005-04-09 03:30:19 +0000956 break;
957 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000958 // If BRTWOWAY_CC is legal for this target, then simply expand this node
959 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
960 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000961 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000962 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattner03d5e872006-01-29 06:00:45 +0000963 Tmp3 = Tmp2.getOperand(0);
964 Tmp4 = Tmp2.getOperand(1);
965 Tmp2 = Tmp2.getOperand(2);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000966 } else {
Chris Lattner03d5e872006-01-29 06:00:45 +0000967 Tmp3 = Tmp2;
968 Tmp4 = DAG.getConstant(0, Tmp2.getValueType());
969 Tmp2 = DAG.getCondCode(ISD::SETNE);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000970 }
Chris Lattner03d5e872006-01-29 06:00:45 +0000971 std::vector<SDOperand> Ops;
972 Ops.push_back(Tmp1);
973 Ops.push_back(Tmp2);
974 Ops.push_back(Tmp3);
975 Ops.push_back(Tmp4);
976 Ops.push_back(Node->getOperand(2));
977 Ops.push_back(Node->getOperand(3));
978 Result = DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000979 } else {
980 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +0000981 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000982 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
983 }
Chris Lattner411e8882005-04-09 03:30:19 +0000984 break;
985 }
986 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +0000987 case ISD::BRTWOWAY_CC: {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000988 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000989 // Ensure that libcalls are emitted before a branch.
990 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
991 Tmp1 = LegalizeOp(Tmp1);
992 LastCALLSEQ_END = DAG.getEntryNode();
993
Nate Begeman750ac1b2006-02-01 07:19:44 +0000994 Tmp2 = Node->getOperand(2); // LHS
995 Tmp3 = Node->getOperand(3); // RHS
996 Tmp4 = Node->getOperand(1); // CC
997
998 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
999
1000 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1001 // the LHS is a legal SETCC itself. In this case, we need to compare
1002 // the result against zero to select between true and false values.
1003 if (Tmp3.Val == 0) {
1004 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1005 Tmp4 = DAG.getCondCode(ISD::SETNE);
1006 }
1007 std::vector<SDOperand> Ops;
1008 Ops.push_back(Tmp1);
1009 Ops.push_back(Tmp4);
1010 Ops.push_back(Tmp2);
1011 Ops.push_back(Tmp3);
1012 Ops.push_back(Node->getOperand(4));
1013 Ops.push_back(Node->getOperand(5));
1014 Result = DAG.UpdateNodeOperands(Result, Ops);
1015
1016 // Everything is legal, see if we should expand this op or something.
1017 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1018 default: assert(0 && "This action is not supported yet!");
1019 case TargetLowering::Legal: break;
1020 case TargetLowering::Expand:
1021 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1,
1022 DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp2,
1023 Tmp3, Tmp4),
1024 Result.getOperand(4));
1025 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Result.getOperand(5));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001026 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001027 }
1028 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001029 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001030 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001031 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1032 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001033
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001034 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001035 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1036 Tmp2 = Result.getValue(0);
1037 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001038
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001039 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1040 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001041 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001042 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001043 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001044 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001045 Tmp2 = LegalizeOp(Tmp1);
1046 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001047 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001048 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001049 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001050 // Since loads produce two values, make sure to remember that we
1051 // legalized both of them.
1052 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1053 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1054 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001055 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001056 case ISD::EXTLOAD:
1057 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001058 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1060 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001061
Chris Lattner5f056bf2005-07-10 01:55:33 +00001062 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001063 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001064 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001065 case TargetLowering::Promote:
1066 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1068 DAG.getValueType(MVT::i8));
1069 Tmp1 = Result.getValue(0);
1070 Tmp2 = Result.getValue(1);
1071 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001072 case TargetLowering::Custom:
1073 isCustom = true;
1074 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001075 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001076 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1077 Node->getOperand(3));
1078 Tmp1 = Result.getValue(0);
1079 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001080
1081 if (isCustom) {
1082 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1083 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001084 Tmp1 = LegalizeOp(Tmp3);
1085 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001086 }
1087 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001088 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001089 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001090 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001091 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1092 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001093 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001094 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1095 Tmp2 = LegalizeOp(Load.getValue(1));
1096 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001097 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001098 assert(Node->getOpcode() != ISD::EXTLOAD &&
1099 "EXTLOAD should always be supported!");
1100 // Turn the unsupported load into an EXTLOAD followed by an explicit
1101 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001102 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1103 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001104 SDOperand ValRes;
1105 if (Node->getOpcode() == ISD::SEXTLOAD)
1106 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001107 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001108 else
1109 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001110 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1111 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1112 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001113 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001114 // Since loads produce two values, make sure to remember that we legalized
1115 // both of them.
1116 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1117 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1118 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001119 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001120 case ISD::EXTRACT_ELEMENT: {
1121 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1122 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001123 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001124 case Legal:
1125 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1126 // 1 -> Hi
1127 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1128 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1129 TLI.getShiftAmountTy()));
1130 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1131 } else {
1132 // 0 -> Lo
1133 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1134 Node->getOperand(0));
1135 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001136 break;
1137 case Expand:
1138 // Get both the low and high parts.
1139 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1140 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1141 Result = Tmp2; // 1 -> Hi
1142 else
1143 Result = Tmp1; // 0 -> Lo
1144 break;
1145 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001146 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001147 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001148
1149 case ISD::CopyToReg:
1150 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001151
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001152 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001153 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001154 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001155 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001156 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001157 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001158 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001159 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001160 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001161 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1163 Tmp3);
1164 } else {
1165 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001166 }
1167
1168 // Since this produces two values, make sure to remember that we legalized
1169 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001170 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001171 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001172 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001173 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001174 break;
1175
1176 case ISD::RET:
1177 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001178
1179 // Ensure that libcalls are emitted before a return.
1180 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1181 Tmp1 = LegalizeOp(Tmp1);
1182 LastCALLSEQ_END = DAG.getEntryNode();
1183
Chris Lattner3e928bb2005-01-07 07:47:09 +00001184 switch (Node->getNumOperands()) {
1185 case 2: // ret val
1186 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1187 case Legal:
1188 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001190 break;
1191 case Expand: {
1192 SDOperand Lo, Hi;
1193 ExpandOp(Node->getOperand(1), Lo, Hi);
1194 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001195 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001196 }
1197 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001198 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001199 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1200 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001201 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001202 }
1203 break;
1204 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001205 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001206 break;
1207 default: { // ret <values>
1208 std::vector<SDOperand> NewValues;
1209 NewValues.push_back(Tmp1);
1210 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1211 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1212 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001213 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001214 break;
1215 case Expand: {
1216 SDOperand Lo, Hi;
1217 ExpandOp(Node->getOperand(i), Lo, Hi);
1218 NewValues.push_back(Lo);
1219 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001220 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001221 }
1222 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001223 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001224 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001225
1226 if (NewValues.size() == Node->getNumOperands())
1227 Result = DAG.UpdateNodeOperands(Result, NewValues);
1228 else
1229 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001230 break;
1231 }
1232 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001233
Chris Lattner6862dbc2006-01-29 21:02:23 +00001234 if (Result.getOpcode() == ISD::RET) {
1235 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1236 default: assert(0 && "This action is not supported yet!");
1237 case TargetLowering::Legal: break;
1238 case TargetLowering::Custom:
1239 Tmp1 = TLI.LowerOperation(Result, DAG);
1240 if (Tmp1.Val) Result = Tmp1;
1241 break;
1242 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001243 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001244 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001245 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001246 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1247 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1248
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001249 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001250 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner03c85462005-01-15 05:21:40 +00001251 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001252 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001253 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001254 } else {
1255 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001256 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001257 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001258 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1259 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001260 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001261 }
1262
Chris Lattner3e928bb2005-01-07 07:47:09 +00001263 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1264 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001265 Tmp3 = LegalizeOp(Node->getOperand(1));
1266 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1267 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001268
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001269 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001270 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1271 default: assert(0 && "This action is not supported yet!");
1272 case TargetLowering::Legal: break;
1273 case TargetLowering::Custom:
1274 Tmp1 = TLI.LowerOperation(Result, DAG);
1275 if (Tmp1.Val) Result = Tmp1;
1276 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001277 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001278 break;
1279 }
1280 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001281 // Truncate the value and store the result.
1282 Tmp3 = PromoteOp(Node->getOperand(1));
1283 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001284 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001285 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001286 break;
1287
Chris Lattner3e928bb2005-01-07 07:47:09 +00001288 case Expand:
1289 SDOperand Lo, Hi;
1290 ExpandOp(Node->getOperand(1), Lo, Hi);
1291
1292 if (!TLI.isLittleEndian())
1293 std::swap(Lo, Hi);
1294
Chris Lattneredb1add2005-05-11 04:51:16 +00001295 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1296 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001297 // If this is a vector type, then we have to calculate the increment as
1298 // the product of the element size in bytes, and the number of elements
1299 // in the high half of the vector.
Chris Lattner456a93a2006-01-28 07:39:30 +00001300 unsigned IncrementSize;
Nate Begemanab48be32005-11-22 18:16:00 +00001301 if (MVT::Vector == Hi.getValueType()) {
1302 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1303 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1304 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1305 } else {
1306 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1307 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001308 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1309 getIntPtrConstant(IncrementSize));
1310 assert(isTypeLegal(Tmp2.getValueType()) &&
1311 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001312 // FIXME: This sets the srcvalue of both halves to be the same, which is
1313 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001314 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1315 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1317 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001318 }
1319 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001320 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001321 case ISD::PCMARKER:
1322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001324 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001325 case ISD::STACKSAVE:
1326 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001327 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1328 Tmp1 = Result.getValue(0);
1329 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001330
Chris Lattner140d53c2006-01-13 02:50:02 +00001331 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1332 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001333 case TargetLowering::Legal: break;
1334 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001335 Tmp3 = TLI.LowerOperation(Result, DAG);
1336 if (Tmp3.Val) {
1337 Tmp1 = LegalizeOp(Tmp3);
1338 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001339 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001340 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001341 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001342 // Expand to CopyFromReg if the target set
1343 // StackPointerRegisterToSaveRestore.
1344 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001345 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001346 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001347 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001348 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001349 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1350 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001351 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001352 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001353 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001354
1355 // Since stacksave produce two values, make sure to remember that we
1356 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001357 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1358 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1359 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001360
Chris Lattner140d53c2006-01-13 02:50:02 +00001361 case ISD::STACKRESTORE:
1362 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1363 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001364 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001365
1366 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1367 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001368 case TargetLowering::Legal: break;
1369 case TargetLowering::Custom:
1370 Tmp1 = TLI.LowerOperation(Result, DAG);
1371 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001372 break;
1373 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001374 // Expand to CopyToReg if the target set
1375 // StackPointerRegisterToSaveRestore.
1376 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1377 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1378 } else {
1379 Result = Tmp1;
1380 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001381 break;
1382 }
1383 break;
1384
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001385 case ISD::READCYCLECOUNTER:
1386 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001387 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001388
1389 // Since rdcc produce two values, make sure to remember that we legalized
1390 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001391 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001392 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001393 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001394
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001395 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1397 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1398
Chris Lattner456a93a2006-01-28 07:39:30 +00001399 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1400 "Cannot handle illegal TRUNCSTORE yet!");
1401 Tmp2 = LegalizeOp(Node->getOperand(1));
1402
1403 // The only promote case we handle is TRUNCSTORE:i1 X into
1404 // -> TRUNCSTORE:i8 (and X, 1)
1405 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1406 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1407 TargetLowering::Promote) {
1408 // Promote the bool to a mask then store.
1409 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1410 DAG.getConstant(1, Tmp2.getValueType()));
1411 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1412 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001413
Chris Lattner456a93a2006-01-28 07:39:30 +00001414 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1415 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001416 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1417 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001418 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001419
Chris Lattner456a93a2006-01-28 07:39:30 +00001420 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1421 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1422 default: assert(0 && "This action is not supported yet!");
1423 case TargetLowering::Legal: break;
1424 case TargetLowering::Custom:
1425 Tmp1 = TLI.LowerOperation(Result, DAG);
1426 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001427 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001428 }
1429 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001430 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001431 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001432 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1433 case Expand: assert(0 && "It's impossible to expand bools");
1434 case Legal:
1435 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1436 break;
1437 case Promote:
1438 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1439 break;
1440 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001441 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001442 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001443
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001445
Nate Begemanb942a3d2005-08-23 04:29:48 +00001446 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001447 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001448 case TargetLowering::Legal: break;
1449 case TargetLowering::Custom: {
1450 Tmp1 = TLI.LowerOperation(Result, DAG);
1451 if (Tmp1.Val) Result = Tmp1;
1452 break;
1453 }
Nate Begeman9373a812005-08-10 20:51:12 +00001454 case TargetLowering::Expand:
1455 if (Tmp1.getOpcode() == ISD::SETCC) {
1456 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1457 Tmp2, Tmp3,
1458 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1459 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001460 // Make sure the condition is either zero or one. It may have been
1461 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001462 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1463 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1464 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001465 Result = DAG.getSelectCC(Tmp1,
1466 DAG.getConstant(0, Tmp1.getValueType()),
1467 Tmp2, Tmp3, ISD::SETNE);
1468 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001469 break;
1470 case TargetLowering::Promote: {
1471 MVT::ValueType NVT =
1472 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1473 unsigned ExtOp, TruncOp;
1474 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001475 ExtOp = ISD::ANY_EXTEND;
1476 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001477 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001478 ExtOp = ISD::FP_EXTEND;
1479 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001480 }
1481 // Promote each of the values to the new type.
1482 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1483 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1484 // Perform the larger operation, then round down.
1485 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1486 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1487 break;
1488 }
1489 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001490 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001491 case ISD::SELECT_CC: {
1492 Tmp1 = Node->getOperand(0); // LHS
1493 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001494 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1495 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001496 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001497
Nate Begeman750ac1b2006-02-01 07:19:44 +00001498 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1499
1500 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1501 // the LHS is a legal SETCC itself. In this case, we need to compare
1502 // the result against zero to select between true and false values.
1503 if (Tmp2.Val == 0) {
1504 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1505 CC = DAG.getCondCode(ISD::SETNE);
1506 }
1507 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1508
1509 // Everything is legal, see if we should expand this op or something.
1510 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1511 default: assert(0 && "This action is not supported yet!");
1512 case TargetLowering::Legal: break;
1513 case TargetLowering::Custom:
1514 Tmp1 = TLI.LowerOperation(Result, DAG);
1515 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001516 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001517 }
1518 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001519 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001520 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001521 Tmp1 = Node->getOperand(0);
1522 Tmp2 = Node->getOperand(1);
1523 Tmp3 = Node->getOperand(2);
1524 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1525
1526 // If we had to Expand the SetCC operands into a SELECT node, then it may
1527 // not always be possible to return a true LHS & RHS. In this case, just
1528 // return the value we legalized, returned in the LHS
1529 if (Tmp2.Val == 0) {
1530 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001531 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001532 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001533
Chris Lattner73e142f2006-01-30 22:43:50 +00001534 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001535 default: assert(0 && "Cannot handle this action for SETCC yet!");
1536 case TargetLowering::Custom:
1537 isCustom = true;
1538 // FALLTHROUGH.
1539 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001540 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001541 if (isCustom) {
1542 Tmp3 = TLI.LowerOperation(Result, DAG);
1543 if (Tmp3.Val) Result = Tmp3;
1544 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001545 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001546 case TargetLowering::Promote: {
1547 // First step, figure out the appropriate operation to use.
1548 // Allow SETCC to not be supported for all legal data types
1549 // Mostly this targets FP
1550 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1551 MVT::ValueType OldVT = NewInTy;
1552
1553 // Scan for the appropriate larger type to use.
1554 while (1) {
1555 NewInTy = (MVT::ValueType)(NewInTy+1);
1556
1557 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1558 "Fell off of the edge of the integer world");
1559 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1560 "Fell off of the edge of the floating point world");
1561
1562 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001563 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001564 break;
1565 }
1566 if (MVT::isInteger(NewInTy))
1567 assert(0 && "Cannot promote Legal Integer SETCC yet");
1568 else {
1569 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1570 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1571 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001572 Tmp1 = LegalizeOp(Tmp1);
1573 Tmp2 = LegalizeOp(Tmp2);
1574 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001575 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001576 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001577 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001578 case TargetLowering::Expand:
1579 // Expand a setcc node into a select_cc of the same condition, lhs, and
1580 // rhs that selects between const 1 (true) and const 0 (false).
1581 MVT::ValueType VT = Node->getValueType(0);
1582 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1583 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1584 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001585 break;
1586 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001587 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001588 case ISD::MEMSET:
1589 case ISD::MEMCPY:
1590 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001591 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001592 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1593
1594 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1595 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1596 case Expand: assert(0 && "Cannot expand a byte!");
1597 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001598 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001599 break;
1600 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001601 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001602 break;
1603 }
1604 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001605 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001606 }
Chris Lattner272455b2005-02-02 03:44:41 +00001607
1608 SDOperand Tmp4;
1609 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001610 case Expand: {
1611 // Length is too big, just take the lo-part of the length.
1612 SDOperand HiPart;
1613 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1614 break;
1615 }
Chris Lattnere5605212005-01-28 22:29:18 +00001616 case Legal:
1617 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001618 break;
1619 case Promote:
1620 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001621 break;
1622 }
1623
1624 SDOperand Tmp5;
1625 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1626 case Expand: assert(0 && "Cannot expand this yet!");
1627 case Legal:
1628 Tmp5 = LegalizeOp(Node->getOperand(4));
1629 break;
1630 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001631 Tmp5 = PromoteOp(Node->getOperand(4));
1632 break;
1633 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001634
1635 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1636 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001637 case TargetLowering::Custom:
1638 isCustom = true;
1639 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001640 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001641 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001642 if (isCustom) {
1643 Tmp1 = TLI.LowerOperation(Result, DAG);
1644 if (Tmp1.Val) Result = Tmp1;
1645 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001646 break;
1647 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001648 // Otherwise, the target does not support this operation. Lower the
1649 // operation to an explicit libcall as appropriate.
1650 MVT::ValueType IntPtr = TLI.getPointerTy();
1651 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1652 std::vector<std::pair<SDOperand, const Type*> > Args;
1653
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001654 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001655 if (Node->getOpcode() == ISD::MEMSET) {
1656 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1657 // Extend the ubyte argument to be an int value for the call.
1658 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1659 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1660 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1661
1662 FnName = "memset";
1663 } else if (Node->getOpcode() == ISD::MEMCPY ||
1664 Node->getOpcode() == ISD::MEMMOVE) {
1665 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1666 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1667 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1668 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1669 } else {
1670 assert(0 && "Unknown op!");
1671 }
Chris Lattner45982da2005-05-12 16:53:42 +00001672
Chris Lattnere1bd8222005-01-11 05:57:22 +00001673 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001674 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001675 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001676 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001677 break;
1678 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001679 }
1680 break;
1681 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001682
1683 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001684 Tmp1 = LegalizeOp(Node->getOperand(0));
1685 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001686 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001687
Chris Lattner52d08bd2005-05-09 20:23:03 +00001688 // Since these produce two values, make sure to remember that we legalized
1689 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001690 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner52d08bd2005-05-09 20:23:03 +00001691 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001692 return Result;
Chris Lattner52d08bd2005-05-09 20:23:03 +00001693 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001694 Tmp1 = LegalizeOp(Node->getOperand(0));
1695 Tmp2 = LegalizeOp(Node->getOperand(1));
1696 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001698 break;
1699
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001700 case ISD::READIO:
1701 Tmp1 = LegalizeOp(Node->getOperand(0));
1702 Tmp2 = LegalizeOp(Node->getOperand(1));
1703
1704 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1705 case TargetLowering::Custom:
1706 default: assert(0 && "This action not implemented for this operation!");
1707 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001708 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001709 break;
1710 case TargetLowering::Expand:
1711 // Replace this with a load from memory.
1712 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1713 Node->getOperand(1), DAG.getSrcValue(NULL));
1714 Result = LegalizeOp(Result);
1715 break;
1716 }
1717
1718 // Since these produce two values, make sure to remember that we legalized
1719 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001720 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001721 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1722 return Result.getValue(Op.ResNo);
1723
1724 case ISD::WRITEIO:
1725 Tmp1 = LegalizeOp(Node->getOperand(0));
1726 Tmp2 = LegalizeOp(Node->getOperand(1));
1727 Tmp3 = LegalizeOp(Node->getOperand(2));
1728
1729 switch (TLI.getOperationAction(Node->getOpcode(),
1730 Node->getOperand(1).getValueType())) {
1731 case TargetLowering::Custom:
1732 default: assert(0 && "This action not implemented for this operation!");
1733 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001734 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001735 break;
1736 case TargetLowering::Expand:
1737 // Replace this with a store to memory.
1738 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1739 Node->getOperand(1), Node->getOperand(2),
1740 DAG.getSrcValue(NULL));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001741 break;
1742 }
1743 break;
1744
Chris Lattner84f67882005-01-20 18:52:28 +00001745 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001746 case ISD::SUB_PARTS:
1747 case ISD::SHL_PARTS:
1748 case ISD::SRA_PARTS:
1749 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001750 std::vector<SDOperand> Ops;
1751 bool Changed = false;
1752 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1753 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1754 Changed |= Ops.back() != Node->getOperand(i);
1755 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001756 if (Changed)
1757 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001758
Evan Cheng05a2d562006-01-09 18:31:59 +00001759 switch (TLI.getOperationAction(Node->getOpcode(),
1760 Node->getValueType(0))) {
1761 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001762 case TargetLowering::Legal: break;
1763 case TargetLowering::Custom:
1764 Tmp1 = TLI.LowerOperation(Result, DAG);
1765 if (Tmp1.Val) {
1766 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001767 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001768 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001769 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1770 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001771 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001772 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001773 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001774 return RetVal;
1775 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001776 break;
1777 }
1778
Chris Lattner2c8086f2005-04-02 05:00:07 +00001779 // Since these produce multiple values, make sure to remember that we
1780 // legalized all of them.
1781 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1782 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1783 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001784 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001785
1786 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001787 case ISD::ADD:
1788 case ISD::SUB:
1789 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001790 case ISD::MULHS:
1791 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001792 case ISD::UDIV:
1793 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001794 case ISD::AND:
1795 case ISD::OR:
1796 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001797 case ISD::SHL:
1798 case ISD::SRL:
1799 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001800 case ISD::FADD:
1801 case ISD::FSUB:
1802 case ISD::FMUL:
1803 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001804 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001805 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1806 case Expand: assert(0 && "Not possible");
1807 case Legal:
1808 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1809 break;
1810 case Promote:
1811 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1812 break;
1813 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001814
1815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001816
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001817 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001818 default: assert(0 && "Operation not supported");
1819 case TargetLowering::Legal: break;
1820 case TargetLowering::Custom:
1821 Tmp1 = TLI.LowerOperation(Result, DAG);
1822 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001823 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001824 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001825 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001826
Nate Begeman419f8b62005-10-18 00:27:41 +00001827 case ISD::BUILD_PAIR: {
1828 MVT::ValueType PairTy = Node->getValueType(0);
1829 // TODO: handle the case where the Lo and Hi operands are not of legal type
1830 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1831 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1832 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001833 case TargetLowering::Promote:
1834 case TargetLowering::Custom:
1835 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001836 case TargetLowering::Legal:
1837 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1838 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1839 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001840 case TargetLowering::Expand:
1841 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1842 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1843 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1844 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1845 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001846 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001847 break;
1848 }
1849 break;
1850 }
1851
Nate Begemanc105e192005-04-06 00:23:54 +00001852 case ISD::UREM:
1853 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001854 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001855 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1856 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001857
Nate Begemanc105e192005-04-06 00:23:54 +00001858 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001859 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1860 case TargetLowering::Custom:
1861 isCustom = true;
1862 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001863 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001864 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001865 if (isCustom) {
1866 Tmp1 = TLI.LowerOperation(Result, DAG);
1867 if (Tmp1.Val) Result = Tmp1;
1868 }
Nate Begemanc105e192005-04-06 00:23:54 +00001869 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001870 case TargetLowering::Expand:
1871 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001872 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001873 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001874 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001875 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1876 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1877 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1878 } else {
1879 // Floating point mod -> fmod libcall.
1880 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1881 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001882 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001883 }
1884 break;
1885 }
1886 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001887 case ISD::VAARG: {
1888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1889 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1890
Chris Lattner5c62f332006-01-28 07:42:08 +00001891 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001892 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1893 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001894 case TargetLowering::Custom:
1895 isCustom = true;
1896 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001897 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1899 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001900 Tmp1 = Result.getValue(1);
1901
1902 if (isCustom) {
1903 Tmp2 = TLI.LowerOperation(Result, DAG);
1904 if (Tmp2.Val) {
1905 Result = LegalizeOp(Tmp2);
1906 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1907 }
1908 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001909 break;
1910 case TargetLowering::Expand: {
1911 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1912 Node->getOperand(2));
1913 // Increment the pointer, VAList, to the next vaarg
1914 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1915 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1916 TLI.getPointerTy()));
1917 // Store the incremented VAList to the legalized pointer
1918 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1919 Node->getOperand(2));
1920 // Load the actual argument out of the pointer VAList
1921 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001922 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001923 Result = LegalizeOp(Result);
1924 break;
1925 }
1926 }
1927 // Since VAARG produces two values, make sure to remember that we
1928 // legalized both of them.
1929 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001930 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1931 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00001932 }
1933
1934 case ISD::VACOPY:
1935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1936 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1937 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1938
1939 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1940 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001941 case TargetLowering::Custom:
1942 isCustom = true;
1943 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001944 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001945 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1946 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001947 if (isCustom) {
1948 Tmp1 = TLI.LowerOperation(Result, DAG);
1949 if (Tmp1.Val) Result = Tmp1;
1950 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001951 break;
1952 case TargetLowering::Expand:
1953 // This defaults to loading a pointer from the input and storing it to the
1954 // output, returning the chain.
1955 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1956 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1957 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00001958 break;
1959 }
1960 break;
1961
1962 case ISD::VAEND:
1963 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1964 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1965
1966 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1967 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001968 case TargetLowering::Custom:
1969 isCustom = true;
1970 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001971 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001973 if (isCustom) {
1974 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
1975 if (Tmp1.Val) Result = Tmp1;
1976 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001977 break;
1978 case TargetLowering::Expand:
1979 Result = Tmp1; // Default to a no-op, return the chain
1980 break;
1981 }
1982 break;
1983
1984 case ISD::VASTART:
1985 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1986 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1987
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001988 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1989
Nate Begemanacc398c2006-01-25 18:21:52 +00001990 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
1991 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001992 case TargetLowering::Legal: break;
1993 case TargetLowering::Custom:
1994 Tmp1 = TLI.LowerOperation(Result, DAG);
1995 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00001996 break;
1997 }
1998 break;
1999
Nate Begeman35ef9132006-01-11 21:21:00 +00002000 case ISD::ROTL:
2001 case ISD::ROTR:
2002 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2003 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002004
2005 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2006 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002008 break;
2009
Nate Begemand88fc032006-01-14 03:14:10 +00002010 case ISD::BSWAP:
2011 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2012 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002013 case TargetLowering::Custom:
2014 assert(0 && "Cannot custom legalize this yet!");
2015 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002016 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002017 break;
2018 case TargetLowering::Promote: {
2019 MVT::ValueType OVT = Tmp1.getValueType();
2020 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2021 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002022
Chris Lattner456a93a2006-01-28 07:39:30 +00002023 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2024 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2025 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2026 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2027 break;
2028 }
2029 case TargetLowering::Expand:
2030 Result = ExpandBSWAP(Tmp1);
2031 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002032 }
2033 break;
2034
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002035 case ISD::CTPOP:
2036 case ISD::CTTZ:
2037 case ISD::CTLZ:
2038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2039 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002040 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002041 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002042 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002043 break;
2044 case TargetLowering::Promote: {
2045 MVT::ValueType OVT = Tmp1.getValueType();
2046 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002047
2048 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002049 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2050 // Perform the larger operation, then subtract if needed.
2051 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002052 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002053 case ISD::CTPOP:
2054 Result = Tmp1;
2055 break;
2056 case ISD::CTTZ:
2057 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002058 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2059 DAG.getConstant(getSizeInBits(NVT), NVT),
2060 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002061 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002062 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2063 break;
2064 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002065 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002066 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2067 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002068 getSizeInBits(OVT), NVT));
2069 break;
2070 }
2071 break;
2072 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002073 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002074 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002075 break;
2076 }
2077 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002078
Chris Lattner2c8086f2005-04-02 05:00:07 +00002079 // Unary operators
2080 case ISD::FABS:
2081 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002082 case ISD::FSQRT:
2083 case ISD::FSIN:
2084 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002085 Tmp1 = LegalizeOp(Node->getOperand(0));
2086 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002087 case TargetLowering::Promote:
2088 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002089 isCustom = true;
2090 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002091 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002092 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002093 if (isCustom) {
2094 Tmp1 = TLI.LowerOperation(Result, DAG);
2095 if (Tmp1.Val) Result = Tmp1;
2096 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002097 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002098 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002099 switch (Node->getOpcode()) {
2100 default: assert(0 && "Unreachable!");
2101 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002102 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2103 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002104 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002105 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002106 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002107 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2108 MVT::ValueType VT = Node->getValueType(0);
2109 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002110 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002111 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2112 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002113 break;
2114 }
2115 case ISD::FSQRT:
2116 case ISD::FSIN:
2117 case ISD::FCOS: {
2118 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002119 const char *FnName = 0;
2120 switch(Node->getOpcode()) {
2121 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2122 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2123 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2124 default: assert(0 && "Unreachable!");
2125 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002126 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002127 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002128 break;
2129 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002130 }
2131 break;
2132 }
2133 break;
Chris Lattner35481892005-12-23 00:16:34 +00002134
2135 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002136 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002137 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002138 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002139 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2140 Node->getOperand(0).getValueType())) {
2141 default: assert(0 && "Unknown operation action!");
2142 case TargetLowering::Expand:
2143 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2144 break;
2145 case TargetLowering::Legal:
2146 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002147 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002148 break;
2149 }
2150 }
2151 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002152 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002153 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002154 case ISD::UINT_TO_FP: {
2155 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002156 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2157 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002158 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002159 Node->getOperand(0).getValueType())) {
2160 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002161 case TargetLowering::Custom:
2162 isCustom = true;
2163 // FALLTHROUGH
2164 case TargetLowering::Legal:
2165 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002166 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002167 if (isCustom) {
2168 Tmp1 = TLI.LowerOperation(Result, DAG);
2169 if (Tmp1.Val) Result = Tmp1;
2170 }
2171 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002172 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002173 Result = ExpandLegalINT_TO_FP(isSigned,
2174 LegalizeOp(Node->getOperand(0)),
2175 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002176 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002177 case TargetLowering::Promote:
2178 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2179 Node->getValueType(0),
2180 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002181 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002182 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002183 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002184 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002185 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2186 Node->getValueType(0), Node->getOperand(0));
2187 break;
2188 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002189 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002190 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002191 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2192 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002193 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002194 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2195 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002196 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002197 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2198 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002199 break;
2200 }
2201 break;
2202 }
2203 case ISD::TRUNCATE:
2204 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2205 case Legal:
2206 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002207 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002208 break;
2209 case Expand:
2210 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2211
2212 // Since the result is legal, we should just be able to truncate the low
2213 // part of the source.
2214 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2215 break;
2216 case Promote:
2217 Result = PromoteOp(Node->getOperand(0));
2218 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2219 break;
2220 }
2221 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002222
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002223 case ISD::FP_TO_SINT:
2224 case ISD::FP_TO_UINT:
2225 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2226 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002227 Tmp1 = LegalizeOp(Node->getOperand(0));
2228
Chris Lattner1618beb2005-07-29 00:11:56 +00002229 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2230 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002231 case TargetLowering::Custom:
2232 isCustom = true;
2233 // FALLTHROUGH
2234 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002235 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002236 if (isCustom) {
2237 Tmp1 = TLI.LowerOperation(Result, DAG);
2238 if (Tmp1.Val) Result = Tmp1;
2239 }
2240 break;
2241 case TargetLowering::Promote:
2242 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2243 Node->getOpcode() == ISD::FP_TO_SINT);
2244 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002245 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002246 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2247 SDOperand True, False;
2248 MVT::ValueType VT = Node->getOperand(0).getValueType();
2249 MVT::ValueType NVT = Node->getValueType(0);
2250 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2251 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2252 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2253 Node->getOperand(0), Tmp2, ISD::SETLT);
2254 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2255 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002256 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002257 Tmp2));
2258 False = DAG.getNode(ISD::XOR, NVT, False,
2259 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002260 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2261 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002262 } else {
2263 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2264 }
2265 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002266 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002267 break;
2268 case Expand:
2269 assert(0 && "Shouldn't need to expand other operators here!");
2270 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002271 Tmp1 = PromoteOp(Node->getOperand(0));
2272 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2273 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002274 break;
2275 }
2276 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002277
Chris Lattner13c78e22005-09-02 00:18:10 +00002278 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002279 case ISD::ZERO_EXTEND:
2280 case ISD::SIGN_EXTEND:
2281 case ISD::FP_EXTEND:
2282 case ISD::FP_ROUND:
2283 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002284 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002285 case Legal:
2286 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002287 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002288 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002289 case Promote:
2290 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002291 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002292 Tmp1 = PromoteOp(Node->getOperand(0));
2293 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002294 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002295 case ISD::ZERO_EXTEND:
2296 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002297 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002298 Result = DAG.getZeroExtendInReg(Result,
2299 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002300 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002301 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002302 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002303 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002304 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002305 Result,
2306 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002307 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002308 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002309 Result = PromoteOp(Node->getOperand(0));
2310 if (Result.getValueType() != Op.getValueType())
2311 // Dynamically dead while we have only 2 FP types.
2312 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2313 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002314 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002315 Result = PromoteOp(Node->getOperand(0));
2316 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2317 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002318 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002319 }
2320 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002321 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002322 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002323 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002324 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002325
2326 // If this operation is not supported, convert it to a shl/shr or load/store
2327 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002328 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2329 default: assert(0 && "This action not supported for this op yet!");
2330 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002331 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002332 break;
2333 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002334 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002335 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002336 // NOTE: we could fall back on load/store here too for targets without
2337 // SAR. However, it is doubtful that any exist.
2338 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2339 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002340 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002341 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2342 Node->getOperand(0), ShiftCst);
2343 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2344 Result, ShiftCst);
2345 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2346 // The only way we can lower this is to turn it into a STORETRUNC,
2347 // EXTLOAD pair, targetting a temporary location (a stack slot).
2348
2349 // NOTE: there is a choice here between constantly creating new stack
2350 // slots and always reusing the same one. We currently always create
2351 // new ones, as reuse may inhibit scheduling.
2352 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2353 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2354 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2355 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002356 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002357 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2358 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2359 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002360 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002361 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002362 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2363 Result, StackSlot, DAG.getSrcValue(NULL),
2364 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002365 } else {
2366 assert(0 && "Unknown op");
2367 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002368 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002369 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002370 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002371 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002372 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002373
2374 // Make sure that the generated code is itself legal.
2375 if (Result != Op)
2376 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002377
Chris Lattner45982da2005-05-12 16:53:42 +00002378 // Note that LegalizeOp may be reentered even from single-use nodes, which
2379 // means that we always must cache transformed nodes.
2380 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002381 return Result;
2382}
2383
Chris Lattner8b6fa222005-01-15 22:16:26 +00002384/// PromoteOp - Given an operation that produces a value in an invalid type,
2385/// promote it to compute the value into a larger type. The produced value will
2386/// have the correct bits for the low portion of the register, but no guarantee
2387/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002388SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2389 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002390 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002391 assert(getTypeAction(VT) == Promote &&
2392 "Caller should expand or legalize operands that are not promotable!");
2393 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2394 "Cannot promote to smaller type!");
2395
Chris Lattner03c85462005-01-15 05:21:40 +00002396 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002397 SDOperand Result;
2398 SDNode *Node = Op.Val;
2399
Chris Lattner6fdcb252005-09-02 20:32:45 +00002400 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2401 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002402
Chris Lattner03c85462005-01-15 05:21:40 +00002403 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002404 case ISD::CopyFromReg:
2405 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002406 default:
2407 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2408 assert(0 && "Do not know how to promote this operator!");
2409 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002410 case ISD::UNDEF:
2411 Result = DAG.getNode(ISD::UNDEF, NVT);
2412 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002413 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002414 if (VT != MVT::i1)
2415 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2416 else
2417 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002418 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2419 break;
2420 case ISD::ConstantFP:
2421 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2422 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2423 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002424
Chris Lattner82fbfb62005-01-18 02:59:52 +00002425 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002426 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002427 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2428 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002429 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002430
2431 case ISD::TRUNCATE:
2432 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2433 case Legal:
2434 Result = LegalizeOp(Node->getOperand(0));
2435 assert(Result.getValueType() >= NVT &&
2436 "This truncation doesn't make sense!");
2437 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2438 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2439 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002440 case Promote:
2441 // The truncation is not required, because we don't guarantee anything
2442 // about high bits anyway.
2443 Result = PromoteOp(Node->getOperand(0));
2444 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002445 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002446 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2447 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002448 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002449 }
2450 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002451 case ISD::SIGN_EXTEND:
2452 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002453 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002454 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2455 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2456 case Legal:
2457 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002458 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002459 break;
2460 case Promote:
2461 // Promote the reg if it's smaller.
2462 Result = PromoteOp(Node->getOperand(0));
2463 // The high bits are not guaranteed to be anything. Insert an extend.
2464 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002465 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002466 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002467 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002468 Result = DAG.getZeroExtendInReg(Result,
2469 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002470 break;
2471 }
2472 break;
Chris Lattner35481892005-12-23 00:16:34 +00002473 case ISD::BIT_CONVERT:
2474 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2475 Result = PromoteOp(Result);
2476 break;
2477
Chris Lattner8b6fa222005-01-15 22:16:26 +00002478 case ISD::FP_EXTEND:
2479 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2480 case ISD::FP_ROUND:
2481 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2482 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2483 case Promote: assert(0 && "Unreachable with 2 FP types!");
2484 case Legal:
2485 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002486 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002487 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002488 break;
2489 }
2490 break;
2491
2492 case ISD::SINT_TO_FP:
2493 case ISD::UINT_TO_FP:
2494 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2495 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002496 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002497 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002498 break;
2499
2500 case Promote:
2501 Result = PromoteOp(Node->getOperand(0));
2502 if (Node->getOpcode() == ISD::SINT_TO_FP)
2503 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002504 Result,
2505 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002506 else
Chris Lattner23993562005-04-13 02:38:47 +00002507 Result = DAG.getZeroExtendInReg(Result,
2508 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002509 // No extra round required here.
2510 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002511 break;
2512 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002513 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2514 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002515 // Round if we cannot tolerate excess precision.
2516 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002517 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2518 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002519 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002520 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002521 break;
2522
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002523 case ISD::SIGN_EXTEND_INREG:
2524 Result = PromoteOp(Node->getOperand(0));
2525 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2526 Node->getOperand(1));
2527 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002528 case ISD::FP_TO_SINT:
2529 case ISD::FP_TO_UINT:
2530 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2531 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002532 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002533 break;
2534 case Promote:
2535 // The input result is prerounded, so we don't have to do anything
2536 // special.
2537 Tmp1 = PromoteOp(Node->getOperand(0));
2538 break;
2539 case Expand:
2540 assert(0 && "not implemented");
2541 }
Nate Begemand2558e32005-08-14 01:20:53 +00002542 // If we're promoting a UINT to a larger size, check to see if the new node
2543 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2544 // we can use that instead. This allows us to generate better code for
2545 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2546 // legal, such as PowerPC.
2547 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002548 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002549 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2550 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002551 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2552 } else {
2553 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2554 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002555 break;
2556
Chris Lattner2c8086f2005-04-02 05:00:07 +00002557 case ISD::FABS:
2558 case ISD::FNEG:
2559 Tmp1 = PromoteOp(Node->getOperand(0));
2560 assert(Tmp1.getValueType() == NVT);
2561 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2562 // NOTE: we do not have to do any extra rounding here for
2563 // NoExcessFPPrecision, because we know the input will have the appropriate
2564 // precision, and these operations don't modify precision at all.
2565 break;
2566
Chris Lattnerda6ba872005-04-28 21:44:33 +00002567 case ISD::FSQRT:
2568 case ISD::FSIN:
2569 case ISD::FCOS:
2570 Tmp1 = PromoteOp(Node->getOperand(0));
2571 assert(Tmp1.getValueType() == NVT);
2572 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002573 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002574 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2575 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002576 break;
2577
Chris Lattner03c85462005-01-15 05:21:40 +00002578 case ISD::AND:
2579 case ISD::OR:
2580 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002581 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002582 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002583 case ISD::MUL:
2584 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002585 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002586 // that too is okay if they are integer operations.
2587 Tmp1 = PromoteOp(Node->getOperand(0));
2588 Tmp2 = PromoteOp(Node->getOperand(1));
2589 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2590 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002591 break;
2592 case ISD::FADD:
2593 case ISD::FSUB:
2594 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002595 Tmp1 = PromoteOp(Node->getOperand(0));
2596 Tmp2 = PromoteOp(Node->getOperand(1));
2597 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2598 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2599
2600 // Floating point operations will give excess precision that we may not be
2601 // able to tolerate. If we DO allow excess precision, just leave it,
2602 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002603 // FIXME: Why would we need to round FP ops more than integer ones?
2604 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002605 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002606 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2607 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002608 break;
2609
Chris Lattner8b6fa222005-01-15 22:16:26 +00002610 case ISD::SDIV:
2611 case ISD::SREM:
2612 // These operators require that their input be sign extended.
2613 Tmp1 = PromoteOp(Node->getOperand(0));
2614 Tmp2 = PromoteOp(Node->getOperand(1));
2615 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002616 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2617 DAG.getValueType(VT));
2618 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2619 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002620 }
2621 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2622
2623 // Perform FP_ROUND: this is probably overly pessimistic.
2624 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002625 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2626 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002627 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002628 case ISD::FDIV:
2629 case ISD::FREM:
2630 // These operators require that their input be fp extended.
2631 Tmp1 = PromoteOp(Node->getOperand(0));
2632 Tmp2 = PromoteOp(Node->getOperand(1));
2633 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2634
2635 // Perform FP_ROUND: this is probably overly pessimistic.
2636 if (NoExcessFPPrecision)
2637 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2638 DAG.getValueType(VT));
2639 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002640
2641 case ISD::UDIV:
2642 case ISD::UREM:
2643 // These operators require that their input be zero extended.
2644 Tmp1 = PromoteOp(Node->getOperand(0));
2645 Tmp2 = PromoteOp(Node->getOperand(1));
2646 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002647 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2648 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002649 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2650 break;
2651
2652 case ISD::SHL:
2653 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002654 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002655 break;
2656 case ISD::SRA:
2657 // The input value must be properly sign extended.
2658 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002659 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2660 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002661 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002662 break;
2663 case ISD::SRL:
2664 // The input value must be properly zero extended.
2665 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002666 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002667 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002668 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002669
2670 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002671 Tmp1 = Node->getOperand(0); // Get the chain.
2672 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002673 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2674 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2675 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2676 } else {
2677 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2678 Node->getOperand(2));
2679 // Increment the pointer, VAList, to the next vaarg
2680 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2681 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2682 TLI.getPointerTy()));
2683 // Store the incremented VAList to the legalized pointer
2684 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2685 Node->getOperand(2));
2686 // Load the actual argument out of the pointer VAList
2687 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2688 DAG.getSrcValue(0), VT);
2689 }
2690 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002691 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002692 break;
2693
Chris Lattner03c85462005-01-15 05:21:40 +00002694 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002695 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2696 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002697 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002698 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002699 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002700 case ISD::SEXTLOAD:
2701 case ISD::ZEXTLOAD:
2702 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002703 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2704 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002705 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002706 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002707 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002708 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002709 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002710 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2711 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002712 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002713 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002714 case ISD::SELECT_CC:
2715 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2716 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2717 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002718 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002719 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002720 case ISD::BSWAP:
2721 Tmp1 = Node->getOperand(0);
2722 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2723 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2724 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2725 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2726 TLI.getShiftAmountTy()));
2727 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002728 case ISD::CTPOP:
2729 case ISD::CTTZ:
2730 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002731 // Zero extend the argument
2732 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002733 // Perform the larger operation, then subtract if needed.
2734 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002735 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002736 case ISD::CTPOP:
2737 Result = Tmp1;
2738 break;
2739 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002740 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002741 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002742 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002743 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002744 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002745 break;
2746 case ISD::CTLZ:
2747 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002748 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2749 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002750 getSizeInBits(VT), NVT));
2751 break;
2752 }
2753 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002754 }
2755
2756 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002757
2758 // Make sure the result is itself legal.
2759 Result = LegalizeOp(Result);
2760
2761 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002762 AddPromotedOperand(Op, Result);
2763 return Result;
2764}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002765
Nate Begeman750ac1b2006-02-01 07:19:44 +00002766/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2767/// with condition CC on the current target. This usually involves legalizing
2768/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2769/// there may be no choice but to create a new SetCC node to represent the
2770/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2771/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2772void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2773 SDOperand &RHS,
2774 SDOperand &CC) {
2775 SDOperand Tmp1, Tmp2, Result;
2776
2777 switch (getTypeAction(LHS.getValueType())) {
2778 case Legal:
2779 Tmp1 = LegalizeOp(LHS); // LHS
2780 Tmp2 = LegalizeOp(RHS); // RHS
2781 break;
2782 case Promote:
2783 Tmp1 = PromoteOp(LHS); // LHS
2784 Tmp2 = PromoteOp(RHS); // RHS
2785
2786 // If this is an FP compare, the operands have already been extended.
2787 if (MVT::isInteger(LHS.getValueType())) {
2788 MVT::ValueType VT = LHS.getValueType();
2789 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2790
2791 // Otherwise, we have to insert explicit sign or zero extends. Note
2792 // that we could insert sign extends for ALL conditions, but zero extend
2793 // is cheaper on many machines (an AND instead of two shifts), so prefer
2794 // it.
2795 switch (cast<CondCodeSDNode>(CC)->get()) {
2796 default: assert(0 && "Unknown integer comparison!");
2797 case ISD::SETEQ:
2798 case ISD::SETNE:
2799 case ISD::SETUGE:
2800 case ISD::SETUGT:
2801 case ISD::SETULE:
2802 case ISD::SETULT:
2803 // ALL of these operations will work if we either sign or zero extend
2804 // the operands (including the unsigned comparisons!). Zero extend is
2805 // usually a simpler/cheaper operation, so prefer it.
2806 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2807 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2808 break;
2809 case ISD::SETGE:
2810 case ISD::SETGT:
2811 case ISD::SETLT:
2812 case ISD::SETLE:
2813 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2814 DAG.getValueType(VT));
2815 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2816 DAG.getValueType(VT));
2817 break;
2818 }
2819 }
2820 break;
2821 case Expand:
2822 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2823 ExpandOp(LHS, LHSLo, LHSHi);
2824 ExpandOp(RHS, RHSLo, RHSHi);
2825 switch (cast<CondCodeSDNode>(CC)->get()) {
2826 case ISD::SETEQ:
2827 case ISD::SETNE:
2828 if (RHSLo == RHSHi)
2829 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2830 if (RHSCST->isAllOnesValue()) {
2831 // Comparison to -1.
2832 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2833 Tmp2 = RHSLo;
2834 break;
2835 }
2836
2837 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2838 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2839 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2840 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2841 break;
2842 default:
2843 // If this is a comparison of the sign bit, just look at the top part.
2844 // X > -1, x < 0
2845 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2846 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2847 CST->getValue() == 0) || // X < 0
2848 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2849 CST->isAllOnesValue())) { // X > -1
2850 Tmp1 = LHSHi;
2851 Tmp2 = RHSHi;
2852 break;
2853 }
2854
2855 // FIXME: This generated code sucks.
2856 ISD::CondCode LowCC;
2857 switch (cast<CondCodeSDNode>(CC)->get()) {
2858 default: assert(0 && "Unknown integer setcc!");
2859 case ISD::SETLT:
2860 case ISD::SETULT: LowCC = ISD::SETULT; break;
2861 case ISD::SETGT:
2862 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2863 case ISD::SETLE:
2864 case ISD::SETULE: LowCC = ISD::SETULE; break;
2865 case ISD::SETGE:
2866 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2867 }
2868
2869 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2870 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2871 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2872
2873 // NOTE: on targets without efficient SELECT of bools, we can always use
2874 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2875 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2876 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2877 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2878 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2879 Result, Tmp1, Tmp2));
2880 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002881 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002882 }
2883 }
2884 LHS = Tmp1;
2885 RHS = Tmp2;
2886}
2887
Chris Lattner35481892005-12-23 00:16:34 +00002888/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002889/// The resultant code need not be legal. Note that SrcOp is the input operand
2890/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002891SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2892 SDOperand SrcOp) {
2893 // Create the stack frame object.
2894 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2895 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002896 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002897 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2898
2899 // Emit a store to the stack slot.
2900 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002901 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002902 // Result is a load from the stack slot.
2903 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2904}
2905
Chris Lattner5b359c62005-04-02 04:00:59 +00002906void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2907 SDOperand Op, SDOperand Amt,
2908 SDOperand &Lo, SDOperand &Hi) {
2909 // Expand the subcomponents.
2910 SDOperand LHSL, LHSH;
2911 ExpandOp(Op, LHSL, LHSH);
2912
2913 std::vector<SDOperand> Ops;
2914 Ops.push_back(LHSL);
2915 Ops.push_back(LHSH);
2916 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002917 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002918 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002919 Hi = Lo.getValue(1);
2920}
2921
2922
Chris Lattnere34b3962005-01-19 04:19:40 +00002923/// ExpandShift - Try to find a clever way to expand this shift operation out to
2924/// smaller elements. If we can't find a way that is more efficient than a
2925/// libcall on this target, return false. Otherwise, return true with the
2926/// low-parts expanded into Lo and Hi.
2927bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2928 SDOperand &Lo, SDOperand &Hi) {
2929 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2930 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002931
Chris Lattnere34b3962005-01-19 04:19:40 +00002932 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002933 SDOperand ShAmt = LegalizeOp(Amt);
2934 MVT::ValueType ShTy = ShAmt.getValueType();
2935 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2936 unsigned NVTBits = MVT::getSizeInBits(NVT);
2937
2938 // Handle the case when Amt is an immediate. Other cases are currently broken
2939 // and are disabled.
2940 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2941 unsigned Cst = CN->getValue();
2942 // Expand the incoming operand to be shifted, so that we have its parts
2943 SDOperand InL, InH;
2944 ExpandOp(Op, InL, InH);
2945 switch(Opc) {
2946 case ISD::SHL:
2947 if (Cst > VTBits) {
2948 Lo = DAG.getConstant(0, NVT);
2949 Hi = DAG.getConstant(0, NVT);
2950 } else if (Cst > NVTBits) {
2951 Lo = DAG.getConstant(0, NVT);
2952 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002953 } else if (Cst == NVTBits) {
2954 Lo = DAG.getConstant(0, NVT);
2955 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002956 } else {
2957 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2958 Hi = DAG.getNode(ISD::OR, NVT,
2959 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2960 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2961 }
2962 return true;
2963 case ISD::SRL:
2964 if (Cst > VTBits) {
2965 Lo = DAG.getConstant(0, NVT);
2966 Hi = DAG.getConstant(0, NVT);
2967 } else if (Cst > NVTBits) {
2968 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2969 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002970 } else if (Cst == NVTBits) {
2971 Lo = InH;
2972 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002973 } else {
2974 Lo = DAG.getNode(ISD::OR, NVT,
2975 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2976 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2977 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2978 }
2979 return true;
2980 case ISD::SRA:
2981 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002982 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002983 DAG.getConstant(NVTBits-1, ShTy));
2984 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002985 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002986 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002987 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002988 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002989 } else if (Cst == NVTBits) {
2990 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002991 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002992 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002993 } else {
2994 Lo = DAG.getNode(ISD::OR, NVT,
2995 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2996 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2997 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2998 }
2999 return true;
3000 }
3001 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003002 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003003}
Chris Lattner77e77a62005-01-21 06:05:23 +00003004
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003005
Chris Lattner77e77a62005-01-21 06:05:23 +00003006// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3007// does not fit into a register, return the lo part and set the hi part to the
3008// by-reg argument. If it does fit into a single register, return the result
3009// and leave the Hi part unset.
3010SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3011 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003012 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3013 // The input chain to this libcall is the entry node of the function.
3014 // Legalizing the call will automatically add the previous call to the
3015 // dependence.
3016 SDOperand InChain = DAG.getEntryNode();
3017
Chris Lattner77e77a62005-01-21 06:05:23 +00003018 TargetLowering::ArgListTy Args;
3019 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3020 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3021 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3022 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3023 }
3024 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003025
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003026 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003027 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003028 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003029 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3030 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003031
Chris Lattner6831a812006-02-13 09:18:02 +00003032 // Legalize the call sequence, starting with the chain. This will advance
3033 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3034 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3035 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003036 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003037 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003038 default: assert(0 && "Unknown thing");
3039 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003040 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003041 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003042 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003043 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003044 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003045 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003046 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003047}
3048
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003049
Chris Lattner77e77a62005-01-21 06:05:23 +00003050/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3051/// destination type is legal.
3052SDOperand SelectionDAGLegalize::
3053ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003054 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003055 assert(getTypeAction(Source.getValueType()) == Expand &&
3056 "This is not an expansion!");
3057 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3058
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003059 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003060 assert(Source.getValueType() == MVT::i64 &&
3061 "This only works for 64-bit -> FP");
3062 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3063 // incoming integer is set. To handle this, we dynamically test to see if
3064 // it is set, and, if so, add a fudge factor.
3065 SDOperand Lo, Hi;
3066 ExpandOp(Source, Lo, Hi);
3067
Chris Lattner66de05b2005-05-13 04:45:13 +00003068 // If this is unsigned, and not supported, first perform the conversion to
3069 // signed, then adjust the result if the sign bit is set.
3070 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3071 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3072
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003073 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3074 DAG.getConstant(0, Hi.getValueType()),
3075 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003076 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3077 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3078 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003079 uint64_t FF = 0x5f800000ULL;
3080 if (TLI.isLittleEndian()) FF <<= 32;
3081 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003082
Chris Lattner5839bf22005-08-26 17:15:30 +00003083 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003084 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3085 SDOperand FudgeInReg;
3086 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003087 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3088 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003089 else {
3090 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003091 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3092 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003093 }
Chris Lattner473a9902005-09-29 06:44:39 +00003094 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003095 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003096
Chris Lattnera88a2602005-05-14 05:33:54 +00003097 // Check to see if the target has a custom way to lower this. If so, use it.
3098 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3099 default: assert(0 && "This action not implemented for this operation!");
3100 case TargetLowering::Legal:
3101 case TargetLowering::Expand:
3102 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003103 case TargetLowering::Custom: {
3104 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3105 Source), DAG);
3106 if (NV.Val)
3107 return LegalizeOp(NV);
3108 break; // The target decided this was legal after all
3109 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003110 }
3111
Chris Lattner13689e22005-05-12 07:00:44 +00003112 // Expand the source, then glue it back together for the call. We must expand
3113 // the source in case it is shared (this pass of legalize must traverse it).
3114 SDOperand SrcLo, SrcHi;
3115 ExpandOp(Source, SrcLo, SrcHi);
3116 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3117
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003118 const char *FnName = 0;
3119 if (DestTy == MVT::f32)
3120 FnName = "__floatdisf";
3121 else {
3122 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3123 FnName = "__floatdidf";
3124 }
Chris Lattner6831a812006-02-13 09:18:02 +00003125
3126 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3127 SDOperand UnusedHiPart;
3128 return ExpandLibCall("__floatdidf", Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003129}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003130
Chris Lattner22cde6a2006-01-28 08:25:58 +00003131/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3132/// INT_TO_FP operation of the specified operand when the target requests that
3133/// we expand it. At this point, we know that the result and operand types are
3134/// legal for the target.
3135SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3136 SDOperand Op0,
3137 MVT::ValueType DestVT) {
3138 if (Op0.getValueType() == MVT::i32) {
3139 // simple 32-bit [signed|unsigned] integer to float/double expansion
3140
3141 // get the stack frame index of a 8 byte buffer
3142 MachineFunction &MF = DAG.getMachineFunction();
3143 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3144 // get address of 8 byte buffer
3145 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3146 // word offset constant for Hi/Lo address computation
3147 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3148 // set up Hi and Lo (into buffer) address based on endian
3149 SDOperand Hi, Lo;
3150 if (TLI.isLittleEndian()) {
3151 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3152 Lo = StackSlot;
3153 } else {
3154 Hi = StackSlot;
3155 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3156 }
3157 // if signed map to unsigned space
3158 SDOperand Op0Mapped;
3159 if (isSigned) {
3160 // constant used to invert sign bit (signed to unsigned mapping)
3161 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3162 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3163 } else {
3164 Op0Mapped = Op0;
3165 }
3166 // store the lo of the constructed double - based on integer input
3167 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3168 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3169 // initial hi portion of constructed double
3170 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3171 // store the hi of the constructed double - biased exponent
3172 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3173 InitialHi, Hi, DAG.getSrcValue(NULL));
3174 // load the constructed double
3175 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3176 DAG.getSrcValue(NULL));
3177 // FP constant to bias correct the final result
3178 SDOperand Bias = DAG.getConstantFP(isSigned ?
3179 BitsToDouble(0x4330000080000000ULL)
3180 : BitsToDouble(0x4330000000000000ULL),
3181 MVT::f64);
3182 // subtract the bias
3183 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3184 // final result
3185 SDOperand Result;
3186 // handle final rounding
3187 if (DestVT == MVT::f64) {
3188 // do nothing
3189 Result = Sub;
3190 } else {
3191 // if f32 then cast to f32
3192 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3193 }
3194 return Result;
3195 }
3196 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3197 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3198
3199 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3200 DAG.getConstant(0, Op0.getValueType()),
3201 ISD::SETLT);
3202 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3203 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3204 SignSet, Four, Zero);
3205
3206 // If the sign bit of the integer is set, the large number will be treated
3207 // as a negative number. To counteract this, the dynamic code adds an
3208 // offset depending on the data type.
3209 uint64_t FF;
3210 switch (Op0.getValueType()) {
3211 default: assert(0 && "Unsupported integer type!");
3212 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3213 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3214 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3215 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3216 }
3217 if (TLI.isLittleEndian()) FF <<= 32;
3218 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3219
3220 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3221 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3222 SDOperand FudgeInReg;
3223 if (DestVT == MVT::f32)
3224 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3225 DAG.getSrcValue(NULL));
3226 else {
3227 assert(DestVT == MVT::f64 && "Unexpected conversion");
3228 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3229 DAG.getEntryNode(), CPIdx,
3230 DAG.getSrcValue(NULL), MVT::f32));
3231 }
3232
3233 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3234}
3235
3236/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3237/// *INT_TO_FP operation of the specified operand when the target requests that
3238/// we promote it. At this point, we know that the result and operand types are
3239/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3240/// operation that takes a larger input.
3241SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3242 MVT::ValueType DestVT,
3243 bool isSigned) {
3244 // First step, figure out the appropriate *INT_TO_FP operation to use.
3245 MVT::ValueType NewInTy = LegalOp.getValueType();
3246
3247 unsigned OpToUse = 0;
3248
3249 // Scan for the appropriate larger type to use.
3250 while (1) {
3251 NewInTy = (MVT::ValueType)(NewInTy+1);
3252 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3253
3254 // If the target supports SINT_TO_FP of this type, use it.
3255 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3256 default: break;
3257 case TargetLowering::Legal:
3258 if (!TLI.isTypeLegal(NewInTy))
3259 break; // Can't use this datatype.
3260 // FALL THROUGH.
3261 case TargetLowering::Custom:
3262 OpToUse = ISD::SINT_TO_FP;
3263 break;
3264 }
3265 if (OpToUse) break;
3266 if (isSigned) continue;
3267
3268 // If the target supports UINT_TO_FP of this type, use it.
3269 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3270 default: break;
3271 case TargetLowering::Legal:
3272 if (!TLI.isTypeLegal(NewInTy))
3273 break; // Can't use this datatype.
3274 // FALL THROUGH.
3275 case TargetLowering::Custom:
3276 OpToUse = ISD::UINT_TO_FP;
3277 break;
3278 }
3279 if (OpToUse) break;
3280
3281 // Otherwise, try a larger type.
3282 }
3283
3284 // Okay, we found the operation and type to use. Zero extend our input to the
3285 // desired type then run the operation on it.
3286 return DAG.getNode(OpToUse, DestVT,
3287 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3288 NewInTy, LegalOp));
3289}
3290
3291/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3292/// FP_TO_*INT operation of the specified operand when the target requests that
3293/// we promote it. At this point, we know that the result and operand types are
3294/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3295/// operation that returns a larger result.
3296SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3297 MVT::ValueType DestVT,
3298 bool isSigned) {
3299 // First step, figure out the appropriate FP_TO*INT operation to use.
3300 MVT::ValueType NewOutTy = DestVT;
3301
3302 unsigned OpToUse = 0;
3303
3304 // Scan for the appropriate larger type to use.
3305 while (1) {
3306 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3307 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3308
3309 // If the target supports FP_TO_SINT returning this type, use it.
3310 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3311 default: break;
3312 case TargetLowering::Legal:
3313 if (!TLI.isTypeLegal(NewOutTy))
3314 break; // Can't use this datatype.
3315 // FALL THROUGH.
3316 case TargetLowering::Custom:
3317 OpToUse = ISD::FP_TO_SINT;
3318 break;
3319 }
3320 if (OpToUse) break;
3321
3322 // If the target supports FP_TO_UINT of this type, use it.
3323 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3324 default: break;
3325 case TargetLowering::Legal:
3326 if (!TLI.isTypeLegal(NewOutTy))
3327 break; // Can't use this datatype.
3328 // FALL THROUGH.
3329 case TargetLowering::Custom:
3330 OpToUse = ISD::FP_TO_UINT;
3331 break;
3332 }
3333 if (OpToUse) break;
3334
3335 // Otherwise, try a larger type.
3336 }
3337
3338 // Okay, we found the operation and type to use. Truncate the result of the
3339 // extended FP_TO_*INT operation to the desired size.
3340 return DAG.getNode(ISD::TRUNCATE, DestVT,
3341 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3342}
3343
3344/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3345///
3346SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3347 MVT::ValueType VT = Op.getValueType();
3348 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3349 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3350 switch (VT) {
3351 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3352 case MVT::i16:
3353 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3354 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3355 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3356 case MVT::i32:
3357 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3358 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3359 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3360 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3361 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3362 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3363 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3364 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3365 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3366 case MVT::i64:
3367 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3368 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3369 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3370 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3371 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3372 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3373 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3374 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3375 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3376 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3377 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3378 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3379 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3380 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3381 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3382 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3383 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3384 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3385 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3386 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3387 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3388 }
3389}
3390
3391/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3392///
3393SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3394 switch (Opc) {
3395 default: assert(0 && "Cannot expand this yet!");
3396 case ISD::CTPOP: {
3397 static const uint64_t mask[6] = {
3398 0x5555555555555555ULL, 0x3333333333333333ULL,
3399 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3400 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3401 };
3402 MVT::ValueType VT = Op.getValueType();
3403 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3404 unsigned len = getSizeInBits(VT);
3405 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3406 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3407 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3408 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3409 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3410 DAG.getNode(ISD::AND, VT,
3411 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3412 }
3413 return Op;
3414 }
3415 case ISD::CTLZ: {
3416 // for now, we do this:
3417 // x = x | (x >> 1);
3418 // x = x | (x >> 2);
3419 // ...
3420 // x = x | (x >>16);
3421 // x = x | (x >>32); // for 64-bit input
3422 // return popcount(~x);
3423 //
3424 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3425 MVT::ValueType VT = Op.getValueType();
3426 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3427 unsigned len = getSizeInBits(VT);
3428 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3429 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3430 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3431 }
3432 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3433 return DAG.getNode(ISD::CTPOP, VT, Op);
3434 }
3435 case ISD::CTTZ: {
3436 // for now, we use: { return popcount(~x & (x - 1)); }
3437 // unless the target has ctlz but not ctpop, in which case we use:
3438 // { return 32 - nlz(~x & (x-1)); }
3439 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3440 MVT::ValueType VT = Op.getValueType();
3441 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3442 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3443 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3444 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3445 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3446 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3447 TLI.isOperationLegal(ISD::CTLZ, VT))
3448 return DAG.getNode(ISD::SUB, VT,
3449 DAG.getConstant(getSizeInBits(VT), VT),
3450 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3451 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3452 }
3453 }
3454}
Chris Lattnere34b3962005-01-19 04:19:40 +00003455
3456
Chris Lattner3e928bb2005-01-07 07:47:09 +00003457/// ExpandOp - Expand the specified SDOperand into its two component pieces
3458/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3459/// LegalizeNodes map is filled in for any results that are not expanded, the
3460/// ExpandedNodes map is filled in for any results that are expanded, and the
3461/// Lo/Hi values are returned.
3462void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3463 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003464 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003465 SDNode *Node = Op.Val;
3466 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003467 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3468 "Cannot expand FP values!");
3469 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003470 "Cannot expand to FP value or to larger int value!");
3471
Chris Lattner6fdcb252005-09-02 20:32:45 +00003472 // See if we already expanded it.
3473 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3474 = ExpandedNodes.find(Op);
3475 if (I != ExpandedNodes.end()) {
3476 Lo = I->second.first;
3477 Hi = I->second.second;
3478 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003479 }
3480
Chris Lattner3e928bb2005-01-07 07:47:09 +00003481 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003482 case ISD::CopyFromReg:
3483 assert(0 && "CopyFromReg must be legal!");
3484 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003485 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3486 assert(0 && "Do not know how to expand this operator!");
3487 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003488 case ISD::UNDEF:
3489 Lo = DAG.getNode(ISD::UNDEF, NVT);
3490 Hi = DAG.getNode(ISD::UNDEF, NVT);
3491 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003492 case ISD::Constant: {
3493 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3494 Lo = DAG.getConstant(Cst, NVT);
3495 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3496 break;
3497 }
Nate Begemancc827e62005-12-07 19:48:11 +00003498 case ISD::ConstantVec: {
3499 unsigned NumElements = Node->getNumOperands();
3500 // If we only have two elements left in the constant vector, just break it
3501 // apart into the two scalar constants it contains. Otherwise, bisect the
3502 // ConstantVec, and return each half as a new ConstantVec.
3503 // FIXME: this is hard coded as big endian, it may have to change to support
3504 // SSE and Alpha MVI
3505 if (NumElements == 2) {
3506 Hi = Node->getOperand(0);
3507 Lo = Node->getOperand(1);
3508 } else {
3509 NumElements /= 2;
3510 std::vector<SDOperand> LoOps, HiOps;
3511 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3512 HiOps.push_back(Node->getOperand(I));
3513 LoOps.push_back(Node->getOperand(I+NumElements));
3514 }
3515 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3516 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3517 }
3518 break;
3519 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003520
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003521 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003522 // Return the operands.
3523 Lo = Node->getOperand(0);
3524 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003525 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003526
3527 case ISD::SIGN_EXTEND_INREG:
3528 ExpandOp(Node->getOperand(0), Lo, Hi);
3529 // Sign extend the lo-part.
3530 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3531 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3532 TLI.getShiftAmountTy()));
3533 // sext_inreg the low part if needed.
3534 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3535 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003536
Nate Begemand88fc032006-01-14 03:14:10 +00003537 case ISD::BSWAP: {
3538 ExpandOp(Node->getOperand(0), Lo, Hi);
3539 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3540 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3541 Lo = TempLo;
3542 break;
3543 }
3544
Chris Lattneredb1add2005-05-11 04:51:16 +00003545 case ISD::CTPOP:
3546 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003547 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3548 DAG.getNode(ISD::CTPOP, NVT, Lo),
3549 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003550 Hi = DAG.getConstant(0, NVT);
3551 break;
3552
Chris Lattner39a8f332005-05-12 19:05:01 +00003553 case ISD::CTLZ: {
3554 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003555 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003556 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3557 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003558 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3559 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003560 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3561 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3562
3563 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3564 Hi = DAG.getConstant(0, NVT);
3565 break;
3566 }
3567
3568 case ISD::CTTZ: {
3569 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003570 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003571 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3572 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003573 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3574 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003575 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3576 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3577
3578 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3579 Hi = DAG.getConstant(0, NVT);
3580 break;
3581 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003582
Nate Begemanacc398c2006-01-25 18:21:52 +00003583 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003584 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3585 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003586 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3587 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3588
3589 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003590 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003591 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3592 if (!TLI.isLittleEndian())
3593 std::swap(Lo, Hi);
3594 break;
3595 }
3596
Chris Lattner3e928bb2005-01-07 07:47:09 +00003597 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003598 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3599 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003600 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003601
3602 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003603 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3605 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003606 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003607 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003608
3609 // Build a factor node to remember that this load is independent of the
3610 // other one.
3611 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3612 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003613
Chris Lattner3e928bb2005-01-07 07:47:09 +00003614 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003615 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003616 if (!TLI.isLittleEndian())
3617 std::swap(Lo, Hi);
3618 break;
3619 }
Nate Begemanab48be32005-11-22 18:16:00 +00003620 case ISD::VLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003621 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3622 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanab48be32005-11-22 18:16:00 +00003623 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3624 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3625
3626 // If we only have two elements, turn into a pair of scalar loads.
3627 // FIXME: handle case where a vector of two elements is fine, such as
3628 // 2 x double on SSE2.
3629 if (NumElements == 2) {
3630 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3631 // Increment the pointer to the other half.
3632 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3633 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3634 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003635 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003636 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3637 } else {
3638 NumElements /= 2; // Split the vector in half
3639 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3640 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3641 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3642 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003643 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003644 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3645 }
3646
3647 // Build a factor node to remember that this load is independent of the
3648 // other one.
3649 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3650 Hi.getValue(1));
3651
3652 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003653 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemanab48be32005-11-22 18:16:00 +00003654 if (!TLI.isLittleEndian())
3655 std::swap(Lo, Hi);
3656 break;
3657 }
3658 case ISD::VADD:
3659 case ISD::VSUB:
3660 case ISD::VMUL: {
3661 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3662 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3663 SDOperand LL, LH, RL, RH;
3664
3665 ExpandOp(Node->getOperand(0), LL, LH);
3666 ExpandOp(Node->getOperand(1), RL, RH);
3667
3668 // If we only have two elements, turn into a pair of scalar loads.
3669 // FIXME: handle case where a vector of two elements is fine, such as
3670 // 2 x double on SSE2.
3671 if (NumElements == 2) {
3672 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3673 Lo = DAG.getNode(Opc, EVT, LL, RL);
3674 Hi = DAG.getNode(Opc, EVT, LH, RH);
3675 } else {
3676 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3677 LL.getOperand(3));
3678 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3679 LH.getOperand(3));
3680 }
3681 break;
3682 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003683 case ISD::AND:
3684 case ISD::OR:
3685 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3686 SDOperand LL, LH, RL, RH;
3687 ExpandOp(Node->getOperand(0), LL, LH);
3688 ExpandOp(Node->getOperand(1), RL, RH);
3689 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3690 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3691 break;
3692 }
3693 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003694 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003695 ExpandOp(Node->getOperand(1), LL, LH);
3696 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003697 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3698 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003699 break;
3700 }
Nate Begeman9373a812005-08-10 20:51:12 +00003701 case ISD::SELECT_CC: {
3702 SDOperand TL, TH, FL, FH;
3703 ExpandOp(Node->getOperand(2), TL, TH);
3704 ExpandOp(Node->getOperand(3), FL, FH);
3705 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3706 Node->getOperand(1), TL, FL, Node->getOperand(4));
3707 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3708 Node->getOperand(1), TH, FH, Node->getOperand(4));
3709 break;
3710 }
Nate Begeman144ff662005-10-13 17:15:37 +00003711 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003712 SDOperand Chain = Node->getOperand(0);
3713 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003714 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3715
3716 if (EVT == NVT)
3717 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3718 else
3719 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3720 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003721
3722 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003723 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003724
Nate Begeman144ff662005-10-13 17:15:37 +00003725 // The high part is obtained by SRA'ing all but one of the bits of the lo
3726 // part.
3727 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3728 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3729 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003730 break;
3731 }
3732 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003733 SDOperand Chain = Node->getOperand(0);
3734 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003735 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3736
3737 if (EVT == NVT)
3738 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3739 else
3740 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3741 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003742
3743 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003744 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003745
Nate Begeman144ff662005-10-13 17:15:37 +00003746 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003747 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003748 break;
3749 }
3750 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003751 SDOperand Chain = Node->getOperand(0);
3752 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003753 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3754
3755 if (EVT == NVT)
3756 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3757 else
3758 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3759 EVT);
3760
3761 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003762 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003763
3764 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003765 Hi = DAG.getNode(ISD::UNDEF, NVT);
3766 break;
3767 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003768 case ISD::ANY_EXTEND:
3769 // The low part is any extension of the input (which degenerates to a copy).
3770 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3771 // The high part is undefined.
3772 Hi = DAG.getNode(ISD::UNDEF, NVT);
3773 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003774 case ISD::SIGN_EXTEND: {
3775 // The low part is just a sign extension of the input (which degenerates to
3776 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003777 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003778
Chris Lattner3e928bb2005-01-07 07:47:09 +00003779 // The high part is obtained by SRA'ing all but one of the bits of the lo
3780 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003781 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003782 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3783 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003784 break;
3785 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003786 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003787 // The low part is just a zero extension of the input (which degenerates to
3788 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003789 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003790
Chris Lattner3e928bb2005-01-07 07:47:09 +00003791 // The high part is just a zero.
3792 Hi = DAG.getConstant(0, NVT);
3793 break;
Chris Lattner35481892005-12-23 00:16:34 +00003794
3795 case ISD::BIT_CONVERT: {
3796 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3797 Node->getOperand(0));
3798 ExpandOp(Tmp, Lo, Hi);
3799 break;
3800 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003801
Chris Lattner8137c9e2006-01-28 05:07:51 +00003802 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003803 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3804 TargetLowering::Custom &&
3805 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003806 Lo = TLI.LowerOperation(Op, DAG);
3807 assert(Lo.Val && "Node must be custom expanded!");
3808 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003809 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003810 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003811 break;
3812
Chris Lattner4e6c7462005-01-08 19:27:05 +00003813 // These operators cannot be expanded directly, emit them as calls to
3814 // library functions.
3815 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003816 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003817 SDOperand Op;
3818 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3819 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003820 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3821 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003822 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003823
Chris Lattnerf20d1832005-07-30 01:40:57 +00003824 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3825
Chris Lattner80a3e942005-07-29 00:33:32 +00003826 // Now that the custom expander is done, expand the result, which is still
3827 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003828 if (Op.Val) {
3829 ExpandOp(Op, Lo, Hi);
3830 break;
3831 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003832 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003833
Chris Lattner4e6c7462005-01-08 19:27:05 +00003834 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003835 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003836 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003837 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003838 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003839
Chris Lattner4e6c7462005-01-08 19:27:05 +00003840 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003841 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003842 SDOperand Op;
3843 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3844 case Expand: assert(0 && "cannot expand FP!");
3845 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3846 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3847 }
3848
3849 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3850
3851 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003852 if (Op.Val) {
3853 ExpandOp(Op, Lo, Hi);
3854 break;
3855 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003856 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003857
Chris Lattner4e6c7462005-01-08 19:27:05 +00003858 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003859 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003860 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003861 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003862 break;
3863
Evan Cheng05a2d562006-01-09 18:31:59 +00003864 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003865 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003866 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003867 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003868 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003869 Op = TLI.LowerOperation(Op, DAG);
3870 if (Op.Val) {
3871 // Now that the custom expander is done, expand the result, which is
3872 // still VT.
3873 ExpandOp(Op, Lo, Hi);
3874 break;
3875 }
3876 }
3877
Chris Lattnere34b3962005-01-19 04:19:40 +00003878 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003879 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003880 break;
Chris Lattner47599822005-04-02 03:38:53 +00003881
3882 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003883 TargetLowering::LegalizeAction Action =
3884 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3885 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3886 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003887 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003888 break;
3889 }
3890
Chris Lattnere34b3962005-01-19 04:19:40 +00003891 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003892 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003893 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003894 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003895
Evan Cheng05a2d562006-01-09 18:31:59 +00003896 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003897 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003898 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003899 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003900 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003901 Op = TLI.LowerOperation(Op, DAG);
3902 if (Op.Val) {
3903 // Now that the custom expander is done, expand the result, which is
3904 // still VT.
3905 ExpandOp(Op, Lo, Hi);
3906 break;
3907 }
3908 }
3909
Chris Lattnere34b3962005-01-19 04:19:40 +00003910 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003911 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003912 break;
Chris Lattner47599822005-04-02 03:38:53 +00003913
3914 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003915 TargetLowering::LegalizeAction Action =
3916 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3917 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3918 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003919 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003920 break;
3921 }
3922
Chris Lattnere34b3962005-01-19 04:19:40 +00003923 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003924 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003925 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003926 }
3927
3928 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003929 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003930 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003931 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003932 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003933 Op = TLI.LowerOperation(Op, DAG);
3934 if (Op.Val) {
3935 // Now that the custom expander is done, expand the result, which is
3936 // still VT.
3937 ExpandOp(Op, Lo, Hi);
3938 break;
3939 }
3940 }
3941
Chris Lattnere34b3962005-01-19 04:19:40 +00003942 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003943 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003944 break;
Chris Lattner47599822005-04-02 03:38:53 +00003945
3946 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003947 TargetLowering::LegalizeAction Action =
3948 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3949 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3950 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003951 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003952 break;
3953 }
3954
Chris Lattnere34b3962005-01-19 04:19:40 +00003955 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003956 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003957 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003958 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003959
Misha Brukmanedf128a2005-04-21 22:36:52 +00003960 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003961 case ISD::SUB: {
3962 // If the target wants to custom expand this, let them.
3963 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3964 TargetLowering::Custom) {
3965 Op = TLI.LowerOperation(Op, DAG);
3966 if (Op.Val) {
3967 ExpandOp(Op, Lo, Hi);
3968 break;
3969 }
3970 }
3971
3972 // Expand the subcomponents.
3973 SDOperand LHSL, LHSH, RHSL, RHSH;
3974 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3975 ExpandOp(Node->getOperand(1), RHSL, RHSH);
3976
3977 std::vector<SDOperand> Ops;
3978 Ops.push_back(LHSL);
3979 Ops.push_back(LHSH);
3980 Ops.push_back(RHSL);
3981 Ops.push_back(RHSH);
3982 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3983 unsigned Opc =
3984 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
3985 Lo = DAG.getNode(Opc, VTs, Ops);
3986 Hi = Lo.getValue(1);
Chris Lattner84f67882005-01-20 18:52:28 +00003987 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00003988 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003989 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003990 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003991 SDOperand LL, LH, RL, RH;
3992 ExpandOp(Node->getOperand(0), LL, LH);
3993 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003994 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3995 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3996 // extended the sign bit of the low half through the upper half, and if so
3997 // emit a MULHS instead of the alternate sequence that is valid for any
3998 // i64 x i64 multiply.
3999 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4000 // is RH an extension of the sign bit of RL?
4001 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4002 RH.getOperand(1).getOpcode() == ISD::Constant &&
4003 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4004 // is LH an extension of the sign bit of LL?
4005 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4006 LH.getOperand(1).getOpcode() == ISD::Constant &&
4007 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4008 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4009 } else {
4010 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4011 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4012 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4013 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4014 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4015 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004016 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4017 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004018 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004019 }
4020 break;
4021 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004022 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4023 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4024 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4025 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004026 }
4027
Chris Lattner83397362005-12-21 18:02:52 +00004028 // Make sure the resultant values have been legalized themselves, unless this
4029 // is a type that requires multi-step expansion.
4030 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4031 Lo = LegalizeOp(Lo);
4032 Hi = LegalizeOp(Hi);
4033 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004034
4035 // Remember in a map if the values will be reused later.
4036 bool isNew =
4037 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4038 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004039}
4040
4041
4042// SelectionDAG::Legalize - This is the entry point for the file.
4043//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004044void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004045 /// run - This is the main entry point to this class.
4046 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004047 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004048}
4049