blob: 5a45323c38a580062b75124bc15fe7fd489d7457 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-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 Lattner462505f2006-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 Lattnerdc750592005-01-07 07:47:09 +000056 enum LegalizeAction {
Chris Lattner2c748af2006-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 Lattnerdc750592005-01-07 07:47:09 +000060 };
Chris Lattner462505f2006-02-13 09:18:02 +000061
Chris Lattnerdc750592005-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 Lattner2c748af2006-01-29 08:42:06 +000065 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000066
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-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 Lattnerea4ca942005-01-07 22:28:47 +000083 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-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 Lattnerea4ca942005-01-07 22:28:47 +000088 }
Chris Lattner1f2c9d82005-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 Lattner2af3ee42005-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 Lattner1f2c9d82005-01-15 05:21:40 +000094 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000095
Chris Lattnerdc750592005-01-07 07:47:09 +000096public:
97
Chris Lattner4add7e32005-01-23 04:42:50 +000098 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000099
Chris Lattnerdc750592005-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 Lattner2c748af2006-01-29 08:42:06 +0000104 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +0000113 void LegalizeDAG();
114
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000115private:
116
Chris Lattnerdc750592005-01-07 07:47:09 +0000117 SDOperand LegalizeOp(SDOperand O);
118 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000119 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000120
Chris Lattner462505f2006-02-13 09:18:02 +0000121 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
122
Nate Begeman7e7f4392006-02-01 07:19:44 +0000123 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
124
Chris Lattneraac464e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000129
Chris Lattner36e663d2005-12-23 00:16:34 +0000130 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000131 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
132 SDOperand LegalOp,
133 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000134 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000136 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
137 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000138
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000139 SDOperand ExpandBSWAP(SDOperand Op);
140 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000141 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000143 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
144 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattner301015a2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattnerdc750592005-01-07 07:47:09 +0000160
Chris Lattner4add7e32005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000164 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000165 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000166}
167
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000168/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
169/// not been visited yet and if all of its operands have already been visited.
170static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
171 std::map<SDNode*, unsigned> &Visited) {
172 if (++Visited[N] != N->getNumOperands())
173 return; // Haven't visited all operands yet
174
175 Order.push_back(N);
176
177 if (N->hasOneUse()) { // Tail recurse in common case.
178 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
179 return;
180 }
181
182 // Now that we have N in, add anything that uses it if all of their operands
183 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000184 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
185 ComputeTopDownOrdering(*UI, Order, Visited);
186}
187
Chris Lattner44fe26f2005-07-29 00:11:56 +0000188
Chris Lattnerdc750592005-01-07 07:47:09 +0000189void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000190 LastCALLSEQ_END = DAG.getEntryNode();
191 IsLegalizingCall = false;
192
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000193 // The legalize process is inherently a bottom-up recursive process (users
194 // legalize their uses before themselves). Given infinite stack space, we
195 // could just start legalizing on the root and traverse the whole graph. In
196 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000197 // blocks. To avoid this problem, compute an ordering of the nodes where each
198 // node is only legalized after all of its operands are legalized.
199 std::map<SDNode*, unsigned> Visited;
200 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000201
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000202 // Compute ordering from all of the leaves in the graphs, those (like the
203 // entry node) that have no operands.
204 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
205 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000206 if (I->getNumOperands() == 0) {
207 Visited[I] = 0 - 1U;
208 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000209 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000210 }
211
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000212 assert(Order.size() == Visited.size() &&
213 Order.size() ==
214 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000215 "Error: DAG is cyclic!");
216 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000217
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000218 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
219 SDNode *N = Order[i];
220 switch (getTypeAction(N->getValueType(0))) {
221 default: assert(0 && "Bad type action!");
222 case Legal:
223 LegalizeOp(SDOperand(N, 0));
224 break;
225 case Promote:
226 PromoteOp(SDOperand(N, 0));
227 break;
228 case Expand: {
229 SDOperand X, Y;
230 ExpandOp(SDOperand(N, 0), X, Y);
231 break;
232 }
233 }
234 }
235
236 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000237 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000238 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
239 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000240
241 ExpandedNodes.clear();
242 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000243 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000244
245 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000246 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000247}
248
Chris Lattner462505f2006-02-13 09:18:02 +0000249
250/// FindCallEndFromCallStart - Given a chained node that is part of a call
251/// sequence, find the CALLSEQ_END node that terminates the call sequence.
252static SDNode *FindCallEndFromCallStart(SDNode *Node) {
253 if (Node->getOpcode() == ISD::CALLSEQ_END)
254 return Node;
255 if (Node->use_empty())
256 return 0; // No CallSeqEnd
257
258 // The chain is usually at the end.
259 SDOperand TheChain(Node, Node->getNumValues()-1);
260 if (TheChain.getValueType() != MVT::Other) {
261 // Sometimes it's at the beginning.
262 TheChain = SDOperand(Node, 0);
263 if (TheChain.getValueType() != MVT::Other) {
264 // Otherwise, hunt for it.
265 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
266 if (Node->getValueType(i) == MVT::Other) {
267 TheChain = SDOperand(Node, i);
268 break;
269 }
270
271 // Otherwise, we walked into a node without a chain.
272 if (TheChain.getValueType() != MVT::Other)
273 return 0;
274 }
275 }
276
277 for (SDNode::use_iterator UI = Node->use_begin(),
278 E = Node->use_end(); UI != E; ++UI) {
279
280 // Make sure to only follow users of our token chain.
281 SDNode *User = *UI;
282 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
283 if (User->getOperand(i) == TheChain)
284 if (SDNode *Result = FindCallEndFromCallStart(User))
285 return Result;
286 }
287 return 0;
288}
289
290/// FindCallStartFromCallEnd - Given a chained node that is part of a call
291/// sequence, find the CALLSEQ_START node that initiates the call sequence.
292static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
293 assert(Node && "Didn't find callseq_start for a call??");
294 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
295
296 assert(Node->getOperand(0).getValueType() == MVT::Other &&
297 "Node doesn't have a token chain argument!");
298 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
299}
300
301/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
302/// see if any uses can reach Dest. If no dest operands can get to dest,
303/// legalize them, legalize ourself, and return false, otherwise, return true.
304bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
305 SDNode *Dest) {
306 if (N == Dest) return true; // N certainly leads to Dest :)
307
308 // If the first result of this node has been already legalized, then it cannot
309 // reach N.
310 switch (getTypeAction(N->getValueType(0))) {
311 case Legal:
312 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
313 break;
314 case Promote:
315 if (PromotedNodes.count(SDOperand(N, 0))) return false;
316 break;
317 case Expand:
318 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
319 break;
320 }
321
322 // Okay, this node has not already been legalized. Check and legalize all
323 // operands. If none lead to Dest, then we can legalize this node.
324 bool OperandsLeadToDest = false;
325 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
326 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
327 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
328
329 if (OperandsLeadToDest) return true;
330
331 // Okay, this node looks safe, legalize it and return false.
332 switch (getTypeAction(N->getValueType(0))) {
333 case Legal:
334 LegalizeOp(SDOperand(N, 0));
335 break;
336 case Promote:
337 PromoteOp(SDOperand(N, 0));
338 break;
339 case Expand: {
340 SDOperand X, Y;
341 ExpandOp(SDOperand(N, 0), X, Y);
342 break;
343 }
344 }
345 return false;
346}
347
348
349
Chris Lattnerdc750592005-01-07 07:47:09 +0000350SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000351 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000352 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000353 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000354
Chris Lattnerdc750592005-01-07 07:47:09 +0000355 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000356 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000357 if (Node->getNumValues() > 1) {
358 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
359 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000360 case Legal: break; // Nothing to do.
361 case Expand: {
362 SDOperand T1, T2;
363 ExpandOp(Op.getValue(i), T1, T2);
364 assert(LegalizedNodes.count(Op) &&
365 "Expansion didn't add legal operands!");
366 return LegalizedNodes[Op];
367 }
368 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000369 PromoteOp(Op.getValue(i));
370 assert(LegalizedNodes.count(Op) &&
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000371 "Promotion didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000372 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000373 }
374 }
375
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000376 // Note that LegalizeOp may be reentered even from single-use nodes, which
377 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000378 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
379 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000380
Nate Begemane5b86d72005-08-10 20:51:12 +0000381 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000382 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000383 bool isCustom = false;
384
Chris Lattnerdc750592005-01-07 07:47:09 +0000385 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000386 case ISD::FrameIndex:
387 case ISD::EntryToken:
388 case ISD::Register:
389 case ISD::BasicBlock:
390 case ISD::TargetFrameIndex:
391 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000392 case ISD::TargetConstantFP:
393 case ISD::TargetConstantVec:
Chris Lattnereb637512006-01-28 08:31:04 +0000394 case ISD::TargetConstantPool:
395 case ISD::TargetGlobalAddress:
396 case ISD::TargetExternalSymbol:
397 case ISD::VALUETYPE:
398 case ISD::SRCVALUE:
399 case ISD::STRING:
400 case ISD::CONDCODE:
401 // Primitives must all be legal.
402 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
403 "This must be legal!");
404 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000405 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000406 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
407 // If this is a target node, legalize it by legalizing the operands then
408 // passing it through.
409 std::vector<SDOperand> Ops;
410 bool Changed = false;
411 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
412 Ops.push_back(LegalizeOp(Node->getOperand(i)));
413 Changed = Changed || Node->getOperand(i) != Ops.back();
414 }
415 if (Changed)
416 if (Node->getNumValues() == 1)
417 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
418 else {
419 std::vector<MVT::ValueType> VTs(Node->value_begin(),
420 Node->value_end());
421 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
422 }
423
424 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
425 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
426 return Result.getValue(Op.ResNo);
427 }
428 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000429 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
430 assert(0 && "Do not know how to legalize this operator!");
431 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000432 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000433 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000434 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000435 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
436 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000437 case TargetLowering::Custom:
438 Tmp1 = TLI.LowerOperation(Op, DAG);
439 if (Tmp1.Val) Result = Tmp1;
440 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000441 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000442 break;
443 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000444 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000445 case ISD::AssertSext:
446 case ISD::AssertZext:
447 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000449 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000450 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000451 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000452 Result = Node->getOperand(Op.ResNo);
453 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000454 case ISD::CopyFromReg:
455 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000456 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000457 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000458 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000459 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000460 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000461 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000462 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
464 } else {
465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
466 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000467 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
468 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000469 // Since CopyFromReg produces two values, make sure to remember that we
470 // legalized both of them.
471 AddLegalizedOperand(Op.getValue(0), Result);
472 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
473 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000474 case ISD::UNDEF: {
475 MVT::ValueType VT = Op.getValueType();
476 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000477 default: assert(0 && "This action is not supported yet!");
478 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000479 if (MVT::isInteger(VT))
480 Result = DAG.getConstant(0, VT);
481 else if (MVT::isFloatingPoint(VT))
482 Result = DAG.getConstantFP(0, VT);
483 else
484 assert(0 && "Unknown value type!");
485 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000486 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000487 break;
488 }
489 break;
490 }
Chris Lattner435b4022005-11-29 06:21:05 +0000491
492 case ISD::LOCATION:
493 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
494 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
495
496 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
497 case TargetLowering::Promote:
498 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000499 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000500 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000501 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
502 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
503
504 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
505 const std::string &FName =
506 cast<StringSDNode>(Node->getOperand(3))->getValue();
507 const std::string &DirName =
508 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000509 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000510
Jim Laskey9e296be2005-12-21 20:51:37 +0000511 std::vector<SDOperand> Ops;
512 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000513 SDOperand LineOp = Node->getOperand(1);
514 SDOperand ColOp = Node->getOperand(2);
515
516 if (useDEBUG_LOC) {
517 Ops.push_back(LineOp); // line #
518 Ops.push_back(ColOp); // col #
519 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
520 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
521 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000522 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
523 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000524 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
525 Ops.push_back(DAG.getConstant(ID, MVT::i32));
526 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
527 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000528 } else {
529 Result = Tmp1; // chain
530 }
Chris Lattner435b4022005-11-29 06:21:05 +0000531 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000532 }
Chris Lattner435b4022005-11-29 06:21:05 +0000533 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000534 if (Tmp1 != Node->getOperand(0) ||
535 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000536 std::vector<SDOperand> Ops;
537 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000538 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
539 Ops.push_back(Node->getOperand(1)); // line # must be legal.
540 Ops.push_back(Node->getOperand(2)); // col # must be legal.
541 } else {
542 // Otherwise promote them.
543 Ops.push_back(PromoteOp(Node->getOperand(1)));
544 Ops.push_back(PromoteOp(Node->getOperand(2)));
545 }
Chris Lattner435b4022005-11-29 06:21:05 +0000546 Ops.push_back(Node->getOperand(3)); // filename must be legal.
547 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000548 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000549 }
550 break;
551 }
552 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000553
554 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000555 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000556 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000557 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000558 case TargetLowering::Legal:
559 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
560 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
561 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
562 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000564 break;
565 }
566 break;
567
568 case ISD::DEBUG_LABEL:
569 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
570 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000571 default: assert(0 && "This action is not supported yet!");
572 case TargetLowering::Legal:
573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
574 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000575 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000576 break;
577 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000578 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000579
Chris Lattnerdc750592005-01-07 07:47:09 +0000580 case ISD::Constant:
581 // We know we don't need to expand constants here, constants only have one
582 // value and we check that it is fine above.
583
584 // FIXME: Maybe we should handle things like targets that don't support full
585 // 32-bit immediates?
586 break;
587 case ISD::ConstantFP: {
588 // Spill FP immediates to the constant pool if the target cannot directly
589 // codegen them. Targets often have some immediate values that can be
590 // efficiently generated into an FP register without a load. We explicitly
591 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000592 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
593
594 // Check to see if this FP immediate is already legal.
595 bool isLegal = false;
596 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
597 E = TLI.legal_fpimm_end(); I != E; ++I)
598 if (CFP->isExactlyValue(*I)) {
599 isLegal = true;
600 break;
601 }
602
Chris Lattner758b0ac2006-01-29 06:26:56 +0000603 // If this is a legal constant, turn it into a TargetConstantFP node.
604 if (isLegal) {
605 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
606 break;
607 }
608
609 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
610 default: assert(0 && "This action is not supported yet!");
611 case TargetLowering::Custom:
612 Tmp3 = TLI.LowerOperation(Result, DAG);
613 if (Tmp3.Val) {
614 Result = Tmp3;
615 break;
616 }
617 // FALLTHROUGH
618 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000619 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000620 bool Extend = false;
621
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000622 // If a FP immediate is precise when represented as a float and if the
623 // target can do an extending load from float to double, we put it into
624 // the constant pool as a float, even if it's is statically typed as a
625 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000626 MVT::ValueType VT = CFP->getValueType(0);
627 bool isDouble = VT == MVT::f64;
628 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
629 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000630 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
631 // Only do this if the target has a native EXTLOAD instruction from
632 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000633 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000634 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
635 VT = MVT::f32;
636 Extend = true;
637 }
Misha Brukman835702a2005-04-21 22:36:52 +0000638
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000639 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000640 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000641 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
642 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000643 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000644 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
645 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000646 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000647 }
648 break;
649 }
Chris Lattner2f2927892006-01-29 06:34:16 +0000650 case ISD::ConstantVec:
651 switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) {
652 default: assert(0 && "This action is not supported yet!");
653 case TargetLowering::Custom:
654 Tmp3 = TLI.LowerOperation(Result, DAG);
655 if (Tmp3.Val) {
656 Result = Tmp3;
657 break;
658 }
659 // FALLTHROUGH
660 case TargetLowering::Expand:
661 // We assume that vector constants are not legal, and will be immediately
662 // spilled to the constant pool.
663 //
664 // Create a ConstantPacked, and put it in the constant pool.
665 MVT::ValueType VT = Node->getValueType(0);
666 const Type *OpNTy =
667 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
668 std::vector<Constant*> CV;
669 if (MVT::isFloatingPoint(VT)) {
670 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
671 double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue();
672 CV.push_back(ConstantFP::get(OpNTy, V));
673 }
674 } else {
675 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
676 uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
677 CV.push_back(ConstantUInt::get(OpNTy, V));
678 }
679 }
680 Constant *CP = ConstantPacked::get(CV);
681 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
682 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
683 DAG.getSrcValue(NULL));
684 break;
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000685 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000686 break;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000687 case ISD::TokenFactor:
688 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000689 Tmp1 = LegalizeOp(Node->getOperand(0));
690 Tmp2 = LegalizeOp(Node->getOperand(1));
691 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
692 } else if (Node->getNumOperands() == 3) {
693 Tmp1 = LegalizeOp(Node->getOperand(0));
694 Tmp2 = LegalizeOp(Node->getOperand(1));
695 Tmp3 = LegalizeOp(Node->getOperand(2));
696 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000697 } else {
698 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000699 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000700 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
701 Ops.push_back(LegalizeOp(Node->getOperand(i)));
702 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000703 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000704 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000705
Chris Lattner462505f2006-02-13 09:18:02 +0000706 case ISD::CALLSEQ_START: {
707 SDNode *CallEnd = FindCallEndFromCallStart(Node);
708
709 // Recursively Legalize all of the inputs of the call end that do not lead
710 // to this call start. This ensures that any libcalls that need be inserted
711 // are inserted *before* the CALLSEQ_START.
712 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
713 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
714
715 // Now that we legalized all of the inputs (which may have inserted
716 // libcalls) create the new CALLSEQ_START node.
717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
718
719 // Merge in the last call, to ensure that this call start after the last
720 // call ended.
721 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
722 Tmp1 = LegalizeOp(Tmp1);
723
724 // Do not try to legalize the target-specific arguments (#1+).
725 if (Tmp1 != Node->getOperand(0)) {
726 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
727 Ops[0] = Tmp1;
728 Result = DAG.UpdateNodeOperands(Result, Ops);
729 }
730
731 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000732 AddLegalizedOperand(Op.getValue(0), Result);
733 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
734 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
735
Chris Lattner462505f2006-02-13 09:18:02 +0000736 // Now that the callseq_start and all of the non-call nodes above this call
737 // sequence have been legalized, legalize the call itself. During this
738 // process, no libcalls can/will be inserted, guaranteeing that no calls
739 // can overlap.
740 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
741 SDOperand InCallSEQ = LastCALLSEQ_END;
742 // Note that we are selecting this call!
743 LastCALLSEQ_END = SDOperand(CallEnd, 0);
744 IsLegalizingCall = true;
745
746 // Legalize the call, starting from the CALLSEQ_END.
747 LegalizeOp(LastCALLSEQ_END);
748 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
749 return Result;
750 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000751 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +0000752 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
753 // will cause this node to be legalized as well as handling libcalls right.
754 if (LastCALLSEQ_END.Val != Node) {
755 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
756 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
757 assert(I != LegalizedNodes.end() &&
758 "Legalizing the call start should have legalized this node!");
759 return I->second;
760 }
761
762 // Otherwise, the call start has been legalized and everything is going
763 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +0000764 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000765 // Do not try to legalize the target-specific arguments (#1+), except for
766 // an optional flag input.
767 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
768 if (Tmp1 != Node->getOperand(0)) {
769 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
770 Ops[0] = Tmp1;
771 Result = DAG.UpdateNodeOperands(Result, Ops);
772 }
773 } else {
774 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
775 if (Tmp1 != Node->getOperand(0) ||
776 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
777 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
778 Ops[0] = Tmp1;
779 Ops.back() = Tmp2;
780 Result = DAG.UpdateNodeOperands(Result, Ops);
781 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +0000782 }
Chris Lattner8e2ee732006-02-14 00:55:02 +0000783 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +0000784 // This finishes up call legalization.
785 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +0000786
787 // If the CALLSEQ_END node has a flag, remember that we legalized it.
788 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
789 if (Node->getNumValues() == 2)
790 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
791 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000792 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +0000793 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
795 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000796 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +0000797
Chris Lattnerd02b0542006-01-28 10:58:55 +0000798 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +0000799 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000800 switch (TLI.getOperationAction(Node->getOpcode(),
801 Node->getValueType(0))) {
802 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +0000803 case TargetLowering::Expand: {
804 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
805 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
806 " not tell us which reg is the stack pointer!");
807 SDOperand Chain = Tmp1.getOperand(0);
808 SDOperand Size = Tmp2.getOperand(1);
809 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
810 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
811 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +0000812 Tmp1 = LegalizeOp(Tmp1);
813 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000814 break;
815 }
816 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +0000817 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
818 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000819 Tmp1 = LegalizeOp(Tmp3);
820 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +0000821 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000822 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000823 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +0000824 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000825 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000826 // Since this op produce two values, make sure to remember that we
827 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000828 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
829 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000830 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000831 }
Chris Lattner476e67b2006-01-26 22:24:51 +0000832 case ISD::INLINEASM:
833 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
834 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000835 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +0000836 Tmp2 = Tmp3 = SDOperand(0, 0);
837 else
838 Tmp3 = LegalizeOp(Tmp2);
839
840 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
841 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
842 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +0000843 if (Tmp3.Val) Ops.back() = Tmp3;
844 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +0000845 }
846
847 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000848 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +0000849 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
850 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +0000851 case ISD::BR:
852 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +0000853 // Ensure that libcalls are emitted before a branch.
854 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
855 Tmp1 = LegalizeOp(Tmp1);
856 LastCALLSEQ_END = DAG.getEntryNode();
857
Chris Lattnerd02b0542006-01-28 10:58:55 +0000858 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +0000859 break;
860
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000861 case ISD::BRCOND:
862 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +0000863 // Ensure that libcalls are emitted before a return.
864 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
865 Tmp1 = LegalizeOp(Tmp1);
866 LastCALLSEQ_END = DAG.getEntryNode();
867
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000868 switch (getTypeAction(Node->getOperand(1).getValueType())) {
869 case Expand: assert(0 && "It's impossible to expand bools");
870 case Legal:
871 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
872 break;
873 case Promote:
874 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
875 break;
876 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000877
878 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000880
881 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
882 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000883 case TargetLowering::Legal: break;
884 case TargetLowering::Custom:
885 Tmp1 = TLI.LowerOperation(Result, DAG);
886 if (Tmp1.Val) Result = Tmp1;
887 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000888 case TargetLowering::Expand:
889 // Expand brcond's setcc into its constituent parts and create a BR_CC
890 // Node.
891 if (Tmp2.getOpcode() == ISD::SETCC) {
892 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
893 Tmp2.getOperand(0), Tmp2.getOperand(1),
894 Node->getOperand(2));
895 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000896 // Make sure the condition is either zero or one. It may have been
897 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +0000898 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
899 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
900 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +0000901
Nate Begeman371e4952005-08-16 19:49:35 +0000902 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
903 DAG.getCondCode(ISD::SETNE), Tmp2,
904 DAG.getConstant(0, Tmp2.getValueType()),
905 Node->getOperand(2));
906 }
907 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000908 }
909 break;
910 case ISD::BR_CC:
911 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +0000912 // Ensure that libcalls are emitted before a branch.
913 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
914 Tmp1 = LegalizeOp(Tmp1);
915 LastCALLSEQ_END = DAG.getEntryNode();
916
Nate Begeman7e7f4392006-02-01 07:19:44 +0000917 Tmp2 = Node->getOperand(2); // LHS
918 Tmp3 = Node->getOperand(3); // RHS
919 Tmp4 = Node->getOperand(1); // CC
920
921 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
922
923 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
924 // the LHS is a legal SETCC itself. In this case, we need to compare
925 // the result against zero to select between true and false values.
926 if (Tmp3.Val == 0) {
927 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
928 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000929 }
Nate Begeman7e7f4392006-02-01 07:19:44 +0000930
931 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
932 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000933
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000934 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
935 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000936 case TargetLowering::Legal: break;
937 case TargetLowering::Custom:
938 Tmp4 = TLI.LowerOperation(Result, DAG);
939 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000940 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000941 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000942 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000943 case ISD::BRCONDTWOWAY:
944 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
945 switch (getTypeAction(Node->getOperand(1).getValueType())) {
946 case Expand: assert(0 && "It's impossible to expand bools");
947 case Legal:
948 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
949 break;
950 case Promote:
951 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
952 break;
953 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000954
Chris Lattnerfd986782005-04-09 03:30:19 +0000955 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
956 // pair.
957 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
958 case TargetLowering::Promote:
959 default: assert(0 && "This action is not supported yet!");
960 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000961 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
962 Node->getOperand(3));
Chris Lattnerfd986782005-04-09 03:30:19 +0000963 break;
964 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000965 // If BRTWOWAY_CC is legal for this target, then simply expand this node
966 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
967 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000968 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000969 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattner678da982006-01-29 06:00:45 +0000970 Tmp3 = Tmp2.getOperand(0);
971 Tmp4 = Tmp2.getOperand(1);
972 Tmp2 = Tmp2.getOperand(2);
Nate Begeman371e4952005-08-16 19:49:35 +0000973 } else {
Chris Lattner678da982006-01-29 06:00:45 +0000974 Tmp3 = Tmp2;
975 Tmp4 = DAG.getConstant(0, Tmp2.getValueType());
976 Tmp2 = DAG.getCondCode(ISD::SETNE);
Nate Begeman371e4952005-08-16 19:49:35 +0000977 }
Chris Lattner678da982006-01-29 06:00:45 +0000978 std::vector<SDOperand> Ops;
979 Ops.push_back(Tmp1);
980 Ops.push_back(Tmp2);
981 Ops.push_back(Tmp3);
982 Ops.push_back(Tmp4);
983 Ops.push_back(Node->getOperand(2));
984 Ops.push_back(Node->getOperand(3));
985 Result = DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000986 } else {
987 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000988 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000989 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
990 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000991 break;
992 }
993 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +0000994 case ISD::BRTWOWAY_CC: {
Nate Begeman371e4952005-08-16 19:49:35 +0000995 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +0000996 // Ensure that libcalls are emitted before a branch.
997 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
998 Tmp1 = LegalizeOp(Tmp1);
999 LastCALLSEQ_END = DAG.getEntryNode();
1000
Nate Begeman7e7f4392006-02-01 07:19:44 +00001001 Tmp2 = Node->getOperand(2); // LHS
1002 Tmp3 = Node->getOperand(3); // RHS
1003 Tmp4 = Node->getOperand(1); // CC
1004
1005 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1006
1007 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1008 // the LHS is a legal SETCC itself. In this case, we need to compare
1009 // the result against zero to select between true and false values.
1010 if (Tmp3.Val == 0) {
1011 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1012 Tmp4 = DAG.getCondCode(ISD::SETNE);
1013 }
1014 std::vector<SDOperand> Ops;
1015 Ops.push_back(Tmp1);
1016 Ops.push_back(Tmp4);
1017 Ops.push_back(Tmp2);
1018 Ops.push_back(Tmp3);
1019 Ops.push_back(Node->getOperand(4));
1020 Ops.push_back(Node->getOperand(5));
1021 Result = DAG.UpdateNodeOperands(Result, Ops);
1022
1023 // Everything is legal, see if we should expand this op or something.
1024 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1025 default: assert(0 && "This action is not supported yet!");
1026 case TargetLowering::Legal: break;
1027 case TargetLowering::Expand:
1028 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1,
1029 DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp2,
1030 Tmp3, Tmp4),
1031 Result.getOperand(4));
1032 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Result.getOperand(5));
Nate Begeman371e4952005-08-16 19:49:35 +00001033 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001034 }
1035 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001036 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001037 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1039 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001040
Evan Cheng31d15fa2005-12-23 07:29:34 +00001041 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1043 Tmp2 = Result.getValue(0);
1044 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001045
Evan Cheng31d15fa2005-12-23 07:29:34 +00001046 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1047 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001048 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001049 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001050 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001051 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001052 Tmp2 = LegalizeOp(Tmp1);
1053 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001054 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001055 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001056 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001057 // Since loads produce two values, make sure to remember that we
1058 // legalized both of them.
1059 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1060 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1061 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001062 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001063 case ISD::EXTLOAD:
1064 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001065 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001066 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1067 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001068
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001069 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001070 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001071 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001072 case TargetLowering::Promote:
1073 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001074 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1075 DAG.getValueType(MVT::i8));
1076 Tmp1 = Result.getValue(0);
1077 Tmp2 = Result.getValue(1);
1078 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001079 case TargetLowering::Custom:
1080 isCustom = true;
1081 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001082 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001083 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1084 Node->getOperand(3));
1085 Tmp1 = Result.getValue(0);
1086 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001087
1088 if (isCustom) {
1089 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1090 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001091 Tmp1 = LegalizeOp(Tmp3);
1092 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001093 }
1094 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001095 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001096 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001097 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001098 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1099 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001100 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001101 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1102 Tmp2 = LegalizeOp(Load.getValue(1));
1103 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001104 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001105 assert(Node->getOpcode() != ISD::EXTLOAD &&
1106 "EXTLOAD should always be supported!");
1107 // Turn the unsupported load into an EXTLOAD followed by an explicit
1108 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001109 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1110 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001111 SDOperand ValRes;
1112 if (Node->getOpcode() == ISD::SEXTLOAD)
1113 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001114 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001115 else
1116 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001117 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1118 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1119 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001120 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001121 // Since loads produce two values, make sure to remember that we legalized
1122 // both of them.
1123 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1124 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1125 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001126 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001127 case ISD::EXTRACT_ELEMENT: {
1128 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1129 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001130 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001131 case Legal:
1132 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1133 // 1 -> Hi
1134 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1135 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1136 TLI.getShiftAmountTy()));
1137 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1138 } else {
1139 // 0 -> Lo
1140 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1141 Node->getOperand(0));
1142 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001143 break;
1144 case Expand:
1145 // Get both the low and high parts.
1146 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1147 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1148 Result = Tmp2; // 1 -> Hi
1149 else
1150 Result = Tmp1; // 0 -> Lo
1151 break;
1152 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001153 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001154 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001155
1156 case ISD::CopyToReg:
1157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001158
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001159 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001160 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001161 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001162 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001163 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001164 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001165 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001166 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001167 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001168 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001169 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1170 Tmp3);
1171 } else {
1172 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001173 }
1174
1175 // Since this produces two values, make sure to remember that we legalized
1176 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001177 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001178 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001179 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001180 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001181 break;
1182
1183 case ISD::RET:
1184 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001185
1186 // Ensure that libcalls are emitted before a return.
1187 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1188 Tmp1 = LegalizeOp(Tmp1);
1189 LastCALLSEQ_END = DAG.getEntryNode();
1190
Chris Lattnerdc750592005-01-07 07:47:09 +00001191 switch (Node->getNumOperands()) {
1192 case 2: // ret val
1193 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1194 case Legal:
1195 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001196 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001197 break;
1198 case Expand: {
1199 SDOperand Lo, Hi;
1200 ExpandOp(Node->getOperand(1), Lo, Hi);
1201 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001202 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001203 }
1204 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001205 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001206 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1207 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001208 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001209 }
1210 break;
1211 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001212 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001213 break;
1214 default: { // ret <values>
1215 std::vector<SDOperand> NewValues;
1216 NewValues.push_back(Tmp1);
1217 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1218 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1219 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001220 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001221 break;
1222 case Expand: {
1223 SDOperand Lo, Hi;
1224 ExpandOp(Node->getOperand(i), Lo, Hi);
1225 NewValues.push_back(Lo);
1226 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001227 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001228 }
1229 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001230 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001231 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001232
1233 if (NewValues.size() == Node->getNumOperands())
1234 Result = DAG.UpdateNodeOperands(Result, NewValues);
1235 else
1236 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001237 break;
1238 }
1239 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001240
Chris Lattner4d1ea712006-01-29 21:02:23 +00001241 if (Result.getOpcode() == ISD::RET) {
1242 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1243 default: assert(0 && "This action is not supported yet!");
1244 case TargetLowering::Legal: break;
1245 case TargetLowering::Custom:
1246 Tmp1 = TLI.LowerOperation(Result, DAG);
1247 if (Tmp1.Val) Result = Tmp1;
1248 break;
1249 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001250 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001251 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001252 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001253 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1254 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1255
Chris Lattnere69daaf2005-01-08 06:25:56 +00001256 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001257 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001258 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001259 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001260 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001261 } else {
1262 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001263 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001264 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001265 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1266 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001267 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001268 }
1269
Chris Lattnerdc750592005-01-07 07:47:09 +00001270 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1271 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001272 Tmp3 = LegalizeOp(Node->getOperand(1));
1273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1274 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001275
Chris Lattnerd02b0542006-01-28 10:58:55 +00001276 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001277 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1278 default: assert(0 && "This action is not supported yet!");
1279 case TargetLowering::Legal: break;
1280 case TargetLowering::Custom:
1281 Tmp1 = TLI.LowerOperation(Result, DAG);
1282 if (Tmp1.Val) Result = Tmp1;
1283 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001284 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001285 break;
1286 }
1287 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001288 // Truncate the value and store the result.
1289 Tmp3 = PromoteOp(Node->getOperand(1));
1290 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001291 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001292 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001293 break;
1294
Chris Lattnerdc750592005-01-07 07:47:09 +00001295 case Expand:
1296 SDOperand Lo, Hi;
1297 ExpandOp(Node->getOperand(1), Lo, Hi);
1298
1299 if (!TLI.isLittleEndian())
1300 std::swap(Lo, Hi);
1301
Chris Lattner55e9cde2005-05-11 04:51:16 +00001302 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1303 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001304 // If this is a vector type, then we have to calculate the increment as
1305 // the product of the element size in bytes, and the number of elements
1306 // in the high half of the vector.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001307 unsigned IncrementSize;
Nate Begemand37c1312005-11-22 18:16:00 +00001308 if (MVT::Vector == Hi.getValueType()) {
1309 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1310 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1311 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1312 } else {
1313 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1314 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001315 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1316 getIntPtrConstant(IncrementSize));
1317 assert(isTypeLegal(Tmp2.getValueType()) &&
1318 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001319 // FIXME: This sets the srcvalue of both halves to be the same, which is
1320 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001321 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1322 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001323 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1324 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001325 }
1326 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001327 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001328 case ISD::PCMARKER:
1329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001331 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001332 case ISD::STACKSAVE:
1333 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001334 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1335 Tmp1 = Result.getValue(0);
1336 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001337
Chris Lattnerb3266452006-01-13 02:50:02 +00001338 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1339 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001340 case TargetLowering::Legal: break;
1341 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001342 Tmp3 = TLI.LowerOperation(Result, DAG);
1343 if (Tmp3.Val) {
1344 Tmp1 = LegalizeOp(Tmp3);
1345 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001346 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001347 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001348 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001349 // Expand to CopyFromReg if the target set
1350 // StackPointerRegisterToSaveRestore.
1351 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001352 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001353 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001354 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001355 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001356 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1357 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001358 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001359 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001360 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001361
1362 // Since stacksave produce two values, make sure to remember that we
1363 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001364 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1365 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1366 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001367
Chris Lattnerb3266452006-01-13 02:50:02 +00001368 case ISD::STACKRESTORE:
1369 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1370 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001371 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001372
1373 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1374 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001375 case TargetLowering::Legal: break;
1376 case TargetLowering::Custom:
1377 Tmp1 = TLI.LowerOperation(Result, DAG);
1378 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001379 break;
1380 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001381 // Expand to CopyToReg if the target set
1382 // StackPointerRegisterToSaveRestore.
1383 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1384 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1385 } else {
1386 Result = Tmp1;
1387 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001388 break;
1389 }
1390 break;
1391
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001392 case ISD::READCYCLECOUNTER:
1393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001394 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001395
1396 // Since rdcc produce two values, make sure to remember that we legalized
1397 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001398 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001399 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001400 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001401
Evan Cheng31d15fa2005-12-23 07:29:34 +00001402 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001403 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1404 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1405
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001406 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1407 "Cannot handle illegal TRUNCSTORE yet!");
1408 Tmp2 = LegalizeOp(Node->getOperand(1));
1409
1410 // The only promote case we handle is TRUNCSTORE:i1 X into
1411 // -> TRUNCSTORE:i8 (and X, 1)
1412 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1413 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1414 TargetLowering::Promote) {
1415 // Promote the bool to a mask then store.
1416 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1417 DAG.getConstant(1, Tmp2.getValueType()));
1418 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1419 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001420
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001421 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1422 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001423 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1424 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001425 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001426
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001427 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1428 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1429 default: assert(0 && "This action is not supported yet!");
1430 case TargetLowering::Legal: break;
1431 case TargetLowering::Custom:
1432 Tmp1 = TLI.LowerOperation(Result, DAG);
1433 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001434 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001435 }
1436 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001437 }
Chris Lattner39c67442005-01-14 22:08:15 +00001438 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001439 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1440 case Expand: assert(0 && "It's impossible to expand bools");
1441 case Legal:
1442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1443 break;
1444 case Promote:
1445 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1446 break;
1447 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001448 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001449 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001450
Chris Lattnerd02b0542006-01-28 10:58:55 +00001451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001452
Nate Begeman987121a2005-08-23 04:29:48 +00001453 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001454 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001455 case TargetLowering::Legal: break;
1456 case TargetLowering::Custom: {
1457 Tmp1 = TLI.LowerOperation(Result, DAG);
1458 if (Tmp1.Val) Result = Tmp1;
1459 break;
1460 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001461 case TargetLowering::Expand:
1462 if (Tmp1.getOpcode() == ISD::SETCC) {
1463 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1464 Tmp2, Tmp3,
1465 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1466 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001467 // Make sure the condition is either zero or one. It may have been
1468 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001469 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1470 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1471 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001472 Result = DAG.getSelectCC(Tmp1,
1473 DAG.getConstant(0, Tmp1.getValueType()),
1474 Tmp2, Tmp3, ISD::SETNE);
1475 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001476 break;
1477 case TargetLowering::Promote: {
1478 MVT::ValueType NVT =
1479 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1480 unsigned ExtOp, TruncOp;
1481 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001482 ExtOp = ISD::ANY_EXTEND;
1483 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001484 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001485 ExtOp = ISD::FP_EXTEND;
1486 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001487 }
1488 // Promote each of the values to the new type.
1489 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1490 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1491 // Perform the larger operation, then round down.
1492 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1493 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1494 break;
1495 }
1496 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001497 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001498 case ISD::SELECT_CC: {
1499 Tmp1 = Node->getOperand(0); // LHS
1500 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001501 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1502 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001503 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001504
Nate Begeman7e7f4392006-02-01 07:19:44 +00001505 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1506
1507 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1508 // the LHS is a legal SETCC itself. In this case, we need to compare
1509 // the result against zero to select between true and false values.
1510 if (Tmp2.Val == 0) {
1511 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1512 CC = DAG.getCondCode(ISD::SETNE);
1513 }
1514 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1515
1516 // Everything is legal, see if we should expand this op or something.
1517 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1518 default: assert(0 && "This action is not supported yet!");
1519 case TargetLowering::Legal: break;
1520 case TargetLowering::Custom:
1521 Tmp1 = TLI.LowerOperation(Result, DAG);
1522 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001523 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001524 }
1525 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001526 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001527 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001528 Tmp1 = Node->getOperand(0);
1529 Tmp2 = Node->getOperand(1);
1530 Tmp3 = Node->getOperand(2);
1531 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1532
1533 // If we had to Expand the SetCC operands into a SELECT node, then it may
1534 // not always be possible to return a true LHS & RHS. In this case, just
1535 // return the value we legalized, returned in the LHS
1536 if (Tmp2.Val == 0) {
1537 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001538 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001539 }
Nate Begeman987121a2005-08-23 04:29:48 +00001540
Chris Lattnerf263a232006-01-30 22:43:50 +00001541 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001542 default: assert(0 && "Cannot handle this action for SETCC yet!");
1543 case TargetLowering::Custom:
1544 isCustom = true;
1545 // FALLTHROUGH.
1546 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001547 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001548 if (isCustom) {
1549 Tmp3 = TLI.LowerOperation(Result, DAG);
1550 if (Tmp3.Val) Result = Tmp3;
1551 }
Nate Begeman987121a2005-08-23 04:29:48 +00001552 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001553 case TargetLowering::Promote: {
1554 // First step, figure out the appropriate operation to use.
1555 // Allow SETCC to not be supported for all legal data types
1556 // Mostly this targets FP
1557 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1558 MVT::ValueType OldVT = NewInTy;
1559
1560 // Scan for the appropriate larger type to use.
1561 while (1) {
1562 NewInTy = (MVT::ValueType)(NewInTy+1);
1563
1564 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1565 "Fell off of the edge of the integer world");
1566 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1567 "Fell off of the edge of the floating point world");
1568
1569 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001570 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001571 break;
1572 }
1573 if (MVT::isInteger(NewInTy))
1574 assert(0 && "Cannot promote Legal Integer SETCC yet");
1575 else {
1576 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1577 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1578 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001579 Tmp1 = LegalizeOp(Tmp1);
1580 Tmp2 = LegalizeOp(Tmp2);
1581 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001582 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001583 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001584 }
Nate Begeman987121a2005-08-23 04:29:48 +00001585 case TargetLowering::Expand:
1586 // Expand a setcc node into a select_cc of the same condition, lhs, and
1587 // rhs that selects between const 1 (true) and const 0 (false).
1588 MVT::ValueType VT = Node->getValueType(0);
1589 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1590 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1591 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001592 break;
1593 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001594 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001595 case ISD::MEMSET:
1596 case ISD::MEMCPY:
1597 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001599 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1600
1601 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1602 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1603 case Expand: assert(0 && "Cannot expand a byte!");
1604 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001605 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001606 break;
1607 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001608 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001609 break;
1610 }
1611 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001612 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001613 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001614
1615 SDOperand Tmp4;
1616 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001617 case Expand: {
1618 // Length is too big, just take the lo-part of the length.
1619 SDOperand HiPart;
1620 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1621 break;
1622 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001623 case Legal:
1624 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001625 break;
1626 case Promote:
1627 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001628 break;
1629 }
1630
1631 SDOperand Tmp5;
1632 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1633 case Expand: assert(0 && "Cannot expand this yet!");
1634 case Legal:
1635 Tmp5 = LegalizeOp(Node->getOperand(4));
1636 break;
1637 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001638 Tmp5 = PromoteOp(Node->getOperand(4));
1639 break;
1640 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001641
1642 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1643 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001644 case TargetLowering::Custom:
1645 isCustom = true;
1646 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001647 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001648 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001649 if (isCustom) {
1650 Tmp1 = TLI.LowerOperation(Result, DAG);
1651 if (Tmp1.Val) Result = Tmp1;
1652 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001653 break;
1654 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001655 // Otherwise, the target does not support this operation. Lower the
1656 // operation to an explicit libcall as appropriate.
1657 MVT::ValueType IntPtr = TLI.getPointerTy();
1658 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1659 std::vector<std::pair<SDOperand, const Type*> > Args;
1660
Reid Spencer6dced922005-01-12 14:53:45 +00001661 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001662 if (Node->getOpcode() == ISD::MEMSET) {
1663 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1664 // Extend the ubyte argument to be an int value for the call.
1665 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1666 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1667 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1668
1669 FnName = "memset";
1670 } else if (Node->getOpcode() == ISD::MEMCPY ||
1671 Node->getOpcode() == ISD::MEMMOVE) {
1672 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1673 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1674 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1675 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1676 } else {
1677 assert(0 && "Unknown op!");
1678 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001679
Chris Lattner85d70c62005-01-11 05:57:22 +00001680 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001681 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001682 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001683 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001684 break;
1685 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001686 }
1687 break;
1688 }
Chris Lattner5385db52005-05-09 20:23:03 +00001689
1690 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001691 Tmp1 = LegalizeOp(Node->getOperand(0));
1692 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001693 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001694
Chris Lattner5385db52005-05-09 20:23:03 +00001695 // Since these produce two values, make sure to remember that we legalized
1696 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001697 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner5385db52005-05-09 20:23:03 +00001698 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001699 return Result;
Chris Lattner5385db52005-05-09 20:23:03 +00001700 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001701 Tmp1 = LegalizeOp(Node->getOperand(0));
1702 Tmp2 = LegalizeOp(Node->getOperand(1));
1703 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001704 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner5385db52005-05-09 20:23:03 +00001705 break;
1706
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001707 case ISD::READIO:
1708 Tmp1 = LegalizeOp(Node->getOperand(0));
1709 Tmp2 = LegalizeOp(Node->getOperand(1));
1710
1711 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1712 case TargetLowering::Custom:
1713 default: assert(0 && "This action not implemented for this operation!");
1714 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001715 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001716 break;
1717 case TargetLowering::Expand:
1718 // Replace this with a load from memory.
1719 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1720 Node->getOperand(1), DAG.getSrcValue(NULL));
1721 Result = LegalizeOp(Result);
1722 break;
1723 }
1724
1725 // Since these produce two values, make sure to remember that we legalized
1726 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001727 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001728 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1729 return Result.getValue(Op.ResNo);
1730
1731 case ISD::WRITEIO:
1732 Tmp1 = LegalizeOp(Node->getOperand(0));
1733 Tmp2 = LegalizeOp(Node->getOperand(1));
1734 Tmp3 = LegalizeOp(Node->getOperand(2));
1735
1736 switch (TLI.getOperationAction(Node->getOpcode(),
1737 Node->getOperand(1).getValueType())) {
1738 case TargetLowering::Custom:
1739 default: assert(0 && "This action not implemented for this operation!");
1740 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001741 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001742 break;
1743 case TargetLowering::Expand:
1744 // Replace this with a store to memory.
1745 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1746 Node->getOperand(1), Node->getOperand(2),
1747 DAG.getSrcValue(NULL));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001748 break;
1749 }
1750 break;
1751
Chris Lattner4157c412005-04-02 04:00:59 +00001752 case ISD::SHL_PARTS:
1753 case ISD::SRA_PARTS:
1754 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001755 std::vector<SDOperand> Ops;
1756 bool Changed = false;
1757 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1758 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1759 Changed |= Ops.back() != Node->getOperand(i);
1760 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001761 if (Changed)
1762 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001763
Evan Cheng870e4f82006-01-09 18:31:59 +00001764 switch (TLI.getOperationAction(Node->getOpcode(),
1765 Node->getValueType(0))) {
1766 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001767 case TargetLowering::Legal: break;
1768 case TargetLowering::Custom:
1769 Tmp1 = TLI.LowerOperation(Result, DAG);
1770 if (Tmp1.Val) {
1771 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001772 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001773 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001774 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1775 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001776 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001777 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001778 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001779 return RetVal;
1780 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001781 break;
1782 }
1783
Chris Lattner13fe99c2005-04-02 05:00:07 +00001784 // Since these produce multiple values, make sure to remember that we
1785 // legalized all of them.
1786 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1787 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1788 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001789 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001790
1791 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001792 case ISD::ADD:
1793 case ISD::SUB:
1794 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001795 case ISD::MULHS:
1796 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001797 case ISD::UDIV:
1798 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001799 case ISD::AND:
1800 case ISD::OR:
1801 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001802 case ISD::SHL:
1803 case ISD::SRL:
1804 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001805 case ISD::FADD:
1806 case ISD::FSUB:
1807 case ISD::FMUL:
1808 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001809 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001810 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1811 case Expand: assert(0 && "Not possible");
1812 case Legal:
1813 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1814 break;
1815 case Promote:
1816 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1817 break;
1818 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001819
1820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001821
Andrew Lenharth72594262005-12-24 23:42:32 +00001822 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001823 default: assert(0 && "Operation not supported");
1824 case TargetLowering::Legal: break;
1825 case TargetLowering::Custom:
1826 Tmp1 = TLI.LowerOperation(Result, DAG);
1827 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001828 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001829 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001830 break;
Nate Begeman5965bd12006-02-17 05:43:56 +00001831
1832 case ISD::ADDC:
1833 case ISD::SUBC:
1834 Tmp1 = LegalizeOp(Node->getOperand(0));
1835 Tmp2 = LegalizeOp(Node->getOperand(1));
1836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1837 // Since this produces two values, make sure to remember that we legalized
1838 // both of them.
1839 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1840 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1841 return Result;
1842 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001843
Nate Begeman5965bd12006-02-17 05:43:56 +00001844 case ISD::ADDE:
1845 case ISD::SUBE:
1846 Tmp1 = LegalizeOp(Node->getOperand(0));
1847 Tmp2 = LegalizeOp(Node->getOperand(1));
1848 Tmp3 = LegalizeOp(Node->getOperand(2));
1849 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1850 // Since this produces two values, make sure to remember that we legalized
1851 // both of them.
1852 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1853 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1854 return Result;
1855 break;
1856
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001857 case ISD::BUILD_PAIR: {
1858 MVT::ValueType PairTy = Node->getValueType(0);
1859 // TODO: handle the case where the Lo and Hi operands are not of legal type
1860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1861 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1862 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001863 case TargetLowering::Promote:
1864 case TargetLowering::Custom:
1865 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001866 case TargetLowering::Legal:
1867 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1868 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1869 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001870 case TargetLowering::Expand:
1871 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1872 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1873 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1874 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1875 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001876 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001877 break;
1878 }
1879 break;
1880 }
1881
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001882 case ISD::UREM:
1883 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001884 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001885 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1886 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001887
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001888 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001889 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1890 case TargetLowering::Custom:
1891 isCustom = true;
1892 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001893 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001894 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001895 if (isCustom) {
1896 Tmp1 = TLI.LowerOperation(Result, DAG);
1897 if (Tmp1.Val) Result = Tmp1;
1898 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001899 break;
Chris Lattner81914422005-08-03 20:31:37 +00001900 case TargetLowering::Expand:
1901 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001902 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00001903 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001904 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00001905 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1906 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1907 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1908 } else {
1909 // Floating point mod -> fmod libcall.
1910 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1911 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00001912 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001913 }
1914 break;
1915 }
1916 break;
Nate Begemane74795c2006-01-25 18:21:52 +00001917 case ISD::VAARG: {
1918 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1919 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1920
Chris Lattner364b89a2006-01-28 07:42:08 +00001921 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00001922 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1923 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001924 case TargetLowering::Custom:
1925 isCustom = true;
1926 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001927 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001928 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1929 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001930 Tmp1 = Result.getValue(1);
1931
1932 if (isCustom) {
1933 Tmp2 = TLI.LowerOperation(Result, DAG);
1934 if (Tmp2.Val) {
1935 Result = LegalizeOp(Tmp2);
1936 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1937 }
1938 }
Nate Begemane74795c2006-01-25 18:21:52 +00001939 break;
1940 case TargetLowering::Expand: {
1941 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1942 Node->getOperand(2));
1943 // Increment the pointer, VAList, to the next vaarg
1944 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1945 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1946 TLI.getPointerTy()));
1947 // Store the incremented VAList to the legalized pointer
1948 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1949 Node->getOperand(2));
1950 // Load the actual argument out of the pointer VAList
1951 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001952 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00001953 Result = LegalizeOp(Result);
1954 break;
1955 }
1956 }
1957 // Since VAARG produces two values, make sure to remember that we
1958 // legalized both of them.
1959 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001960 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1961 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00001962 }
1963
1964 case ISD::VACOPY:
1965 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1966 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1967 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1968
1969 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1970 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001971 case TargetLowering::Custom:
1972 isCustom = true;
1973 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001974 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1976 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001977 if (isCustom) {
1978 Tmp1 = TLI.LowerOperation(Result, DAG);
1979 if (Tmp1.Val) Result = Tmp1;
1980 }
Nate Begemane74795c2006-01-25 18:21:52 +00001981 break;
1982 case TargetLowering::Expand:
1983 // This defaults to loading a pointer from the input and storing it to the
1984 // output, returning the chain.
1985 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1986 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1987 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00001988 break;
1989 }
1990 break;
1991
1992 case ISD::VAEND:
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1994 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1995
1996 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1997 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001998 case TargetLowering::Custom:
1999 isCustom = true;
2000 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002001 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002002 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002003 if (isCustom) {
2004 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2005 if (Tmp1.Val) Result = Tmp1;
2006 }
Nate Begemane74795c2006-01-25 18:21:52 +00002007 break;
2008 case TargetLowering::Expand:
2009 Result = Tmp1; // Default to a no-op, return the chain
2010 break;
2011 }
2012 break;
2013
2014 case ISD::VASTART:
2015 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2016 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2017
Chris Lattnerd02b0542006-01-28 10:58:55 +00002018 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2019
Nate Begemane74795c2006-01-25 18:21:52 +00002020 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2021 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002022 case TargetLowering::Legal: break;
2023 case TargetLowering::Custom:
2024 Tmp1 = TLI.LowerOperation(Result, DAG);
2025 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002026 break;
2027 }
2028 break;
2029
Nate Begeman1b8121b2006-01-11 21:21:00 +00002030 case ISD::ROTL:
2031 case ISD::ROTR:
2032 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2033 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002034
2035 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2036 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002037 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002038 break;
2039
Nate Begeman2fba8a32006-01-14 03:14:10 +00002040 case ISD::BSWAP:
2041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2042 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002043 case TargetLowering::Custom:
2044 assert(0 && "Cannot custom legalize this yet!");
2045 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002046 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002047 break;
2048 case TargetLowering::Promote: {
2049 MVT::ValueType OVT = Tmp1.getValueType();
2050 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2051 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002052
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002053 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2054 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2055 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2056 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2057 break;
2058 }
2059 case TargetLowering::Expand:
2060 Result = ExpandBSWAP(Tmp1);
2061 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002062 }
2063 break;
2064
Andrew Lenharth5e177822005-05-03 17:19:30 +00002065 case ISD::CTPOP:
2066 case ISD::CTTZ:
2067 case ISD::CTLZ:
2068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2069 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002070 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002071 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002072 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002073 break;
2074 case TargetLowering::Promote: {
2075 MVT::ValueType OVT = Tmp1.getValueType();
2076 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002077
2078 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002079 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2080 // Perform the larger operation, then subtract if needed.
2081 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002082 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002083 case ISD::CTPOP:
2084 Result = Tmp1;
2085 break;
2086 case ISD::CTTZ:
2087 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002088 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2089 DAG.getConstant(getSizeInBits(NVT), NVT),
2090 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002091 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002092 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2093 break;
2094 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002095 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002096 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2097 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002098 getSizeInBits(OVT), NVT));
2099 break;
2100 }
2101 break;
2102 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002103 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002104 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002105 break;
2106 }
2107 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002108
Chris Lattner13fe99c2005-04-02 05:00:07 +00002109 // Unary operators
2110 case ISD::FABS:
2111 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002112 case ISD::FSQRT:
2113 case ISD::FSIN:
2114 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002115 Tmp1 = LegalizeOp(Node->getOperand(0));
2116 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002117 case TargetLowering::Promote:
2118 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002119 isCustom = true;
2120 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002121 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002122 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002123 if (isCustom) {
2124 Tmp1 = TLI.LowerOperation(Result, DAG);
2125 if (Tmp1.Val) Result = Tmp1;
2126 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002127 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002128 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002129 switch (Node->getOpcode()) {
2130 default: assert(0 && "Unreachable!");
2131 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002132 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2133 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002134 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002135 break;
Chris Lattner80026402005-04-30 04:43:14 +00002136 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002137 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2138 MVT::ValueType VT = Node->getValueType(0);
2139 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002140 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002141 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2142 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002143 break;
2144 }
2145 case ISD::FSQRT:
2146 case ISD::FSIN:
2147 case ISD::FCOS: {
2148 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002149 const char *FnName = 0;
2150 switch(Node->getOpcode()) {
2151 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2152 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2153 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2154 default: assert(0 && "Unreachable!");
2155 }
Nate Begeman77558da2005-08-04 21:43:28 +00002156 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002157 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002158 break;
2159 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002160 }
2161 break;
2162 }
2163 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002164
2165 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002166 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002167 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002168 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002169 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2170 Node->getOperand(0).getValueType())) {
2171 default: assert(0 && "Unknown operation action!");
2172 case TargetLowering::Expand:
2173 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2174 break;
2175 case TargetLowering::Legal:
2176 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002177 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002178 break;
2179 }
2180 }
2181 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002182 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002183 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002184 case ISD::UINT_TO_FP: {
2185 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002186 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2187 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002188 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002189 Node->getOperand(0).getValueType())) {
2190 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002191 case TargetLowering::Custom:
2192 isCustom = true;
2193 // FALLTHROUGH
2194 case TargetLowering::Legal:
2195 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002196 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002197 if (isCustom) {
2198 Tmp1 = TLI.LowerOperation(Result, DAG);
2199 if (Tmp1.Val) Result = Tmp1;
2200 }
2201 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002202 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002203 Result = ExpandLegalINT_TO_FP(isSigned,
2204 LegalizeOp(Node->getOperand(0)),
2205 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002206 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002207 case TargetLowering::Promote:
2208 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2209 Node->getValueType(0),
2210 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002211 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002212 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002213 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002214 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002215 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2216 Node->getValueType(0), Node->getOperand(0));
2217 break;
2218 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002219 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002220 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002221 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2222 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002223 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002224 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2225 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002226 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002227 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2228 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002229 break;
2230 }
2231 break;
2232 }
2233 case ISD::TRUNCATE:
2234 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2235 case Legal:
2236 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002237 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002238 break;
2239 case Expand:
2240 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2241
2242 // Since the result is legal, we should just be able to truncate the low
2243 // part of the source.
2244 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2245 break;
2246 case Promote:
2247 Result = PromoteOp(Node->getOperand(0));
2248 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2249 break;
2250 }
2251 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002252
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002253 case ISD::FP_TO_SINT:
2254 case ISD::FP_TO_UINT:
2255 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2256 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002257 Tmp1 = LegalizeOp(Node->getOperand(0));
2258
Chris Lattner44fe26f2005-07-29 00:11:56 +00002259 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2260 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002261 case TargetLowering::Custom:
2262 isCustom = true;
2263 // FALLTHROUGH
2264 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002265 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002266 if (isCustom) {
2267 Tmp1 = TLI.LowerOperation(Result, DAG);
2268 if (Tmp1.Val) Result = Tmp1;
2269 }
2270 break;
2271 case TargetLowering::Promote:
2272 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2273 Node->getOpcode() == ISD::FP_TO_SINT);
2274 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002275 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002276 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2277 SDOperand True, False;
2278 MVT::ValueType VT = Node->getOperand(0).getValueType();
2279 MVT::ValueType NVT = Node->getValueType(0);
2280 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2281 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2282 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2283 Node->getOperand(0), Tmp2, ISD::SETLT);
2284 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2285 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002286 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002287 Tmp2));
2288 False = DAG.getNode(ISD::XOR, NVT, False,
2289 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002290 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2291 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002292 } else {
2293 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2294 }
2295 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002296 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002297 break;
2298 case Expand:
2299 assert(0 && "Shouldn't need to expand other operators here!");
2300 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002301 Tmp1 = PromoteOp(Node->getOperand(0));
2302 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2303 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002304 break;
2305 }
2306 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002307
Chris Lattner7753f172005-09-02 00:18:10 +00002308 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002309 case ISD::ZERO_EXTEND:
2310 case ISD::SIGN_EXTEND:
2311 case ISD::FP_EXTEND:
2312 case ISD::FP_ROUND:
2313 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002314 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002315 case Legal:
2316 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002317 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002318 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002319 case Promote:
2320 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002321 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002322 Tmp1 = PromoteOp(Node->getOperand(0));
2323 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002324 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002325 case ISD::ZERO_EXTEND:
2326 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002327 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002328 Result = DAG.getZeroExtendInReg(Result,
2329 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002330 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002331 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002332 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002333 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002334 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002335 Result,
2336 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002337 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002338 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002339 Result = PromoteOp(Node->getOperand(0));
2340 if (Result.getValueType() != Op.getValueType())
2341 // Dynamically dead while we have only 2 FP types.
2342 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2343 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002344 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002345 Result = PromoteOp(Node->getOperand(0));
2346 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2347 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002348 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002349 }
2350 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002351 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002352 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002353 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002354 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002355
2356 // If this operation is not supported, convert it to a shl/shr or load/store
2357 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002358 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2359 default: assert(0 && "This action not supported for this op yet!");
2360 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002361 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002362 break;
2363 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002364 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002365 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002366 // NOTE: we could fall back on load/store here too for targets without
2367 // SAR. However, it is doubtful that any exist.
2368 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2369 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002370 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002371 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2372 Node->getOperand(0), ShiftCst);
2373 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2374 Result, ShiftCst);
2375 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2376 // The only way we can lower this is to turn it into a STORETRUNC,
2377 // EXTLOAD pair, targetting a temporary location (a stack slot).
2378
2379 // NOTE: there is a choice here between constantly creating new stack
2380 // slots and always reusing the same one. We currently always create
2381 // new ones, as reuse may inhibit scheduling.
2382 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2383 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2384 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2385 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002386 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002387 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2388 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2389 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002390 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002391 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002392 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2393 Result, StackSlot, DAG.getSrcValue(NULL),
2394 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002395 } else {
2396 assert(0 && "Unknown op");
2397 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002398 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002399 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002400 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002401 }
Chris Lattner99222f72005-01-15 07:15:18 +00002402 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002403
2404 // Make sure that the generated code is itself legal.
2405 if (Result != Op)
2406 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002407
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002408 // Note that LegalizeOp may be reentered even from single-use nodes, which
2409 // means that we always must cache transformed nodes.
2410 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002411 return Result;
2412}
2413
Chris Lattner4d978642005-01-15 22:16:26 +00002414/// PromoteOp - Given an operation that produces a value in an invalid type,
2415/// promote it to compute the value into a larger type. The produced value will
2416/// have the correct bits for the low portion of the register, but no guarantee
2417/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002418SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2419 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002420 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002421 assert(getTypeAction(VT) == Promote &&
2422 "Caller should expand or legalize operands that are not promotable!");
2423 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2424 "Cannot promote to smaller type!");
2425
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002426 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002427 SDOperand Result;
2428 SDNode *Node = Op.Val;
2429
Chris Lattner1a570f12005-09-02 20:32:45 +00002430 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2431 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002432
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002433 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002434 case ISD::CopyFromReg:
2435 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002436 default:
2437 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2438 assert(0 && "Do not know how to promote this operator!");
2439 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002440 case ISD::UNDEF:
2441 Result = DAG.getNode(ISD::UNDEF, NVT);
2442 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002443 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002444 if (VT != MVT::i1)
2445 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2446 else
2447 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002448 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2449 break;
2450 case ISD::ConstantFP:
2451 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2452 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2453 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002454
Chris Lattner2cb338d2005-01-18 02:59:52 +00002455 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002456 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002457 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2458 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002459 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002460
2461 case ISD::TRUNCATE:
2462 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2463 case Legal:
2464 Result = LegalizeOp(Node->getOperand(0));
2465 assert(Result.getValueType() >= NVT &&
2466 "This truncation doesn't make sense!");
2467 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2468 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2469 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002470 case Promote:
2471 // The truncation is not required, because we don't guarantee anything
2472 // about high bits anyway.
2473 Result = PromoteOp(Node->getOperand(0));
2474 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002475 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002476 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2477 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002478 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002479 }
2480 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002481 case ISD::SIGN_EXTEND:
2482 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002483 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002484 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2485 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2486 case Legal:
2487 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002488 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002489 break;
2490 case Promote:
2491 // Promote the reg if it's smaller.
2492 Result = PromoteOp(Node->getOperand(0));
2493 // The high bits are not guaranteed to be anything. Insert an extend.
2494 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002495 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002496 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002497 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002498 Result = DAG.getZeroExtendInReg(Result,
2499 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002500 break;
2501 }
2502 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002503 case ISD::BIT_CONVERT:
2504 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2505 Result = PromoteOp(Result);
2506 break;
2507
Chris Lattner4d978642005-01-15 22:16:26 +00002508 case ISD::FP_EXTEND:
2509 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2510 case ISD::FP_ROUND:
2511 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2512 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2513 case Promote: assert(0 && "Unreachable with 2 FP types!");
2514 case Legal:
2515 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002516 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002517 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002518 break;
2519 }
2520 break;
2521
2522 case ISD::SINT_TO_FP:
2523 case ISD::UINT_TO_FP:
2524 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2525 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002526 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002527 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002528 break;
2529
2530 case Promote:
2531 Result = PromoteOp(Node->getOperand(0));
2532 if (Node->getOpcode() == ISD::SINT_TO_FP)
2533 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002534 Result,
2535 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002536 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002537 Result = DAG.getZeroExtendInReg(Result,
2538 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002539 // No extra round required here.
2540 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002541 break;
2542 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002543 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2544 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002545 // Round if we cannot tolerate excess precision.
2546 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002547 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2548 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002549 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002550 }
Chris Lattner4d978642005-01-15 22:16:26 +00002551 break;
2552
Chris Lattner268d4572005-12-09 17:32:47 +00002553 case ISD::SIGN_EXTEND_INREG:
2554 Result = PromoteOp(Node->getOperand(0));
2555 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2556 Node->getOperand(1));
2557 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002558 case ISD::FP_TO_SINT:
2559 case ISD::FP_TO_UINT:
2560 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2561 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002562 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002563 break;
2564 case Promote:
2565 // The input result is prerounded, so we don't have to do anything
2566 // special.
2567 Tmp1 = PromoteOp(Node->getOperand(0));
2568 break;
2569 case Expand:
2570 assert(0 && "not implemented");
2571 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002572 // If we're promoting a UINT to a larger size, check to see if the new node
2573 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2574 // we can use that instead. This allows us to generate better code for
2575 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2576 // legal, such as PowerPC.
2577 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002578 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002579 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2580 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002581 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2582 } else {
2583 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2584 }
Chris Lattner4d978642005-01-15 22:16:26 +00002585 break;
2586
Chris Lattner13fe99c2005-04-02 05:00:07 +00002587 case ISD::FABS:
2588 case ISD::FNEG:
2589 Tmp1 = PromoteOp(Node->getOperand(0));
2590 assert(Tmp1.getValueType() == NVT);
2591 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2592 // NOTE: we do not have to do any extra rounding here for
2593 // NoExcessFPPrecision, because we know the input will have the appropriate
2594 // precision, and these operations don't modify precision at all.
2595 break;
2596
Chris Lattner9d6fa982005-04-28 21:44:33 +00002597 case ISD::FSQRT:
2598 case ISD::FSIN:
2599 case ISD::FCOS:
2600 Tmp1 = PromoteOp(Node->getOperand(0));
2601 assert(Tmp1.getValueType() == NVT);
2602 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002603 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002604 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2605 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002606 break;
2607
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002608 case ISD::AND:
2609 case ISD::OR:
2610 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002611 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002612 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002613 case ISD::MUL:
2614 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002615 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002616 // that too is okay if they are integer operations.
2617 Tmp1 = PromoteOp(Node->getOperand(0));
2618 Tmp2 = PromoteOp(Node->getOperand(1));
2619 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2620 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002621 break;
2622 case ISD::FADD:
2623 case ISD::FSUB:
2624 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002625 Tmp1 = PromoteOp(Node->getOperand(0));
2626 Tmp2 = PromoteOp(Node->getOperand(1));
2627 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2628 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2629
2630 // Floating point operations will give excess precision that we may not be
2631 // able to tolerate. If we DO allow excess precision, just leave it,
2632 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002633 // FIXME: Why would we need to round FP ops more than integer ones?
2634 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002635 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002636 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2637 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002638 break;
2639
Chris Lattner4d978642005-01-15 22:16:26 +00002640 case ISD::SDIV:
2641 case ISD::SREM:
2642 // These operators require that their input be sign extended.
2643 Tmp1 = PromoteOp(Node->getOperand(0));
2644 Tmp2 = PromoteOp(Node->getOperand(1));
2645 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002646 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2647 DAG.getValueType(VT));
2648 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2649 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002650 }
2651 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2652
2653 // Perform FP_ROUND: this is probably overly pessimistic.
2654 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002655 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2656 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002657 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002658 case ISD::FDIV:
2659 case ISD::FREM:
2660 // These operators require that their input be fp extended.
2661 Tmp1 = PromoteOp(Node->getOperand(0));
2662 Tmp2 = PromoteOp(Node->getOperand(1));
2663 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2664
2665 // Perform FP_ROUND: this is probably overly pessimistic.
2666 if (NoExcessFPPrecision)
2667 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2668 DAG.getValueType(VT));
2669 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002670
2671 case ISD::UDIV:
2672 case ISD::UREM:
2673 // These operators require that their input be zero extended.
2674 Tmp1 = PromoteOp(Node->getOperand(0));
2675 Tmp2 = PromoteOp(Node->getOperand(1));
2676 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002677 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2678 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002679 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2680 break;
2681
2682 case ISD::SHL:
2683 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002684 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002685 break;
2686 case ISD::SRA:
2687 // The input value must be properly sign extended.
2688 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002689 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2690 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002691 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002692 break;
2693 case ISD::SRL:
2694 // The input value must be properly zero extended.
2695 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002696 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002697 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002698 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002699
2700 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002701 Tmp1 = Node->getOperand(0); // Get the chain.
2702 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002703 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2704 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2705 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2706 } else {
2707 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2708 Node->getOperand(2));
2709 // Increment the pointer, VAList, to the next vaarg
2710 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2711 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2712 TLI.getPointerTy()));
2713 // Store the incremented VAList to the legalized pointer
2714 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2715 Node->getOperand(2));
2716 // Load the actual argument out of the pointer VAList
2717 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2718 DAG.getSrcValue(0), VT);
2719 }
2720 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002721 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002722 break;
2723
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002724 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002725 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2726 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002727 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002728 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002729 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002730 case ISD::SEXTLOAD:
2731 case ISD::ZEXTLOAD:
2732 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002733 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2734 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002735 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002736 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002737 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002738 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002739 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002740 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2741 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002742 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002743 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002744 case ISD::SELECT_CC:
2745 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2746 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2747 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002748 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002749 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002750 case ISD::BSWAP:
2751 Tmp1 = Node->getOperand(0);
2752 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2753 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2754 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2755 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2756 TLI.getShiftAmountTy()));
2757 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002758 case ISD::CTPOP:
2759 case ISD::CTTZ:
2760 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002761 // Zero extend the argument
2762 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002763 // Perform the larger operation, then subtract if needed.
2764 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002765 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002766 case ISD::CTPOP:
2767 Result = Tmp1;
2768 break;
2769 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002770 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002771 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002772 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002773 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002774 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002775 break;
2776 case ISD::CTLZ:
2777 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002778 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2779 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002780 getSizeInBits(VT), NVT));
2781 break;
2782 }
2783 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002784 }
2785
2786 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002787
2788 // Make sure the result is itself legal.
2789 Result = LegalizeOp(Result);
2790
2791 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002792 AddPromotedOperand(Op, Result);
2793 return Result;
2794}
Chris Lattnerdc750592005-01-07 07:47:09 +00002795
Nate Begeman7e7f4392006-02-01 07:19:44 +00002796/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2797/// with condition CC on the current target. This usually involves legalizing
2798/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2799/// there may be no choice but to create a new SetCC node to represent the
2800/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2801/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2802void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2803 SDOperand &RHS,
2804 SDOperand &CC) {
2805 SDOperand Tmp1, Tmp2, Result;
2806
2807 switch (getTypeAction(LHS.getValueType())) {
2808 case Legal:
2809 Tmp1 = LegalizeOp(LHS); // LHS
2810 Tmp2 = LegalizeOp(RHS); // RHS
2811 break;
2812 case Promote:
2813 Tmp1 = PromoteOp(LHS); // LHS
2814 Tmp2 = PromoteOp(RHS); // RHS
2815
2816 // If this is an FP compare, the operands have already been extended.
2817 if (MVT::isInteger(LHS.getValueType())) {
2818 MVT::ValueType VT = LHS.getValueType();
2819 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2820
2821 // Otherwise, we have to insert explicit sign or zero extends. Note
2822 // that we could insert sign extends for ALL conditions, but zero extend
2823 // is cheaper on many machines (an AND instead of two shifts), so prefer
2824 // it.
2825 switch (cast<CondCodeSDNode>(CC)->get()) {
2826 default: assert(0 && "Unknown integer comparison!");
2827 case ISD::SETEQ:
2828 case ISD::SETNE:
2829 case ISD::SETUGE:
2830 case ISD::SETUGT:
2831 case ISD::SETULE:
2832 case ISD::SETULT:
2833 // ALL of these operations will work if we either sign or zero extend
2834 // the operands (including the unsigned comparisons!). Zero extend is
2835 // usually a simpler/cheaper operation, so prefer it.
2836 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2837 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2838 break;
2839 case ISD::SETGE:
2840 case ISD::SETGT:
2841 case ISD::SETLT:
2842 case ISD::SETLE:
2843 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2844 DAG.getValueType(VT));
2845 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2846 DAG.getValueType(VT));
2847 break;
2848 }
2849 }
2850 break;
2851 case Expand:
2852 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2853 ExpandOp(LHS, LHSLo, LHSHi);
2854 ExpandOp(RHS, RHSLo, RHSHi);
2855 switch (cast<CondCodeSDNode>(CC)->get()) {
2856 case ISD::SETEQ:
2857 case ISD::SETNE:
2858 if (RHSLo == RHSHi)
2859 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2860 if (RHSCST->isAllOnesValue()) {
2861 // Comparison to -1.
2862 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2863 Tmp2 = RHSLo;
2864 break;
2865 }
2866
2867 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2868 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2869 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2870 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2871 break;
2872 default:
2873 // If this is a comparison of the sign bit, just look at the top part.
2874 // X > -1, x < 0
2875 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2876 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2877 CST->getValue() == 0) || // X < 0
2878 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2879 CST->isAllOnesValue())) { // X > -1
2880 Tmp1 = LHSHi;
2881 Tmp2 = RHSHi;
2882 break;
2883 }
2884
2885 // FIXME: This generated code sucks.
2886 ISD::CondCode LowCC;
2887 switch (cast<CondCodeSDNode>(CC)->get()) {
2888 default: assert(0 && "Unknown integer setcc!");
2889 case ISD::SETLT:
2890 case ISD::SETULT: LowCC = ISD::SETULT; break;
2891 case ISD::SETGT:
2892 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2893 case ISD::SETLE:
2894 case ISD::SETULE: LowCC = ISD::SETULE; break;
2895 case ISD::SETGE:
2896 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2897 }
2898
2899 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2900 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2901 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2902
2903 // NOTE: on targets without efficient SELECT of bools, we can always use
2904 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2905 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2906 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2907 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2908 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2909 Result, Tmp1, Tmp2));
2910 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00002911 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00002912 }
2913 }
2914 LHS = Tmp1;
2915 RHS = Tmp2;
2916}
2917
Chris Lattner36e663d2005-12-23 00:16:34 +00002918/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002919/// The resultant code need not be legal. Note that SrcOp is the input operand
2920/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002921SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2922 SDOperand SrcOp) {
2923 // Create the stack frame object.
2924 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2925 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002926 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002927 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2928
2929 // Emit a store to the stack slot.
2930 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002931 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002932 // Result is a load from the stack slot.
2933 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2934}
2935
Chris Lattner4157c412005-04-02 04:00:59 +00002936void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2937 SDOperand Op, SDOperand Amt,
2938 SDOperand &Lo, SDOperand &Hi) {
2939 // Expand the subcomponents.
2940 SDOperand LHSL, LHSH;
2941 ExpandOp(Op, LHSL, LHSH);
2942
2943 std::vector<SDOperand> Ops;
2944 Ops.push_back(LHSL);
2945 Ops.push_back(LHSH);
2946 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002947 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002948 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002949 Hi = Lo.getValue(1);
2950}
2951
2952
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002953/// ExpandShift - Try to find a clever way to expand this shift operation out to
2954/// smaller elements. If we can't find a way that is more efficient than a
2955/// libcall on this target, return false. Otherwise, return true with the
2956/// low-parts expanded into Lo and Hi.
2957bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2958 SDOperand &Lo, SDOperand &Hi) {
2959 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2960 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002961
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002962 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002963 SDOperand ShAmt = LegalizeOp(Amt);
2964 MVT::ValueType ShTy = ShAmt.getValueType();
2965 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2966 unsigned NVTBits = MVT::getSizeInBits(NVT);
2967
2968 // Handle the case when Amt is an immediate. Other cases are currently broken
2969 // and are disabled.
2970 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2971 unsigned Cst = CN->getValue();
2972 // Expand the incoming operand to be shifted, so that we have its parts
2973 SDOperand InL, InH;
2974 ExpandOp(Op, InL, InH);
2975 switch(Opc) {
2976 case ISD::SHL:
2977 if (Cst > VTBits) {
2978 Lo = DAG.getConstant(0, NVT);
2979 Hi = DAG.getConstant(0, NVT);
2980 } else if (Cst > NVTBits) {
2981 Lo = DAG.getConstant(0, NVT);
2982 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002983 } else if (Cst == NVTBits) {
2984 Lo = DAG.getConstant(0, NVT);
2985 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002986 } else {
2987 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2988 Hi = DAG.getNode(ISD::OR, NVT,
2989 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2990 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2991 }
2992 return true;
2993 case ISD::SRL:
2994 if (Cst > VTBits) {
2995 Lo = DAG.getConstant(0, NVT);
2996 Hi = DAG.getConstant(0, NVT);
2997 } else if (Cst > NVTBits) {
2998 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2999 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003000 } else if (Cst == NVTBits) {
3001 Lo = InH;
3002 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003003 } else {
3004 Lo = DAG.getNode(ISD::OR, NVT,
3005 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3006 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3007 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3008 }
3009 return true;
3010 case ISD::SRA:
3011 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003012 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003013 DAG.getConstant(NVTBits-1, ShTy));
3014 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003015 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003016 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003017 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003018 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003019 } else if (Cst == NVTBits) {
3020 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003021 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003022 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003023 } else {
3024 Lo = DAG.getNode(ISD::OR, NVT,
3025 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3026 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3027 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3028 }
3029 return true;
3030 }
3031 }
Nate Begemanb0674922005-04-06 21:13:14 +00003032 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003033}
Chris Lattneraac464e2005-01-21 06:05:23 +00003034
Chris Lattner4add7e32005-01-23 04:42:50 +00003035
Chris Lattneraac464e2005-01-21 06:05:23 +00003036// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3037// does not fit into a register, return the lo part and set the hi part to the
3038// by-reg argument. If it does fit into a single register, return the result
3039// and leave the Hi part unset.
3040SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3041 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003042 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3043 // The input chain to this libcall is the entry node of the function.
3044 // Legalizing the call will automatically add the previous call to the
3045 // dependence.
3046 SDOperand InChain = DAG.getEntryNode();
3047
Chris Lattneraac464e2005-01-21 06:05:23 +00003048 TargetLowering::ArgListTy Args;
3049 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3050 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3051 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3052 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3053 }
3054 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003055
Chris Lattner06bbeb62005-05-11 19:02:11 +00003056 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003057 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003058 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003059 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3060 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003061
Chris Lattner462505f2006-02-13 09:18:02 +00003062 // Legalize the call sequence, starting with the chain. This will advance
3063 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3064 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3065 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003066 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003067 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003068 default: assert(0 && "Unknown thing");
3069 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003070 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003071 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003072 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003073 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003074 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003075 }
Chris Lattner63022662005-09-02 20:26:58 +00003076 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003077}
3078
Chris Lattner4add7e32005-01-23 04:42:50 +00003079
Chris Lattneraac464e2005-01-21 06:05:23 +00003080/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3081/// destination type is legal.
3082SDOperand SelectionDAGLegalize::
3083ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003084 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003085 assert(getTypeAction(Source.getValueType()) == Expand &&
3086 "This is not an expansion!");
3087 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3088
Chris Lattner06bbeb62005-05-11 19:02:11 +00003089 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003090 assert(Source.getValueType() == MVT::i64 &&
3091 "This only works for 64-bit -> FP");
3092 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3093 // incoming integer is set. To handle this, we dynamically test to see if
3094 // it is set, and, if so, add a fudge factor.
3095 SDOperand Lo, Hi;
3096 ExpandOp(Source, Lo, Hi);
3097
Chris Lattner2a4f7312005-05-13 04:45:13 +00003098 // If this is unsigned, and not supported, first perform the conversion to
3099 // signed, then adjust the result if the sign bit is set.
3100 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3101 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3102
Chris Lattnerd47675e2005-08-09 20:20:18 +00003103 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3104 DAG.getConstant(0, Hi.getValueType()),
3105 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003106 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3107 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3108 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003109 uint64_t FF = 0x5f800000ULL;
3110 if (TLI.isLittleEndian()) FF <<= 32;
3111 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003112
Chris Lattnerc30405e2005-08-26 17:15:30 +00003113 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003114 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3115 SDOperand FudgeInReg;
3116 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003117 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3118 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003119 else {
3120 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003121 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3122 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003123 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003124 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003125 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003126
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003127 // Check to see if the target has a custom way to lower this. If so, use it.
3128 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3129 default: assert(0 && "This action not implemented for this operation!");
3130 case TargetLowering::Legal:
3131 case TargetLowering::Expand:
3132 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003133 case TargetLowering::Custom: {
3134 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3135 Source), DAG);
3136 if (NV.Val)
3137 return LegalizeOp(NV);
3138 break; // The target decided this was legal after all
3139 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003140 }
3141
Chris Lattner153587e2005-05-12 07:00:44 +00003142 // Expand the source, then glue it back together for the call. We must expand
3143 // the source in case it is shared (this pass of legalize must traverse it).
3144 SDOperand SrcLo, SrcHi;
3145 ExpandOp(Source, SrcLo, SrcHi);
3146 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3147
Chris Lattner06bbeb62005-05-11 19:02:11 +00003148 const char *FnName = 0;
3149 if (DestTy == MVT::f32)
3150 FnName = "__floatdisf";
3151 else {
3152 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3153 FnName = "__floatdidf";
3154 }
Chris Lattner462505f2006-02-13 09:18:02 +00003155
3156 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3157 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003158 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003159}
Misha Brukman835702a2005-04-21 22:36:52 +00003160
Chris Lattner689bdcc2006-01-28 08:25:58 +00003161/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3162/// INT_TO_FP operation of the specified operand when the target requests that
3163/// we expand it. At this point, we know that the result and operand types are
3164/// legal for the target.
3165SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3166 SDOperand Op0,
3167 MVT::ValueType DestVT) {
3168 if (Op0.getValueType() == MVT::i32) {
3169 // simple 32-bit [signed|unsigned] integer to float/double expansion
3170
3171 // get the stack frame index of a 8 byte buffer
3172 MachineFunction &MF = DAG.getMachineFunction();
3173 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3174 // get address of 8 byte buffer
3175 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3176 // word offset constant for Hi/Lo address computation
3177 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3178 // set up Hi and Lo (into buffer) address based on endian
3179 SDOperand Hi, Lo;
3180 if (TLI.isLittleEndian()) {
3181 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3182 Lo = StackSlot;
3183 } else {
3184 Hi = StackSlot;
3185 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3186 }
3187 // if signed map to unsigned space
3188 SDOperand Op0Mapped;
3189 if (isSigned) {
3190 // constant used to invert sign bit (signed to unsigned mapping)
3191 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3192 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3193 } else {
3194 Op0Mapped = Op0;
3195 }
3196 // store the lo of the constructed double - based on integer input
3197 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3198 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3199 // initial hi portion of constructed double
3200 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3201 // store the hi of the constructed double - biased exponent
3202 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3203 InitialHi, Hi, DAG.getSrcValue(NULL));
3204 // load the constructed double
3205 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3206 DAG.getSrcValue(NULL));
3207 // FP constant to bias correct the final result
3208 SDOperand Bias = DAG.getConstantFP(isSigned ?
3209 BitsToDouble(0x4330000080000000ULL)
3210 : BitsToDouble(0x4330000000000000ULL),
3211 MVT::f64);
3212 // subtract the bias
3213 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3214 // final result
3215 SDOperand Result;
3216 // handle final rounding
3217 if (DestVT == MVT::f64) {
3218 // do nothing
3219 Result = Sub;
3220 } else {
3221 // if f32 then cast to f32
3222 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3223 }
3224 return Result;
3225 }
3226 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3227 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3228
3229 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3230 DAG.getConstant(0, Op0.getValueType()),
3231 ISD::SETLT);
3232 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3233 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3234 SignSet, Four, Zero);
3235
3236 // If the sign bit of the integer is set, the large number will be treated
3237 // as a negative number. To counteract this, the dynamic code adds an
3238 // offset depending on the data type.
3239 uint64_t FF;
3240 switch (Op0.getValueType()) {
3241 default: assert(0 && "Unsupported integer type!");
3242 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3243 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3244 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3245 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3246 }
3247 if (TLI.isLittleEndian()) FF <<= 32;
3248 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3249
3250 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3251 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3252 SDOperand FudgeInReg;
3253 if (DestVT == MVT::f32)
3254 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3255 DAG.getSrcValue(NULL));
3256 else {
3257 assert(DestVT == MVT::f64 && "Unexpected conversion");
3258 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3259 DAG.getEntryNode(), CPIdx,
3260 DAG.getSrcValue(NULL), MVT::f32));
3261 }
3262
3263 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3264}
3265
3266/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3267/// *INT_TO_FP operation of the specified operand when the target requests that
3268/// we promote it. At this point, we know that the result and operand types are
3269/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3270/// operation that takes a larger input.
3271SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3272 MVT::ValueType DestVT,
3273 bool isSigned) {
3274 // First step, figure out the appropriate *INT_TO_FP operation to use.
3275 MVT::ValueType NewInTy = LegalOp.getValueType();
3276
3277 unsigned OpToUse = 0;
3278
3279 // Scan for the appropriate larger type to use.
3280 while (1) {
3281 NewInTy = (MVT::ValueType)(NewInTy+1);
3282 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3283
3284 // If the target supports SINT_TO_FP of this type, use it.
3285 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3286 default: break;
3287 case TargetLowering::Legal:
3288 if (!TLI.isTypeLegal(NewInTy))
3289 break; // Can't use this datatype.
3290 // FALL THROUGH.
3291 case TargetLowering::Custom:
3292 OpToUse = ISD::SINT_TO_FP;
3293 break;
3294 }
3295 if (OpToUse) break;
3296 if (isSigned) continue;
3297
3298 // If the target supports UINT_TO_FP of this type, use it.
3299 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3300 default: break;
3301 case TargetLowering::Legal:
3302 if (!TLI.isTypeLegal(NewInTy))
3303 break; // Can't use this datatype.
3304 // FALL THROUGH.
3305 case TargetLowering::Custom:
3306 OpToUse = ISD::UINT_TO_FP;
3307 break;
3308 }
3309 if (OpToUse) break;
3310
3311 // Otherwise, try a larger type.
3312 }
3313
3314 // Okay, we found the operation and type to use. Zero extend our input to the
3315 // desired type then run the operation on it.
3316 return DAG.getNode(OpToUse, DestVT,
3317 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3318 NewInTy, LegalOp));
3319}
3320
3321/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3322/// FP_TO_*INT operation of the specified operand when the target requests that
3323/// we promote it. At this point, we know that the result and operand types are
3324/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3325/// operation that returns a larger result.
3326SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3327 MVT::ValueType DestVT,
3328 bool isSigned) {
3329 // First step, figure out the appropriate FP_TO*INT operation to use.
3330 MVT::ValueType NewOutTy = DestVT;
3331
3332 unsigned OpToUse = 0;
3333
3334 // Scan for the appropriate larger type to use.
3335 while (1) {
3336 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3337 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3338
3339 // If the target supports FP_TO_SINT returning this type, use it.
3340 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3341 default: break;
3342 case TargetLowering::Legal:
3343 if (!TLI.isTypeLegal(NewOutTy))
3344 break; // Can't use this datatype.
3345 // FALL THROUGH.
3346 case TargetLowering::Custom:
3347 OpToUse = ISD::FP_TO_SINT;
3348 break;
3349 }
3350 if (OpToUse) break;
3351
3352 // If the target supports FP_TO_UINT of this type, use it.
3353 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3354 default: break;
3355 case TargetLowering::Legal:
3356 if (!TLI.isTypeLegal(NewOutTy))
3357 break; // Can't use this datatype.
3358 // FALL THROUGH.
3359 case TargetLowering::Custom:
3360 OpToUse = ISD::FP_TO_UINT;
3361 break;
3362 }
3363 if (OpToUse) break;
3364
3365 // Otherwise, try a larger type.
3366 }
3367
3368 // Okay, we found the operation and type to use. Truncate the result of the
3369 // extended FP_TO_*INT operation to the desired size.
3370 return DAG.getNode(ISD::TRUNCATE, DestVT,
3371 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3372}
3373
3374/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3375///
3376SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3377 MVT::ValueType VT = Op.getValueType();
3378 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3379 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3380 switch (VT) {
3381 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3382 case MVT::i16:
3383 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3384 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3385 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3386 case MVT::i32:
3387 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3388 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3389 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3390 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3391 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3392 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3393 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3394 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3395 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3396 case MVT::i64:
3397 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3398 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3399 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3400 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3401 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3402 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3403 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3404 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3405 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3406 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3407 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3408 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3409 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3410 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3411 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3412 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3413 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3414 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3415 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3416 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3417 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3418 }
3419}
3420
3421/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3422///
3423SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3424 switch (Opc) {
3425 default: assert(0 && "Cannot expand this yet!");
3426 case ISD::CTPOP: {
3427 static const uint64_t mask[6] = {
3428 0x5555555555555555ULL, 0x3333333333333333ULL,
3429 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3430 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3431 };
3432 MVT::ValueType VT = Op.getValueType();
3433 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3434 unsigned len = getSizeInBits(VT);
3435 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3436 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3437 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3438 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3439 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3440 DAG.getNode(ISD::AND, VT,
3441 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3442 }
3443 return Op;
3444 }
3445 case ISD::CTLZ: {
3446 // for now, we do this:
3447 // x = x | (x >> 1);
3448 // x = x | (x >> 2);
3449 // ...
3450 // x = x | (x >>16);
3451 // x = x | (x >>32); // for 64-bit input
3452 // return popcount(~x);
3453 //
3454 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3455 MVT::ValueType VT = Op.getValueType();
3456 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3457 unsigned len = getSizeInBits(VT);
3458 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3459 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3460 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3461 }
3462 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3463 return DAG.getNode(ISD::CTPOP, VT, Op);
3464 }
3465 case ISD::CTTZ: {
3466 // for now, we use: { return popcount(~x & (x - 1)); }
3467 // unless the target has ctlz but not ctpop, in which case we use:
3468 // { return 32 - nlz(~x & (x-1)); }
3469 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3470 MVT::ValueType VT = Op.getValueType();
3471 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3472 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3473 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3474 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3475 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3476 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3477 TLI.isOperationLegal(ISD::CTLZ, VT))
3478 return DAG.getNode(ISD::SUB, VT,
3479 DAG.getConstant(getSizeInBits(VT), VT),
3480 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3481 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3482 }
3483 }
3484}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003485
3486
Chris Lattnerdc750592005-01-07 07:47:09 +00003487/// ExpandOp - Expand the specified SDOperand into its two component pieces
3488/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3489/// LegalizeNodes map is filled in for any results that are not expanded, the
3490/// ExpandedNodes map is filled in for any results that are expanded, and the
3491/// Lo/Hi values are returned.
3492void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3493 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003494 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003495 SDNode *Node = Op.Val;
3496 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003497 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3498 "Cannot expand FP values!");
3499 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003500 "Cannot expand to FP value or to larger int value!");
3501
Chris Lattner1a570f12005-09-02 20:32:45 +00003502 // See if we already expanded it.
3503 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3504 = ExpandedNodes.find(Op);
3505 if (I != ExpandedNodes.end()) {
3506 Lo = I->second.first;
3507 Hi = I->second.second;
3508 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003509 }
3510
Chris Lattnerdc750592005-01-07 07:47:09 +00003511 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003512 case ISD::CopyFromReg:
3513 assert(0 && "CopyFromReg must be legal!");
3514 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003515 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3516 assert(0 && "Do not know how to expand this operator!");
3517 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003518 case ISD::UNDEF:
3519 Lo = DAG.getNode(ISD::UNDEF, NVT);
3520 Hi = DAG.getNode(ISD::UNDEF, NVT);
3521 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003522 case ISD::Constant: {
3523 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3524 Lo = DAG.getConstant(Cst, NVT);
3525 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3526 break;
3527 }
Nate Begemanae89d862005-12-07 19:48:11 +00003528 case ISD::ConstantVec: {
3529 unsigned NumElements = Node->getNumOperands();
3530 // If we only have two elements left in the constant vector, just break it
3531 // apart into the two scalar constants it contains. Otherwise, bisect the
3532 // ConstantVec, and return each half as a new ConstantVec.
3533 // FIXME: this is hard coded as big endian, it may have to change to support
3534 // SSE and Alpha MVI
3535 if (NumElements == 2) {
3536 Hi = Node->getOperand(0);
3537 Lo = Node->getOperand(1);
3538 } else {
3539 NumElements /= 2;
3540 std::vector<SDOperand> LoOps, HiOps;
3541 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3542 HiOps.push_back(Node->getOperand(I));
3543 LoOps.push_back(Node->getOperand(I+NumElements));
3544 }
3545 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3546 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3547 }
3548 break;
3549 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003550
Chris Lattner32e08b72005-03-28 22:03:13 +00003551 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003552 // Return the operands.
3553 Lo = Node->getOperand(0);
3554 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003555 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003556
3557 case ISD::SIGN_EXTEND_INREG:
3558 ExpandOp(Node->getOperand(0), Lo, Hi);
3559 // Sign extend the lo-part.
3560 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3561 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3562 TLI.getShiftAmountTy()));
3563 // sext_inreg the low part if needed.
3564 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3565 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003566
Nate Begeman2fba8a32006-01-14 03:14:10 +00003567 case ISD::BSWAP: {
3568 ExpandOp(Node->getOperand(0), Lo, Hi);
3569 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3570 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3571 Lo = TempLo;
3572 break;
3573 }
3574
Chris Lattner55e9cde2005-05-11 04:51:16 +00003575 case ISD::CTPOP:
3576 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003577 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3578 DAG.getNode(ISD::CTPOP, NVT, Lo),
3579 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003580 Hi = DAG.getConstant(0, NVT);
3581 break;
3582
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003583 case ISD::CTLZ: {
3584 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003585 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003586 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3587 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003588 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3589 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003590 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3591 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3592
3593 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3594 Hi = DAG.getConstant(0, NVT);
3595 break;
3596 }
3597
3598 case ISD::CTTZ: {
3599 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003600 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003601 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3602 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003603 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3604 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003605 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3606 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3607
3608 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3609 Hi = DAG.getConstant(0, NVT);
3610 break;
3611 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003612
Nate Begemane74795c2006-01-25 18:21:52 +00003613 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003614 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3615 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003616 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3617 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3618
3619 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003620 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003621 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3622 if (!TLI.isLittleEndian())
3623 std::swap(Lo, Hi);
3624 break;
3625 }
3626
Chris Lattnerdc750592005-01-07 07:47:09 +00003627 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003628 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3629 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003630 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003631
3632 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003633 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003634 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3635 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003636 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003637 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003638
3639 // Build a factor node to remember that this load is independent of the
3640 // other one.
3641 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3642 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003643
Chris Lattnerdc750592005-01-07 07:47:09 +00003644 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003645 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003646 if (!TLI.isLittleEndian())
3647 std::swap(Lo, Hi);
3648 break;
3649 }
Nate Begemand37c1312005-11-22 18:16:00 +00003650 case ISD::VLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003651 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3652 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemand37c1312005-11-22 18:16:00 +00003653 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3654 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3655
3656 // If we only have two elements, turn into a pair of scalar loads.
3657 // FIXME: handle case where a vector of two elements is fine, such as
3658 // 2 x double on SSE2.
3659 if (NumElements == 2) {
3660 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3661 // Increment the pointer to the other half.
3662 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3663 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3664 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003665 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003666 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3667 } else {
3668 NumElements /= 2; // Split the vector in half
3669 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3670 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3671 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3672 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003673 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003674 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3675 }
3676
3677 // Build a factor node to remember that this load is independent of the
3678 // other one.
3679 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3680 Hi.getValue(1));
3681
3682 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003683 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemand37c1312005-11-22 18:16:00 +00003684 if (!TLI.isLittleEndian())
3685 std::swap(Lo, Hi);
3686 break;
3687 }
3688 case ISD::VADD:
3689 case ISD::VSUB:
3690 case ISD::VMUL: {
3691 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3692 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3693 SDOperand LL, LH, RL, RH;
3694
3695 ExpandOp(Node->getOperand(0), LL, LH);
3696 ExpandOp(Node->getOperand(1), RL, RH);
3697
3698 // If we only have two elements, turn into a pair of scalar loads.
3699 // FIXME: handle case where a vector of two elements is fine, such as
3700 // 2 x double on SSE2.
3701 if (NumElements == 2) {
3702 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3703 Lo = DAG.getNode(Opc, EVT, LL, RL);
3704 Hi = DAG.getNode(Opc, EVT, LH, RH);
3705 } else {
3706 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3707 LL.getOperand(3));
3708 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3709 LH.getOperand(3));
3710 }
3711 break;
3712 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003713 case ISD::AND:
3714 case ISD::OR:
3715 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3716 SDOperand LL, LH, RL, RH;
3717 ExpandOp(Node->getOperand(0), LL, LH);
3718 ExpandOp(Node->getOperand(1), RL, RH);
3719 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3720 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3721 break;
3722 }
3723 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003724 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003725 ExpandOp(Node->getOperand(1), LL, LH);
3726 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003727 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3728 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003729 break;
3730 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003731 case ISD::SELECT_CC: {
3732 SDOperand TL, TH, FL, FH;
3733 ExpandOp(Node->getOperand(2), TL, TH);
3734 ExpandOp(Node->getOperand(3), FL, FH);
3735 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3736 Node->getOperand(1), TL, FL, Node->getOperand(4));
3737 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3738 Node->getOperand(1), TH, FH, Node->getOperand(4));
3739 break;
3740 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003741 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003742 SDOperand Chain = Node->getOperand(0);
3743 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003744 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3745
3746 if (EVT == NVT)
3747 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3748 else
3749 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3750 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003751
3752 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003753 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003754
Nate Begemanc3a89c52005-10-13 17:15:37 +00003755 // The high part is obtained by SRA'ing all but one of the bits of the lo
3756 // part.
3757 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3758 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3759 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003760 break;
3761 }
3762 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003763 SDOperand Chain = Node->getOperand(0);
3764 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003765 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3766
3767 if (EVT == NVT)
3768 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3769 else
3770 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3771 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003772
3773 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003774 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003775
Nate Begemanc3a89c52005-10-13 17:15:37 +00003776 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003777 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003778 break;
3779 }
3780 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003781 SDOperand Chain = Node->getOperand(0);
3782 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003783 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3784
3785 if (EVT == NVT)
3786 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3787 else
3788 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3789 EVT);
3790
3791 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003792 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003793
3794 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003795 Hi = DAG.getNode(ISD::UNDEF, NVT);
3796 break;
3797 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003798 case ISD::ANY_EXTEND:
3799 // The low part is any extension of the input (which degenerates to a copy).
3800 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3801 // The high part is undefined.
3802 Hi = DAG.getNode(ISD::UNDEF, NVT);
3803 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003804 case ISD::SIGN_EXTEND: {
3805 // The low part is just a sign extension of the input (which degenerates to
3806 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003807 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003808
Chris Lattnerdc750592005-01-07 07:47:09 +00003809 // The high part is obtained by SRA'ing all but one of the bits of the lo
3810 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003811 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003812 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3813 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003814 break;
3815 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003816 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003817 // The low part is just a zero extension of the input (which degenerates to
3818 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003819 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003820
Chris Lattnerdc750592005-01-07 07:47:09 +00003821 // The high part is just a zero.
3822 Hi = DAG.getConstant(0, NVT);
3823 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003824
3825 case ISD::BIT_CONVERT: {
3826 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3827 Node->getOperand(0));
3828 ExpandOp(Tmp, Lo, Hi);
3829 break;
3830 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003831
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003832 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00003833 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3834 TargetLowering::Custom &&
3835 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003836 Lo = TLI.LowerOperation(Op, DAG);
3837 assert(Lo.Val && "Node must be custom expanded!");
3838 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00003839 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003840 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003841 break;
3842
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003843 // These operators cannot be expanded directly, emit them as calls to
3844 // library functions.
3845 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003846 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003847 SDOperand Op;
3848 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3849 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003850 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3851 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00003852 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003853
Chris Lattner941d84a2005-07-30 01:40:57 +00003854 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3855
Chris Lattnerfe68d752005-07-29 00:33:32 +00003856 // Now that the custom expander is done, expand the result, which is still
3857 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003858 if (Op.Val) {
3859 ExpandOp(Op, Lo, Hi);
3860 break;
3861 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003862 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003863
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003864 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003865 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003866 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003867 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003868 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003869
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003870 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003871 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003872 SDOperand Op;
3873 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3874 case Expand: assert(0 && "cannot expand FP!");
3875 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3876 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3877 }
3878
3879 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3880
3881 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003882 if (Op.Val) {
3883 ExpandOp(Op, Lo, Hi);
3884 break;
3885 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003886 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003887
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003888 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003889 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003890 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003891 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003892 break;
3893
Evan Cheng870e4f82006-01-09 18:31:59 +00003894 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003895 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003896 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003897 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003898 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003899 Op = TLI.LowerOperation(Op, DAG);
3900 if (Op.Val) {
3901 // Now that the custom expander is done, expand the result, which is
3902 // still VT.
3903 ExpandOp(Op, Lo, Hi);
3904 break;
3905 }
3906 }
3907
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003908 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003909 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003910 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003911
3912 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003913 TargetLowering::LegalizeAction Action =
3914 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3915 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3916 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003917 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003918 break;
3919 }
3920
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003921 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003922 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003923 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003924 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003925
Evan Cheng870e4f82006-01-09 18:31:59 +00003926 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003927 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003928 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003929 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003930 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003931 Op = TLI.LowerOperation(Op, DAG);
3932 if (Op.Val) {
3933 // Now that the custom expander is done, expand the result, which is
3934 // still VT.
3935 ExpandOp(Op, Lo, Hi);
3936 break;
3937 }
3938 }
3939
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003940 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003941 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003942 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003943
3944 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003945 TargetLowering::LegalizeAction Action =
3946 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3947 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3948 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003949 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003950 break;
3951 }
3952
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003953 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003954 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003955 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003956 }
3957
3958 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003959 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003960 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003961 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003962 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003963 Op = TLI.LowerOperation(Op, DAG);
3964 if (Op.Val) {
3965 // Now that the custom expander is done, expand the result, which is
3966 // still VT.
3967 ExpandOp(Op, Lo, Hi);
3968 break;
3969 }
3970 }
3971
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003972 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003973 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003974 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003975
3976 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003977 TargetLowering::LegalizeAction Action =
3978 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3979 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3980 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003981 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003982 break;
3983 }
3984
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003985 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003986 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003987 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003988 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003989
Misha Brukman835702a2005-04-21 22:36:52 +00003990 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003991 case ISD::SUB: {
3992 // If the target wants to custom expand this, let them.
3993 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3994 TargetLowering::Custom) {
3995 Op = TLI.LowerOperation(Op, DAG);
3996 if (Op.Val) {
3997 ExpandOp(Op, Lo, Hi);
3998 break;
3999 }
4000 }
4001
4002 // Expand the subcomponents.
4003 SDOperand LHSL, LHSH, RHSL, RHSH;
4004 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4005 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004006 std::vector<MVT::ValueType> VTs;
4007 std::vector<SDOperand> LoOps, HiOps;
4008 VTs.push_back(LHSL.getValueType());
4009 VTs.push_back(MVT::Flag);
4010 LoOps.push_back(LHSL);
4011 LoOps.push_back(RHSL);
4012 HiOps.push_back(LHSH);
4013 HiOps.push_back(RHSH);
4014 if (Node->getOpcode() == ISD::ADD) {
4015 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4016 HiOps.push_back(Lo.getValue(1));
4017 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4018 } else {
4019 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4020 HiOps.push_back(Lo.getValue(1));
4021 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4022 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004023 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004024 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004025 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004026 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004027 SDOperand LL, LH, RL, RH;
4028 ExpandOp(Node->getOperand(0), LL, LH);
4029 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004030 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4031 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4032 // extended the sign bit of the low half through the upper half, and if so
4033 // emit a MULHS instead of the alternate sequence that is valid for any
4034 // i64 x i64 multiply.
4035 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4036 // is RH an extension of the sign bit of RL?
4037 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4038 RH.getOperand(1).getOpcode() == ISD::Constant &&
4039 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4040 // is LH an extension of the sign bit of LL?
4041 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4042 LH.getOperand(1).getOpcode() == ISD::Constant &&
4043 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4044 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4045 } else {
4046 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4047 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4048 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4049 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4050 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4051 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004052 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4053 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004054 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004055 }
4056 break;
4057 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004058 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4059 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4060 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4061 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004062 }
4063
Chris Lattnerac12f682005-12-21 18:02:52 +00004064 // Make sure the resultant values have been legalized themselves, unless this
4065 // is a type that requires multi-step expansion.
4066 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4067 Lo = LegalizeOp(Lo);
4068 Hi = LegalizeOp(Hi);
4069 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004070
4071 // Remember in a map if the values will be reused later.
4072 bool isNew =
4073 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4074 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004075}
4076
4077
4078// SelectionDAG::Legalize - This is the entry point for the file.
4079//
Chris Lattner4add7e32005-01-23 04:42:50 +00004080void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004081 /// run - This is the main entry point to this class.
4082 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004083 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004084}
4085