blob: 4c91f3e4bfe6d9476d3071c856da9462a8c475d7 [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!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +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 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
159 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
160 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
161 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
162 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000163 }
164}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000165
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000171}
172
Chris Lattner32fca002005-10-06 01:20:27 +0000173/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
174/// not been visited yet and if all of its operands have already been visited.
175static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
176 std::map<SDNode*, unsigned> &Visited) {
177 if (++Visited[N] != N->getNumOperands())
178 return; // Haven't visited all operands yet
179
180 Order.push_back(N);
181
182 if (N->hasOneUse()) { // Tail recurse in common case.
183 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
184 return;
185 }
186
187 // Now that we have N in, add anything that uses it if all of their operands
188 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000189 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
190 ComputeTopDownOrdering(*UI, Order, Visited);
191}
192
Chris Lattner1618beb2005-07-29 00:11:56 +0000193
Chris Lattner3e928bb2005-01-07 07:47:09 +0000194void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000195 LastCALLSEQ_END = DAG.getEntryNode();
196 IsLegalizingCall = false;
197
Chris Lattnerab510a72005-10-02 17:49:46 +0000198 // The legalize process is inherently a bottom-up recursive process (users
199 // legalize their uses before themselves). Given infinite stack space, we
200 // could just start legalizing on the root and traverse the whole graph. In
201 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000202 // blocks. To avoid this problem, compute an ordering of the nodes where each
203 // node is only legalized after all of its operands are legalized.
204 std::map<SDNode*, unsigned> Visited;
205 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000206
Chris Lattner32fca002005-10-06 01:20:27 +0000207 // Compute ordering from all of the leaves in the graphs, those (like the
208 // entry node) that have no operands.
209 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
210 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000211 if (I->getNumOperands() == 0) {
212 Visited[I] = 0 - 1U;
213 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000214 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000215 }
216
Chris Lattnerde202b32005-11-09 23:47:37 +0000217 assert(Order.size() == Visited.size() &&
218 Order.size() ==
219 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000220 "Error: DAG is cyclic!");
221 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000222
Chris Lattner32fca002005-10-06 01:20:27 +0000223 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
224 SDNode *N = Order[i];
225 switch (getTypeAction(N->getValueType(0))) {
226 default: assert(0 && "Bad type action!");
227 case Legal:
228 LegalizeOp(SDOperand(N, 0));
229 break;
230 case Promote:
231 PromoteOp(SDOperand(N, 0));
232 break;
233 case Expand: {
234 SDOperand X, Y;
235 ExpandOp(SDOperand(N, 0), X, Y);
236 break;
237 }
238 }
239 }
240
241 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000243 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
244 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000245
246 ExpandedNodes.clear();
247 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000248 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000249
250 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000251 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000252}
253
Chris Lattner6831a812006-02-13 09:18:02 +0000254
255/// FindCallEndFromCallStart - Given a chained node that is part of a call
256/// sequence, find the CALLSEQ_END node that terminates the call sequence.
257static SDNode *FindCallEndFromCallStart(SDNode *Node) {
258 if (Node->getOpcode() == ISD::CALLSEQ_END)
259 return Node;
260 if (Node->use_empty())
261 return 0; // No CallSeqEnd
262
263 // The chain is usually at the end.
264 SDOperand TheChain(Node, Node->getNumValues()-1);
265 if (TheChain.getValueType() != MVT::Other) {
266 // Sometimes it's at the beginning.
267 TheChain = SDOperand(Node, 0);
268 if (TheChain.getValueType() != MVT::Other) {
269 // Otherwise, hunt for it.
270 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
271 if (Node->getValueType(i) == MVT::Other) {
272 TheChain = SDOperand(Node, i);
273 break;
274 }
275
276 // Otherwise, we walked into a node without a chain.
277 if (TheChain.getValueType() != MVT::Other)
278 return 0;
279 }
280 }
281
282 for (SDNode::use_iterator UI = Node->use_begin(),
283 E = Node->use_end(); UI != E; ++UI) {
284
285 // Make sure to only follow users of our token chain.
286 SDNode *User = *UI;
287 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
288 if (User->getOperand(i) == TheChain)
289 if (SDNode *Result = FindCallEndFromCallStart(User))
290 return Result;
291 }
292 return 0;
293}
294
295/// FindCallStartFromCallEnd - Given a chained node that is part of a call
296/// sequence, find the CALLSEQ_START node that initiates the call sequence.
297static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
298 assert(Node && "Didn't find callseq_start for a call??");
299 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
300
301 assert(Node->getOperand(0).getValueType() == MVT::Other &&
302 "Node doesn't have a token chain argument!");
303 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
304}
305
306/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
307/// see if any uses can reach Dest. If no dest operands can get to dest,
308/// legalize them, legalize ourself, and return false, otherwise, return true.
309bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
310 SDNode *Dest) {
311 if (N == Dest) return true; // N certainly leads to Dest :)
312
313 // If the first result of this node has been already legalized, then it cannot
314 // reach N.
315 switch (getTypeAction(N->getValueType(0))) {
316 case Legal:
317 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
318 break;
319 case Promote:
320 if (PromotedNodes.count(SDOperand(N, 0))) return false;
321 break;
322 case Expand:
323 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
324 break;
325 }
326
327 // Okay, this node has not already been legalized. Check and legalize all
328 // operands. If none lead to Dest, then we can legalize this node.
329 bool OperandsLeadToDest = false;
330 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
331 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
332 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
333
334 if (OperandsLeadToDest) return true;
335
336 // Okay, this node looks safe, legalize it and return false.
337 switch (getTypeAction(N->getValueType(0))) {
338 case Legal:
339 LegalizeOp(SDOperand(N, 0));
340 break;
341 case Promote:
342 PromoteOp(SDOperand(N, 0));
343 break;
344 case Expand: {
345 SDOperand X, Y;
346 ExpandOp(SDOperand(N, 0), X, Y);
347 break;
348 }
349 }
350 return false;
351}
352
353
354
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000356 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000357 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000358 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000359
Chris Lattner3e928bb2005-01-07 07:47:09 +0000360 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000361 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000362 if (Node->getNumValues() > 1) {
363 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
364 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000365 case Legal: break; // Nothing to do.
366 case Expand: {
367 SDOperand T1, T2;
368 ExpandOp(Op.getValue(i), T1, T2);
369 assert(LegalizedNodes.count(Op) &&
370 "Expansion didn't add legal operands!");
371 return LegalizedNodes[Op];
372 }
373 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000374 PromoteOp(Op.getValue(i));
375 assert(LegalizedNodes.count(Op) &&
Chris Lattner456a93a2006-01-28 07:39:30 +0000376 "Promotion didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000377 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000378 }
379 }
380
Chris Lattner45982da2005-05-12 16:53:42 +0000381 // Note that LegalizeOp may be reentered even from single-use nodes, which
382 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000383 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
384 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000385
Nate Begeman9373a812005-08-10 20:51:12 +0000386 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000387 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000388 bool isCustom = false;
389
Chris Lattner3e928bb2005-01-07 07:47:09 +0000390 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000391 case ISD::FrameIndex:
392 case ISD::EntryToken:
393 case ISD::Register:
394 case ISD::BasicBlock:
395 case ISD::TargetFrameIndex:
396 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000397 case ISD::TargetConstantFP:
398 case ISD::TargetConstantVec:
Chris Lattner948c1b12006-01-28 08:31:04 +0000399 case ISD::TargetConstantPool:
400 case ISD::TargetGlobalAddress:
401 case ISD::TargetExternalSymbol:
402 case ISD::VALUETYPE:
403 case ISD::SRCVALUE:
404 case ISD::STRING:
405 case ISD::CONDCODE:
406 // Primitives must all be legal.
407 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
408 "This must be legal!");
409 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000410 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000411 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
412 // If this is a target node, legalize it by legalizing the operands then
413 // passing it through.
414 std::vector<SDOperand> Ops;
415 bool Changed = false;
416 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
417 Ops.push_back(LegalizeOp(Node->getOperand(i)));
418 Changed = Changed || Node->getOperand(i) != Ops.back();
419 }
420 if (Changed)
421 if (Node->getNumValues() == 1)
422 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
423 else {
424 std::vector<MVT::ValueType> VTs(Node->value_begin(),
425 Node->value_end());
426 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
427 }
428
429 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
430 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
431 return Result.getValue(Op.ResNo);
432 }
433 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000434 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
435 assert(0 && "Do not know how to legalize this operator!");
436 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000437 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000438 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000439 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000440 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
441 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000442 case TargetLowering::Custom:
443 Tmp1 = TLI.LowerOperation(Op, DAG);
444 if (Tmp1.Val) Result = Tmp1;
445 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000446 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000447 break;
448 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000449 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000450 case ISD::AssertSext:
451 case ISD::AssertZext:
452 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000454 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000455 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000456 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000457 Result = Node->getOperand(Op.ResNo);
458 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000459 case ISD::CopyFromReg:
460 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000461 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000462 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000464 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000465 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000466 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000467 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
469 } else {
470 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
471 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000472 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
473 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000474 // Since CopyFromReg produces two values, make sure to remember that we
475 // legalized both of them.
476 AddLegalizedOperand(Op.getValue(0), Result);
477 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
478 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000479 case ISD::UNDEF: {
480 MVT::ValueType VT = Op.getValueType();
481 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000482 default: assert(0 && "This action is not supported yet!");
483 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000484 if (MVT::isInteger(VT))
485 Result = DAG.getConstant(0, VT);
486 else if (MVT::isFloatingPoint(VT))
487 Result = DAG.getConstantFP(0, VT);
488 else
489 assert(0 && "Unknown value type!");
490 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000491 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000492 break;
493 }
494 break;
495 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000496
497 case ISD::LOCATION:
498 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
499 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
500
501 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
502 case TargetLowering::Promote:
503 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000504 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000505 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000506 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
507 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
508
509 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
510 const std::string &FName =
511 cast<StringSDNode>(Node->getOperand(3))->getValue();
512 const std::string &DirName =
513 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000514 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000515
Jim Laskeye81aecb2005-12-21 20:51:37 +0000516 std::vector<SDOperand> Ops;
517 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000518 SDOperand LineOp = Node->getOperand(1);
519 SDOperand ColOp = Node->getOperand(2);
520
521 if (useDEBUG_LOC) {
522 Ops.push_back(LineOp); // line #
523 Ops.push_back(ColOp); // col #
524 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
525 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
526 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000527 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
528 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000529 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
530 Ops.push_back(DAG.getConstant(ID, MVT::i32));
531 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
532 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000533 } else {
534 Result = Tmp1; // chain
535 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000536 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000537 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000538 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000539 if (Tmp1 != Node->getOperand(0) ||
540 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000541 std::vector<SDOperand> Ops;
542 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000543 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
544 Ops.push_back(Node->getOperand(1)); // line # must be legal.
545 Ops.push_back(Node->getOperand(2)); // col # must be legal.
546 } else {
547 // Otherwise promote them.
548 Ops.push_back(PromoteOp(Node->getOperand(1)));
549 Ops.push_back(PromoteOp(Node->getOperand(2)));
550 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000551 Ops.push_back(Node->getOperand(3)); // filename must be legal.
552 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000553 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000554 }
555 break;
556 }
557 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000558
559 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000560 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000561 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000562 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000563 case TargetLowering::Legal:
564 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
565 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
566 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
567 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000569 break;
570 }
571 break;
572
573 case ISD::DEBUG_LABEL:
574 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
575 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000576 default: assert(0 && "This action is not supported yet!");
577 case TargetLowering::Legal:
578 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
579 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000580 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000581 break;
582 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000583 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000584
Chris Lattner3e928bb2005-01-07 07:47:09 +0000585 case ISD::Constant:
586 // We know we don't need to expand constants here, constants only have one
587 // value and we check that it is fine above.
588
589 // FIXME: Maybe we should handle things like targets that don't support full
590 // 32-bit immediates?
591 break;
592 case ISD::ConstantFP: {
593 // Spill FP immediates to the constant pool if the target cannot directly
594 // codegen them. Targets often have some immediate values that can be
595 // efficiently generated into an FP register without a load. We explicitly
596 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000597 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
598
599 // Check to see if this FP immediate is already legal.
600 bool isLegal = false;
601 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
602 E = TLI.legal_fpimm_end(); I != E; ++I)
603 if (CFP->isExactlyValue(*I)) {
604 isLegal = true;
605 break;
606 }
607
Chris Lattner3181a772006-01-29 06:26:56 +0000608 // If this is a legal constant, turn it into a TargetConstantFP node.
609 if (isLegal) {
610 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
611 break;
612 }
613
614 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
615 default: assert(0 && "This action is not supported yet!");
616 case TargetLowering::Custom:
617 Tmp3 = TLI.LowerOperation(Result, DAG);
618 if (Tmp3.Val) {
619 Result = Tmp3;
620 break;
621 }
622 // FALLTHROUGH
623 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000624 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000625 bool Extend = false;
626
Chris Lattner456a93a2006-01-28 07:39:30 +0000627 // If a FP immediate is precise when represented as a float and if the
628 // target can do an extending load from float to double, we put it into
629 // the constant pool as a float, even if it's is statically typed as a
630 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000631 MVT::ValueType VT = CFP->getValueType(0);
632 bool isDouble = VT == MVT::f64;
633 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
634 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000635 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
636 // Only do this if the target has a native EXTLOAD instruction from
637 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000638 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000639 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
640 VT = MVT::f32;
641 Extend = true;
642 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000643
Chris Lattner456a93a2006-01-28 07:39:30 +0000644 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000645 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000646 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
647 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000648 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000649 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
650 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000651 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000652 }
653 break;
654 }
Chris Lattner8ca05e02006-01-29 06:34:16 +0000655 case ISD::ConstantVec:
656 switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) {
657 default: assert(0 && "This action is not supported yet!");
658 case TargetLowering::Custom:
659 Tmp3 = TLI.LowerOperation(Result, DAG);
660 if (Tmp3.Val) {
661 Result = Tmp3;
662 break;
663 }
664 // FALLTHROUGH
665 case TargetLowering::Expand:
666 // We assume that vector constants are not legal, and will be immediately
667 // spilled to the constant pool.
668 //
669 // Create a ConstantPacked, and put it in the constant pool.
670 MVT::ValueType VT = Node->getValueType(0);
671 const Type *OpNTy =
672 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
673 std::vector<Constant*> CV;
674 if (MVT::isFloatingPoint(VT)) {
675 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
676 double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue();
677 CV.push_back(ConstantFP::get(OpNTy, V));
678 }
679 } else {
680 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
681 uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
682 CV.push_back(ConstantUInt::get(OpNTy, V));
683 }
684 }
685 Constant *CP = ConstantPacked::get(CV);
686 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
687 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
688 DAG.getSrcValue(NULL));
689 break;
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000690 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000691 break;
Chris Lattner040c11c2005-11-09 18:48:57 +0000692 case ISD::TokenFactor:
693 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000694 Tmp1 = LegalizeOp(Node->getOperand(0));
695 Tmp2 = LegalizeOp(Node->getOperand(1));
696 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
697 } else if (Node->getNumOperands() == 3) {
698 Tmp1 = LegalizeOp(Node->getOperand(0));
699 Tmp2 = LegalizeOp(Node->getOperand(1));
700 Tmp3 = LegalizeOp(Node->getOperand(2));
701 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000702 } else {
703 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000704 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000705 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
706 Ops.push_back(LegalizeOp(Node->getOperand(i)));
707 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000708 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000709 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000710
Chris Lattner6831a812006-02-13 09:18:02 +0000711 case ISD::CALLSEQ_START: {
712 SDNode *CallEnd = FindCallEndFromCallStart(Node);
713
714 // Recursively Legalize all of the inputs of the call end that do not lead
715 // to this call start. This ensures that any libcalls that need be inserted
716 // are inserted *before* the CALLSEQ_START.
717 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
718 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
719
720 // Now that we legalized all of the inputs (which may have inserted
721 // libcalls) create the new CALLSEQ_START node.
722 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
723
724 // Merge in the last call, to ensure that this call start after the last
725 // call ended.
726 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
727 Tmp1 = LegalizeOp(Tmp1);
728
729 // Do not try to legalize the target-specific arguments (#1+).
730 if (Tmp1 != Node->getOperand(0)) {
731 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
732 Ops[0] = Tmp1;
733 Result = DAG.UpdateNodeOperands(Result, Ops);
734 }
735
736 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000737 AddLegalizedOperand(Op.getValue(0), Result);
738 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
739 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
740
Chris Lattner6831a812006-02-13 09:18:02 +0000741 // Now that the callseq_start and all of the non-call nodes above this call
742 // sequence have been legalized, legalize the call itself. During this
743 // process, no libcalls can/will be inserted, guaranteeing that no calls
744 // can overlap.
745 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
746 SDOperand InCallSEQ = LastCALLSEQ_END;
747 // Note that we are selecting this call!
748 LastCALLSEQ_END = SDOperand(CallEnd, 0);
749 IsLegalizingCall = true;
750
751 // Legalize the call, starting from the CALLSEQ_END.
752 LegalizeOp(LastCALLSEQ_END);
753 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
754 return Result;
755 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000756 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000757 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
758 // will cause this node to be legalized as well as handling libcalls right.
759 if (LastCALLSEQ_END.Val != Node) {
760 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
761 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
762 assert(I != LegalizedNodes.end() &&
763 "Legalizing the call start should have legalized this node!");
764 return I->second;
765 }
766
767 // Otherwise, the call start has been legalized and everything is going
768 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000770 // Do not try to legalize the target-specific arguments (#1+), except for
771 // an optional flag input.
772 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
773 if (Tmp1 != Node->getOperand(0)) {
774 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
775 Ops[0] = Tmp1;
776 Result = DAG.UpdateNodeOperands(Result, Ops);
777 }
778 } else {
779 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
780 if (Tmp1 != Node->getOperand(0) ||
781 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
782 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
783 Ops[0] = Tmp1;
784 Ops.back() = Tmp2;
785 Result = DAG.UpdateNodeOperands(Result, Ops);
786 }
Chris Lattner6a542892006-01-24 05:48:21 +0000787 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000788 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000789 // This finishes up call legalization.
790 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000791
792 // If the CALLSEQ_END node has a flag, remember that we legalized it.
793 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
794 if (Node->getNumValues() == 2)
795 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
796 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000797 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000798 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
799 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
800 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000801 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000802
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000803 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000804 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000805 switch (TLI.getOperationAction(Node->getOpcode(),
806 Node->getValueType(0))) {
807 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000808 case TargetLowering::Expand: {
809 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
810 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
811 " not tell us which reg is the stack pointer!");
812 SDOperand Chain = Tmp1.getOperand(0);
813 SDOperand Size = Tmp2.getOperand(1);
814 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
815 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
816 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000817 Tmp1 = LegalizeOp(Tmp1);
818 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000819 break;
820 }
821 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000822 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
823 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000824 Tmp1 = LegalizeOp(Tmp3);
825 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000826 }
Chris Lattner903d2782006-01-15 08:54:32 +0000827 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000828 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000829 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000830 }
Chris Lattner903d2782006-01-15 08:54:32 +0000831 // Since this op produce two values, make sure to remember that we
832 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000833 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
834 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000835 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000836 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000837 case ISD::INLINEASM:
838 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
839 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000840 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000841 Tmp2 = Tmp3 = SDOperand(0, 0);
842 else
843 Tmp3 = LegalizeOp(Tmp2);
844
845 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
846 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
847 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000848 if (Tmp3.Val) Ops.back() = Tmp3;
849 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000850 }
851
852 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000853 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000854 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
855 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000856 case ISD::BR:
857 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000858 // Ensure that libcalls are emitted before a branch.
859 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
860 Tmp1 = LegalizeOp(Tmp1);
861 LastCALLSEQ_END = DAG.getEntryNode();
862
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000863 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +0000864 break;
865
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000866 case ISD::BRCOND:
867 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000868 // Ensure that libcalls are emitted before a return.
869 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
870 Tmp1 = LegalizeOp(Tmp1);
871 LastCALLSEQ_END = DAG.getEntryNode();
872
Chris Lattner47e92232005-01-18 19:27:06 +0000873 switch (getTypeAction(Node->getOperand(1).getValueType())) {
874 case Expand: assert(0 && "It's impossible to expand bools");
875 case Legal:
876 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
877 break;
878 case Promote:
879 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
880 break;
881 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000882
883 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000885
886 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
887 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000888 case TargetLowering::Legal: break;
889 case TargetLowering::Custom:
890 Tmp1 = TLI.LowerOperation(Result, DAG);
891 if (Tmp1.Val) Result = Tmp1;
892 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000893 case TargetLowering::Expand:
894 // Expand brcond's setcc into its constituent parts and create a BR_CC
895 // Node.
896 if (Tmp2.getOpcode() == ISD::SETCC) {
897 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
898 Tmp2.getOperand(0), Tmp2.getOperand(1),
899 Node->getOperand(2));
900 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000901 // Make sure the condition is either zero or one. It may have been
902 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +0000903 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
904 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
905 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +0000906
Nate Begeman7cbd5252005-08-16 19:49:35 +0000907 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
908 DAG.getCondCode(ISD::SETNE), Tmp2,
909 DAG.getConstant(0, Tmp2.getValueType()),
910 Node->getOperand(2));
911 }
912 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000913 }
914 break;
915 case ISD::BR_CC:
916 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000917 // Ensure that libcalls are emitted before a branch.
918 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
919 Tmp1 = LegalizeOp(Tmp1);
920 LastCALLSEQ_END = DAG.getEntryNode();
921
Nate Begeman750ac1b2006-02-01 07:19:44 +0000922 Tmp2 = Node->getOperand(2); // LHS
923 Tmp3 = Node->getOperand(3); // RHS
924 Tmp4 = Node->getOperand(1); // CC
925
926 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
927
928 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
929 // the LHS is a legal SETCC itself. In this case, we need to compare
930 // the result against zero to select between true and false values.
931 if (Tmp3.Val == 0) {
932 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
933 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +0000934 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000935
936 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
937 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +0000938
Chris Lattner181b7a32005-12-17 23:46:46 +0000939 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
940 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000941 case TargetLowering::Legal: break;
942 case TargetLowering::Custom:
943 Tmp4 = TLI.LowerOperation(Result, DAG);
944 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +0000945 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000946 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000947 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000948 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000949 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
950 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000951
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000952 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000953 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
954 Tmp2 = Result.getValue(0);
955 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000956
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000957 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
958 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000959 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +0000960 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000961 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +0000962 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000963 Tmp2 = LegalizeOp(Tmp1);
964 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000965 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000966 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000967 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000968 // Since loads produce two values, make sure to remember that we
969 // legalized both of them.
970 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
971 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
972 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +0000973 }
Chris Lattner0f69b292005-01-15 06:18:18 +0000974 case ISD::EXTLOAD:
975 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000976 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000977 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
978 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000979
Chris Lattner5f056bf2005-07-10 01:55:33 +0000980 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000981 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000982 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000983 case TargetLowering::Promote:
984 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000985 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
986 DAG.getValueType(MVT::i8));
987 Tmp1 = Result.getValue(0);
988 Tmp2 = Result.getValue(1);
989 break;
Chris Lattner456a93a2006-01-28 07:39:30 +0000990 case TargetLowering::Custom:
991 isCustom = true;
992 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +0000993 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000994 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
995 Node->getOperand(3));
996 Tmp1 = Result.getValue(0);
997 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +0000998
999 if (isCustom) {
1000 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1001 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001002 Tmp1 = LegalizeOp(Tmp3);
1003 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001004 }
1005 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001006 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001007 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001008 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001009 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1010 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001011 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001012 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1013 Tmp2 = LegalizeOp(Load.getValue(1));
1014 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001015 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001016 assert(Node->getOpcode() != ISD::EXTLOAD &&
1017 "EXTLOAD should always be supported!");
1018 // Turn the unsupported load into an EXTLOAD followed by an explicit
1019 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001020 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1021 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001022 SDOperand ValRes;
1023 if (Node->getOpcode() == ISD::SEXTLOAD)
1024 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001025 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001026 else
1027 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001028 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1029 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1030 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001031 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001032 // Since loads produce two values, make sure to remember that we legalized
1033 // both of them.
1034 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1035 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1036 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001037 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001038 case ISD::EXTRACT_ELEMENT: {
1039 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1040 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001041 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001042 case Legal:
1043 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1044 // 1 -> Hi
1045 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1046 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1047 TLI.getShiftAmountTy()));
1048 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1049 } else {
1050 // 0 -> Lo
1051 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1052 Node->getOperand(0));
1053 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001054 break;
1055 case Expand:
1056 // Get both the low and high parts.
1057 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1058 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1059 Result = Tmp2; // 1 -> Hi
1060 else
1061 Result = Tmp1; // 0 -> Lo
1062 break;
1063 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001064 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001065 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001066
1067 case ISD::CopyToReg:
1068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001069
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001070 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001071 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001072 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001073 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001074 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001076 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001077 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001078 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001079 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001080 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1081 Tmp3);
1082 } else {
1083 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001084 }
1085
1086 // Since this produces two values, make sure to remember that we legalized
1087 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001088 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001089 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001090 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001091 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001092 break;
1093
1094 case ISD::RET:
1095 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001096
1097 // Ensure that libcalls are emitted before a return.
1098 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1099 Tmp1 = LegalizeOp(Tmp1);
1100 LastCALLSEQ_END = DAG.getEntryNode();
1101
Chris Lattner3e928bb2005-01-07 07:47:09 +00001102 switch (Node->getNumOperands()) {
1103 case 2: // ret val
1104 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1105 case Legal:
1106 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001108 break;
1109 case Expand: {
1110 SDOperand Lo, Hi;
1111 ExpandOp(Node->getOperand(1), Lo, Hi);
1112 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001113 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001114 }
1115 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001116 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001117 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1118 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001119 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001120 }
1121 break;
1122 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001123 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001124 break;
1125 default: { // ret <values>
1126 std::vector<SDOperand> NewValues;
1127 NewValues.push_back(Tmp1);
1128 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1129 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1130 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001131 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001132 break;
1133 case Expand: {
1134 SDOperand Lo, Hi;
1135 ExpandOp(Node->getOperand(i), Lo, Hi);
1136 NewValues.push_back(Lo);
1137 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001138 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001139 }
1140 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001141 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001142 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001143
1144 if (NewValues.size() == Node->getNumOperands())
1145 Result = DAG.UpdateNodeOperands(Result, NewValues);
1146 else
1147 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001148 break;
1149 }
1150 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001151
Chris Lattner6862dbc2006-01-29 21:02:23 +00001152 if (Result.getOpcode() == ISD::RET) {
1153 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1154 default: assert(0 && "This action is not supported yet!");
1155 case TargetLowering::Legal: break;
1156 case TargetLowering::Custom:
1157 Tmp1 = TLI.LowerOperation(Result, DAG);
1158 if (Tmp1.Val) Result = Tmp1;
1159 break;
1160 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001161 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001162 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001163 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001164 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1165 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1166
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001167 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001168 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001169 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001170 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001171 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001172 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001173 } else {
1174 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001175 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001176 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001177 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1178 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001179 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001180 }
1181
Chris Lattner3e928bb2005-01-07 07:47:09 +00001182 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1183 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001184 Tmp3 = LegalizeOp(Node->getOperand(1));
1185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1186 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001187
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001188 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001189 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1190 default: assert(0 && "This action is not supported yet!");
1191 case TargetLowering::Legal: break;
1192 case TargetLowering::Custom:
1193 Tmp1 = TLI.LowerOperation(Result, DAG);
1194 if (Tmp1.Val) Result = Tmp1;
1195 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001196 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001197 break;
1198 }
1199 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001200 // Truncate the value and store the result.
1201 Tmp3 = PromoteOp(Node->getOperand(1));
1202 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001203 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001204 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001205 break;
1206
Chris Lattner3e928bb2005-01-07 07:47:09 +00001207 case Expand:
1208 SDOperand Lo, Hi;
1209 ExpandOp(Node->getOperand(1), Lo, Hi);
1210
1211 if (!TLI.isLittleEndian())
1212 std::swap(Lo, Hi);
1213
Chris Lattneredb1add2005-05-11 04:51:16 +00001214 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1215 Node->getOperand(3));
Nate Begemanab48be32005-11-22 18:16:00 +00001216 // If this is a vector type, then we have to calculate the increment as
1217 // the product of the element size in bytes, and the number of elements
1218 // in the high half of the vector.
Chris Lattner456a93a2006-01-28 07:39:30 +00001219 unsigned IncrementSize;
Nate Begemanab48be32005-11-22 18:16:00 +00001220 if (MVT::Vector == Hi.getValueType()) {
Evan Cheng860771d2006-03-01 01:09:54 +00001221 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(0))->getValue();
1222 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(1))->getVT();
Nate Begemanab48be32005-11-22 18:16:00 +00001223 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1224 } else {
1225 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1226 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001227 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1228 getIntPtrConstant(IncrementSize));
1229 assert(isTypeLegal(Tmp2.getValueType()) &&
1230 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001231 // FIXME: This sets the srcvalue of both halves to be the same, which is
1232 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001233 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1234 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001235 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1236 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001237 }
1238 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001239 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001240 case ISD::PCMARKER:
1241 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001243 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001244 case ISD::STACKSAVE:
1245 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001246 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1247 Tmp1 = Result.getValue(0);
1248 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001249
Chris Lattner140d53c2006-01-13 02:50:02 +00001250 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1251 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001252 case TargetLowering::Legal: break;
1253 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001254 Tmp3 = TLI.LowerOperation(Result, DAG);
1255 if (Tmp3.Val) {
1256 Tmp1 = LegalizeOp(Tmp3);
1257 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001258 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001259 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001260 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001261 // Expand to CopyFromReg if the target set
1262 // StackPointerRegisterToSaveRestore.
1263 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001264 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001265 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001266 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001267 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001268 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1269 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001270 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001271 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001272 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001273
1274 // Since stacksave produce two values, make sure to remember that we
1275 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001276 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1277 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1278 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001279
Chris Lattner140d53c2006-01-13 02:50:02 +00001280 case ISD::STACKRESTORE:
1281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1282 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001284
1285 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1286 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001287 case TargetLowering::Legal: break;
1288 case TargetLowering::Custom:
1289 Tmp1 = TLI.LowerOperation(Result, DAG);
1290 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001291 break;
1292 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001293 // Expand to CopyToReg if the target set
1294 // StackPointerRegisterToSaveRestore.
1295 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1296 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1297 } else {
1298 Result = Tmp1;
1299 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001300 break;
1301 }
1302 break;
1303
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001304 case ISD::READCYCLECOUNTER:
1305 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001306 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001307
1308 // Since rdcc produce two values, make sure to remember that we legalized
1309 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001310 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001311 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001312 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001313
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001314 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1316 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1317
Chris Lattner456a93a2006-01-28 07:39:30 +00001318 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1319 "Cannot handle illegal TRUNCSTORE yet!");
1320 Tmp2 = LegalizeOp(Node->getOperand(1));
1321
1322 // The only promote case we handle is TRUNCSTORE:i1 X into
1323 // -> TRUNCSTORE:i8 (and X, 1)
1324 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1325 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1326 TargetLowering::Promote) {
1327 // Promote the bool to a mask then store.
1328 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1329 DAG.getConstant(1, Tmp2.getValueType()));
1330 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1331 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001332
Chris Lattner456a93a2006-01-28 07:39:30 +00001333 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1334 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001335 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1336 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001337 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001338
Chris Lattner456a93a2006-01-28 07:39:30 +00001339 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1340 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1341 default: assert(0 && "This action is not supported yet!");
1342 case TargetLowering::Legal: break;
1343 case TargetLowering::Custom:
1344 Tmp1 = TLI.LowerOperation(Result, DAG);
1345 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001346 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001347 }
1348 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001349 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001350 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001351 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1352 case Expand: assert(0 && "It's impossible to expand bools");
1353 case Legal:
1354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1355 break;
1356 case Promote:
1357 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1358 break;
1359 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001360 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001361 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001362
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001364
Nate Begemanb942a3d2005-08-23 04:29:48 +00001365 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001366 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001367 case TargetLowering::Legal: break;
1368 case TargetLowering::Custom: {
1369 Tmp1 = TLI.LowerOperation(Result, DAG);
1370 if (Tmp1.Val) Result = Tmp1;
1371 break;
1372 }
Nate Begeman9373a812005-08-10 20:51:12 +00001373 case TargetLowering::Expand:
1374 if (Tmp1.getOpcode() == ISD::SETCC) {
1375 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1376 Tmp2, Tmp3,
1377 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1378 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001379 // Make sure the condition is either zero or one. It may have been
1380 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001381 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1382 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1383 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001384 Result = DAG.getSelectCC(Tmp1,
1385 DAG.getConstant(0, Tmp1.getValueType()),
1386 Tmp2, Tmp3, ISD::SETNE);
1387 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001388 break;
1389 case TargetLowering::Promote: {
1390 MVT::ValueType NVT =
1391 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1392 unsigned ExtOp, TruncOp;
1393 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001394 ExtOp = ISD::ANY_EXTEND;
1395 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001396 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001397 ExtOp = ISD::FP_EXTEND;
1398 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001399 }
1400 // Promote each of the values to the new type.
1401 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1402 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1403 // Perform the larger operation, then round down.
1404 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1405 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1406 break;
1407 }
1408 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001409 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001410 case ISD::SELECT_CC: {
1411 Tmp1 = Node->getOperand(0); // LHS
1412 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001413 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1414 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001415 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001416
Nate Begeman750ac1b2006-02-01 07:19:44 +00001417 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1418
1419 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1420 // the LHS is a legal SETCC itself. In this case, we need to compare
1421 // the result against zero to select between true and false values.
1422 if (Tmp2.Val == 0) {
1423 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1424 CC = DAG.getCondCode(ISD::SETNE);
1425 }
1426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1427
1428 // Everything is legal, see if we should expand this op or something.
1429 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1430 default: assert(0 && "This action is not supported yet!");
1431 case TargetLowering::Legal: break;
1432 case TargetLowering::Custom:
1433 Tmp1 = TLI.LowerOperation(Result, DAG);
1434 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001435 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001436 }
1437 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001438 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001439 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001440 Tmp1 = Node->getOperand(0);
1441 Tmp2 = Node->getOperand(1);
1442 Tmp3 = Node->getOperand(2);
1443 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1444
1445 // If we had to Expand the SetCC operands into a SELECT node, then it may
1446 // not always be possible to return a true LHS & RHS. In this case, just
1447 // return the value we legalized, returned in the LHS
1448 if (Tmp2.Val == 0) {
1449 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001450 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001451 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001452
Chris Lattner73e142f2006-01-30 22:43:50 +00001453 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001454 default: assert(0 && "Cannot handle this action for SETCC yet!");
1455 case TargetLowering::Custom:
1456 isCustom = true;
1457 // FALLTHROUGH.
1458 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001459 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001460 if (isCustom) {
1461 Tmp3 = TLI.LowerOperation(Result, DAG);
1462 if (Tmp3.Val) Result = Tmp3;
1463 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001464 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001465 case TargetLowering::Promote: {
1466 // First step, figure out the appropriate operation to use.
1467 // Allow SETCC to not be supported for all legal data types
1468 // Mostly this targets FP
1469 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1470 MVT::ValueType OldVT = NewInTy;
1471
1472 // Scan for the appropriate larger type to use.
1473 while (1) {
1474 NewInTy = (MVT::ValueType)(NewInTy+1);
1475
1476 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1477 "Fell off of the edge of the integer world");
1478 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1479 "Fell off of the edge of the floating point world");
1480
1481 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001482 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001483 break;
1484 }
1485 if (MVT::isInteger(NewInTy))
1486 assert(0 && "Cannot promote Legal Integer SETCC yet");
1487 else {
1488 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1489 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1490 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001491 Tmp1 = LegalizeOp(Tmp1);
1492 Tmp2 = LegalizeOp(Tmp2);
1493 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001494 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001495 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001496 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001497 case TargetLowering::Expand:
1498 // Expand a setcc node into a select_cc of the same condition, lhs, and
1499 // rhs that selects between const 1 (true) and const 0 (false).
1500 MVT::ValueType VT = Node->getValueType(0);
1501 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1502 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1503 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001504 break;
1505 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001506 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001507 case ISD::MEMSET:
1508 case ISD::MEMCPY:
1509 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001510 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001511 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1512
1513 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1514 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1515 case Expand: assert(0 && "Cannot expand a byte!");
1516 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001517 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001518 break;
1519 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001520 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001521 break;
1522 }
1523 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001524 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001525 }
Chris Lattner272455b2005-02-02 03:44:41 +00001526
1527 SDOperand Tmp4;
1528 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001529 case Expand: {
1530 // Length is too big, just take the lo-part of the length.
1531 SDOperand HiPart;
1532 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1533 break;
1534 }
Chris Lattnere5605212005-01-28 22:29:18 +00001535 case Legal:
1536 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001537 break;
1538 case Promote:
1539 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001540 break;
1541 }
1542
1543 SDOperand Tmp5;
1544 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1545 case Expand: assert(0 && "Cannot expand this yet!");
1546 case Legal:
1547 Tmp5 = LegalizeOp(Node->getOperand(4));
1548 break;
1549 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001550 Tmp5 = PromoteOp(Node->getOperand(4));
1551 break;
1552 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001553
1554 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1555 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001556 case TargetLowering::Custom:
1557 isCustom = true;
1558 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001559 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001560 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001561 if (isCustom) {
1562 Tmp1 = TLI.LowerOperation(Result, DAG);
1563 if (Tmp1.Val) Result = Tmp1;
1564 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001565 break;
1566 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001567 // Otherwise, the target does not support this operation. Lower the
1568 // operation to an explicit libcall as appropriate.
1569 MVT::ValueType IntPtr = TLI.getPointerTy();
1570 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1571 std::vector<std::pair<SDOperand, const Type*> > Args;
1572
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001573 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001574 if (Node->getOpcode() == ISD::MEMSET) {
1575 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001576 // Extend the (previously legalized) ubyte argument to be an int value
1577 // for the call.
1578 if (Tmp3.getValueType() > MVT::i32)
1579 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1580 else
1581 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001582 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1583 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1584
1585 FnName = "memset";
1586 } else if (Node->getOpcode() == ISD::MEMCPY ||
1587 Node->getOpcode() == ISD::MEMMOVE) {
1588 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1589 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1590 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1591 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1592 } else {
1593 assert(0 && "Unknown op!");
1594 }
Chris Lattner45982da2005-05-12 16:53:42 +00001595
Chris Lattnere1bd8222005-01-11 05:57:22 +00001596 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001597 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001598 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001599 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001600 break;
1601 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001602 }
1603 break;
1604 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001605
Chris Lattner5b359c62005-04-02 04:00:59 +00001606 case ISD::SHL_PARTS:
1607 case ISD::SRA_PARTS:
1608 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001609 std::vector<SDOperand> Ops;
1610 bool Changed = false;
1611 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1612 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1613 Changed |= Ops.back() != Node->getOperand(i);
1614 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001615 if (Changed)
1616 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001617
Evan Cheng05a2d562006-01-09 18:31:59 +00001618 switch (TLI.getOperationAction(Node->getOpcode(),
1619 Node->getValueType(0))) {
1620 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001621 case TargetLowering::Legal: break;
1622 case TargetLowering::Custom:
1623 Tmp1 = TLI.LowerOperation(Result, DAG);
1624 if (Tmp1.Val) {
1625 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001626 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001627 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001628 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1629 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001630 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001631 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001632 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001633 return RetVal;
1634 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001635 break;
1636 }
1637
Chris Lattner2c8086f2005-04-02 05:00:07 +00001638 // Since these produce multiple values, make sure to remember that we
1639 // legalized all of them.
1640 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1641 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1642 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001643 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001644
1645 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001646 case ISD::ADD:
1647 case ISD::SUB:
1648 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001649 case ISD::MULHS:
1650 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001651 case ISD::UDIV:
1652 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001653 case ISD::AND:
1654 case ISD::OR:
1655 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001656 case ISD::SHL:
1657 case ISD::SRL:
1658 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001659 case ISD::FADD:
1660 case ISD::FSUB:
1661 case ISD::FMUL:
1662 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001663 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001664 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1665 case Expand: assert(0 && "Not possible");
1666 case Legal:
1667 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1668 break;
1669 case Promote:
1670 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1671 break;
1672 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001673
1674 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001675
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001676 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001677 default: assert(0 && "Operation not supported");
1678 case TargetLowering::Legal: break;
1679 case TargetLowering::Custom:
1680 Tmp1 = TLI.LowerOperation(Result, DAG);
1681 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001682 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001683 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001684 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001685
1686 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1687 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1688 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1689 case Expand: assert(0 && "Not possible");
1690 case Legal:
1691 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1692 break;
1693 case Promote:
1694 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1695 break;
1696 }
1697
1698 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1699
1700 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1701 default: assert(0 && "Operation not supported");
1702 case TargetLowering::Custom:
1703 Tmp1 = TLI.LowerOperation(Result, DAG);
1704 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001705 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001706 case TargetLowering::Legal: break;
1707 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001708 // If this target supports fabs/fneg natively, do this efficiently.
1709 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1710 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1711 // Get the sign bit of the RHS.
1712 MVT::ValueType IVT =
1713 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1714 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1715 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1716 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1717 // Get the absolute value of the result.
1718 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1719 // Select between the nabs and abs value based on the sign bit of
1720 // the input.
1721 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1722 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1723 AbsVal),
1724 AbsVal);
1725 Result = LegalizeOp(Result);
1726 break;
1727 }
1728
1729 // Otherwise, do bitwise ops!
1730
1731 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001732 const char *FnName;
1733 if (Node->getValueType(0) == MVT::f32) {
1734 FnName = "copysignf";
1735 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1736 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1737 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1738 } else {
1739 FnName = "copysign";
1740 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1741 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1742 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1743 }
1744 SDOperand Dummy;
1745 Result = ExpandLibCall(FnName, Node, Dummy);
1746 break;
1747 }
1748 break;
1749
Nate Begeman551bf3f2006-02-17 05:43:56 +00001750 case ISD::ADDC:
1751 case ISD::SUBC:
1752 Tmp1 = LegalizeOp(Node->getOperand(0));
1753 Tmp2 = LegalizeOp(Node->getOperand(1));
1754 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1755 // Since this produces two values, make sure to remember that we legalized
1756 // both of them.
1757 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1758 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1759 return Result;
1760 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001761
Nate Begeman551bf3f2006-02-17 05:43:56 +00001762 case ISD::ADDE:
1763 case ISD::SUBE:
1764 Tmp1 = LegalizeOp(Node->getOperand(0));
1765 Tmp2 = LegalizeOp(Node->getOperand(1));
1766 Tmp3 = LegalizeOp(Node->getOperand(2));
1767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1768 // Since this produces two values, make sure to remember that we legalized
1769 // both of them.
1770 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1771 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1772 return Result;
1773 break;
1774
Nate Begeman419f8b62005-10-18 00:27:41 +00001775 case ISD::BUILD_PAIR: {
1776 MVT::ValueType PairTy = Node->getValueType(0);
1777 // TODO: handle the case where the Lo and Hi operands are not of legal type
1778 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1779 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1780 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001781 case TargetLowering::Promote:
1782 case TargetLowering::Custom:
1783 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001784 case TargetLowering::Legal:
1785 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1786 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1787 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001788 case TargetLowering::Expand:
1789 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1790 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1791 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1792 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1793 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001794 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001795 break;
1796 }
1797 break;
1798 }
1799
Nate Begemanc105e192005-04-06 00:23:54 +00001800 case ISD::UREM:
1801 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001802 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001803 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1804 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001805
Nate Begemanc105e192005-04-06 00:23:54 +00001806 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001807 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1808 case TargetLowering::Custom:
1809 isCustom = true;
1810 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001811 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001812 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001813 if (isCustom) {
1814 Tmp1 = TLI.LowerOperation(Result, DAG);
1815 if (Tmp1.Val) Result = Tmp1;
1816 }
Nate Begemanc105e192005-04-06 00:23:54 +00001817 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001818 case TargetLowering::Expand:
1819 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001820 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001821 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001822 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001823 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1824 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1825 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1826 } else {
1827 // Floating point mod -> fmod libcall.
1828 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1829 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001830 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001831 }
1832 break;
1833 }
1834 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001835 case ISD::VAARG: {
1836 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1837 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1838
Chris Lattner5c62f332006-01-28 07:42:08 +00001839 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001840 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1841 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001842 case TargetLowering::Custom:
1843 isCustom = true;
1844 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001845 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001846 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1847 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001848 Tmp1 = Result.getValue(1);
1849
1850 if (isCustom) {
1851 Tmp2 = TLI.LowerOperation(Result, DAG);
1852 if (Tmp2.Val) {
1853 Result = LegalizeOp(Tmp2);
1854 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1855 }
1856 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001857 break;
1858 case TargetLowering::Expand: {
1859 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1860 Node->getOperand(2));
1861 // Increment the pointer, VAList, to the next vaarg
1862 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1863 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1864 TLI.getPointerTy()));
1865 // Store the incremented VAList to the legalized pointer
1866 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1867 Node->getOperand(2));
1868 // Load the actual argument out of the pointer VAList
1869 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001870 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001871 Result = LegalizeOp(Result);
1872 break;
1873 }
1874 }
1875 // Since VAARG produces two values, make sure to remember that we
1876 // legalized both of them.
1877 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001878 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1879 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00001880 }
1881
1882 case ISD::VACOPY:
1883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1885 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1886
1887 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1888 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001889 case TargetLowering::Custom:
1890 isCustom = true;
1891 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001892 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001893 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1894 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001895 if (isCustom) {
1896 Tmp1 = TLI.LowerOperation(Result, DAG);
1897 if (Tmp1.Val) Result = Tmp1;
1898 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001899 break;
1900 case TargetLowering::Expand:
1901 // This defaults to loading a pointer from the input and storing it to the
1902 // output, returning the chain.
1903 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1904 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1905 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00001906 break;
1907 }
1908 break;
1909
1910 case ISD::VAEND:
1911 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1912 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1913
1914 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1915 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001916 case TargetLowering::Custom:
1917 isCustom = true;
1918 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001919 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001920 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001921 if (isCustom) {
1922 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
1923 if (Tmp1.Val) Result = Tmp1;
1924 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001925 break;
1926 case TargetLowering::Expand:
1927 Result = Tmp1; // Default to a no-op, return the chain
1928 break;
1929 }
1930 break;
1931
1932 case ISD::VASTART:
1933 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1934 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1935
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001936 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1937
Nate Begemanacc398c2006-01-25 18:21:52 +00001938 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
1939 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001940 case TargetLowering::Legal: break;
1941 case TargetLowering::Custom:
1942 Tmp1 = TLI.LowerOperation(Result, DAG);
1943 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00001944 break;
1945 }
1946 break;
1947
Nate Begeman35ef9132006-01-11 21:21:00 +00001948 case ISD::ROTL:
1949 case ISD::ROTR:
1950 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1951 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001952
1953 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
1954 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001955 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00001956 break;
1957
Nate Begemand88fc032006-01-14 03:14:10 +00001958 case ISD::BSWAP:
1959 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1960 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001961 case TargetLowering::Custom:
1962 assert(0 && "Cannot custom legalize this yet!");
1963 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001964 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001965 break;
1966 case TargetLowering::Promote: {
1967 MVT::ValueType OVT = Tmp1.getValueType();
1968 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1969 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00001970
Chris Lattner456a93a2006-01-28 07:39:30 +00001971 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1972 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
1973 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
1974 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
1975 break;
1976 }
1977 case TargetLowering::Expand:
1978 Result = ExpandBSWAP(Tmp1);
1979 break;
Nate Begemand88fc032006-01-14 03:14:10 +00001980 }
1981 break;
1982
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001983 case ISD::CTPOP:
1984 case ISD::CTTZ:
1985 case ISD::CTLZ:
1986 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1987 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001988 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001989 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001990 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001991 break;
1992 case TargetLowering::Promote: {
1993 MVT::ValueType OVT = Tmp1.getValueType();
1994 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001995
1996 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001997 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1998 // Perform the larger operation, then subtract if needed.
1999 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002000 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002001 case ISD::CTPOP:
2002 Result = Tmp1;
2003 break;
2004 case ISD::CTTZ:
2005 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002006 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2007 DAG.getConstant(getSizeInBits(NVT), NVT),
2008 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002009 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002010 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2011 break;
2012 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002013 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002014 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2015 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002016 getSizeInBits(OVT), NVT));
2017 break;
2018 }
2019 break;
2020 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002021 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002022 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002023 break;
2024 }
2025 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002026
Chris Lattner2c8086f2005-04-02 05:00:07 +00002027 // Unary operators
2028 case ISD::FABS:
2029 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002030 case ISD::FSQRT:
2031 case ISD::FSIN:
2032 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002033 Tmp1 = LegalizeOp(Node->getOperand(0));
2034 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002035 case TargetLowering::Promote:
2036 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002037 isCustom = true;
2038 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002039 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002040 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002041 if (isCustom) {
2042 Tmp1 = TLI.LowerOperation(Result, DAG);
2043 if (Tmp1.Val) Result = Tmp1;
2044 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002045 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002046 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002047 switch (Node->getOpcode()) {
2048 default: assert(0 && "Unreachable!");
2049 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002050 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2051 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002052 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002053 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002054 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002055 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2056 MVT::ValueType VT = Node->getValueType(0);
2057 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002058 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002059 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2060 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002061 break;
2062 }
2063 case ISD::FSQRT:
2064 case ISD::FSIN:
2065 case ISD::FCOS: {
2066 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002067 const char *FnName = 0;
2068 switch(Node->getOpcode()) {
2069 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2070 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2071 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2072 default: assert(0 && "Unreachable!");
2073 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002074 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002075 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002076 break;
2077 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002078 }
2079 break;
2080 }
2081 break;
Chris Lattner35481892005-12-23 00:16:34 +00002082
2083 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002084 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002085 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002086 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002087 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2088 Node->getOperand(0).getValueType())) {
2089 default: assert(0 && "Unknown operation action!");
2090 case TargetLowering::Expand:
2091 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2092 break;
2093 case TargetLowering::Legal:
2094 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002095 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002096 break;
2097 }
2098 }
2099 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002100 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002101 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002102 case ISD::UINT_TO_FP: {
2103 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002104 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2105 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002106 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002107 Node->getOperand(0).getValueType())) {
2108 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002109 case TargetLowering::Custom:
2110 isCustom = true;
2111 // FALLTHROUGH
2112 case TargetLowering::Legal:
2113 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002114 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002115 if (isCustom) {
2116 Tmp1 = TLI.LowerOperation(Result, DAG);
2117 if (Tmp1.Val) Result = Tmp1;
2118 }
2119 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002120 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002121 Result = ExpandLegalINT_TO_FP(isSigned,
2122 LegalizeOp(Node->getOperand(0)),
2123 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002124 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002125 case TargetLowering::Promote:
2126 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2127 Node->getValueType(0),
2128 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002129 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002130 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002131 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002132 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002133 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2134 Node->getValueType(0), Node->getOperand(0));
2135 break;
2136 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002137 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002138 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002139 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2140 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002141 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002142 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2143 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002144 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002145 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2146 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002147 break;
2148 }
2149 break;
2150 }
2151 case ISD::TRUNCATE:
2152 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2153 case Legal:
2154 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002155 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002156 break;
2157 case Expand:
2158 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2159
2160 // Since the result is legal, we should just be able to truncate the low
2161 // part of the source.
2162 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2163 break;
2164 case Promote:
2165 Result = PromoteOp(Node->getOperand(0));
2166 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2167 break;
2168 }
2169 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002170
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002171 case ISD::FP_TO_SINT:
2172 case ISD::FP_TO_UINT:
2173 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2174 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002175 Tmp1 = LegalizeOp(Node->getOperand(0));
2176
Chris Lattner1618beb2005-07-29 00:11:56 +00002177 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2178 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002179 case TargetLowering::Custom:
2180 isCustom = true;
2181 // FALLTHROUGH
2182 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002183 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002184 if (isCustom) {
2185 Tmp1 = TLI.LowerOperation(Result, DAG);
2186 if (Tmp1.Val) Result = Tmp1;
2187 }
2188 break;
2189 case TargetLowering::Promote:
2190 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2191 Node->getOpcode() == ISD::FP_TO_SINT);
2192 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002193 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002194 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2195 SDOperand True, False;
2196 MVT::ValueType VT = Node->getOperand(0).getValueType();
2197 MVT::ValueType NVT = Node->getValueType(0);
2198 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2199 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2200 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2201 Node->getOperand(0), Tmp2, ISD::SETLT);
2202 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2203 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002204 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002205 Tmp2));
2206 False = DAG.getNode(ISD::XOR, NVT, False,
2207 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002208 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2209 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002210 } else {
2211 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2212 }
2213 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002214 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002215 break;
2216 case Expand:
2217 assert(0 && "Shouldn't need to expand other operators here!");
2218 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002219 Tmp1 = PromoteOp(Node->getOperand(0));
2220 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2221 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002222 break;
2223 }
2224 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002225
Chris Lattner13c78e22005-09-02 00:18:10 +00002226 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002227 case ISD::ZERO_EXTEND:
2228 case ISD::SIGN_EXTEND:
2229 case ISD::FP_EXTEND:
2230 case ISD::FP_ROUND:
2231 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002232 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002233 case Legal:
2234 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002235 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002236 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002237 case Promote:
2238 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002239 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002240 Tmp1 = PromoteOp(Node->getOperand(0));
2241 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002242 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002243 case ISD::ZERO_EXTEND:
2244 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002245 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002246 Result = DAG.getZeroExtendInReg(Result,
2247 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002248 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002249 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002250 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002251 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002252 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002253 Result,
2254 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002255 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002256 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002257 Result = PromoteOp(Node->getOperand(0));
2258 if (Result.getValueType() != Op.getValueType())
2259 // Dynamically dead while we have only 2 FP types.
2260 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2261 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002262 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002263 Result = PromoteOp(Node->getOperand(0));
2264 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2265 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002266 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002267 }
2268 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002269 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002270 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002271 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002272 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002273
2274 // If this operation is not supported, convert it to a shl/shr or load/store
2275 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002276 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2277 default: assert(0 && "This action not supported for this op yet!");
2278 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002279 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002280 break;
2281 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002282 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002283 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002284 // NOTE: we could fall back on load/store here too for targets without
2285 // SAR. However, it is doubtful that any exist.
2286 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2287 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002288 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002289 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2290 Node->getOperand(0), ShiftCst);
2291 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2292 Result, ShiftCst);
2293 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2294 // The only way we can lower this is to turn it into a STORETRUNC,
2295 // EXTLOAD pair, targetting a temporary location (a stack slot).
2296
2297 // NOTE: there is a choice here between constantly creating new stack
2298 // slots and always reusing the same one. We currently always create
2299 // new ones, as reuse may inhibit scheduling.
2300 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2301 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2302 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2303 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002304 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002305 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2306 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2307 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002308 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002309 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002310 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2311 Result, StackSlot, DAG.getSrcValue(NULL),
2312 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002313 } else {
2314 assert(0 && "Unknown op");
2315 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002316 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002317 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002318 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002319 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002320 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002321
2322 // Make sure that the generated code is itself legal.
2323 if (Result != Op)
2324 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002325
Chris Lattner45982da2005-05-12 16:53:42 +00002326 // Note that LegalizeOp may be reentered even from single-use nodes, which
2327 // means that we always must cache transformed nodes.
2328 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002329 return Result;
2330}
2331
Chris Lattner8b6fa222005-01-15 22:16:26 +00002332/// PromoteOp - Given an operation that produces a value in an invalid type,
2333/// promote it to compute the value into a larger type. The produced value will
2334/// have the correct bits for the low portion of the register, but no guarantee
2335/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002336SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2337 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002338 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002339 assert(getTypeAction(VT) == Promote &&
2340 "Caller should expand or legalize operands that are not promotable!");
2341 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2342 "Cannot promote to smaller type!");
2343
Chris Lattner03c85462005-01-15 05:21:40 +00002344 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002345 SDOperand Result;
2346 SDNode *Node = Op.Val;
2347
Chris Lattner6fdcb252005-09-02 20:32:45 +00002348 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2349 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002350
Chris Lattner03c85462005-01-15 05:21:40 +00002351 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002352 case ISD::CopyFromReg:
2353 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002354 default:
2355 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2356 assert(0 && "Do not know how to promote this operator!");
2357 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002358 case ISD::UNDEF:
2359 Result = DAG.getNode(ISD::UNDEF, NVT);
2360 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002361 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002362 if (VT != MVT::i1)
2363 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2364 else
2365 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002366 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2367 break;
2368 case ISD::ConstantFP:
2369 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2370 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2371 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002372
Chris Lattner82fbfb62005-01-18 02:59:52 +00002373 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002374 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002375 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2376 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002377 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002378
2379 case ISD::TRUNCATE:
2380 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2381 case Legal:
2382 Result = LegalizeOp(Node->getOperand(0));
2383 assert(Result.getValueType() >= NVT &&
2384 "This truncation doesn't make sense!");
2385 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2386 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2387 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002388 case Promote:
2389 // The truncation is not required, because we don't guarantee anything
2390 // about high bits anyway.
2391 Result = PromoteOp(Node->getOperand(0));
2392 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002393 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002394 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2395 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002396 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002397 }
2398 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002399 case ISD::SIGN_EXTEND:
2400 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002401 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002402 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2403 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2404 case Legal:
2405 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002406 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002407 break;
2408 case Promote:
2409 // Promote the reg if it's smaller.
2410 Result = PromoteOp(Node->getOperand(0));
2411 // The high bits are not guaranteed to be anything. Insert an extend.
2412 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002413 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002414 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002415 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002416 Result = DAG.getZeroExtendInReg(Result,
2417 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002418 break;
2419 }
2420 break;
Chris Lattner35481892005-12-23 00:16:34 +00002421 case ISD::BIT_CONVERT:
2422 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2423 Result = PromoteOp(Result);
2424 break;
2425
Chris Lattner8b6fa222005-01-15 22:16:26 +00002426 case ISD::FP_EXTEND:
2427 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2428 case ISD::FP_ROUND:
2429 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2430 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2431 case Promote: assert(0 && "Unreachable with 2 FP types!");
2432 case Legal:
2433 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002434 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002435 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002436 break;
2437 }
2438 break;
2439
2440 case ISD::SINT_TO_FP:
2441 case ISD::UINT_TO_FP:
2442 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2443 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002444 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002445 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002446 break;
2447
2448 case Promote:
2449 Result = PromoteOp(Node->getOperand(0));
2450 if (Node->getOpcode() == ISD::SINT_TO_FP)
2451 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002452 Result,
2453 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002454 else
Chris Lattner23993562005-04-13 02:38:47 +00002455 Result = DAG.getZeroExtendInReg(Result,
2456 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002457 // No extra round required here.
2458 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002459 break;
2460 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002461 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2462 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002463 // Round if we cannot tolerate excess precision.
2464 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002465 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2466 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002467 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002468 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002469 break;
2470
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002471 case ISD::SIGN_EXTEND_INREG:
2472 Result = PromoteOp(Node->getOperand(0));
2473 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2474 Node->getOperand(1));
2475 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002476 case ISD::FP_TO_SINT:
2477 case ISD::FP_TO_UINT:
2478 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2479 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002480 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002481 break;
2482 case Promote:
2483 // The input result is prerounded, so we don't have to do anything
2484 // special.
2485 Tmp1 = PromoteOp(Node->getOperand(0));
2486 break;
2487 case Expand:
2488 assert(0 && "not implemented");
2489 }
Nate Begemand2558e32005-08-14 01:20:53 +00002490 // If we're promoting a UINT to a larger size, check to see if the new node
2491 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2492 // we can use that instead. This allows us to generate better code for
2493 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2494 // legal, such as PowerPC.
2495 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002496 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002497 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2498 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002499 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2500 } else {
2501 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2502 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002503 break;
2504
Chris Lattner2c8086f2005-04-02 05:00:07 +00002505 case ISD::FABS:
2506 case ISD::FNEG:
2507 Tmp1 = PromoteOp(Node->getOperand(0));
2508 assert(Tmp1.getValueType() == NVT);
2509 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2510 // NOTE: we do not have to do any extra rounding here for
2511 // NoExcessFPPrecision, because we know the input will have the appropriate
2512 // precision, and these operations don't modify precision at all.
2513 break;
2514
Chris Lattnerda6ba872005-04-28 21:44:33 +00002515 case ISD::FSQRT:
2516 case ISD::FSIN:
2517 case ISD::FCOS:
2518 Tmp1 = PromoteOp(Node->getOperand(0));
2519 assert(Tmp1.getValueType() == NVT);
2520 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002521 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002522 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2523 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002524 break;
2525
Chris Lattner03c85462005-01-15 05:21:40 +00002526 case ISD::AND:
2527 case ISD::OR:
2528 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002529 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002530 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002531 case ISD::MUL:
2532 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002533 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002534 // that too is okay if they are integer operations.
2535 Tmp1 = PromoteOp(Node->getOperand(0));
2536 Tmp2 = PromoteOp(Node->getOperand(1));
2537 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2538 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002539 break;
2540 case ISD::FADD:
2541 case ISD::FSUB:
2542 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002543 Tmp1 = PromoteOp(Node->getOperand(0));
2544 Tmp2 = PromoteOp(Node->getOperand(1));
2545 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2546 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2547
2548 // Floating point operations will give excess precision that we may not be
2549 // able to tolerate. If we DO allow excess precision, just leave it,
2550 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002551 // FIXME: Why would we need to round FP ops more than integer ones?
2552 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002553 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002554 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2555 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002556 break;
2557
Chris Lattner8b6fa222005-01-15 22:16:26 +00002558 case ISD::SDIV:
2559 case ISD::SREM:
2560 // These operators require that their input be sign extended.
2561 Tmp1 = PromoteOp(Node->getOperand(0));
2562 Tmp2 = PromoteOp(Node->getOperand(1));
2563 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002564 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2565 DAG.getValueType(VT));
2566 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2567 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002568 }
2569 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2570
2571 // Perform FP_ROUND: this is probably overly pessimistic.
2572 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002573 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2574 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002575 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002576 case ISD::FDIV:
2577 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002578 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002579 // These operators require that their input be fp extended.
2580 Tmp1 = PromoteOp(Node->getOperand(0));
2581 Tmp2 = PromoteOp(Node->getOperand(1));
2582 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2583
2584 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002585 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002586 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2587 DAG.getValueType(VT));
2588 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002589
2590 case ISD::UDIV:
2591 case ISD::UREM:
2592 // These operators require that their input be zero extended.
2593 Tmp1 = PromoteOp(Node->getOperand(0));
2594 Tmp2 = PromoteOp(Node->getOperand(1));
2595 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002596 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2597 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002598 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2599 break;
2600
2601 case ISD::SHL:
2602 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002603 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002604 break;
2605 case ISD::SRA:
2606 // The input value must be properly sign extended.
2607 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002608 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2609 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002610 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002611 break;
2612 case ISD::SRL:
2613 // The input value must be properly zero extended.
2614 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002615 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002616 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002617 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002618
2619 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002620 Tmp1 = Node->getOperand(0); // Get the chain.
2621 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002622 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2623 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2624 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2625 } else {
2626 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2627 Node->getOperand(2));
2628 // Increment the pointer, VAList, to the next vaarg
2629 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2630 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2631 TLI.getPointerTy()));
2632 // Store the incremented VAList to the legalized pointer
2633 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2634 Node->getOperand(2));
2635 // Load the actual argument out of the pointer VAList
2636 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2637 DAG.getSrcValue(0), VT);
2638 }
2639 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002640 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002641 break;
2642
Chris Lattner03c85462005-01-15 05:21:40 +00002643 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002644 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2645 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002646 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002647 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002648 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002649 case ISD::SEXTLOAD:
2650 case ISD::ZEXTLOAD:
2651 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002652 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2653 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002654 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002655 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002656 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002657 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002658 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002659 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2660 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002661 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002662 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002663 case ISD::SELECT_CC:
2664 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2665 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2666 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002667 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002668 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002669 case ISD::BSWAP:
2670 Tmp1 = Node->getOperand(0);
2671 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2672 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2673 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2674 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2675 TLI.getShiftAmountTy()));
2676 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002677 case ISD::CTPOP:
2678 case ISD::CTTZ:
2679 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002680 // Zero extend the argument
2681 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002682 // Perform the larger operation, then subtract if needed.
2683 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002684 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002685 case ISD::CTPOP:
2686 Result = Tmp1;
2687 break;
2688 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002689 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002690 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002691 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002692 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002693 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002694 break;
2695 case ISD::CTLZ:
2696 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002697 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2698 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002699 getSizeInBits(VT), NVT));
2700 break;
2701 }
2702 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002703 }
2704
2705 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002706
2707 // Make sure the result is itself legal.
2708 Result = LegalizeOp(Result);
2709
2710 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002711 AddPromotedOperand(Op, Result);
2712 return Result;
2713}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002714
Nate Begeman750ac1b2006-02-01 07:19:44 +00002715/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2716/// with condition CC on the current target. This usually involves legalizing
2717/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2718/// there may be no choice but to create a new SetCC node to represent the
2719/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2720/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2721void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2722 SDOperand &RHS,
2723 SDOperand &CC) {
2724 SDOperand Tmp1, Tmp2, Result;
2725
2726 switch (getTypeAction(LHS.getValueType())) {
2727 case Legal:
2728 Tmp1 = LegalizeOp(LHS); // LHS
2729 Tmp2 = LegalizeOp(RHS); // RHS
2730 break;
2731 case Promote:
2732 Tmp1 = PromoteOp(LHS); // LHS
2733 Tmp2 = PromoteOp(RHS); // RHS
2734
2735 // If this is an FP compare, the operands have already been extended.
2736 if (MVT::isInteger(LHS.getValueType())) {
2737 MVT::ValueType VT = LHS.getValueType();
2738 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2739
2740 // Otherwise, we have to insert explicit sign or zero extends. Note
2741 // that we could insert sign extends for ALL conditions, but zero extend
2742 // is cheaper on many machines (an AND instead of two shifts), so prefer
2743 // it.
2744 switch (cast<CondCodeSDNode>(CC)->get()) {
2745 default: assert(0 && "Unknown integer comparison!");
2746 case ISD::SETEQ:
2747 case ISD::SETNE:
2748 case ISD::SETUGE:
2749 case ISD::SETUGT:
2750 case ISD::SETULE:
2751 case ISD::SETULT:
2752 // ALL of these operations will work if we either sign or zero extend
2753 // the operands (including the unsigned comparisons!). Zero extend is
2754 // usually a simpler/cheaper operation, so prefer it.
2755 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2756 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2757 break;
2758 case ISD::SETGE:
2759 case ISD::SETGT:
2760 case ISD::SETLT:
2761 case ISD::SETLE:
2762 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2763 DAG.getValueType(VT));
2764 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2765 DAG.getValueType(VT));
2766 break;
2767 }
2768 }
2769 break;
2770 case Expand:
2771 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2772 ExpandOp(LHS, LHSLo, LHSHi);
2773 ExpandOp(RHS, RHSLo, RHSHi);
2774 switch (cast<CondCodeSDNode>(CC)->get()) {
2775 case ISD::SETEQ:
2776 case ISD::SETNE:
2777 if (RHSLo == RHSHi)
2778 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2779 if (RHSCST->isAllOnesValue()) {
2780 // Comparison to -1.
2781 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2782 Tmp2 = RHSLo;
2783 break;
2784 }
2785
2786 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2787 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2788 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2789 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2790 break;
2791 default:
2792 // If this is a comparison of the sign bit, just look at the top part.
2793 // X > -1, x < 0
2794 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2795 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2796 CST->getValue() == 0) || // X < 0
2797 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2798 CST->isAllOnesValue())) { // X > -1
2799 Tmp1 = LHSHi;
2800 Tmp2 = RHSHi;
2801 break;
2802 }
2803
2804 // FIXME: This generated code sucks.
2805 ISD::CondCode LowCC;
2806 switch (cast<CondCodeSDNode>(CC)->get()) {
2807 default: assert(0 && "Unknown integer setcc!");
2808 case ISD::SETLT:
2809 case ISD::SETULT: LowCC = ISD::SETULT; break;
2810 case ISD::SETGT:
2811 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2812 case ISD::SETLE:
2813 case ISD::SETULE: LowCC = ISD::SETULE; break;
2814 case ISD::SETGE:
2815 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2816 }
2817
2818 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2819 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2820 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2821
2822 // NOTE: on targets without efficient SELECT of bools, we can always use
2823 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2824 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2825 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2826 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2827 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2828 Result, Tmp1, Tmp2));
2829 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002830 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002831 }
2832 }
2833 LHS = Tmp1;
2834 RHS = Tmp2;
2835}
2836
Chris Lattner35481892005-12-23 00:16:34 +00002837/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002838/// The resultant code need not be legal. Note that SrcOp is the input operand
2839/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002840SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2841 SDOperand SrcOp) {
2842 // Create the stack frame object.
2843 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2844 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00002845 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00002846 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2847
2848 // Emit a store to the stack slot.
2849 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002850 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002851 // Result is a load from the stack slot.
2852 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2853}
2854
Chris Lattner5b359c62005-04-02 04:00:59 +00002855void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2856 SDOperand Op, SDOperand Amt,
2857 SDOperand &Lo, SDOperand &Hi) {
2858 // Expand the subcomponents.
2859 SDOperand LHSL, LHSH;
2860 ExpandOp(Op, LHSL, LHSH);
2861
2862 std::vector<SDOperand> Ops;
2863 Ops.push_back(LHSL);
2864 Ops.push_back(LHSH);
2865 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002866 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002867 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002868 Hi = Lo.getValue(1);
2869}
2870
2871
Chris Lattnere34b3962005-01-19 04:19:40 +00002872/// ExpandShift - Try to find a clever way to expand this shift operation out to
2873/// smaller elements. If we can't find a way that is more efficient than a
2874/// libcall on this target, return false. Otherwise, return true with the
2875/// low-parts expanded into Lo and Hi.
2876bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2877 SDOperand &Lo, SDOperand &Hi) {
2878 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2879 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002880
Chris Lattnere34b3962005-01-19 04:19:40 +00002881 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002882 SDOperand ShAmt = LegalizeOp(Amt);
2883 MVT::ValueType ShTy = ShAmt.getValueType();
2884 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2885 unsigned NVTBits = MVT::getSizeInBits(NVT);
2886
2887 // Handle the case when Amt is an immediate. Other cases are currently broken
2888 // and are disabled.
2889 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2890 unsigned Cst = CN->getValue();
2891 // Expand the incoming operand to be shifted, so that we have its parts
2892 SDOperand InL, InH;
2893 ExpandOp(Op, InL, InH);
2894 switch(Opc) {
2895 case ISD::SHL:
2896 if (Cst > VTBits) {
2897 Lo = DAG.getConstant(0, NVT);
2898 Hi = DAG.getConstant(0, NVT);
2899 } else if (Cst > NVTBits) {
2900 Lo = DAG.getConstant(0, NVT);
2901 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002902 } else if (Cst == NVTBits) {
2903 Lo = DAG.getConstant(0, NVT);
2904 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002905 } else {
2906 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2907 Hi = DAG.getNode(ISD::OR, NVT,
2908 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2909 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2910 }
2911 return true;
2912 case ISD::SRL:
2913 if (Cst > VTBits) {
2914 Lo = DAG.getConstant(0, NVT);
2915 Hi = DAG.getConstant(0, NVT);
2916 } else if (Cst > NVTBits) {
2917 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2918 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002919 } else if (Cst == NVTBits) {
2920 Lo = InH;
2921 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002922 } else {
2923 Lo = DAG.getNode(ISD::OR, NVT,
2924 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2925 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2926 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2927 }
2928 return true;
2929 case ISD::SRA:
2930 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002931 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002932 DAG.getConstant(NVTBits-1, ShTy));
2933 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002934 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002935 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002936 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002937 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002938 } else if (Cst == NVTBits) {
2939 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002940 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002941 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002942 } else {
2943 Lo = DAG.getNode(ISD::OR, NVT,
2944 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2945 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2946 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2947 }
2948 return true;
2949 }
2950 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002951 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002952}
Chris Lattner77e77a62005-01-21 06:05:23 +00002953
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002954
Chris Lattner77e77a62005-01-21 06:05:23 +00002955// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2956// does not fit into a register, return the lo part and set the hi part to the
2957// by-reg argument. If it does fit into a single register, return the result
2958// and leave the Hi part unset.
2959SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2960 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00002961 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
2962 // The input chain to this libcall is the entry node of the function.
2963 // Legalizing the call will automatically add the previous call to the
2964 // dependence.
2965 SDOperand InChain = DAG.getEntryNode();
2966
Chris Lattner77e77a62005-01-21 06:05:23 +00002967 TargetLowering::ArgListTy Args;
2968 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2969 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2970 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2971 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2972 }
2973 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002974
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002975 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002976 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002977 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002978 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2979 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002980
Chris Lattner6831a812006-02-13 09:18:02 +00002981 // Legalize the call sequence, starting with the chain. This will advance
2982 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
2983 // was added by LowerCallTo (guaranteeing proper serialization of calls).
2984 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00002985 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002986 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002987 default: assert(0 && "Unknown thing");
2988 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002989 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00002990 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002991 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002992 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00002993 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002994 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002995 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002996}
2997
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002998
Chris Lattner77e77a62005-01-21 06:05:23 +00002999/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3000/// destination type is legal.
3001SDOperand SelectionDAGLegalize::
3002ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003003 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003004 assert(getTypeAction(Source.getValueType()) == Expand &&
3005 "This is not an expansion!");
3006 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3007
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003008 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003009 assert(Source.getValueType() == MVT::i64 &&
3010 "This only works for 64-bit -> FP");
3011 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3012 // incoming integer is set. To handle this, we dynamically test to see if
3013 // it is set, and, if so, add a fudge factor.
3014 SDOperand Lo, Hi;
3015 ExpandOp(Source, Lo, Hi);
3016
Chris Lattner66de05b2005-05-13 04:45:13 +00003017 // If this is unsigned, and not supported, first perform the conversion to
3018 // signed, then adjust the result if the sign bit is set.
3019 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3020 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3021
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003022 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3023 DAG.getConstant(0, Hi.getValueType()),
3024 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003025 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3026 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3027 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003028 uint64_t FF = 0x5f800000ULL;
3029 if (TLI.isLittleEndian()) FF <<= 32;
3030 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003031
Chris Lattner5839bf22005-08-26 17:15:30 +00003032 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003033 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3034 SDOperand FudgeInReg;
3035 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003036 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3037 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003038 else {
3039 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003040 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3041 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003042 }
Chris Lattner473a9902005-09-29 06:44:39 +00003043 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003044 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003045
Chris Lattnera88a2602005-05-14 05:33:54 +00003046 // Check to see if the target has a custom way to lower this. If so, use it.
3047 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3048 default: assert(0 && "This action not implemented for this operation!");
3049 case TargetLowering::Legal:
3050 case TargetLowering::Expand:
3051 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003052 case TargetLowering::Custom: {
3053 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3054 Source), DAG);
3055 if (NV.Val)
3056 return LegalizeOp(NV);
3057 break; // The target decided this was legal after all
3058 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003059 }
3060
Chris Lattner13689e22005-05-12 07:00:44 +00003061 // Expand the source, then glue it back together for the call. We must expand
3062 // the source in case it is shared (this pass of legalize must traverse it).
3063 SDOperand SrcLo, SrcHi;
3064 ExpandOp(Source, SrcLo, SrcHi);
3065 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3066
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003067 const char *FnName = 0;
3068 if (DestTy == MVT::f32)
3069 FnName = "__floatdisf";
3070 else {
3071 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3072 FnName = "__floatdidf";
3073 }
Chris Lattner6831a812006-02-13 09:18:02 +00003074
3075 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3076 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003077 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003078}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003079
Chris Lattner22cde6a2006-01-28 08:25:58 +00003080/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3081/// INT_TO_FP operation of the specified operand when the target requests that
3082/// we expand it. At this point, we know that the result and operand types are
3083/// legal for the target.
3084SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3085 SDOperand Op0,
3086 MVT::ValueType DestVT) {
3087 if (Op0.getValueType() == MVT::i32) {
3088 // simple 32-bit [signed|unsigned] integer to float/double expansion
3089
3090 // get the stack frame index of a 8 byte buffer
3091 MachineFunction &MF = DAG.getMachineFunction();
3092 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3093 // get address of 8 byte buffer
3094 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3095 // word offset constant for Hi/Lo address computation
3096 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3097 // set up Hi and Lo (into buffer) address based on endian
3098 SDOperand Hi, Lo;
3099 if (TLI.isLittleEndian()) {
3100 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3101 Lo = StackSlot;
3102 } else {
3103 Hi = StackSlot;
3104 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3105 }
3106 // if signed map to unsigned space
3107 SDOperand Op0Mapped;
3108 if (isSigned) {
3109 // constant used to invert sign bit (signed to unsigned mapping)
3110 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3111 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3112 } else {
3113 Op0Mapped = Op0;
3114 }
3115 // store the lo of the constructed double - based on integer input
3116 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3117 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3118 // initial hi portion of constructed double
3119 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3120 // store the hi of the constructed double - biased exponent
3121 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3122 InitialHi, Hi, DAG.getSrcValue(NULL));
3123 // load the constructed double
3124 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3125 DAG.getSrcValue(NULL));
3126 // FP constant to bias correct the final result
3127 SDOperand Bias = DAG.getConstantFP(isSigned ?
3128 BitsToDouble(0x4330000080000000ULL)
3129 : BitsToDouble(0x4330000000000000ULL),
3130 MVT::f64);
3131 // subtract the bias
3132 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3133 // final result
3134 SDOperand Result;
3135 // handle final rounding
3136 if (DestVT == MVT::f64) {
3137 // do nothing
3138 Result = Sub;
3139 } else {
3140 // if f32 then cast to f32
3141 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3142 }
3143 return Result;
3144 }
3145 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3146 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3147
3148 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3149 DAG.getConstant(0, Op0.getValueType()),
3150 ISD::SETLT);
3151 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3152 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3153 SignSet, Four, Zero);
3154
3155 // If the sign bit of the integer is set, the large number will be treated
3156 // as a negative number. To counteract this, the dynamic code adds an
3157 // offset depending on the data type.
3158 uint64_t FF;
3159 switch (Op0.getValueType()) {
3160 default: assert(0 && "Unsupported integer type!");
3161 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3162 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3163 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3164 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3165 }
3166 if (TLI.isLittleEndian()) FF <<= 32;
3167 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3168
3169 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3170 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3171 SDOperand FudgeInReg;
3172 if (DestVT == MVT::f32)
3173 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3174 DAG.getSrcValue(NULL));
3175 else {
3176 assert(DestVT == MVT::f64 && "Unexpected conversion");
3177 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3178 DAG.getEntryNode(), CPIdx,
3179 DAG.getSrcValue(NULL), MVT::f32));
3180 }
3181
3182 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3183}
3184
3185/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3186/// *INT_TO_FP operation of the specified operand when the target requests that
3187/// we promote it. At this point, we know that the result and operand types are
3188/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3189/// operation that takes a larger input.
3190SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3191 MVT::ValueType DestVT,
3192 bool isSigned) {
3193 // First step, figure out the appropriate *INT_TO_FP operation to use.
3194 MVT::ValueType NewInTy = LegalOp.getValueType();
3195
3196 unsigned OpToUse = 0;
3197
3198 // Scan for the appropriate larger type to use.
3199 while (1) {
3200 NewInTy = (MVT::ValueType)(NewInTy+1);
3201 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3202
3203 // If the target supports SINT_TO_FP of this type, use it.
3204 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3205 default: break;
3206 case TargetLowering::Legal:
3207 if (!TLI.isTypeLegal(NewInTy))
3208 break; // Can't use this datatype.
3209 // FALL THROUGH.
3210 case TargetLowering::Custom:
3211 OpToUse = ISD::SINT_TO_FP;
3212 break;
3213 }
3214 if (OpToUse) break;
3215 if (isSigned) continue;
3216
3217 // If the target supports UINT_TO_FP of this type, use it.
3218 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3219 default: break;
3220 case TargetLowering::Legal:
3221 if (!TLI.isTypeLegal(NewInTy))
3222 break; // Can't use this datatype.
3223 // FALL THROUGH.
3224 case TargetLowering::Custom:
3225 OpToUse = ISD::UINT_TO_FP;
3226 break;
3227 }
3228 if (OpToUse) break;
3229
3230 // Otherwise, try a larger type.
3231 }
3232
3233 // Okay, we found the operation and type to use. Zero extend our input to the
3234 // desired type then run the operation on it.
3235 return DAG.getNode(OpToUse, DestVT,
3236 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3237 NewInTy, LegalOp));
3238}
3239
3240/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3241/// FP_TO_*INT operation of the specified operand when the target requests that
3242/// we promote it. At this point, we know that the result and operand types are
3243/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3244/// operation that returns a larger result.
3245SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3246 MVT::ValueType DestVT,
3247 bool isSigned) {
3248 // First step, figure out the appropriate FP_TO*INT operation to use.
3249 MVT::ValueType NewOutTy = DestVT;
3250
3251 unsigned OpToUse = 0;
3252
3253 // Scan for the appropriate larger type to use.
3254 while (1) {
3255 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3256 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3257
3258 // If the target supports FP_TO_SINT returning this type, use it.
3259 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3260 default: break;
3261 case TargetLowering::Legal:
3262 if (!TLI.isTypeLegal(NewOutTy))
3263 break; // Can't use this datatype.
3264 // FALL THROUGH.
3265 case TargetLowering::Custom:
3266 OpToUse = ISD::FP_TO_SINT;
3267 break;
3268 }
3269 if (OpToUse) break;
3270
3271 // If the target supports FP_TO_UINT of this type, use it.
3272 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3273 default: break;
3274 case TargetLowering::Legal:
3275 if (!TLI.isTypeLegal(NewOutTy))
3276 break; // Can't use this datatype.
3277 // FALL THROUGH.
3278 case TargetLowering::Custom:
3279 OpToUse = ISD::FP_TO_UINT;
3280 break;
3281 }
3282 if (OpToUse) break;
3283
3284 // Otherwise, try a larger type.
3285 }
3286
3287 // Okay, we found the operation and type to use. Truncate the result of the
3288 // extended FP_TO_*INT operation to the desired size.
3289 return DAG.getNode(ISD::TRUNCATE, DestVT,
3290 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3291}
3292
3293/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3294///
3295SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3296 MVT::ValueType VT = Op.getValueType();
3297 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3298 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3299 switch (VT) {
3300 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3301 case MVT::i16:
3302 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3303 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3304 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3305 case MVT::i32:
3306 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3307 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3308 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3309 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3310 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3311 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3312 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3313 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3314 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3315 case MVT::i64:
3316 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3317 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3318 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3319 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3320 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3321 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3322 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3323 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3324 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3325 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3326 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3327 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3328 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3329 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3330 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3331 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3332 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3333 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3334 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3335 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3336 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3337 }
3338}
3339
3340/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3341///
3342SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3343 switch (Opc) {
3344 default: assert(0 && "Cannot expand this yet!");
3345 case ISD::CTPOP: {
3346 static const uint64_t mask[6] = {
3347 0x5555555555555555ULL, 0x3333333333333333ULL,
3348 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3349 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3350 };
3351 MVT::ValueType VT = Op.getValueType();
3352 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3353 unsigned len = getSizeInBits(VT);
3354 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3355 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3356 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3357 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3358 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3359 DAG.getNode(ISD::AND, VT,
3360 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3361 }
3362 return Op;
3363 }
3364 case ISD::CTLZ: {
3365 // for now, we do this:
3366 // x = x | (x >> 1);
3367 // x = x | (x >> 2);
3368 // ...
3369 // x = x | (x >>16);
3370 // x = x | (x >>32); // for 64-bit input
3371 // return popcount(~x);
3372 //
3373 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3374 MVT::ValueType VT = Op.getValueType();
3375 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3376 unsigned len = getSizeInBits(VT);
3377 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3378 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3379 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3380 }
3381 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3382 return DAG.getNode(ISD::CTPOP, VT, Op);
3383 }
3384 case ISD::CTTZ: {
3385 // for now, we use: { return popcount(~x & (x - 1)); }
3386 // unless the target has ctlz but not ctpop, in which case we use:
3387 // { return 32 - nlz(~x & (x-1)); }
3388 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3389 MVT::ValueType VT = Op.getValueType();
3390 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3391 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3392 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3393 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3394 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3395 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3396 TLI.isOperationLegal(ISD::CTLZ, VT))
3397 return DAG.getNode(ISD::SUB, VT,
3398 DAG.getConstant(getSizeInBits(VT), VT),
3399 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3400 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3401 }
3402 }
3403}
Chris Lattnere34b3962005-01-19 04:19:40 +00003404
3405
Chris Lattner3e928bb2005-01-07 07:47:09 +00003406/// ExpandOp - Expand the specified SDOperand into its two component pieces
3407/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3408/// LegalizeNodes map is filled in for any results that are not expanded, the
3409/// ExpandedNodes map is filled in for any results that are expanded, and the
3410/// Lo/Hi values are returned.
3411void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3412 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003413 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003414 SDNode *Node = Op.Val;
3415 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003416 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3417 "Cannot expand FP values!");
3418 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003419 "Cannot expand to FP value or to larger int value!");
3420
Chris Lattner6fdcb252005-09-02 20:32:45 +00003421 // See if we already expanded it.
3422 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3423 = ExpandedNodes.find(Op);
3424 if (I != ExpandedNodes.end()) {
3425 Lo = I->second.first;
3426 Hi = I->second.second;
3427 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003428 }
3429
Chris Lattner3e928bb2005-01-07 07:47:09 +00003430 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003431 case ISD::CopyFromReg:
3432 assert(0 && "CopyFromReg must be legal!");
3433 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003434 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3435 assert(0 && "Do not know how to expand this operator!");
3436 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003437 case ISD::UNDEF:
3438 Lo = DAG.getNode(ISD::UNDEF, NVT);
3439 Hi = DAG.getNode(ISD::UNDEF, NVT);
3440 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003441 case ISD::Constant: {
3442 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3443 Lo = DAG.getConstant(Cst, NVT);
3444 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3445 break;
3446 }
Evan Cheng860771d2006-03-01 01:09:54 +00003447 case ISD::VConstant: {
3448 unsigned NumElements =
3449 cast<ConstantSDNode>(Node->getOperand(0))->getValue() / 2;
3450 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3451 MVT::ValueType TVT = (NumElements > 1)
3452 ? getVectorType(EVT, NumElements) : EVT;
3453 // If type of bisected vector is legal, turn it into a ConstantVec (which
3454 // will be lowered to a ConstantPool or something else). Otherwise, bisect
3455 // the VConstant, and return each half as a new VConstant.
3456 unsigned Opc = ISD::ConstantVec;
3457 std::vector<SDOperand> LoOps, HiOps;
3458 if (!(TVT != MVT::Other &&
3459 (!MVT::isVector(TVT) || TLI.isTypeLegal(TVT)))) {
3460 Opc = ISD::VConstant;
3461 TVT = MVT::Vector;
3462 SDOperand Num = DAG.getConstant(NumElements, MVT::i32);
3463 SDOperand Typ = DAG.getValueType(EVT);
3464 HiOps.push_back(Num);
3465 HiOps.push_back(Typ);
3466 LoOps.push_back(Num);
3467 LoOps.push_back(Typ);
3468 }
3469
3470 if (NumElements == 1) {
3471 Hi = Node->getOperand(2);
3472 Lo = Node->getOperand(3);
Nate Begemancc827e62005-12-07 19:48:11 +00003473 } else {
Nate Begemancc827e62005-12-07 19:48:11 +00003474 for (unsigned I = 0, E = NumElements; I < E; ++I) {
Evan Cheng860771d2006-03-01 01:09:54 +00003475 HiOps.push_back(Node->getOperand(I+2));
3476 LoOps.push_back(Node->getOperand(I+2+NumElements));
Nate Begemancc827e62005-12-07 19:48:11 +00003477 }
Evan Cheng860771d2006-03-01 01:09:54 +00003478 Hi = DAG.getNode(Opc, TVT, HiOps);
3479 Lo = DAG.getNode(Opc, TVT, LoOps);
Nate Begemancc827e62005-12-07 19:48:11 +00003480 }
3481 break;
3482 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003483
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003484 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003485 // Return the operands.
3486 Lo = Node->getOperand(0);
3487 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003488 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003489
3490 case ISD::SIGN_EXTEND_INREG:
3491 ExpandOp(Node->getOperand(0), Lo, Hi);
3492 // Sign extend the lo-part.
3493 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3494 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3495 TLI.getShiftAmountTy()));
3496 // sext_inreg the low part if needed.
3497 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3498 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003499
Nate Begemand88fc032006-01-14 03:14:10 +00003500 case ISD::BSWAP: {
3501 ExpandOp(Node->getOperand(0), Lo, Hi);
3502 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3503 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3504 Lo = TempLo;
3505 break;
3506 }
3507
Chris Lattneredb1add2005-05-11 04:51:16 +00003508 case ISD::CTPOP:
3509 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003510 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3511 DAG.getNode(ISD::CTPOP, NVT, Lo),
3512 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003513 Hi = DAG.getConstant(0, NVT);
3514 break;
3515
Chris Lattner39a8f332005-05-12 19:05:01 +00003516 case ISD::CTLZ: {
3517 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003518 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003519 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3520 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003521 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3522 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003523 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3524 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3525
3526 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3527 Hi = DAG.getConstant(0, NVT);
3528 break;
3529 }
3530
3531 case ISD::CTTZ: {
3532 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003533 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003534 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3535 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003536 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3537 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003538 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3539 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3540
3541 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3542 Hi = DAG.getConstant(0, NVT);
3543 break;
3544 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003545
Nate Begemanacc398c2006-01-25 18:21:52 +00003546 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003547 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3548 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003549 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3550 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3551
3552 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003553 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003554 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3555 if (!TLI.isLittleEndian())
3556 std::swap(Lo, Hi);
3557 break;
3558 }
3559
Chris Lattner3e928bb2005-01-07 07:47:09 +00003560 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003561 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3562 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003563 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003564
3565 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003566 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003567 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3568 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003569 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003570 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003571
3572 // Build a factor node to remember that this load is independent of the
3573 // other one.
3574 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3575 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003576
Chris Lattner3e928bb2005-01-07 07:47:09 +00003577 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003578 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003579 if (!TLI.isLittleEndian())
3580 std::swap(Lo, Hi);
3581 break;
3582 }
Nate Begemanab48be32005-11-22 18:16:00 +00003583 case ISD::VLOAD: {
Evan Cheng860771d2006-03-01 01:09:54 +00003584 SDOperand Ch = Node->getOperand(2); // Legalize the chain.
3585 SDOperand Ptr = Node->getOperand(3); // Legalize the pointer.
3586 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(0))->getValue();
3587 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3588 MVT::ValueType TVT = (NumElements/2 > 1)
3589 ? getVectorType(EVT, NumElements/2) : EVT;
Nate Begemanab48be32005-11-22 18:16:00 +00003590
Evan Cheng860771d2006-03-01 01:09:54 +00003591 // If type of split vector is legal, turn into a pair of scalar or
3592 // packed loads.
3593 if (TVT != MVT::Other &&
3594 (!MVT::isVector(TVT) ||
3595 (TLI.isTypeLegal(TVT) && TLI.isOperationLegal(ISD::LOAD, TVT)))) {
3596 Lo = DAG.getLoad(TVT, Ch, Ptr, Node->getOperand(4));
Nate Begemanab48be32005-11-22 18:16:00 +00003597 // Increment the pointer to the other half.
Evan Cheng860771d2006-03-01 01:09:54 +00003598 unsigned IncrementSize = MVT::getSizeInBits(TVT)/8;
Nate Begemanab48be32005-11-22 18:16:00 +00003599 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3600 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003601 // FIXME: This creates a bogus srcvalue!
Evan Cheng860771d2006-03-01 01:09:54 +00003602 Hi = DAG.getLoad(TVT, Ch, Ptr, Node->getOperand(4));
Nate Begemanab48be32005-11-22 18:16:00 +00003603 } else {
3604 NumElements /= 2; // Split the vector in half
3605 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3606 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3607 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3608 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003609 // FIXME: This creates a bogus srcvalue!
Nate Begemanab48be32005-11-22 18:16:00 +00003610 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3611 }
3612
3613 // Build a factor node to remember that this load is independent of the
3614 // other one.
3615 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3616 Hi.getValue(1));
3617
3618 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003619 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemanab48be32005-11-22 18:16:00 +00003620 if (!TLI.isLittleEndian())
3621 std::swap(Lo, Hi);
3622 break;
3623 }
3624 case ISD::VADD:
3625 case ISD::VSUB:
Evan Cheng3e1ce5a2006-03-03 07:01:07 +00003626 case ISD::VMUL:
3627 case ISD::VSDIV:
3628 case ISD::VUDIV:
3629 case ISD::VAND:
3630 case ISD::VOR:
3631 case ISD::VXOR: {
Evan Cheng860771d2006-03-01 01:09:54 +00003632 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(0))->getValue();
3633 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3634 MVT::ValueType TVT = (NumElements/2 > 1)
3635 ? getVectorType(EVT, NumElements/2) : EVT;
Nate Begemanab48be32005-11-22 18:16:00 +00003636 SDOperand LL, LH, RL, RH;
3637
Evan Cheng860771d2006-03-01 01:09:54 +00003638 ExpandOp(Node->getOperand(2), LL, LH);
3639 ExpandOp(Node->getOperand(3), RL, RH);
Nate Begemanab48be32005-11-22 18:16:00 +00003640
Evan Cheng860771d2006-03-01 01:09:54 +00003641 // If type of split vector is legal, turn into a pair of scalar / packed
3642 // ADD, SUB, or MUL.
3643 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3644 if (TVT != MVT::Other &&
3645 (!MVT::isVector(TVT) ||
3646 (TLI.isTypeLegal(TVT) && TLI.isOperationLegal(Opc, TVT)))) {
3647 Lo = DAG.getNode(Opc, TVT, LL, RL);
3648 Hi = DAG.getNode(Opc, TVT, LH, RH);
Nate Begemanab48be32005-11-22 18:16:00 +00003649 } else {
Evan Cheng860771d2006-03-01 01:09:54 +00003650 SDOperand Num = DAG.getConstant(NumElements/2, MVT::i32);
3651 SDOperand Typ = DAG.getValueType(EVT);
3652 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, Num, Typ, LL, RL);
3653 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, Num, Typ, LH, RH);
Nate Begemanab48be32005-11-22 18:16:00 +00003654 }
3655 break;
3656 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003657 case ISD::AND:
3658 case ISD::OR:
3659 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3660 SDOperand LL, LH, RL, RH;
3661 ExpandOp(Node->getOperand(0), LL, LH);
3662 ExpandOp(Node->getOperand(1), RL, RH);
3663 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3664 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3665 break;
3666 }
3667 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003668 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003669 ExpandOp(Node->getOperand(1), LL, LH);
3670 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003671 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3672 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003673 break;
3674 }
Nate Begeman9373a812005-08-10 20:51:12 +00003675 case ISD::SELECT_CC: {
3676 SDOperand TL, TH, FL, FH;
3677 ExpandOp(Node->getOperand(2), TL, TH);
3678 ExpandOp(Node->getOperand(3), FL, FH);
3679 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3680 Node->getOperand(1), TL, FL, Node->getOperand(4));
3681 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3682 Node->getOperand(1), TH, FH, Node->getOperand(4));
3683 break;
3684 }
Nate Begeman144ff662005-10-13 17:15:37 +00003685 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003686 SDOperand Chain = Node->getOperand(0);
3687 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003688 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3689
3690 if (EVT == NVT)
3691 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3692 else
3693 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3694 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003695
3696 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003697 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003698
Nate Begeman144ff662005-10-13 17:15:37 +00003699 // The high part is obtained by SRA'ing all but one of the bits of the lo
3700 // part.
3701 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3702 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3703 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003704 break;
3705 }
3706 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003707 SDOperand Chain = Node->getOperand(0);
3708 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003709 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3710
3711 if (EVT == NVT)
3712 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3713 else
3714 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3715 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003716
3717 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003718 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003719
Nate Begeman144ff662005-10-13 17:15:37 +00003720 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003721 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003722 break;
3723 }
3724 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003725 SDOperand Chain = Node->getOperand(0);
3726 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003727 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3728
3729 if (EVT == NVT)
3730 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3731 else
3732 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3733 EVT);
3734
3735 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003736 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003737
3738 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003739 Hi = DAG.getNode(ISD::UNDEF, NVT);
3740 break;
3741 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003742 case ISD::ANY_EXTEND:
3743 // The low part is any extension of the input (which degenerates to a copy).
3744 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3745 // The high part is undefined.
3746 Hi = DAG.getNode(ISD::UNDEF, NVT);
3747 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003748 case ISD::SIGN_EXTEND: {
3749 // The low part is just a sign extension of the input (which degenerates to
3750 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003751 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003752
Chris Lattner3e928bb2005-01-07 07:47:09 +00003753 // The high part is obtained by SRA'ing all but one of the bits of the lo
3754 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003755 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003756 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3757 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003758 break;
3759 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003760 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003761 // The low part is just a zero extension of the input (which degenerates to
3762 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003763 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003764
Chris Lattner3e928bb2005-01-07 07:47:09 +00003765 // The high part is just a zero.
3766 Hi = DAG.getConstant(0, NVT);
3767 break;
Chris Lattner35481892005-12-23 00:16:34 +00003768
3769 case ISD::BIT_CONVERT: {
3770 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3771 Node->getOperand(0));
3772 ExpandOp(Tmp, Lo, Hi);
3773 break;
3774 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003775
Chris Lattner8137c9e2006-01-28 05:07:51 +00003776 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003777 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3778 TargetLowering::Custom &&
3779 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003780 Lo = TLI.LowerOperation(Op, DAG);
3781 assert(Lo.Val && "Node must be custom expanded!");
3782 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003783 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003784 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003785 break;
3786
Chris Lattner4e6c7462005-01-08 19:27:05 +00003787 // These operators cannot be expanded directly, emit them as calls to
3788 // library functions.
3789 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003790 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003791 SDOperand Op;
3792 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3793 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003794 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3795 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003796 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003797
Chris Lattnerf20d1832005-07-30 01:40:57 +00003798 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3799
Chris Lattner80a3e942005-07-29 00:33:32 +00003800 // Now that the custom expander is done, expand the result, which is still
3801 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003802 if (Op.Val) {
3803 ExpandOp(Op, Lo, Hi);
3804 break;
3805 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003806 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003807
Chris Lattner4e6c7462005-01-08 19:27:05 +00003808 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003809 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003810 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003811 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003812 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003813
Chris Lattner4e6c7462005-01-08 19:27:05 +00003814 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003815 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003816 SDOperand Op;
3817 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3818 case Expand: assert(0 && "cannot expand FP!");
3819 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3820 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3821 }
3822
3823 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3824
3825 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003826 if (Op.Val) {
3827 ExpandOp(Op, Lo, Hi);
3828 break;
3829 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003830 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003831
Chris Lattner4e6c7462005-01-08 19:27:05 +00003832 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003833 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003834 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003835 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003836 break;
3837
Evan Cheng05a2d562006-01-09 18:31:59 +00003838 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003839 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003840 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003841 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003842 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003843 Op = TLI.LowerOperation(Op, DAG);
3844 if (Op.Val) {
3845 // Now that the custom expander is done, expand the result, which is
3846 // still VT.
3847 ExpandOp(Op, Lo, Hi);
3848 break;
3849 }
3850 }
3851
Chris Lattnere34b3962005-01-19 04:19:40 +00003852 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003853 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003854 break;
Chris Lattner47599822005-04-02 03:38:53 +00003855
3856 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003857 TargetLowering::LegalizeAction Action =
3858 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3859 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3860 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003861 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003862 break;
3863 }
3864
Chris Lattnere34b3962005-01-19 04:19:40 +00003865 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003866 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003867 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003868 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003869
Evan Cheng05a2d562006-01-09 18:31:59 +00003870 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003871 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003872 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003873 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003874 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003875 Op = TLI.LowerOperation(Op, DAG);
3876 if (Op.Val) {
3877 // Now that the custom expander is done, expand the result, which is
3878 // still VT.
3879 ExpandOp(Op, Lo, Hi);
3880 break;
3881 }
3882 }
3883
Chris Lattnere34b3962005-01-19 04:19:40 +00003884 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003885 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003886 break;
Chris Lattner47599822005-04-02 03:38:53 +00003887
3888 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003889 TargetLowering::LegalizeAction Action =
3890 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3891 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3892 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003893 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003894 break;
3895 }
3896
Chris Lattnere34b3962005-01-19 04:19:40 +00003897 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003898 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003899 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003900 }
3901
3902 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003903 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003904 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003905 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003906 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003907 Op = TLI.LowerOperation(Op, DAG);
3908 if (Op.Val) {
3909 // Now that the custom expander is done, expand the result, which is
3910 // still VT.
3911 ExpandOp(Op, Lo, Hi);
3912 break;
3913 }
3914 }
3915
Chris Lattnere34b3962005-01-19 04:19:40 +00003916 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003917 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003918 break;
Chris Lattner47599822005-04-02 03:38:53 +00003919
3920 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003921 TargetLowering::LegalizeAction Action =
3922 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3923 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3924 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003925 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003926 break;
3927 }
3928
Chris Lattnere34b3962005-01-19 04:19:40 +00003929 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003930 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003931 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003932 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003933
Misha Brukmanedf128a2005-04-21 22:36:52 +00003934 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003935 case ISD::SUB: {
3936 // If the target wants to custom expand this, let them.
3937 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3938 TargetLowering::Custom) {
3939 Op = TLI.LowerOperation(Op, DAG);
3940 if (Op.Val) {
3941 ExpandOp(Op, Lo, Hi);
3942 break;
3943 }
3944 }
3945
3946 // Expand the subcomponents.
3947 SDOperand LHSL, LHSH, RHSL, RHSH;
3948 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3949 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00003950 std::vector<MVT::ValueType> VTs;
3951 std::vector<SDOperand> LoOps, HiOps;
3952 VTs.push_back(LHSL.getValueType());
3953 VTs.push_back(MVT::Flag);
3954 LoOps.push_back(LHSL);
3955 LoOps.push_back(RHSL);
3956 HiOps.push_back(LHSH);
3957 HiOps.push_back(RHSH);
3958 if (Node->getOpcode() == ISD::ADD) {
3959 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
3960 HiOps.push_back(Lo.getValue(1));
3961 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
3962 } else {
3963 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
3964 HiOps.push_back(Lo.getValue(1));
3965 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
3966 }
Chris Lattner84f67882005-01-20 18:52:28 +00003967 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00003968 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003969 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003970 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003971 SDOperand LL, LH, RL, RH;
3972 ExpandOp(Node->getOperand(0), LL, LH);
3973 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003974 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3975 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3976 // extended the sign bit of the low half through the upper half, and if so
3977 // emit a MULHS instead of the alternate sequence that is valid for any
3978 // i64 x i64 multiply.
3979 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3980 // is RH an extension of the sign bit of RL?
3981 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3982 RH.getOperand(1).getOpcode() == ISD::Constant &&
3983 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3984 // is LH an extension of the sign bit of LL?
3985 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3986 LH.getOperand(1).getOpcode() == ISD::Constant &&
3987 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3988 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3989 } else {
3990 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3991 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3992 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3993 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3994 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3995 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003996 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3997 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00003998 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00003999 }
4000 break;
4001 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004002 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4003 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4004 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4005 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004006 }
4007
Chris Lattner83397362005-12-21 18:02:52 +00004008 // Make sure the resultant values have been legalized themselves, unless this
4009 // is a type that requires multi-step expansion.
4010 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4011 Lo = LegalizeOp(Lo);
4012 Hi = LegalizeOp(Hi);
4013 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004014
4015 // Remember in a map if the values will be reused later.
4016 bool isNew =
4017 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4018 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004019}
4020
4021
4022// SelectionDAG::Legalize - This is the entry point for the file.
4023//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004024void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004025 /// run - This is the main entry point to this class.
4026 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004027 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004028}
4029