blob: a6d74c85e7cde84d615592f2895b24516b5352c5 [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!");
Evan Cheng3bf916d2006-03-03 07:01:07 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
159 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
160 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
161 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
162 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begemanb2e089c2005-11-19 00:36:38 +0000163 }
164}
Chris Lattnerdc750592005-01-07 07:47:09 +0000165
Chris Lattner4add7e32005-01-23 04:42:50 +0000166SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
167 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
168 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000169 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000170 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000171}
172
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000173/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
174/// not been visited yet and if all of its operands have already been visited.
175static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
176 std::map<SDNode*, unsigned> &Visited) {
177 if (++Visited[N] != N->getNumOperands())
178 return; // Haven't visited all operands yet
179
180 Order.push_back(N);
181
182 if (N->hasOneUse()) { // Tail recurse in common case.
183 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
184 return;
185 }
186
187 // Now that we have N in, add anything that uses it if all of their operands
188 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000189 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
190 ComputeTopDownOrdering(*UI, Order, Visited);
191}
192
Chris Lattner44fe26f2005-07-29 00:11:56 +0000193
Chris Lattnerdc750592005-01-07 07:47:09 +0000194void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000195 LastCALLSEQ_END = DAG.getEntryNode();
196 IsLegalizingCall = false;
197
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000198 // The legalize process is inherently a bottom-up recursive process (users
199 // legalize their uses before themselves). Given infinite stack space, we
200 // could just start legalizing on the root and traverse the whole graph. In
201 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000202 // blocks. To avoid this problem, compute an ordering of the nodes where each
203 // node is only legalized after all of its operands are legalized.
204 std::map<SDNode*, unsigned> Visited;
205 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000206
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000207 // Compute ordering from all of the leaves in the graphs, those (like the
208 // entry node) that have no operands.
209 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
210 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000211 if (I->getNumOperands() == 0) {
212 Visited[I] = 0 - 1U;
213 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000214 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000215 }
216
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000217 assert(Order.size() == Visited.size() &&
218 Order.size() ==
219 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000220 "Error: DAG is cyclic!");
221 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000222
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000223 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
224 SDNode *N = Order[i];
225 switch (getTypeAction(N->getValueType(0))) {
226 default: assert(0 && "Bad type action!");
227 case Legal:
228 LegalizeOp(SDOperand(N, 0));
229 break;
230 case Promote:
231 PromoteOp(SDOperand(N, 0));
232 break;
233 case Expand: {
234 SDOperand X, Y;
235 ExpandOp(SDOperand(N, 0), X, Y);
236 break;
237 }
238 }
239 }
240
241 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000242 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000243 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
244 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000245
246 ExpandedNodes.clear();
247 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000248 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000249
250 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000251 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000252}
253
Chris Lattner462505f2006-02-13 09:18:02 +0000254
255/// FindCallEndFromCallStart - Given a chained node that is part of a call
256/// sequence, find the CALLSEQ_END node that terminates the call sequence.
257static SDNode *FindCallEndFromCallStart(SDNode *Node) {
258 if (Node->getOpcode() == ISD::CALLSEQ_END)
259 return Node;
260 if (Node->use_empty())
261 return 0; // No CallSeqEnd
262
263 // The chain is usually at the end.
264 SDOperand TheChain(Node, Node->getNumValues()-1);
265 if (TheChain.getValueType() != MVT::Other) {
266 // Sometimes it's at the beginning.
267 TheChain = SDOperand(Node, 0);
268 if (TheChain.getValueType() != MVT::Other) {
269 // Otherwise, hunt for it.
270 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
271 if (Node->getValueType(i) == MVT::Other) {
272 TheChain = SDOperand(Node, i);
273 break;
274 }
275
276 // Otherwise, we walked into a node without a chain.
277 if (TheChain.getValueType() != MVT::Other)
278 return 0;
279 }
280 }
281
282 for (SDNode::use_iterator UI = Node->use_begin(),
283 E = Node->use_end(); UI != E; ++UI) {
284
285 // Make sure to only follow users of our token chain.
286 SDNode *User = *UI;
287 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
288 if (User->getOperand(i) == TheChain)
289 if (SDNode *Result = FindCallEndFromCallStart(User))
290 return Result;
291 }
292 return 0;
293}
294
295/// FindCallStartFromCallEnd - Given a chained node that is part of a call
296/// sequence, find the CALLSEQ_START node that initiates the call sequence.
297static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
298 assert(Node && "Didn't find callseq_start for a call??");
299 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
300
301 assert(Node->getOperand(0).getValueType() == MVT::Other &&
302 "Node doesn't have a token chain argument!");
303 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
304}
305
306/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
307/// see if any uses can reach Dest. If no dest operands can get to dest,
308/// legalize them, legalize ourself, and return false, otherwise, return true.
309bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
310 SDNode *Dest) {
311 if (N == Dest) return true; // N certainly leads to Dest :)
312
313 // If the first result of this node has been already legalized, then it cannot
314 // reach N.
315 switch (getTypeAction(N->getValueType(0))) {
316 case Legal:
317 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
318 break;
319 case Promote:
320 if (PromotedNodes.count(SDOperand(N, 0))) return false;
321 break;
322 case Expand:
323 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
324 break;
325 }
326
327 // Okay, this node has not already been legalized. Check and legalize all
328 // operands. If none lead to Dest, then we can legalize this node.
329 bool OperandsLeadToDest = false;
330 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
331 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
332 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
333
334 if (OperandsLeadToDest) return true;
335
336 // Okay, this node looks safe, legalize it and return false.
337 switch (getTypeAction(N->getValueType(0))) {
338 case Legal:
339 LegalizeOp(SDOperand(N, 0));
340 break;
341 case Promote:
342 PromoteOp(SDOperand(N, 0));
343 break;
344 case Expand: {
345 SDOperand X, Y;
346 ExpandOp(SDOperand(N, 0), X, Y);
347 break;
348 }
349 }
350 return false;
351}
352
353
354
Chris Lattnerdc750592005-01-07 07:47:09 +0000355SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000356 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000357 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000358 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000359
Chris Lattnerdc750592005-01-07 07:47:09 +0000360 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000361 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000362 if (Node->getNumValues() > 1) {
363 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
364 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000365 case Legal: break; // Nothing to do.
366 case Expand: {
367 SDOperand T1, T2;
368 ExpandOp(Op.getValue(i), T1, T2);
369 assert(LegalizedNodes.count(Op) &&
370 "Expansion didn't add legal operands!");
371 return LegalizedNodes[Op];
372 }
373 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000374 PromoteOp(Op.getValue(i));
375 assert(LegalizedNodes.count(Op) &&
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000376 "Promotion didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000377 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000378 }
379 }
380
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000381 // Note that LegalizeOp may be reentered even from single-use nodes, which
382 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000383 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
384 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000385
Nate Begemane5b86d72005-08-10 20:51:12 +0000386 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000387 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000388 bool isCustom = false;
389
Chris Lattnerdc750592005-01-07 07:47:09 +0000390 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000391 case ISD::FrameIndex:
392 case ISD::EntryToken:
393 case ISD::Register:
394 case ISD::BasicBlock:
395 case ISD::TargetFrameIndex:
396 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000397 case ISD::TargetConstantFP:
398 case ISD::TargetConstantVec:
Chris Lattnereb637512006-01-28 08:31:04 +0000399 case ISD::TargetConstantPool:
400 case ISD::TargetGlobalAddress:
401 case ISD::TargetExternalSymbol:
402 case ISD::VALUETYPE:
403 case ISD::SRCVALUE:
404 case ISD::STRING:
405 case ISD::CONDCODE:
406 // Primitives must all be legal.
407 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
408 "This must be legal!");
409 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000410 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000411 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
412 // If this is a target node, legalize it by legalizing the operands then
413 // passing it through.
414 std::vector<SDOperand> Ops;
415 bool Changed = false;
416 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
417 Ops.push_back(LegalizeOp(Node->getOperand(i)));
418 Changed = Changed || Node->getOperand(i) != Ops.back();
419 }
420 if (Changed)
421 if (Node->getNumValues() == 1)
422 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
423 else {
424 std::vector<MVT::ValueType> VTs(Node->value_begin(),
425 Node->value_end());
426 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
427 }
428
429 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
430 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
431 return Result.getValue(Op.ResNo);
432 }
433 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000434 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
435 assert(0 && "Do not know how to legalize this operator!");
436 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000437 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000438 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000439 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000440 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
441 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000442 case TargetLowering::Custom:
443 Tmp1 = TLI.LowerOperation(Op, DAG);
444 if (Tmp1.Val) Result = Tmp1;
445 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000446 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000447 break;
448 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000449 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000450 case ISD::AssertSext:
451 case ISD::AssertZext:
452 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000454 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000455 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000456 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000457 Result = Node->getOperand(Op.ResNo);
458 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000459 case ISD::CopyFromReg:
460 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000461 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000462 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000464 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000465 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000466 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000467 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
469 } else {
470 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
471 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000472 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
473 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000474 // Since CopyFromReg produces two values, make sure to remember that we
475 // legalized both of them.
476 AddLegalizedOperand(Op.getValue(0), Result);
477 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
478 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000479 case ISD::UNDEF: {
480 MVT::ValueType VT = Op.getValueType();
481 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000482 default: assert(0 && "This action is not supported yet!");
483 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000484 if (MVT::isInteger(VT))
485 Result = DAG.getConstant(0, VT);
486 else if (MVT::isFloatingPoint(VT))
487 Result = DAG.getConstantFP(0, VT);
488 else
489 assert(0 && "Unknown value type!");
490 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000491 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000492 break;
493 }
494 break;
495 }
Chris Lattner435b4022005-11-29 06:21:05 +0000496
497 case ISD::LOCATION:
498 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
499 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
500
501 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
502 case TargetLowering::Promote:
503 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000504 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000505 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000506 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
507 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
508
509 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
510 const std::string &FName =
511 cast<StringSDNode>(Node->getOperand(3))->getValue();
512 const std::string &DirName =
513 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000514 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000515
Jim Laskey9e296be2005-12-21 20:51:37 +0000516 std::vector<SDOperand> Ops;
517 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000518 SDOperand LineOp = Node->getOperand(1);
519 SDOperand ColOp = Node->getOperand(2);
520
521 if (useDEBUG_LOC) {
522 Ops.push_back(LineOp); // line #
523 Ops.push_back(ColOp); // col #
524 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
525 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
526 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000527 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
528 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000529 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
530 Ops.push_back(DAG.getConstant(ID, MVT::i32));
531 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
532 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000533 } else {
534 Result = Tmp1; // chain
535 }
Chris Lattner435b4022005-11-29 06:21:05 +0000536 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000537 }
Chris Lattner435b4022005-11-29 06:21:05 +0000538 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000539 if (Tmp1 != Node->getOperand(0) ||
540 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000541 std::vector<SDOperand> Ops;
542 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000543 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
544 Ops.push_back(Node->getOperand(1)); // line # must be legal.
545 Ops.push_back(Node->getOperand(2)); // col # must be legal.
546 } else {
547 // Otherwise promote them.
548 Ops.push_back(PromoteOp(Node->getOperand(1)));
549 Ops.push_back(PromoteOp(Node->getOperand(2)));
550 }
Chris Lattner435b4022005-11-29 06:21:05 +0000551 Ops.push_back(Node->getOperand(3)); // filename must be legal.
552 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000553 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000554 }
555 break;
556 }
557 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000558
559 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000560 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000561 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000562 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000563 case TargetLowering::Legal:
564 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
565 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
566 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
567 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000569 break;
570 }
571 break;
572
573 case ISD::DEBUG_LABEL:
574 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
575 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000576 default: assert(0 && "This action is not supported yet!");
577 case TargetLowering::Legal:
578 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
579 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000580 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000581 break;
582 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000583 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000584
Chris Lattnerdc750592005-01-07 07:47:09 +0000585 case ISD::Constant:
586 // We know we don't need to expand constants here, constants only have one
587 // value and we check that it is fine above.
588
589 // FIXME: Maybe we should handle things like targets that don't support full
590 // 32-bit immediates?
591 break;
592 case ISD::ConstantFP: {
593 // Spill FP immediates to the constant pool if the target cannot directly
594 // codegen them. Targets often have some immediate values that can be
595 // efficiently generated into an FP register without a load. We explicitly
596 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000597 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
598
599 // Check to see if this FP immediate is already legal.
600 bool isLegal = false;
601 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
602 E = TLI.legal_fpimm_end(); I != E; ++I)
603 if (CFP->isExactlyValue(*I)) {
604 isLegal = true;
605 break;
606 }
607
Chris Lattner758b0ac2006-01-29 06:26:56 +0000608 // If this is a legal constant, turn it into a TargetConstantFP node.
609 if (isLegal) {
610 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
611 break;
612 }
613
614 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
615 default: assert(0 && "This action is not supported yet!");
616 case TargetLowering::Custom:
617 Tmp3 = TLI.LowerOperation(Result, DAG);
618 if (Tmp3.Val) {
619 Result = Tmp3;
620 break;
621 }
622 // FALLTHROUGH
623 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000624 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000625 bool Extend = false;
626
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000627 // If a FP immediate is precise when represented as a float and if the
628 // target can do an extending load from float to double, we put it into
629 // the constant pool as a float, even if it's is statically typed as a
630 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000631 MVT::ValueType VT = CFP->getValueType(0);
632 bool isDouble = VT == MVT::f64;
633 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
634 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000635 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
636 // Only do this if the target has a native EXTLOAD instruction from
637 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000638 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000639 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
640 VT = MVT::f32;
641 Extend = true;
642 }
Misha Brukman835702a2005-04-21 22:36:52 +0000643
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000644 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000645 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000646 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
647 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000648 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000649 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
650 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000651 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000652 }
653 break;
654 }
Chris Lattner2f2927892006-01-29 06:34:16 +0000655 case ISD::ConstantVec:
656 switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) {
657 default: assert(0 && "This action is not supported yet!");
658 case TargetLowering::Custom:
659 Tmp3 = TLI.LowerOperation(Result, DAG);
660 if (Tmp3.Val) {
661 Result = Tmp3;
662 break;
663 }
664 // FALLTHROUGH
665 case TargetLowering::Expand:
666 // We assume that vector constants are not legal, and will be immediately
667 // spilled to the constant pool.
668 //
669 // Create a ConstantPacked, and put it in the constant pool.
670 MVT::ValueType VT = Node->getValueType(0);
671 const Type *OpNTy =
672 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
673 std::vector<Constant*> CV;
674 if (MVT::isFloatingPoint(VT)) {
675 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
676 double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue();
677 CV.push_back(ConstantFP::get(OpNTy, V));
678 }
679 } else {
680 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
681 uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
682 CV.push_back(ConstantUInt::get(OpNTy, V));
683 }
684 }
685 Constant *CP = ConstantPacked::get(CV);
686 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
687 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
688 DAG.getSrcValue(NULL));
689 break;
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000690 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000691 break;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000692 case ISD::TokenFactor:
693 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000694 Tmp1 = LegalizeOp(Node->getOperand(0));
695 Tmp2 = LegalizeOp(Node->getOperand(1));
696 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
697 } else if (Node->getNumOperands() == 3) {
698 Tmp1 = LegalizeOp(Node->getOperand(0));
699 Tmp2 = LegalizeOp(Node->getOperand(1));
700 Tmp3 = LegalizeOp(Node->getOperand(2));
701 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000702 } else {
703 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000704 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000705 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
706 Ops.push_back(LegalizeOp(Node->getOperand(i)));
707 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000708 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000709 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000710
Chris Lattner462505f2006-02-13 09:18:02 +0000711 case ISD::CALLSEQ_START: {
712 SDNode *CallEnd = FindCallEndFromCallStart(Node);
713
714 // Recursively Legalize all of the inputs of the call end that do not lead
715 // to this call start. This ensures that any libcalls that need be inserted
716 // are inserted *before* the CALLSEQ_START.
717 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
718 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
719
720 // Now that we legalized all of the inputs (which may have inserted
721 // libcalls) create the new CALLSEQ_START node.
722 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
723
724 // Merge in the last call, to ensure that this call start after the last
725 // call ended.
726 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
727 Tmp1 = LegalizeOp(Tmp1);
728
729 // Do not try to legalize the target-specific arguments (#1+).
730 if (Tmp1 != Node->getOperand(0)) {
731 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
732 Ops[0] = Tmp1;
733 Result = DAG.UpdateNodeOperands(Result, Ops);
734 }
735
736 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000737 AddLegalizedOperand(Op.getValue(0), Result);
738 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
739 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
740
Chris Lattner462505f2006-02-13 09:18:02 +0000741 // Now that the callseq_start and all of the non-call nodes above this call
742 // sequence have been legalized, legalize the call itself. During this
743 // process, no libcalls can/will be inserted, guaranteeing that no calls
744 // can overlap.
745 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
746 SDOperand InCallSEQ = LastCALLSEQ_END;
747 // Note that we are selecting this call!
748 LastCALLSEQ_END = SDOperand(CallEnd, 0);
749 IsLegalizingCall = true;
750
751 // Legalize the call, starting from the CALLSEQ_END.
752 LegalizeOp(LastCALLSEQ_END);
753 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
754 return Result;
755 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000756 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +0000757 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
758 // will cause this node to be legalized as well as handling libcalls right.
759 if (LastCALLSEQ_END.Val != Node) {
760 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
761 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
762 assert(I != LegalizedNodes.end() &&
763 "Legalizing the call start should have legalized this node!");
764 return I->second;
765 }
766
767 // Otherwise, the call start has been legalized and everything is going
768 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +0000769 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000770 // Do not try to legalize the target-specific arguments (#1+), except for
771 // an optional flag input.
772 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
773 if (Tmp1 != Node->getOperand(0)) {
774 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
775 Ops[0] = Tmp1;
776 Result = DAG.UpdateNodeOperands(Result, Ops);
777 }
778 } else {
779 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
780 if (Tmp1 != Node->getOperand(0) ||
781 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
782 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
783 Ops[0] = Tmp1;
784 Ops.back() = Tmp2;
785 Result = DAG.UpdateNodeOperands(Result, Ops);
786 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +0000787 }
Chris Lattner8e2ee732006-02-14 00:55:02 +0000788 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +0000789 // This finishes up call legalization.
790 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +0000791
792 // If the CALLSEQ_END node has a flag, remember that we legalized it.
793 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
794 if (Node->getNumValues() == 2)
795 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
796 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000797 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +0000798 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
799 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
800 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000801 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +0000802
Chris Lattnerd02b0542006-01-28 10:58:55 +0000803 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +0000804 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000805 switch (TLI.getOperationAction(Node->getOpcode(),
806 Node->getValueType(0))) {
807 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +0000808 case TargetLowering::Expand: {
809 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
810 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
811 " not tell us which reg is the stack pointer!");
812 SDOperand Chain = Tmp1.getOperand(0);
813 SDOperand Size = Tmp2.getOperand(1);
814 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
815 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
816 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +0000817 Tmp1 = LegalizeOp(Tmp1);
818 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000819 break;
820 }
821 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +0000822 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
823 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000824 Tmp1 = LegalizeOp(Tmp3);
825 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +0000826 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000827 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000828 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +0000829 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000830 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000831 // Since this op produce two values, make sure to remember that we
832 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000833 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
834 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000835 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000836 }
Chris Lattner476e67b2006-01-26 22:24:51 +0000837 case ISD::INLINEASM:
838 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
839 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000840 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +0000841 Tmp2 = Tmp3 = SDOperand(0, 0);
842 else
843 Tmp3 = LegalizeOp(Tmp2);
844
845 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
846 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
847 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +0000848 if (Tmp3.Val) Ops.back() = Tmp3;
849 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +0000850 }
851
852 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000853 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +0000854 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
855 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +0000856 case ISD::BR:
857 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +0000858 // Ensure that libcalls are emitted before a branch.
859 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
860 Tmp1 = LegalizeOp(Tmp1);
861 LastCALLSEQ_END = DAG.getEntryNode();
862
Chris Lattnerd02b0542006-01-28 10:58:55 +0000863 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +0000864 break;
865
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000866 case ISD::BRCOND:
867 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +0000868 // Ensure that libcalls are emitted before a return.
869 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
870 Tmp1 = LegalizeOp(Tmp1);
871 LastCALLSEQ_END = DAG.getEntryNode();
872
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000873 switch (getTypeAction(Node->getOperand(1).getValueType())) {
874 case Expand: assert(0 && "It's impossible to expand bools");
875 case Legal:
876 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
877 break;
878 case Promote:
879 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
880 break;
881 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000882
883 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000885
886 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
887 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000888 case TargetLowering::Legal: break;
889 case TargetLowering::Custom:
890 Tmp1 = TLI.LowerOperation(Result, DAG);
891 if (Tmp1.Val) Result = Tmp1;
892 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000893 case TargetLowering::Expand:
894 // Expand brcond's setcc into its constituent parts and create a BR_CC
895 // Node.
896 if (Tmp2.getOpcode() == ISD::SETCC) {
897 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
898 Tmp2.getOperand(0), Tmp2.getOperand(1),
899 Node->getOperand(2));
900 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000901 // Make sure the condition is either zero or one. It may have been
902 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +0000903 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
904 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
905 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +0000906
Nate Begeman371e4952005-08-16 19:49:35 +0000907 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
908 DAG.getCondCode(ISD::SETNE), Tmp2,
909 DAG.getConstant(0, Tmp2.getValueType()),
910 Node->getOperand(2));
911 }
912 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000913 }
914 break;
915 case ISD::BR_CC:
916 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +0000917 // Ensure that libcalls are emitted before a branch.
918 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
919 Tmp1 = LegalizeOp(Tmp1);
920 LastCALLSEQ_END = DAG.getEntryNode();
921
Nate Begeman7e7f4392006-02-01 07:19:44 +0000922 Tmp2 = Node->getOperand(2); // LHS
923 Tmp3 = Node->getOperand(3); // RHS
924 Tmp4 = Node->getOperand(1); // CC
925
926 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
927
928 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
929 // the LHS is a legal SETCC itself. In this case, we need to compare
930 // the result against zero to select between true and false values.
931 if (Tmp3.Val == 0) {
932 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
933 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000934 }
Nate Begeman7e7f4392006-02-01 07:19:44 +0000935
936 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
937 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000938
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000939 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
940 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000941 case TargetLowering::Legal: break;
942 case TargetLowering::Custom:
943 Tmp4 = TLI.LowerOperation(Result, DAG);
944 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000945 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000946 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000947 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000948 case ISD::BRCONDTWOWAY:
949 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
950 switch (getTypeAction(Node->getOperand(1).getValueType())) {
951 case Expand: assert(0 && "It's impossible to expand bools");
952 case Legal:
953 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
954 break;
955 case Promote:
956 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
957 break;
958 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000959
Chris Lattnerfd986782005-04-09 03:30:19 +0000960 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
961 // pair.
962 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
963 case TargetLowering::Promote:
964 default: assert(0 && "This action is not supported yet!");
965 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000966 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
967 Node->getOperand(3));
Chris Lattnerfd986782005-04-09 03:30:19 +0000968 break;
969 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000970 // If BRTWOWAY_CC is legal for this target, then simply expand this node
971 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
972 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000973 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000974 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattner678da982006-01-29 06:00:45 +0000975 Tmp3 = Tmp2.getOperand(0);
976 Tmp4 = Tmp2.getOperand(1);
977 Tmp2 = Tmp2.getOperand(2);
Nate Begeman371e4952005-08-16 19:49:35 +0000978 } else {
Chris Lattner678da982006-01-29 06:00:45 +0000979 Tmp3 = Tmp2;
980 Tmp4 = DAG.getConstant(0, Tmp2.getValueType());
981 Tmp2 = DAG.getCondCode(ISD::SETNE);
Nate Begeman371e4952005-08-16 19:49:35 +0000982 }
Chris Lattner678da982006-01-29 06:00:45 +0000983 std::vector<SDOperand> Ops;
984 Ops.push_back(Tmp1);
985 Ops.push_back(Tmp2);
986 Ops.push_back(Tmp3);
987 Ops.push_back(Tmp4);
988 Ops.push_back(Node->getOperand(2));
989 Ops.push_back(Node->getOperand(3));
990 Result = DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000991 } else {
992 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000993 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000994 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
995 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000996 break;
997 }
998 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +0000999 case ISD::BRTWOWAY_CC: {
Nate Begeman371e4952005-08-16 19:49:35 +00001000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001001 // Ensure that libcalls are emitted before a branch.
1002 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1003 Tmp1 = LegalizeOp(Tmp1);
1004 LastCALLSEQ_END = DAG.getEntryNode();
1005
Nate Begeman7e7f4392006-02-01 07:19:44 +00001006 Tmp2 = Node->getOperand(2); // LHS
1007 Tmp3 = Node->getOperand(3); // RHS
1008 Tmp4 = Node->getOperand(1); // CC
1009
1010 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1011
1012 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1013 // the LHS is a legal SETCC itself. In this case, we need to compare
1014 // the result against zero to select between true and false values.
1015 if (Tmp3.Val == 0) {
1016 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1017 Tmp4 = DAG.getCondCode(ISD::SETNE);
1018 }
1019 std::vector<SDOperand> Ops;
1020 Ops.push_back(Tmp1);
1021 Ops.push_back(Tmp4);
1022 Ops.push_back(Tmp2);
1023 Ops.push_back(Tmp3);
1024 Ops.push_back(Node->getOperand(4));
1025 Ops.push_back(Node->getOperand(5));
1026 Result = DAG.UpdateNodeOperands(Result, Ops);
1027
1028 // Everything is legal, see if we should expand this op or something.
1029 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
1030 default: assert(0 && "This action is not supported yet!");
1031 case TargetLowering::Legal: break;
1032 case TargetLowering::Expand:
1033 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1,
1034 DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp2,
1035 Tmp3, Tmp4),
1036 Result.getOperand(4));
1037 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Result.getOperand(5));
Nate Begeman371e4952005-08-16 19:49:35 +00001038 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001039 }
1040 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001041 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001042 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1044 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001045
Evan Cheng31d15fa2005-12-23 07:29:34 +00001046 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001047 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1048 Tmp2 = Result.getValue(0);
1049 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001050
Evan Cheng31d15fa2005-12-23 07:29:34 +00001051 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1052 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001053 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001054 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001055 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001056 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001057 Tmp2 = LegalizeOp(Tmp1);
1058 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001059 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001060 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001061 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001062 // Since loads produce two values, make sure to remember that we
1063 // legalized both of them.
1064 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1065 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1066 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001067 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001068 case ISD::EXTLOAD:
1069 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001070 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001071 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1072 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001073
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001074 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001075 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001076 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001077 case TargetLowering::Promote:
1078 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001079 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1080 DAG.getValueType(MVT::i8));
1081 Tmp1 = Result.getValue(0);
1082 Tmp2 = Result.getValue(1);
1083 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001084 case TargetLowering::Custom:
1085 isCustom = true;
1086 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001087 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001088 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1089 Node->getOperand(3));
1090 Tmp1 = Result.getValue(0);
1091 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001092
1093 if (isCustom) {
1094 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1095 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001096 Tmp1 = LegalizeOp(Tmp3);
1097 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001098 }
1099 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001100 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001101 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001102 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001103 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1104 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001105 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001106 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1107 Tmp2 = LegalizeOp(Load.getValue(1));
1108 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001109 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001110 assert(Node->getOpcode() != ISD::EXTLOAD &&
1111 "EXTLOAD should always be supported!");
1112 // Turn the unsupported load into an EXTLOAD followed by an explicit
1113 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001114 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1115 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001116 SDOperand ValRes;
1117 if (Node->getOpcode() == ISD::SEXTLOAD)
1118 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001119 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001120 else
1121 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001122 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1123 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1124 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001125 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001126 // Since loads produce two values, make sure to remember that we legalized
1127 // both of them.
1128 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1129 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1130 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001131 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001132 case ISD::EXTRACT_ELEMENT: {
1133 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1134 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001135 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001136 case Legal:
1137 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1138 // 1 -> Hi
1139 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1140 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1141 TLI.getShiftAmountTy()));
1142 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1143 } else {
1144 // 0 -> Lo
1145 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1146 Node->getOperand(0));
1147 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001148 break;
1149 case Expand:
1150 // Get both the low and high parts.
1151 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1152 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1153 Result = Tmp2; // 1 -> Hi
1154 else
1155 Result = Tmp1; // 0 -> Lo
1156 break;
1157 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001158 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001159 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001160
1161 case ISD::CopyToReg:
1162 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001163
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001164 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001165 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001166 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001167 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001168 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001169 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001170 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001171 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001172 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001173 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001174 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1175 Tmp3);
1176 } else {
1177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001178 }
1179
1180 // Since this produces two values, make sure to remember that we legalized
1181 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001182 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001183 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001184 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001185 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001186 break;
1187
1188 case ISD::RET:
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001190
1191 // Ensure that libcalls are emitted before a return.
1192 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1193 Tmp1 = LegalizeOp(Tmp1);
1194 LastCALLSEQ_END = DAG.getEntryNode();
1195
Chris Lattnerdc750592005-01-07 07:47:09 +00001196 switch (Node->getNumOperands()) {
1197 case 2: // ret val
1198 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1199 case Legal:
1200 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001201 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001202 break;
1203 case Expand: {
1204 SDOperand Lo, Hi;
1205 ExpandOp(Node->getOperand(1), Lo, Hi);
1206 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001207 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001208 }
1209 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001210 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001211 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1212 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001213 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001214 }
1215 break;
1216 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001217 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001218 break;
1219 default: { // ret <values>
1220 std::vector<SDOperand> NewValues;
1221 NewValues.push_back(Tmp1);
1222 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1223 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1224 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001225 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001226 break;
1227 case Expand: {
1228 SDOperand Lo, Hi;
1229 ExpandOp(Node->getOperand(i), Lo, Hi);
1230 NewValues.push_back(Lo);
1231 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001232 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001233 }
1234 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001235 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001236 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001237
1238 if (NewValues.size() == Node->getNumOperands())
1239 Result = DAG.UpdateNodeOperands(Result, NewValues);
1240 else
1241 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001242 break;
1243 }
1244 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001245
Chris Lattner4d1ea712006-01-29 21:02:23 +00001246 if (Result.getOpcode() == ISD::RET) {
1247 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1248 default: assert(0 && "This action is not supported yet!");
1249 case TargetLowering::Legal: break;
1250 case TargetLowering::Custom:
1251 Tmp1 = TLI.LowerOperation(Result, DAG);
1252 if (Tmp1.Val) Result = Tmp1;
1253 break;
1254 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001255 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001256 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001257 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001258 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1259 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1260
Chris Lattnere69daaf2005-01-08 06:25:56 +00001261 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001262 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001263 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001264 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001265 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001266 } else {
1267 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001268 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001269 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001270 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1271 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001272 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001273 }
1274
Chris Lattnerdc750592005-01-07 07:47:09 +00001275 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1276 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001277 Tmp3 = LegalizeOp(Node->getOperand(1));
1278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1279 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001280
Chris Lattnerd02b0542006-01-28 10:58:55 +00001281 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001282 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1283 default: assert(0 && "This action is not supported yet!");
1284 case TargetLowering::Legal: break;
1285 case TargetLowering::Custom:
1286 Tmp1 = TLI.LowerOperation(Result, DAG);
1287 if (Tmp1.Val) Result = Tmp1;
1288 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001289 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001290 break;
1291 }
1292 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001293 // Truncate the value and store the result.
1294 Tmp3 = PromoteOp(Node->getOperand(1));
1295 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001296 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001297 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001298 break;
1299
Chris Lattnerdc750592005-01-07 07:47:09 +00001300 case Expand:
1301 SDOperand Lo, Hi;
1302 ExpandOp(Node->getOperand(1), Lo, Hi);
1303
1304 if (!TLI.isLittleEndian())
1305 std::swap(Lo, Hi);
1306
Chris Lattner55e9cde2005-05-11 04:51:16 +00001307 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1308 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001309 // If this is a vector type, then we have to calculate the increment as
1310 // the product of the element size in bytes, and the number of elements
1311 // in the high half of the vector.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001312 unsigned IncrementSize;
Nate Begemand37c1312005-11-22 18:16:00 +00001313 if (MVT::Vector == Hi.getValueType()) {
Evan Chengb97aab42006-03-01 01:09:54 +00001314 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(0))->getValue();
1315 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(1))->getVT();
Nate Begemand37c1312005-11-22 18:16:00 +00001316 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1317 } else {
1318 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1319 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001320 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1321 getIntPtrConstant(IncrementSize));
1322 assert(isTypeLegal(Tmp2.getValueType()) &&
1323 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001324 // FIXME: This sets the srcvalue of both halves to be the same, which is
1325 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001326 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1327 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001328 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1329 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001330 }
1331 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001332 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001333 case ISD::PCMARKER:
1334 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001335 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001336 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001337 case ISD::STACKSAVE:
1338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001339 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1340 Tmp1 = Result.getValue(0);
1341 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001342
Chris Lattnerb3266452006-01-13 02:50:02 +00001343 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1344 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001345 case TargetLowering::Legal: break;
1346 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001347 Tmp3 = TLI.LowerOperation(Result, DAG);
1348 if (Tmp3.Val) {
1349 Tmp1 = LegalizeOp(Tmp3);
1350 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001351 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001352 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001353 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001354 // Expand to CopyFromReg if the target set
1355 // StackPointerRegisterToSaveRestore.
1356 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001357 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001358 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001359 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001360 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001361 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1362 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001363 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001364 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001365 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001366
1367 // Since stacksave produce two values, make sure to remember that we
1368 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001369 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1370 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1371 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001372
Chris Lattnerb3266452006-01-13 02:50:02 +00001373 case ISD::STACKRESTORE:
1374 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1375 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001376 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001377
1378 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1379 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001380 case TargetLowering::Legal: break;
1381 case TargetLowering::Custom:
1382 Tmp1 = TLI.LowerOperation(Result, DAG);
1383 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001384 break;
1385 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001386 // Expand to CopyToReg if the target set
1387 // StackPointerRegisterToSaveRestore.
1388 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1389 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1390 } else {
1391 Result = Tmp1;
1392 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001393 break;
1394 }
1395 break;
1396
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001397 case ISD::READCYCLECOUNTER:
1398 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001399 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001400
1401 // Since rdcc produce two values, make sure to remember that we legalized
1402 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001403 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001404 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001405 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001406
Evan Cheng31d15fa2005-12-23 07:29:34 +00001407 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001408 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1409 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1410
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001411 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1412 "Cannot handle illegal TRUNCSTORE yet!");
1413 Tmp2 = LegalizeOp(Node->getOperand(1));
1414
1415 // The only promote case we handle is TRUNCSTORE:i1 X into
1416 // -> TRUNCSTORE:i8 (and X, 1)
1417 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1418 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1419 TargetLowering::Promote) {
1420 // Promote the bool to a mask then store.
1421 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1422 DAG.getConstant(1, Tmp2.getValueType()));
1423 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1424 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001425
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001426 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1427 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001428 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1429 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001430 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001431
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001432 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1433 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1434 default: assert(0 && "This action is not supported yet!");
1435 case TargetLowering::Legal: break;
1436 case TargetLowering::Custom:
1437 Tmp1 = TLI.LowerOperation(Result, DAG);
1438 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001439 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001440 }
1441 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001442 }
Chris Lattner39c67442005-01-14 22:08:15 +00001443 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001444 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1445 case Expand: assert(0 && "It's impossible to expand bools");
1446 case Legal:
1447 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1448 break;
1449 case Promote:
1450 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1451 break;
1452 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001453 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001454 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001455
Chris Lattnerd02b0542006-01-28 10:58:55 +00001456 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001457
Nate Begeman987121a2005-08-23 04:29:48 +00001458 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001459 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001460 case TargetLowering::Legal: break;
1461 case TargetLowering::Custom: {
1462 Tmp1 = TLI.LowerOperation(Result, DAG);
1463 if (Tmp1.Val) Result = Tmp1;
1464 break;
1465 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001466 case TargetLowering::Expand:
1467 if (Tmp1.getOpcode() == ISD::SETCC) {
1468 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1469 Tmp2, Tmp3,
1470 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1471 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001472 // Make sure the condition is either zero or one. It may have been
1473 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001474 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1475 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1476 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001477 Result = DAG.getSelectCC(Tmp1,
1478 DAG.getConstant(0, Tmp1.getValueType()),
1479 Tmp2, Tmp3, ISD::SETNE);
1480 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001481 break;
1482 case TargetLowering::Promote: {
1483 MVT::ValueType NVT =
1484 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1485 unsigned ExtOp, TruncOp;
1486 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001487 ExtOp = ISD::ANY_EXTEND;
1488 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001489 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001490 ExtOp = ISD::FP_EXTEND;
1491 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001492 }
1493 // Promote each of the values to the new type.
1494 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1495 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1496 // Perform the larger operation, then round down.
1497 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1498 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1499 break;
1500 }
1501 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001502 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001503 case ISD::SELECT_CC: {
1504 Tmp1 = Node->getOperand(0); // LHS
1505 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001506 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1507 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001508 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001509
Nate Begeman7e7f4392006-02-01 07:19:44 +00001510 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1511
1512 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1513 // the LHS is a legal SETCC itself. In this case, we need to compare
1514 // the result against zero to select between true and false values.
1515 if (Tmp2.Val == 0) {
1516 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1517 CC = DAG.getCondCode(ISD::SETNE);
1518 }
1519 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1520
1521 // Everything is legal, see if we should expand this op or something.
1522 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1523 default: assert(0 && "This action is not supported yet!");
1524 case TargetLowering::Legal: break;
1525 case TargetLowering::Custom:
1526 Tmp1 = TLI.LowerOperation(Result, DAG);
1527 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001528 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001529 }
1530 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001531 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001532 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001533 Tmp1 = Node->getOperand(0);
1534 Tmp2 = Node->getOperand(1);
1535 Tmp3 = Node->getOperand(2);
1536 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1537
1538 // If we had to Expand the SetCC operands into a SELECT node, then it may
1539 // not always be possible to return a true LHS & RHS. In this case, just
1540 // return the value we legalized, returned in the LHS
1541 if (Tmp2.Val == 0) {
1542 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001543 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001544 }
Nate Begeman987121a2005-08-23 04:29:48 +00001545
Chris Lattnerf263a232006-01-30 22:43:50 +00001546 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001547 default: assert(0 && "Cannot handle this action for SETCC yet!");
1548 case TargetLowering::Custom:
1549 isCustom = true;
1550 // FALLTHROUGH.
1551 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001552 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001553 if (isCustom) {
1554 Tmp3 = TLI.LowerOperation(Result, DAG);
1555 if (Tmp3.Val) Result = Tmp3;
1556 }
Nate Begeman987121a2005-08-23 04:29:48 +00001557 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001558 case TargetLowering::Promote: {
1559 // First step, figure out the appropriate operation to use.
1560 // Allow SETCC to not be supported for all legal data types
1561 // Mostly this targets FP
1562 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1563 MVT::ValueType OldVT = NewInTy;
1564
1565 // Scan for the appropriate larger type to use.
1566 while (1) {
1567 NewInTy = (MVT::ValueType)(NewInTy+1);
1568
1569 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1570 "Fell off of the edge of the integer world");
1571 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1572 "Fell off of the edge of the floating point world");
1573
1574 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001575 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001576 break;
1577 }
1578 if (MVT::isInteger(NewInTy))
1579 assert(0 && "Cannot promote Legal Integer SETCC yet");
1580 else {
1581 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1582 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1583 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001584 Tmp1 = LegalizeOp(Tmp1);
1585 Tmp2 = LegalizeOp(Tmp2);
1586 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001587 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001588 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001589 }
Nate Begeman987121a2005-08-23 04:29:48 +00001590 case TargetLowering::Expand:
1591 // Expand a setcc node into a select_cc of the same condition, lhs, and
1592 // rhs that selects between const 1 (true) and const 0 (false).
1593 MVT::ValueType VT = Node->getValueType(0);
1594 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1595 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1596 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001597 break;
1598 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001599 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001600 case ISD::MEMSET:
1601 case ISD::MEMCPY:
1602 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001603 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001604 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1605
1606 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1607 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1608 case Expand: assert(0 && "Cannot expand a byte!");
1609 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001610 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001611 break;
1612 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001613 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001614 break;
1615 }
1616 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001617 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001618 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001619
1620 SDOperand Tmp4;
1621 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001622 case Expand: {
1623 // Length is too big, just take the lo-part of the length.
1624 SDOperand HiPart;
1625 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1626 break;
1627 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001628 case Legal:
1629 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001630 break;
1631 case Promote:
1632 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001633 break;
1634 }
1635
1636 SDOperand Tmp5;
1637 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1638 case Expand: assert(0 && "Cannot expand this yet!");
1639 case Legal:
1640 Tmp5 = LegalizeOp(Node->getOperand(4));
1641 break;
1642 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001643 Tmp5 = PromoteOp(Node->getOperand(4));
1644 break;
1645 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001646
1647 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1648 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001649 case TargetLowering::Custom:
1650 isCustom = true;
1651 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001652 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001653 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001654 if (isCustom) {
1655 Tmp1 = TLI.LowerOperation(Result, DAG);
1656 if (Tmp1.Val) Result = Tmp1;
1657 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001658 break;
1659 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001660 // Otherwise, the target does not support this operation. Lower the
1661 // operation to an explicit libcall as appropriate.
1662 MVT::ValueType IntPtr = TLI.getPointerTy();
1663 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1664 std::vector<std::pair<SDOperand, const Type*> > Args;
1665
Reid Spencer6dced922005-01-12 14:53:45 +00001666 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001667 if (Node->getOpcode() == ISD::MEMSET) {
1668 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001669 // Extend the (previously legalized) ubyte argument to be an int value
1670 // for the call.
1671 if (Tmp3.getValueType() > MVT::i32)
1672 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1673 else
1674 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001675 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1676 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1677
1678 FnName = "memset";
1679 } else if (Node->getOpcode() == ISD::MEMCPY ||
1680 Node->getOpcode() == ISD::MEMMOVE) {
1681 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1682 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1683 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1684 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1685 } else {
1686 assert(0 && "Unknown op!");
1687 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001688
Chris Lattner85d70c62005-01-11 05:57:22 +00001689 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001690 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001691 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001692 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001693 break;
1694 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001695 }
1696 break;
1697 }
Chris Lattner5385db52005-05-09 20:23:03 +00001698
Chris Lattner4157c412005-04-02 04:00:59 +00001699 case ISD::SHL_PARTS:
1700 case ISD::SRA_PARTS:
1701 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001702 std::vector<SDOperand> Ops;
1703 bool Changed = false;
1704 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1705 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1706 Changed |= Ops.back() != Node->getOperand(i);
1707 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001708 if (Changed)
1709 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001710
Evan Cheng870e4f82006-01-09 18:31:59 +00001711 switch (TLI.getOperationAction(Node->getOpcode(),
1712 Node->getValueType(0))) {
1713 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001714 case TargetLowering::Legal: break;
1715 case TargetLowering::Custom:
1716 Tmp1 = TLI.LowerOperation(Result, DAG);
1717 if (Tmp1.Val) {
1718 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001719 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001720 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001721 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1722 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001723 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001724 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001725 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001726 return RetVal;
1727 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001728 break;
1729 }
1730
Chris Lattner13fe99c2005-04-02 05:00:07 +00001731 // Since these produce multiple values, make sure to remember that we
1732 // legalized all of them.
1733 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1734 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1735 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001736 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001737
1738 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001739 case ISD::ADD:
1740 case ISD::SUB:
1741 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001742 case ISD::MULHS:
1743 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001744 case ISD::UDIV:
1745 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001746 case ISD::AND:
1747 case ISD::OR:
1748 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001749 case ISD::SHL:
1750 case ISD::SRL:
1751 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001752 case ISD::FADD:
1753 case ISD::FSUB:
1754 case ISD::FMUL:
1755 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001756 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001757 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1758 case Expand: assert(0 && "Not possible");
1759 case Legal:
1760 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1761 break;
1762 case Promote:
1763 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1764 break;
1765 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001766
1767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001768
Andrew Lenharth72594262005-12-24 23:42:32 +00001769 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001770 default: assert(0 && "Operation not supported");
1771 case TargetLowering::Legal: break;
1772 case TargetLowering::Custom:
1773 Tmp1 = TLI.LowerOperation(Result, DAG);
1774 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001775 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001776 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001777 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001778
1779 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1780 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1781 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1782 case Expand: assert(0 && "Not possible");
1783 case Legal:
1784 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1785 break;
1786 case Promote:
1787 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1788 break;
1789 }
1790
1791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1792
1793 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1794 default: assert(0 && "Operation not supported");
1795 case TargetLowering::Custom:
1796 Tmp1 = TLI.LowerOperation(Result, DAG);
1797 if (Tmp1.Val) Result = Tmp1;
Chris Lattneraf5e26c2006-03-08 04:37:58 +00001798 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001799 case TargetLowering::Legal: break;
1800 case TargetLowering::Expand:
Chris Lattneraf5e26c2006-03-08 04:37:58 +00001801 // If this target supports fabs/fneg natively, do this efficiently.
1802 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1803 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1804 // Get the sign bit of the RHS.
1805 MVT::ValueType IVT =
1806 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1807 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1808 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1809 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1810 // Get the absolute value of the result.
1811 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1812 // Select between the nabs and abs value based on the sign bit of
1813 // the input.
1814 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1815 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1816 AbsVal),
1817 AbsVal);
1818 Result = LegalizeOp(Result);
1819 break;
1820 }
1821
1822 // Otherwise, do bitwise ops!
1823
1824 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001825 const char *FnName;
1826 if (Node->getValueType(0) == MVT::f32) {
1827 FnName = "copysignf";
1828 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1829 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1830 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1831 } else {
1832 FnName = "copysign";
1833 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1834 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1835 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1836 }
1837 SDOperand Dummy;
1838 Result = ExpandLibCall(FnName, Node, Dummy);
1839 break;
1840 }
1841 break;
1842
Nate Begeman5965bd12006-02-17 05:43:56 +00001843 case ISD::ADDC:
1844 case ISD::SUBC:
1845 Tmp1 = LegalizeOp(Node->getOperand(0));
1846 Tmp2 = LegalizeOp(Node->getOperand(1));
1847 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1848 // Since this produces two values, make sure to remember that we legalized
1849 // both of them.
1850 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1851 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1852 return Result;
1853 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001854
Nate Begeman5965bd12006-02-17 05:43:56 +00001855 case ISD::ADDE:
1856 case ISD::SUBE:
1857 Tmp1 = LegalizeOp(Node->getOperand(0));
1858 Tmp2 = LegalizeOp(Node->getOperand(1));
1859 Tmp3 = LegalizeOp(Node->getOperand(2));
1860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1861 // Since this produces two values, make sure to remember that we legalized
1862 // both of them.
1863 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1864 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1865 return Result;
1866 break;
1867
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001868 case ISD::BUILD_PAIR: {
1869 MVT::ValueType PairTy = Node->getValueType(0);
1870 // TODO: handle the case where the Lo and Hi operands are not of legal type
1871 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1872 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1873 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001874 case TargetLowering::Promote:
1875 case TargetLowering::Custom:
1876 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001877 case TargetLowering::Legal:
1878 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1879 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1880 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001881 case TargetLowering::Expand:
1882 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1883 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1884 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1885 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1886 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001887 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001888 break;
1889 }
1890 break;
1891 }
1892
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001893 case ISD::UREM:
1894 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001895 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001896 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001898
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001899 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001900 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1901 case TargetLowering::Custom:
1902 isCustom = true;
1903 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001904 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001905 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001906 if (isCustom) {
1907 Tmp1 = TLI.LowerOperation(Result, DAG);
1908 if (Tmp1.Val) Result = Tmp1;
1909 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001910 break;
Chris Lattner81914422005-08-03 20:31:37 +00001911 case TargetLowering::Expand:
1912 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001913 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00001914 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001915 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00001916 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1917 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1918 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1919 } else {
1920 // Floating point mod -> fmod libcall.
1921 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1922 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00001923 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001924 }
1925 break;
1926 }
1927 break;
Nate Begemane74795c2006-01-25 18:21:52 +00001928 case ISD::VAARG: {
1929 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1930 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1931
Chris Lattner364b89a2006-01-28 07:42:08 +00001932 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00001933 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1934 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001935 case TargetLowering::Custom:
1936 isCustom = true;
1937 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001938 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001939 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1940 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001941 Tmp1 = Result.getValue(1);
1942
1943 if (isCustom) {
1944 Tmp2 = TLI.LowerOperation(Result, DAG);
1945 if (Tmp2.Val) {
1946 Result = LegalizeOp(Tmp2);
1947 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1948 }
1949 }
Nate Begemane74795c2006-01-25 18:21:52 +00001950 break;
1951 case TargetLowering::Expand: {
1952 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1953 Node->getOperand(2));
1954 // Increment the pointer, VAList, to the next vaarg
1955 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1956 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1957 TLI.getPointerTy()));
1958 // Store the incremented VAList to the legalized pointer
1959 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1960 Node->getOperand(2));
1961 // Load the actual argument out of the pointer VAList
1962 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001963 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00001964 Result = LegalizeOp(Result);
1965 break;
1966 }
1967 }
1968 // Since VAARG produces two values, make sure to remember that we
1969 // legalized both of them.
1970 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001971 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1972 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00001973 }
1974
1975 case ISD::VACOPY:
1976 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1977 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1978 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1979
1980 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1981 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001982 case TargetLowering::Custom:
1983 isCustom = true;
1984 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001985 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001986 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1987 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001988 if (isCustom) {
1989 Tmp1 = TLI.LowerOperation(Result, DAG);
1990 if (Tmp1.Val) Result = Tmp1;
1991 }
Nate Begemane74795c2006-01-25 18:21:52 +00001992 break;
1993 case TargetLowering::Expand:
1994 // This defaults to loading a pointer from the input and storing it to the
1995 // output, returning the chain.
1996 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1997 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1998 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00001999 break;
2000 }
2001 break;
2002
2003 case ISD::VAEND:
2004 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2005 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2006
2007 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2008 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002009 case TargetLowering::Custom:
2010 isCustom = true;
2011 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002012 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002014 if (isCustom) {
2015 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2016 if (Tmp1.Val) Result = Tmp1;
2017 }
Nate Begemane74795c2006-01-25 18:21:52 +00002018 break;
2019 case TargetLowering::Expand:
2020 Result = Tmp1; // Default to a no-op, return the chain
2021 break;
2022 }
2023 break;
2024
2025 case ISD::VASTART:
2026 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2027 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2028
Chris Lattnerd02b0542006-01-28 10:58:55 +00002029 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2030
Nate Begemane74795c2006-01-25 18:21:52 +00002031 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2032 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002033 case TargetLowering::Legal: break;
2034 case TargetLowering::Custom:
2035 Tmp1 = TLI.LowerOperation(Result, DAG);
2036 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002037 break;
2038 }
2039 break;
2040
Nate Begeman1b8121b2006-01-11 21:21:00 +00002041 case ISD::ROTL:
2042 case ISD::ROTR:
2043 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2044 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002045
2046 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2047 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002048 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002049 break;
2050
Nate Begeman2fba8a32006-01-14 03:14:10 +00002051 case ISD::BSWAP:
2052 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2053 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002054 case TargetLowering::Custom:
2055 assert(0 && "Cannot custom legalize this yet!");
2056 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002057 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002058 break;
2059 case TargetLowering::Promote: {
2060 MVT::ValueType OVT = Tmp1.getValueType();
2061 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2062 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002063
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002064 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2065 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2066 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2067 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2068 break;
2069 }
2070 case TargetLowering::Expand:
2071 Result = ExpandBSWAP(Tmp1);
2072 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002073 }
2074 break;
2075
Andrew Lenharth5e177822005-05-03 17:19:30 +00002076 case ISD::CTPOP:
2077 case ISD::CTTZ:
2078 case ISD::CTLZ:
2079 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2080 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002081 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002082 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002083 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002084 break;
2085 case TargetLowering::Promote: {
2086 MVT::ValueType OVT = Tmp1.getValueType();
2087 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002088
2089 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002090 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2091 // Perform the larger operation, then subtract if needed.
2092 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002093 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002094 case ISD::CTPOP:
2095 Result = Tmp1;
2096 break;
2097 case ISD::CTTZ:
2098 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002099 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2100 DAG.getConstant(getSizeInBits(NVT), NVT),
2101 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002102 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002103 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2104 break;
2105 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002106 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002107 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2108 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002109 getSizeInBits(OVT), NVT));
2110 break;
2111 }
2112 break;
2113 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002114 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002115 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002116 break;
2117 }
2118 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002119
Chris Lattner13fe99c2005-04-02 05:00:07 +00002120 // Unary operators
2121 case ISD::FABS:
2122 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002123 case ISD::FSQRT:
2124 case ISD::FSIN:
2125 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002126 Tmp1 = LegalizeOp(Node->getOperand(0));
2127 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002128 case TargetLowering::Promote:
2129 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002130 isCustom = true;
2131 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002132 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002133 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002134 if (isCustom) {
2135 Tmp1 = TLI.LowerOperation(Result, DAG);
2136 if (Tmp1.Val) Result = Tmp1;
2137 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002138 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002139 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002140 switch (Node->getOpcode()) {
2141 default: assert(0 && "Unreachable!");
2142 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002143 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2144 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002145 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002146 break;
Chris Lattner80026402005-04-30 04:43:14 +00002147 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002148 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2149 MVT::ValueType VT = Node->getValueType(0);
2150 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002151 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002152 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2153 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002154 break;
2155 }
2156 case ISD::FSQRT:
2157 case ISD::FSIN:
2158 case ISD::FCOS: {
2159 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002160 const char *FnName = 0;
2161 switch(Node->getOpcode()) {
2162 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2163 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2164 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2165 default: assert(0 && "Unreachable!");
2166 }
Nate Begeman77558da2005-08-04 21:43:28 +00002167 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002168 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002169 break;
2170 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002171 }
2172 break;
2173 }
2174 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002175
2176 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002177 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002178 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002179 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002180 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2181 Node->getOperand(0).getValueType())) {
2182 default: assert(0 && "Unknown operation action!");
2183 case TargetLowering::Expand:
2184 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2185 break;
2186 case TargetLowering::Legal:
2187 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002188 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002189 break;
2190 }
2191 }
2192 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002193 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002194 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002195 case ISD::UINT_TO_FP: {
2196 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002197 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2198 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002199 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002200 Node->getOperand(0).getValueType())) {
2201 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002202 case TargetLowering::Custom:
2203 isCustom = true;
2204 // FALLTHROUGH
2205 case TargetLowering::Legal:
2206 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002207 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002208 if (isCustom) {
2209 Tmp1 = TLI.LowerOperation(Result, DAG);
2210 if (Tmp1.Val) Result = Tmp1;
2211 }
2212 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002213 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002214 Result = ExpandLegalINT_TO_FP(isSigned,
2215 LegalizeOp(Node->getOperand(0)),
2216 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002217 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002218 case TargetLowering::Promote:
2219 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2220 Node->getValueType(0),
2221 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002222 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002223 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002224 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002225 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002226 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2227 Node->getValueType(0), Node->getOperand(0));
2228 break;
2229 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002230 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002231 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002232 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2233 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002234 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002235 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2236 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002237 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002238 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2239 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002240 break;
2241 }
2242 break;
2243 }
2244 case ISD::TRUNCATE:
2245 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2246 case Legal:
2247 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002248 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002249 break;
2250 case Expand:
2251 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2252
2253 // Since the result is legal, we should just be able to truncate the low
2254 // part of the source.
2255 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2256 break;
2257 case Promote:
2258 Result = PromoteOp(Node->getOperand(0));
2259 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2260 break;
2261 }
2262 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002263
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002264 case ISD::FP_TO_SINT:
2265 case ISD::FP_TO_UINT:
2266 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2267 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002268 Tmp1 = LegalizeOp(Node->getOperand(0));
2269
Chris Lattner44fe26f2005-07-29 00:11:56 +00002270 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2271 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002272 case TargetLowering::Custom:
2273 isCustom = true;
2274 // FALLTHROUGH
2275 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002276 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002277 if (isCustom) {
2278 Tmp1 = TLI.LowerOperation(Result, DAG);
2279 if (Tmp1.Val) Result = Tmp1;
2280 }
2281 break;
2282 case TargetLowering::Promote:
2283 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2284 Node->getOpcode() == ISD::FP_TO_SINT);
2285 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002286 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002287 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2288 SDOperand True, False;
2289 MVT::ValueType VT = Node->getOperand(0).getValueType();
2290 MVT::ValueType NVT = Node->getValueType(0);
2291 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2292 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2293 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2294 Node->getOperand(0), Tmp2, ISD::SETLT);
2295 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2296 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002297 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002298 Tmp2));
2299 False = DAG.getNode(ISD::XOR, NVT, False,
2300 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002301 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2302 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002303 } else {
2304 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2305 }
2306 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002307 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002308 break;
2309 case Expand:
2310 assert(0 && "Shouldn't need to expand other operators here!");
2311 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002312 Tmp1 = PromoteOp(Node->getOperand(0));
2313 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2314 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002315 break;
2316 }
2317 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002318
Chris Lattner7753f172005-09-02 00:18:10 +00002319 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002320 case ISD::ZERO_EXTEND:
2321 case ISD::SIGN_EXTEND:
2322 case ISD::FP_EXTEND:
2323 case ISD::FP_ROUND:
2324 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002325 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002326 case Legal:
2327 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002328 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002329 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002330 case Promote:
2331 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002332 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002333 Tmp1 = PromoteOp(Node->getOperand(0));
2334 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002335 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002336 case ISD::ZERO_EXTEND:
2337 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002338 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002339 Result = DAG.getZeroExtendInReg(Result,
2340 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002341 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002342 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002343 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002344 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002345 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002346 Result,
2347 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002348 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002349 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002350 Result = PromoteOp(Node->getOperand(0));
2351 if (Result.getValueType() != Op.getValueType())
2352 // Dynamically dead while we have only 2 FP types.
2353 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2354 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002355 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002356 Result = PromoteOp(Node->getOperand(0));
2357 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2358 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002359 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002360 }
2361 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002362 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002363 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002364 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002365 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002366
2367 // If this operation is not supported, convert it to a shl/shr or load/store
2368 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002369 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2370 default: assert(0 && "This action not supported for this op yet!");
2371 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002372 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002373 break;
2374 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002375 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002376 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002377 // NOTE: we could fall back on load/store here too for targets without
2378 // SAR. However, it is doubtful that any exist.
2379 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2380 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002381 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002382 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2383 Node->getOperand(0), ShiftCst);
2384 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2385 Result, ShiftCst);
2386 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2387 // The only way we can lower this is to turn it into a STORETRUNC,
2388 // EXTLOAD pair, targetting a temporary location (a stack slot).
2389
2390 // NOTE: there is a choice here between constantly creating new stack
2391 // slots and always reusing the same one. We currently always create
2392 // new ones, as reuse may inhibit scheduling.
2393 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2394 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2395 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2396 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002397 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002398 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2399 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2400 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002401 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002402 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002403 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2404 Result, StackSlot, DAG.getSrcValue(NULL),
2405 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002406 } else {
2407 assert(0 && "Unknown op");
2408 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002409 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002410 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002411 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002412 }
Chris Lattner99222f72005-01-15 07:15:18 +00002413 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002414
2415 // Make sure that the generated code is itself legal.
2416 if (Result != Op)
2417 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002418
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002419 // Note that LegalizeOp may be reentered even from single-use nodes, which
2420 // means that we always must cache transformed nodes.
2421 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002422 return Result;
2423}
2424
Chris Lattner4d978642005-01-15 22:16:26 +00002425/// PromoteOp - Given an operation that produces a value in an invalid type,
2426/// promote it to compute the value into a larger type. The produced value will
2427/// have the correct bits for the low portion of the register, but no guarantee
2428/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002429SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2430 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002431 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002432 assert(getTypeAction(VT) == Promote &&
2433 "Caller should expand or legalize operands that are not promotable!");
2434 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2435 "Cannot promote to smaller type!");
2436
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002437 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002438 SDOperand Result;
2439 SDNode *Node = Op.Val;
2440
Chris Lattner1a570f12005-09-02 20:32:45 +00002441 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2442 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002443
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002444 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002445 case ISD::CopyFromReg:
2446 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002447 default:
2448 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2449 assert(0 && "Do not know how to promote this operator!");
2450 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002451 case ISD::UNDEF:
2452 Result = DAG.getNode(ISD::UNDEF, NVT);
2453 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002454 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002455 if (VT != MVT::i1)
2456 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2457 else
2458 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002459 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2460 break;
2461 case ISD::ConstantFP:
2462 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2463 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2464 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002465
Chris Lattner2cb338d2005-01-18 02:59:52 +00002466 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002467 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002468 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2469 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002470 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002471
2472 case ISD::TRUNCATE:
2473 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2474 case Legal:
2475 Result = LegalizeOp(Node->getOperand(0));
2476 assert(Result.getValueType() >= NVT &&
2477 "This truncation doesn't make sense!");
2478 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2479 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2480 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002481 case Promote:
2482 // The truncation is not required, because we don't guarantee anything
2483 // about high bits anyway.
2484 Result = PromoteOp(Node->getOperand(0));
2485 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002486 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002487 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2488 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002489 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002490 }
2491 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002492 case ISD::SIGN_EXTEND:
2493 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002494 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002495 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2496 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2497 case Legal:
2498 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002499 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002500 break;
2501 case Promote:
2502 // Promote the reg if it's smaller.
2503 Result = PromoteOp(Node->getOperand(0));
2504 // The high bits are not guaranteed to be anything. Insert an extend.
2505 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002506 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002507 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002508 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002509 Result = DAG.getZeroExtendInReg(Result,
2510 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002511 break;
2512 }
2513 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002514 case ISD::BIT_CONVERT:
2515 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2516 Result = PromoteOp(Result);
2517 break;
2518
Chris Lattner4d978642005-01-15 22:16:26 +00002519 case ISD::FP_EXTEND:
2520 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2521 case ISD::FP_ROUND:
2522 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2523 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2524 case Promote: assert(0 && "Unreachable with 2 FP types!");
2525 case Legal:
2526 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002527 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002528 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002529 break;
2530 }
2531 break;
2532
2533 case ISD::SINT_TO_FP:
2534 case ISD::UINT_TO_FP:
2535 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2536 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002537 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002538 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002539 break;
2540
2541 case Promote:
2542 Result = PromoteOp(Node->getOperand(0));
2543 if (Node->getOpcode() == ISD::SINT_TO_FP)
2544 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002545 Result,
2546 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002547 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002548 Result = DAG.getZeroExtendInReg(Result,
2549 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002550 // No extra round required here.
2551 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002552 break;
2553 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002554 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2555 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002556 // Round if we cannot tolerate excess precision.
2557 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002558 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2559 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002560 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002561 }
Chris Lattner4d978642005-01-15 22:16:26 +00002562 break;
2563
Chris Lattner268d4572005-12-09 17:32:47 +00002564 case ISD::SIGN_EXTEND_INREG:
2565 Result = PromoteOp(Node->getOperand(0));
2566 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2567 Node->getOperand(1));
2568 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002569 case ISD::FP_TO_SINT:
2570 case ISD::FP_TO_UINT:
2571 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2572 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002573 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002574 break;
2575 case Promote:
2576 // The input result is prerounded, so we don't have to do anything
2577 // special.
2578 Tmp1 = PromoteOp(Node->getOperand(0));
2579 break;
2580 case Expand:
2581 assert(0 && "not implemented");
2582 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002583 // If we're promoting a UINT to a larger size, check to see if the new node
2584 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2585 // we can use that instead. This allows us to generate better code for
2586 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2587 // legal, such as PowerPC.
2588 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002589 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002590 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2591 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002592 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2593 } else {
2594 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2595 }
Chris Lattner4d978642005-01-15 22:16:26 +00002596 break;
2597
Chris Lattner13fe99c2005-04-02 05:00:07 +00002598 case ISD::FABS:
2599 case ISD::FNEG:
2600 Tmp1 = PromoteOp(Node->getOperand(0));
2601 assert(Tmp1.getValueType() == NVT);
2602 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2603 // NOTE: we do not have to do any extra rounding here for
2604 // NoExcessFPPrecision, because we know the input will have the appropriate
2605 // precision, and these operations don't modify precision at all.
2606 break;
2607
Chris Lattner9d6fa982005-04-28 21:44:33 +00002608 case ISD::FSQRT:
2609 case ISD::FSIN:
2610 case ISD::FCOS:
2611 Tmp1 = PromoteOp(Node->getOperand(0));
2612 assert(Tmp1.getValueType() == NVT);
2613 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002614 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002615 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2616 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002617 break;
2618
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002619 case ISD::AND:
2620 case ISD::OR:
2621 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002622 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002623 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002624 case ISD::MUL:
2625 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002626 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002627 // that too is okay if they are integer operations.
2628 Tmp1 = PromoteOp(Node->getOperand(0));
2629 Tmp2 = PromoteOp(Node->getOperand(1));
2630 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2631 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002632 break;
2633 case ISD::FADD:
2634 case ISD::FSUB:
2635 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002636 Tmp1 = PromoteOp(Node->getOperand(0));
2637 Tmp2 = PromoteOp(Node->getOperand(1));
2638 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2639 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2640
2641 // Floating point operations will give excess precision that we may not be
2642 // able to tolerate. If we DO allow excess precision, just leave it,
2643 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002644 // FIXME: Why would we need to round FP ops more than integer ones?
2645 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002646 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002647 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2648 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002649 break;
2650
Chris Lattner4d978642005-01-15 22:16:26 +00002651 case ISD::SDIV:
2652 case ISD::SREM:
2653 // These operators require that their input be sign extended.
2654 Tmp1 = PromoteOp(Node->getOperand(0));
2655 Tmp2 = PromoteOp(Node->getOperand(1));
2656 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002657 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2658 DAG.getValueType(VT));
2659 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2660 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002661 }
2662 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2663
2664 // Perform FP_ROUND: this is probably overly pessimistic.
2665 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002666 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2667 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002668 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002669 case ISD::FDIV:
2670 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002671 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002672 // These operators require that their input be fp extended.
2673 Tmp1 = PromoteOp(Node->getOperand(0));
2674 Tmp2 = PromoteOp(Node->getOperand(1));
2675 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2676
2677 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002678 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002679 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2680 DAG.getValueType(VT));
2681 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002682
2683 case ISD::UDIV:
2684 case ISD::UREM:
2685 // These operators require that their input be zero extended.
2686 Tmp1 = PromoteOp(Node->getOperand(0));
2687 Tmp2 = PromoteOp(Node->getOperand(1));
2688 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002689 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2690 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002691 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2692 break;
2693
2694 case ISD::SHL:
2695 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002696 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002697 break;
2698 case ISD::SRA:
2699 // The input value must be properly sign extended.
2700 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002701 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2702 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002703 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002704 break;
2705 case ISD::SRL:
2706 // The input value must be properly zero extended.
2707 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002708 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002709 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002710 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002711
2712 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002713 Tmp1 = Node->getOperand(0); // Get the chain.
2714 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002715 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2716 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2717 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2718 } else {
2719 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2720 Node->getOperand(2));
2721 // Increment the pointer, VAList, to the next vaarg
2722 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2723 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2724 TLI.getPointerTy()));
2725 // Store the incremented VAList to the legalized pointer
2726 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2727 Node->getOperand(2));
2728 // Load the actual argument out of the pointer VAList
2729 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2730 DAG.getSrcValue(0), VT);
2731 }
2732 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002733 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002734 break;
2735
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002736 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002737 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2738 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002739 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002740 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002741 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002742 case ISD::SEXTLOAD:
2743 case ISD::ZEXTLOAD:
2744 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002745 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2746 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002747 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002748 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002749 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002750 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002751 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002752 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2753 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002754 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002755 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002756 case ISD::SELECT_CC:
2757 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2758 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2759 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002760 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002761 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002762 case ISD::BSWAP:
2763 Tmp1 = Node->getOperand(0);
2764 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2765 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2766 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2767 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2768 TLI.getShiftAmountTy()));
2769 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002770 case ISD::CTPOP:
2771 case ISD::CTTZ:
2772 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002773 // Zero extend the argument
2774 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002775 // Perform the larger operation, then subtract if needed.
2776 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002777 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002778 case ISD::CTPOP:
2779 Result = Tmp1;
2780 break;
2781 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002782 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002783 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002784 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002785 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002786 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002787 break;
2788 case ISD::CTLZ:
2789 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002790 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2791 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002792 getSizeInBits(VT), NVT));
2793 break;
2794 }
2795 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002796 }
2797
2798 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002799
2800 // Make sure the result is itself legal.
2801 Result = LegalizeOp(Result);
2802
2803 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002804 AddPromotedOperand(Op, Result);
2805 return Result;
2806}
Chris Lattnerdc750592005-01-07 07:47:09 +00002807
Nate Begeman7e7f4392006-02-01 07:19:44 +00002808/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2809/// with condition CC on the current target. This usually involves legalizing
2810/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2811/// there may be no choice but to create a new SetCC node to represent the
2812/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2813/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2814void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2815 SDOperand &RHS,
2816 SDOperand &CC) {
2817 SDOperand Tmp1, Tmp2, Result;
2818
2819 switch (getTypeAction(LHS.getValueType())) {
2820 case Legal:
2821 Tmp1 = LegalizeOp(LHS); // LHS
2822 Tmp2 = LegalizeOp(RHS); // RHS
2823 break;
2824 case Promote:
2825 Tmp1 = PromoteOp(LHS); // LHS
2826 Tmp2 = PromoteOp(RHS); // RHS
2827
2828 // If this is an FP compare, the operands have already been extended.
2829 if (MVT::isInteger(LHS.getValueType())) {
2830 MVT::ValueType VT = LHS.getValueType();
2831 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2832
2833 // Otherwise, we have to insert explicit sign or zero extends. Note
2834 // that we could insert sign extends for ALL conditions, but zero extend
2835 // is cheaper on many machines (an AND instead of two shifts), so prefer
2836 // it.
2837 switch (cast<CondCodeSDNode>(CC)->get()) {
2838 default: assert(0 && "Unknown integer comparison!");
2839 case ISD::SETEQ:
2840 case ISD::SETNE:
2841 case ISD::SETUGE:
2842 case ISD::SETUGT:
2843 case ISD::SETULE:
2844 case ISD::SETULT:
2845 // ALL of these operations will work if we either sign or zero extend
2846 // the operands (including the unsigned comparisons!). Zero extend is
2847 // usually a simpler/cheaper operation, so prefer it.
2848 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2849 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2850 break;
2851 case ISD::SETGE:
2852 case ISD::SETGT:
2853 case ISD::SETLT:
2854 case ISD::SETLE:
2855 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2856 DAG.getValueType(VT));
2857 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2858 DAG.getValueType(VT));
2859 break;
2860 }
2861 }
2862 break;
2863 case Expand:
2864 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2865 ExpandOp(LHS, LHSLo, LHSHi);
2866 ExpandOp(RHS, RHSLo, RHSHi);
2867 switch (cast<CondCodeSDNode>(CC)->get()) {
2868 case ISD::SETEQ:
2869 case ISD::SETNE:
2870 if (RHSLo == RHSHi)
2871 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2872 if (RHSCST->isAllOnesValue()) {
2873 // Comparison to -1.
2874 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2875 Tmp2 = RHSLo;
2876 break;
2877 }
2878
2879 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2880 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2881 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2882 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2883 break;
2884 default:
2885 // If this is a comparison of the sign bit, just look at the top part.
2886 // X > -1, x < 0
2887 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2888 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2889 CST->getValue() == 0) || // X < 0
2890 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2891 CST->isAllOnesValue())) { // X > -1
2892 Tmp1 = LHSHi;
2893 Tmp2 = RHSHi;
2894 break;
2895 }
2896
2897 // FIXME: This generated code sucks.
2898 ISD::CondCode LowCC;
2899 switch (cast<CondCodeSDNode>(CC)->get()) {
2900 default: assert(0 && "Unknown integer setcc!");
2901 case ISD::SETLT:
2902 case ISD::SETULT: LowCC = ISD::SETULT; break;
2903 case ISD::SETGT:
2904 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2905 case ISD::SETLE:
2906 case ISD::SETULE: LowCC = ISD::SETULE; break;
2907 case ISD::SETGE:
2908 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2909 }
2910
2911 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2912 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2913 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2914
2915 // NOTE: on targets without efficient SELECT of bools, we can always use
2916 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2917 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2918 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2919 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2920 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2921 Result, Tmp1, Tmp2));
2922 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00002923 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00002924 }
2925 }
2926 LHS = Tmp1;
2927 RHS = Tmp2;
2928}
2929
Chris Lattner36e663d2005-12-23 00:16:34 +00002930/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002931/// The resultant code need not be legal. Note that SrcOp is the input operand
2932/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002933SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2934 SDOperand SrcOp) {
2935 // Create the stack frame object.
2936 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2937 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002938 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002939 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2940
2941 // Emit a store to the stack slot.
2942 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002943 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002944 // Result is a load from the stack slot.
2945 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2946}
2947
Chris Lattner4157c412005-04-02 04:00:59 +00002948void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2949 SDOperand Op, SDOperand Amt,
2950 SDOperand &Lo, SDOperand &Hi) {
2951 // Expand the subcomponents.
2952 SDOperand LHSL, LHSH;
2953 ExpandOp(Op, LHSL, LHSH);
2954
2955 std::vector<SDOperand> Ops;
2956 Ops.push_back(LHSL);
2957 Ops.push_back(LHSH);
2958 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002959 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002960 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002961 Hi = Lo.getValue(1);
2962}
2963
2964
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002965/// ExpandShift - Try to find a clever way to expand this shift operation out to
2966/// smaller elements. If we can't find a way that is more efficient than a
2967/// libcall on this target, return false. Otherwise, return true with the
2968/// low-parts expanded into Lo and Hi.
2969bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2970 SDOperand &Lo, SDOperand &Hi) {
2971 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2972 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002973
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002974 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002975 SDOperand ShAmt = LegalizeOp(Amt);
2976 MVT::ValueType ShTy = ShAmt.getValueType();
2977 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2978 unsigned NVTBits = MVT::getSizeInBits(NVT);
2979
2980 // Handle the case when Amt is an immediate. Other cases are currently broken
2981 // and are disabled.
2982 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2983 unsigned Cst = CN->getValue();
2984 // Expand the incoming operand to be shifted, so that we have its parts
2985 SDOperand InL, InH;
2986 ExpandOp(Op, InL, InH);
2987 switch(Opc) {
2988 case ISD::SHL:
2989 if (Cst > VTBits) {
2990 Lo = DAG.getConstant(0, NVT);
2991 Hi = DAG.getConstant(0, NVT);
2992 } else if (Cst > NVTBits) {
2993 Lo = DAG.getConstant(0, NVT);
2994 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002995 } else if (Cst == NVTBits) {
2996 Lo = DAG.getConstant(0, NVT);
2997 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002998 } else {
2999 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3000 Hi = DAG.getNode(ISD::OR, NVT,
3001 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3002 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3003 }
3004 return true;
3005 case ISD::SRL:
3006 if (Cst > VTBits) {
3007 Lo = DAG.getConstant(0, NVT);
3008 Hi = DAG.getConstant(0, NVT);
3009 } else if (Cst > NVTBits) {
3010 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3011 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003012 } else if (Cst == NVTBits) {
3013 Lo = InH;
3014 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003015 } else {
3016 Lo = DAG.getNode(ISD::OR, NVT,
3017 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3018 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3019 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3020 }
3021 return true;
3022 case ISD::SRA:
3023 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003024 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003025 DAG.getConstant(NVTBits-1, ShTy));
3026 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003027 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003028 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003029 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003030 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003031 } else if (Cst == NVTBits) {
3032 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003033 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003034 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003035 } else {
3036 Lo = DAG.getNode(ISD::OR, NVT,
3037 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3038 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3039 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3040 }
3041 return true;
3042 }
3043 }
Nate Begemanb0674922005-04-06 21:13:14 +00003044 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003045}
Chris Lattneraac464e2005-01-21 06:05:23 +00003046
Chris Lattner4add7e32005-01-23 04:42:50 +00003047
Chris Lattneraac464e2005-01-21 06:05:23 +00003048// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3049// does not fit into a register, return the lo part and set the hi part to the
3050// by-reg argument. If it does fit into a single register, return the result
3051// and leave the Hi part unset.
3052SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3053 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003054 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3055 // The input chain to this libcall is the entry node of the function.
3056 // Legalizing the call will automatically add the previous call to the
3057 // dependence.
3058 SDOperand InChain = DAG.getEntryNode();
3059
Chris Lattneraac464e2005-01-21 06:05:23 +00003060 TargetLowering::ArgListTy Args;
3061 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3062 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3063 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3064 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3065 }
3066 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003067
Chris Lattner06bbeb62005-05-11 19:02:11 +00003068 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003069 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003070 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003071 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3072 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003073
Chris Lattner462505f2006-02-13 09:18:02 +00003074 // Legalize the call sequence, starting with the chain. This will advance
3075 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3076 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3077 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003078 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003079 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003080 default: assert(0 && "Unknown thing");
3081 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003082 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003083 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003084 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003085 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003086 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003087 }
Chris Lattner63022662005-09-02 20:26:58 +00003088 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003089}
3090
Chris Lattner4add7e32005-01-23 04:42:50 +00003091
Chris Lattneraac464e2005-01-21 06:05:23 +00003092/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3093/// destination type is legal.
3094SDOperand SelectionDAGLegalize::
3095ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003096 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003097 assert(getTypeAction(Source.getValueType()) == Expand &&
3098 "This is not an expansion!");
3099 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3100
Chris Lattner06bbeb62005-05-11 19:02:11 +00003101 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003102 assert(Source.getValueType() == MVT::i64 &&
3103 "This only works for 64-bit -> FP");
3104 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3105 // incoming integer is set. To handle this, we dynamically test to see if
3106 // it is set, and, if so, add a fudge factor.
3107 SDOperand Lo, Hi;
3108 ExpandOp(Source, Lo, Hi);
3109
Chris Lattner2a4f7312005-05-13 04:45:13 +00003110 // If this is unsigned, and not supported, first perform the conversion to
3111 // signed, then adjust the result if the sign bit is set.
3112 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3113 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3114
Chris Lattnerd47675e2005-08-09 20:20:18 +00003115 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3116 DAG.getConstant(0, Hi.getValueType()),
3117 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003118 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3119 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3120 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003121 uint64_t FF = 0x5f800000ULL;
3122 if (TLI.isLittleEndian()) FF <<= 32;
3123 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003124
Chris Lattnerc30405e2005-08-26 17:15:30 +00003125 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003126 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3127 SDOperand FudgeInReg;
3128 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003129 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3130 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003131 else {
3132 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003133 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3134 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003135 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003136 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003137 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003138
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003139 // Check to see if the target has a custom way to lower this. If so, use it.
3140 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3141 default: assert(0 && "This action not implemented for this operation!");
3142 case TargetLowering::Legal:
3143 case TargetLowering::Expand:
3144 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003145 case TargetLowering::Custom: {
3146 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3147 Source), DAG);
3148 if (NV.Val)
3149 return LegalizeOp(NV);
3150 break; // The target decided this was legal after all
3151 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003152 }
3153
Chris Lattner153587e2005-05-12 07:00:44 +00003154 // Expand the source, then glue it back together for the call. We must expand
3155 // the source in case it is shared (this pass of legalize must traverse it).
3156 SDOperand SrcLo, SrcHi;
3157 ExpandOp(Source, SrcLo, SrcHi);
3158 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3159
Chris Lattner06bbeb62005-05-11 19:02:11 +00003160 const char *FnName = 0;
3161 if (DestTy == MVT::f32)
3162 FnName = "__floatdisf";
3163 else {
3164 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3165 FnName = "__floatdidf";
3166 }
Chris Lattner462505f2006-02-13 09:18:02 +00003167
3168 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3169 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003170 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003171}
Misha Brukman835702a2005-04-21 22:36:52 +00003172
Chris Lattner689bdcc2006-01-28 08:25:58 +00003173/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3174/// INT_TO_FP operation of the specified operand when the target requests that
3175/// we expand it. At this point, we know that the result and operand types are
3176/// legal for the target.
3177SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3178 SDOperand Op0,
3179 MVT::ValueType DestVT) {
3180 if (Op0.getValueType() == MVT::i32) {
3181 // simple 32-bit [signed|unsigned] integer to float/double expansion
3182
3183 // get the stack frame index of a 8 byte buffer
3184 MachineFunction &MF = DAG.getMachineFunction();
3185 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3186 // get address of 8 byte buffer
3187 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3188 // word offset constant for Hi/Lo address computation
3189 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3190 // set up Hi and Lo (into buffer) address based on endian
3191 SDOperand Hi, Lo;
3192 if (TLI.isLittleEndian()) {
3193 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3194 Lo = StackSlot;
3195 } else {
3196 Hi = StackSlot;
3197 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3198 }
3199 // if signed map to unsigned space
3200 SDOperand Op0Mapped;
3201 if (isSigned) {
3202 // constant used to invert sign bit (signed to unsigned mapping)
3203 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3204 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3205 } else {
3206 Op0Mapped = Op0;
3207 }
3208 // store the lo of the constructed double - based on integer input
3209 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3210 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3211 // initial hi portion of constructed double
3212 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3213 // store the hi of the constructed double - biased exponent
3214 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3215 InitialHi, Hi, DAG.getSrcValue(NULL));
3216 // load the constructed double
3217 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3218 DAG.getSrcValue(NULL));
3219 // FP constant to bias correct the final result
3220 SDOperand Bias = DAG.getConstantFP(isSigned ?
3221 BitsToDouble(0x4330000080000000ULL)
3222 : BitsToDouble(0x4330000000000000ULL),
3223 MVT::f64);
3224 // subtract the bias
3225 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3226 // final result
3227 SDOperand Result;
3228 // handle final rounding
3229 if (DestVT == MVT::f64) {
3230 // do nothing
3231 Result = Sub;
3232 } else {
3233 // if f32 then cast to f32
3234 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3235 }
3236 return Result;
3237 }
3238 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3239 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3240
3241 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3242 DAG.getConstant(0, Op0.getValueType()),
3243 ISD::SETLT);
3244 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3245 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3246 SignSet, Four, Zero);
3247
3248 // If the sign bit of the integer is set, the large number will be treated
3249 // as a negative number. To counteract this, the dynamic code adds an
3250 // offset depending on the data type.
3251 uint64_t FF;
3252 switch (Op0.getValueType()) {
3253 default: assert(0 && "Unsupported integer type!");
3254 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3255 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3256 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3257 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3258 }
3259 if (TLI.isLittleEndian()) FF <<= 32;
3260 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3261
3262 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3263 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3264 SDOperand FudgeInReg;
3265 if (DestVT == MVT::f32)
3266 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3267 DAG.getSrcValue(NULL));
3268 else {
3269 assert(DestVT == MVT::f64 && "Unexpected conversion");
3270 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3271 DAG.getEntryNode(), CPIdx,
3272 DAG.getSrcValue(NULL), MVT::f32));
3273 }
3274
3275 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3276}
3277
3278/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3279/// *INT_TO_FP operation of the specified operand when the target requests that
3280/// we promote it. At this point, we know that the result and operand types are
3281/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3282/// operation that takes a larger input.
3283SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3284 MVT::ValueType DestVT,
3285 bool isSigned) {
3286 // First step, figure out the appropriate *INT_TO_FP operation to use.
3287 MVT::ValueType NewInTy = LegalOp.getValueType();
3288
3289 unsigned OpToUse = 0;
3290
3291 // Scan for the appropriate larger type to use.
3292 while (1) {
3293 NewInTy = (MVT::ValueType)(NewInTy+1);
3294 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3295
3296 // If the target supports SINT_TO_FP of this type, use it.
3297 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3298 default: break;
3299 case TargetLowering::Legal:
3300 if (!TLI.isTypeLegal(NewInTy))
3301 break; // Can't use this datatype.
3302 // FALL THROUGH.
3303 case TargetLowering::Custom:
3304 OpToUse = ISD::SINT_TO_FP;
3305 break;
3306 }
3307 if (OpToUse) break;
3308 if (isSigned) continue;
3309
3310 // If the target supports UINT_TO_FP of this type, use it.
3311 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3312 default: break;
3313 case TargetLowering::Legal:
3314 if (!TLI.isTypeLegal(NewInTy))
3315 break; // Can't use this datatype.
3316 // FALL THROUGH.
3317 case TargetLowering::Custom:
3318 OpToUse = ISD::UINT_TO_FP;
3319 break;
3320 }
3321 if (OpToUse) break;
3322
3323 // Otherwise, try a larger type.
3324 }
3325
3326 // Okay, we found the operation and type to use. Zero extend our input to the
3327 // desired type then run the operation on it.
3328 return DAG.getNode(OpToUse, DestVT,
3329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3330 NewInTy, LegalOp));
3331}
3332
3333/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3334/// FP_TO_*INT operation of the specified operand when the target requests that
3335/// we promote it. At this point, we know that the result and operand types are
3336/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3337/// operation that returns a larger result.
3338SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3339 MVT::ValueType DestVT,
3340 bool isSigned) {
3341 // First step, figure out the appropriate FP_TO*INT operation to use.
3342 MVT::ValueType NewOutTy = DestVT;
3343
3344 unsigned OpToUse = 0;
3345
3346 // Scan for the appropriate larger type to use.
3347 while (1) {
3348 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3349 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3350
3351 // If the target supports FP_TO_SINT returning this type, use it.
3352 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3353 default: break;
3354 case TargetLowering::Legal:
3355 if (!TLI.isTypeLegal(NewOutTy))
3356 break; // Can't use this datatype.
3357 // FALL THROUGH.
3358 case TargetLowering::Custom:
3359 OpToUse = ISD::FP_TO_SINT;
3360 break;
3361 }
3362 if (OpToUse) break;
3363
3364 // If the target supports FP_TO_UINT of this type, use it.
3365 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3366 default: break;
3367 case TargetLowering::Legal:
3368 if (!TLI.isTypeLegal(NewOutTy))
3369 break; // Can't use this datatype.
3370 // FALL THROUGH.
3371 case TargetLowering::Custom:
3372 OpToUse = ISD::FP_TO_UINT;
3373 break;
3374 }
3375 if (OpToUse) break;
3376
3377 // Otherwise, try a larger type.
3378 }
3379
3380 // Okay, we found the operation and type to use. Truncate the result of the
3381 // extended FP_TO_*INT operation to the desired size.
3382 return DAG.getNode(ISD::TRUNCATE, DestVT,
3383 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3384}
3385
3386/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3387///
3388SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3389 MVT::ValueType VT = Op.getValueType();
3390 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3391 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3392 switch (VT) {
3393 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3394 case MVT::i16:
3395 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3396 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3397 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3398 case MVT::i32:
3399 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3400 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3401 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3402 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3403 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3404 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3405 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3406 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3407 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3408 case MVT::i64:
3409 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3410 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3411 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3412 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3413 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3414 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3415 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3416 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3417 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3418 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3419 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3420 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3421 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3422 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3423 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3424 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3425 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3426 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3427 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3428 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3429 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3430 }
3431}
3432
3433/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3434///
3435SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3436 switch (Opc) {
3437 default: assert(0 && "Cannot expand this yet!");
3438 case ISD::CTPOP: {
3439 static const uint64_t mask[6] = {
3440 0x5555555555555555ULL, 0x3333333333333333ULL,
3441 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3442 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3443 };
3444 MVT::ValueType VT = Op.getValueType();
3445 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3446 unsigned len = getSizeInBits(VT);
3447 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3448 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3449 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3450 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3451 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3452 DAG.getNode(ISD::AND, VT,
3453 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3454 }
3455 return Op;
3456 }
3457 case ISD::CTLZ: {
3458 // for now, we do this:
3459 // x = x | (x >> 1);
3460 // x = x | (x >> 2);
3461 // ...
3462 // x = x | (x >>16);
3463 // x = x | (x >>32); // for 64-bit input
3464 // return popcount(~x);
3465 //
3466 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3467 MVT::ValueType VT = Op.getValueType();
3468 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3469 unsigned len = getSizeInBits(VT);
3470 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3471 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3472 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3473 }
3474 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3475 return DAG.getNode(ISD::CTPOP, VT, Op);
3476 }
3477 case ISD::CTTZ: {
3478 // for now, we use: { return popcount(~x & (x - 1)); }
3479 // unless the target has ctlz but not ctpop, in which case we use:
3480 // { return 32 - nlz(~x & (x-1)); }
3481 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3482 MVT::ValueType VT = Op.getValueType();
3483 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3484 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3485 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3486 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3487 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3488 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3489 TLI.isOperationLegal(ISD::CTLZ, VT))
3490 return DAG.getNode(ISD::SUB, VT,
3491 DAG.getConstant(getSizeInBits(VT), VT),
3492 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3493 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3494 }
3495 }
3496}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003497
3498
Chris Lattnerdc750592005-01-07 07:47:09 +00003499/// ExpandOp - Expand the specified SDOperand into its two component pieces
3500/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3501/// LegalizeNodes map is filled in for any results that are not expanded, the
3502/// ExpandedNodes map is filled in for any results that are expanded, and the
3503/// Lo/Hi values are returned.
3504void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3505 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003506 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003507 SDNode *Node = Op.Val;
3508 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003509 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3510 "Cannot expand FP values!");
3511 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003512 "Cannot expand to FP value or to larger int value!");
3513
Chris Lattner1a570f12005-09-02 20:32:45 +00003514 // See if we already expanded it.
3515 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3516 = ExpandedNodes.find(Op);
3517 if (I != ExpandedNodes.end()) {
3518 Lo = I->second.first;
3519 Hi = I->second.second;
3520 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003521 }
3522
Chris Lattnerdc750592005-01-07 07:47:09 +00003523 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003524 case ISD::CopyFromReg:
3525 assert(0 && "CopyFromReg must be legal!");
3526 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3528 assert(0 && "Do not know how to expand this operator!");
3529 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003530 case ISD::UNDEF:
3531 Lo = DAG.getNode(ISD::UNDEF, NVT);
3532 Hi = DAG.getNode(ISD::UNDEF, NVT);
3533 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003534 case ISD::Constant: {
3535 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3536 Lo = DAG.getConstant(Cst, NVT);
3537 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3538 break;
3539 }
Evan Chengb97aab42006-03-01 01:09:54 +00003540 case ISD::VConstant: {
3541 unsigned NumElements =
3542 cast<ConstantSDNode>(Node->getOperand(0))->getValue() / 2;
3543 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3544 MVT::ValueType TVT = (NumElements > 1)
3545 ? getVectorType(EVT, NumElements) : EVT;
3546 // If type of bisected vector is legal, turn it into a ConstantVec (which
3547 // will be lowered to a ConstantPool or something else). Otherwise, bisect
3548 // the VConstant, and return each half as a new VConstant.
3549 unsigned Opc = ISD::ConstantVec;
3550 std::vector<SDOperand> LoOps, HiOps;
3551 if (!(TVT != MVT::Other &&
3552 (!MVT::isVector(TVT) || TLI.isTypeLegal(TVT)))) {
3553 Opc = ISD::VConstant;
3554 TVT = MVT::Vector;
3555 SDOperand Num = DAG.getConstant(NumElements, MVT::i32);
3556 SDOperand Typ = DAG.getValueType(EVT);
3557 HiOps.push_back(Num);
3558 HiOps.push_back(Typ);
3559 LoOps.push_back(Num);
3560 LoOps.push_back(Typ);
3561 }
3562
3563 if (NumElements == 1) {
3564 Hi = Node->getOperand(2);
3565 Lo = Node->getOperand(3);
Nate Begemanae89d862005-12-07 19:48:11 +00003566 } else {
Nate Begemanae89d862005-12-07 19:48:11 +00003567 for (unsigned I = 0, E = NumElements; I < E; ++I) {
Evan Chengb97aab42006-03-01 01:09:54 +00003568 HiOps.push_back(Node->getOperand(I+2));
3569 LoOps.push_back(Node->getOperand(I+2+NumElements));
Nate Begemanae89d862005-12-07 19:48:11 +00003570 }
Evan Chengb97aab42006-03-01 01:09:54 +00003571 Hi = DAG.getNode(Opc, TVT, HiOps);
3572 Lo = DAG.getNode(Opc, TVT, LoOps);
Nate Begemanae89d862005-12-07 19:48:11 +00003573 }
3574 break;
3575 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003576
Chris Lattner32e08b72005-03-28 22:03:13 +00003577 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003578 // Return the operands.
3579 Lo = Node->getOperand(0);
3580 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003581 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003582
3583 case ISD::SIGN_EXTEND_INREG:
3584 ExpandOp(Node->getOperand(0), Lo, Hi);
3585 // Sign extend the lo-part.
3586 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3587 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3588 TLI.getShiftAmountTy()));
3589 // sext_inreg the low part if needed.
3590 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3591 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003592
Nate Begeman2fba8a32006-01-14 03:14:10 +00003593 case ISD::BSWAP: {
3594 ExpandOp(Node->getOperand(0), Lo, Hi);
3595 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3596 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3597 Lo = TempLo;
3598 break;
3599 }
3600
Chris Lattner55e9cde2005-05-11 04:51:16 +00003601 case ISD::CTPOP:
3602 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003603 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3604 DAG.getNode(ISD::CTPOP, NVT, Lo),
3605 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003606 Hi = DAG.getConstant(0, NVT);
3607 break;
3608
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003609 case ISD::CTLZ: {
3610 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003611 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003612 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3613 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003614 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3615 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003616 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3617 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3618
3619 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3620 Hi = DAG.getConstant(0, NVT);
3621 break;
3622 }
3623
3624 case ISD::CTTZ: {
3625 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003626 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003627 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3628 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003629 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3630 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003631 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3632 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3633
3634 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3635 Hi = DAG.getConstant(0, NVT);
3636 break;
3637 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003638
Nate Begemane74795c2006-01-25 18:21:52 +00003639 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003640 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3641 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003642 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3643 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3644
3645 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003646 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003647 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3648 if (!TLI.isLittleEndian())
3649 std::swap(Lo, Hi);
3650 break;
3651 }
3652
Chris Lattnerdc750592005-01-07 07:47:09 +00003653 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003654 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3655 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003656 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003657
3658 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003659 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003660 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3661 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003662 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003663 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003664
3665 // Build a factor node to remember that this load is independent of the
3666 // other one.
3667 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3668 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003669
Chris Lattnerdc750592005-01-07 07:47:09 +00003670 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003671 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003672 if (!TLI.isLittleEndian())
3673 std::swap(Lo, Hi);
3674 break;
3675 }
Nate Begemand37c1312005-11-22 18:16:00 +00003676 case ISD::VLOAD: {
Evan Chengb97aab42006-03-01 01:09:54 +00003677 SDOperand Ch = Node->getOperand(2); // Legalize the chain.
3678 SDOperand Ptr = Node->getOperand(3); // Legalize the pointer.
3679 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(0))->getValue();
3680 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3681 MVT::ValueType TVT = (NumElements/2 > 1)
3682 ? getVectorType(EVT, NumElements/2) : EVT;
Nate Begemand37c1312005-11-22 18:16:00 +00003683
Evan Chengb97aab42006-03-01 01:09:54 +00003684 // If type of split vector is legal, turn into a pair of scalar or
3685 // packed loads.
3686 if (TVT != MVT::Other &&
3687 (!MVT::isVector(TVT) ||
3688 (TLI.isTypeLegal(TVT) && TLI.isOperationLegal(ISD::LOAD, TVT)))) {
3689 Lo = DAG.getLoad(TVT, Ch, Ptr, Node->getOperand(4));
Nate Begemand37c1312005-11-22 18:16:00 +00003690 // Increment the pointer to the other half.
Evan Chengb97aab42006-03-01 01:09:54 +00003691 unsigned IncrementSize = MVT::getSizeInBits(TVT)/8;
Nate Begemand37c1312005-11-22 18:16:00 +00003692 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3693 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003694 // FIXME: This creates a bogus srcvalue!
Evan Chengb97aab42006-03-01 01:09:54 +00003695 Hi = DAG.getLoad(TVT, Ch, Ptr, Node->getOperand(4));
Nate Begemand37c1312005-11-22 18:16:00 +00003696 } else {
3697 NumElements /= 2; // Split the vector in half
3698 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3699 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3700 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3701 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003702 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003703 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3704 }
3705
3706 // Build a factor node to remember that this load is independent of the
3707 // other one.
3708 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3709 Hi.getValue(1));
3710
3711 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003712 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemand37c1312005-11-22 18:16:00 +00003713 if (!TLI.isLittleEndian())
3714 std::swap(Lo, Hi);
3715 break;
3716 }
3717 case ISD::VADD:
3718 case ISD::VSUB:
Evan Cheng3bf916d2006-03-03 07:01:07 +00003719 case ISD::VMUL:
3720 case ISD::VSDIV:
3721 case ISD::VUDIV:
3722 case ISD::VAND:
3723 case ISD::VOR:
3724 case ISD::VXOR: {
Evan Chengb97aab42006-03-01 01:09:54 +00003725 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(0))->getValue();
3726 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3727 MVT::ValueType TVT = (NumElements/2 > 1)
3728 ? getVectorType(EVT, NumElements/2) : EVT;
Nate Begemand37c1312005-11-22 18:16:00 +00003729 SDOperand LL, LH, RL, RH;
3730
Evan Chengb97aab42006-03-01 01:09:54 +00003731 ExpandOp(Node->getOperand(2), LL, LH);
3732 ExpandOp(Node->getOperand(3), RL, RH);
Nate Begemand37c1312005-11-22 18:16:00 +00003733
Evan Chengb97aab42006-03-01 01:09:54 +00003734 // If type of split vector is legal, turn into a pair of scalar / packed
3735 // ADD, SUB, or MUL.
3736 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3737 if (TVT != MVT::Other &&
3738 (!MVT::isVector(TVT) ||
3739 (TLI.isTypeLegal(TVT) && TLI.isOperationLegal(Opc, TVT)))) {
3740 Lo = DAG.getNode(Opc, TVT, LL, RL);
3741 Hi = DAG.getNode(Opc, TVT, LH, RH);
Nate Begemand37c1312005-11-22 18:16:00 +00003742 } else {
Evan Chengb97aab42006-03-01 01:09:54 +00003743 SDOperand Num = DAG.getConstant(NumElements/2, MVT::i32);
3744 SDOperand Typ = DAG.getValueType(EVT);
3745 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, Num, Typ, LL, RL);
3746 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, Num, Typ, LH, RH);
Nate Begemand37c1312005-11-22 18:16:00 +00003747 }
3748 break;
3749 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003750 case ISD::AND:
3751 case ISD::OR:
3752 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3753 SDOperand LL, LH, RL, RH;
3754 ExpandOp(Node->getOperand(0), LL, LH);
3755 ExpandOp(Node->getOperand(1), RL, RH);
3756 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3757 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3758 break;
3759 }
3760 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003761 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003762 ExpandOp(Node->getOperand(1), LL, LH);
3763 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003764 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3765 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003766 break;
3767 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003768 case ISD::SELECT_CC: {
3769 SDOperand TL, TH, FL, FH;
3770 ExpandOp(Node->getOperand(2), TL, TH);
3771 ExpandOp(Node->getOperand(3), FL, FH);
3772 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3773 Node->getOperand(1), TL, FL, Node->getOperand(4));
3774 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3775 Node->getOperand(1), TH, FH, Node->getOperand(4));
3776 break;
3777 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003778 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003779 SDOperand Chain = Node->getOperand(0);
3780 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003781 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3782
3783 if (EVT == NVT)
3784 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3785 else
3786 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3787 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003788
3789 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003790 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003791
Nate Begemanc3a89c52005-10-13 17:15:37 +00003792 // The high part is obtained by SRA'ing all but one of the bits of the lo
3793 // part.
3794 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3795 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3796 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003797 break;
3798 }
3799 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003800 SDOperand Chain = Node->getOperand(0);
3801 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003802 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3803
3804 if (EVT == NVT)
3805 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3806 else
3807 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3808 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003809
3810 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003811 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003812
Nate Begemanc3a89c52005-10-13 17:15:37 +00003813 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003814 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003815 break;
3816 }
3817 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003818 SDOperand Chain = Node->getOperand(0);
3819 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003820 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3821
3822 if (EVT == NVT)
3823 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3824 else
3825 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3826 EVT);
3827
3828 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003829 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003830
3831 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003832 Hi = DAG.getNode(ISD::UNDEF, NVT);
3833 break;
3834 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003835 case ISD::ANY_EXTEND:
3836 // The low part is any extension of the input (which degenerates to a copy).
3837 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3838 // The high part is undefined.
3839 Hi = DAG.getNode(ISD::UNDEF, NVT);
3840 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003841 case ISD::SIGN_EXTEND: {
3842 // The low part is just a sign extension of the input (which degenerates to
3843 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003844 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003845
Chris Lattnerdc750592005-01-07 07:47:09 +00003846 // The high part is obtained by SRA'ing all but one of the bits of the lo
3847 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003848 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003849 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3850 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003851 break;
3852 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003853 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003854 // The low part is just a zero extension of the input (which degenerates to
3855 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003856 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003857
Chris Lattnerdc750592005-01-07 07:47:09 +00003858 // The high part is just a zero.
3859 Hi = DAG.getConstant(0, NVT);
3860 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003861
3862 case ISD::BIT_CONVERT: {
3863 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3864 Node->getOperand(0));
3865 ExpandOp(Tmp, Lo, Hi);
3866 break;
3867 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003868
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003869 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00003870 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3871 TargetLowering::Custom &&
3872 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003873 Lo = TLI.LowerOperation(Op, DAG);
3874 assert(Lo.Val && "Node must be custom expanded!");
3875 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00003876 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003877 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003878 break;
3879
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003880 // These operators cannot be expanded directly, emit them as calls to
3881 // library functions.
3882 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003883 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003884 SDOperand Op;
3885 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3886 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003887 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3888 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00003889 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003890
Chris Lattner941d84a2005-07-30 01:40:57 +00003891 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3892
Chris Lattnerfe68d752005-07-29 00:33:32 +00003893 // Now that the custom expander is done, expand the result, which is still
3894 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003895 if (Op.Val) {
3896 ExpandOp(Op, Lo, Hi);
3897 break;
3898 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003899 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003900
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003901 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003902 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003903 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003904 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003905 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003906
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003907 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003908 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003909 SDOperand Op;
3910 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3911 case Expand: assert(0 && "cannot expand FP!");
3912 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3913 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3914 }
3915
3916 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3917
3918 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003919 if (Op.Val) {
3920 ExpandOp(Op, Lo, Hi);
3921 break;
3922 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003923 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003924
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003925 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003926 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003927 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003928 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003929 break;
3930
Evan Cheng870e4f82006-01-09 18:31:59 +00003931 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003932 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003933 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003934 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003935 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003936 Op = TLI.LowerOperation(Op, DAG);
3937 if (Op.Val) {
3938 // Now that the custom expander is done, expand the result, which is
3939 // still VT.
3940 ExpandOp(Op, Lo, Hi);
3941 break;
3942 }
3943 }
3944
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003945 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003946 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003947 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003948
3949 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003950 TargetLowering::LegalizeAction Action =
3951 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3952 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3953 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003954 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003955 break;
3956 }
3957
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003958 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003959 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003960 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003961 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003962
Evan Cheng870e4f82006-01-09 18:31:59 +00003963 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003964 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003965 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003966 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003967 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003968 Op = TLI.LowerOperation(Op, DAG);
3969 if (Op.Val) {
3970 // Now that the custom expander is done, expand the result, which is
3971 // still VT.
3972 ExpandOp(Op, Lo, Hi);
3973 break;
3974 }
3975 }
3976
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003977 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003978 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003979 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003980
3981 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003982 TargetLowering::LegalizeAction Action =
3983 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3984 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3985 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003986 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003987 break;
3988 }
3989
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003990 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003991 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003992 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003993 }
3994
3995 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003996 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003997 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003998 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003999 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004000 Op = TLI.LowerOperation(Op, DAG);
4001 if (Op.Val) {
4002 // Now that the custom expander is done, expand the result, which is
4003 // still VT.
4004 ExpandOp(Op, Lo, Hi);
4005 break;
4006 }
4007 }
4008
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004009 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004010 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004011 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004012
4013 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004014 TargetLowering::LegalizeAction Action =
4015 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4016 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4017 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004018 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004019 break;
4020 }
4021
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004022 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004023 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004024 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004025 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004026
Misha Brukman835702a2005-04-21 22:36:52 +00004027 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004028 case ISD::SUB: {
4029 // If the target wants to custom expand this, let them.
4030 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4031 TargetLowering::Custom) {
4032 Op = TLI.LowerOperation(Op, DAG);
4033 if (Op.Val) {
4034 ExpandOp(Op, Lo, Hi);
4035 break;
4036 }
4037 }
4038
4039 // Expand the subcomponents.
4040 SDOperand LHSL, LHSH, RHSL, RHSH;
4041 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4042 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004043 std::vector<MVT::ValueType> VTs;
4044 std::vector<SDOperand> LoOps, HiOps;
4045 VTs.push_back(LHSL.getValueType());
4046 VTs.push_back(MVT::Flag);
4047 LoOps.push_back(LHSL);
4048 LoOps.push_back(RHSL);
4049 HiOps.push_back(LHSH);
4050 HiOps.push_back(RHSH);
4051 if (Node->getOpcode() == ISD::ADD) {
4052 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4053 HiOps.push_back(Lo.getValue(1));
4054 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4055 } else {
4056 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4057 HiOps.push_back(Lo.getValue(1));
4058 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4059 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004060 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004061 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004062 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004063 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004064 SDOperand LL, LH, RL, RH;
4065 ExpandOp(Node->getOperand(0), LL, LH);
4066 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004067 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4068 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4069 // extended the sign bit of the low half through the upper half, and if so
4070 // emit a MULHS instead of the alternate sequence that is valid for any
4071 // i64 x i64 multiply.
4072 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4073 // is RH an extension of the sign bit of RL?
4074 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4075 RH.getOperand(1).getOpcode() == ISD::Constant &&
4076 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4077 // is LH an extension of the sign bit of LL?
4078 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4079 LH.getOperand(1).getOpcode() == ISD::Constant &&
4080 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4081 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4082 } else {
4083 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4084 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4085 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4086 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4087 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4088 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004089 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4090 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004091 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004092 }
4093 break;
4094 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004095 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4096 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4097 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4098 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004099 }
4100
Chris Lattnerac12f682005-12-21 18:02:52 +00004101 // Make sure the resultant values have been legalized themselves, unless this
4102 // is a type that requires multi-step expansion.
4103 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4104 Lo = LegalizeOp(Lo);
4105 Hi = LegalizeOp(Hi);
4106 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004107
4108 // Remember in a map if the values will be reused later.
4109 bool isNew =
4110 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4111 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004112}
4113
4114
4115// SelectionDAG::Legalize - This is the entry point for the file.
4116//
Chris Lattner4add7e32005-01-23 04:42:50 +00004117void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004118 /// run - This is the main entry point to this class.
4119 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004120 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004121}
4122