blob: aeac2622d8a17df80ffa471372e9a12fec52ae6e [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 Lattnercad70c32006-03-15 22:19:18 +00001263 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001264 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001265 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001266 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001267 } else {
1268 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001269 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001270 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001271 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1272 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001273 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001274 }
1275
Chris Lattnerdc750592005-01-07 07:47:09 +00001276 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1277 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001278 Tmp3 = LegalizeOp(Node->getOperand(1));
1279 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1280 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001281
Chris Lattnerd02b0542006-01-28 10:58:55 +00001282 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001283 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1284 default: assert(0 && "This action is not supported yet!");
1285 case TargetLowering::Legal: break;
1286 case TargetLowering::Custom:
1287 Tmp1 = TLI.LowerOperation(Result, DAG);
1288 if (Tmp1.Val) Result = Tmp1;
1289 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001290 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001291 break;
1292 }
1293 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001294 // Truncate the value and store the result.
1295 Tmp3 = PromoteOp(Node->getOperand(1));
1296 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001297 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001298 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001299 break;
1300
Chris Lattnerdc750592005-01-07 07:47:09 +00001301 case Expand:
1302 SDOperand Lo, Hi;
1303 ExpandOp(Node->getOperand(1), Lo, Hi);
1304
1305 if (!TLI.isLittleEndian())
1306 std::swap(Lo, Hi);
1307
Chris Lattner55e9cde2005-05-11 04:51:16 +00001308 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1309 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001310 // If this is a vector type, then we have to calculate the increment as
1311 // the product of the element size in bytes, and the number of elements
1312 // in the high half of the vector.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001313 unsigned IncrementSize;
Nate Begemand37c1312005-11-22 18:16:00 +00001314 if (MVT::Vector == Hi.getValueType()) {
Evan Chengb97aab42006-03-01 01:09:54 +00001315 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(0))->getValue();
1316 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(1))->getVT();
Nate Begemand37c1312005-11-22 18:16:00 +00001317 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1318 } else {
1319 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1320 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001321 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1322 getIntPtrConstant(IncrementSize));
1323 assert(isTypeLegal(Tmp2.getValueType()) &&
1324 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001325 // FIXME: This sets the srcvalue of both halves to be the same, which is
1326 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001327 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1328 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001329 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1330 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001331 }
1332 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001333 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001334 case ISD::PCMARKER:
1335 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001336 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001337 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001338 case ISD::STACKSAVE:
1339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001340 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1341 Tmp1 = Result.getValue(0);
1342 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001343
Chris Lattnerb3266452006-01-13 02:50:02 +00001344 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1345 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001346 case TargetLowering::Legal: break;
1347 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001348 Tmp3 = TLI.LowerOperation(Result, DAG);
1349 if (Tmp3.Val) {
1350 Tmp1 = LegalizeOp(Tmp3);
1351 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001352 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001353 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001354 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001355 // Expand to CopyFromReg if the target set
1356 // StackPointerRegisterToSaveRestore.
1357 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001358 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001359 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001360 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001361 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001362 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1363 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001364 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001365 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001366 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001367
1368 // Since stacksave produce two values, make sure to remember that we
1369 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001370 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1371 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1372 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001373
Chris Lattnerb3266452006-01-13 02:50:02 +00001374 case ISD::STACKRESTORE:
1375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1376 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001377 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001378
1379 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1380 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001381 case TargetLowering::Legal: break;
1382 case TargetLowering::Custom:
1383 Tmp1 = TLI.LowerOperation(Result, DAG);
1384 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001385 break;
1386 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001387 // Expand to CopyToReg if the target set
1388 // StackPointerRegisterToSaveRestore.
1389 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1390 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1391 } else {
1392 Result = Tmp1;
1393 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001394 break;
1395 }
1396 break;
1397
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001398 case ISD::READCYCLECOUNTER:
1399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001400 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001401
1402 // Since rdcc produce two values, make sure to remember that we legalized
1403 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001404 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001405 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001406 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001407
Evan Cheng31d15fa2005-12-23 07:29:34 +00001408 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001409 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1410 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1411
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001412 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1413 "Cannot handle illegal TRUNCSTORE yet!");
1414 Tmp2 = LegalizeOp(Node->getOperand(1));
1415
1416 // The only promote case we handle is TRUNCSTORE:i1 X into
1417 // -> TRUNCSTORE:i8 (and X, 1)
1418 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1419 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1420 TargetLowering::Promote) {
1421 // Promote the bool to a mask then store.
1422 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1423 DAG.getConstant(1, Tmp2.getValueType()));
1424 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1425 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001426
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001427 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1428 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001429 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1430 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001431 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001432
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001433 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1434 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1435 default: assert(0 && "This action is not supported yet!");
1436 case TargetLowering::Legal: break;
1437 case TargetLowering::Custom:
1438 Tmp1 = TLI.LowerOperation(Result, DAG);
1439 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001440 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001441 }
1442 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001443 }
Chris Lattner39c67442005-01-14 22:08:15 +00001444 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001445 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1446 case Expand: assert(0 && "It's impossible to expand bools");
1447 case Legal:
1448 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1449 break;
1450 case Promote:
1451 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1452 break;
1453 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001454 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001455 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001456
Chris Lattnerd02b0542006-01-28 10:58:55 +00001457 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001458
Nate Begeman987121a2005-08-23 04:29:48 +00001459 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001460 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001461 case TargetLowering::Legal: break;
1462 case TargetLowering::Custom: {
1463 Tmp1 = TLI.LowerOperation(Result, DAG);
1464 if (Tmp1.Val) Result = Tmp1;
1465 break;
1466 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001467 case TargetLowering::Expand:
1468 if (Tmp1.getOpcode() == ISD::SETCC) {
1469 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1470 Tmp2, Tmp3,
1471 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1472 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001473 // Make sure the condition is either zero or one. It may have been
1474 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001475 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1476 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1477 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001478 Result = DAG.getSelectCC(Tmp1,
1479 DAG.getConstant(0, Tmp1.getValueType()),
1480 Tmp2, Tmp3, ISD::SETNE);
1481 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001482 break;
1483 case TargetLowering::Promote: {
1484 MVT::ValueType NVT =
1485 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1486 unsigned ExtOp, TruncOp;
1487 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001488 ExtOp = ISD::ANY_EXTEND;
1489 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001490 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001491 ExtOp = ISD::FP_EXTEND;
1492 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001493 }
1494 // Promote each of the values to the new type.
1495 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1496 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1497 // Perform the larger operation, then round down.
1498 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1499 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1500 break;
1501 }
1502 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001503 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001504 case ISD::SELECT_CC: {
1505 Tmp1 = Node->getOperand(0); // LHS
1506 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001507 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1508 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001509 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001510
Nate Begeman7e7f4392006-02-01 07:19:44 +00001511 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1512
1513 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1514 // the LHS is a legal SETCC itself. In this case, we need to compare
1515 // the result against zero to select between true and false values.
1516 if (Tmp2.Val == 0) {
1517 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1518 CC = DAG.getCondCode(ISD::SETNE);
1519 }
1520 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1521
1522 // Everything is legal, see if we should expand this op or something.
1523 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1524 default: assert(0 && "This action is not supported yet!");
1525 case TargetLowering::Legal: break;
1526 case TargetLowering::Custom:
1527 Tmp1 = TLI.LowerOperation(Result, DAG);
1528 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001529 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001530 }
1531 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001532 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001533 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001534 Tmp1 = Node->getOperand(0);
1535 Tmp2 = Node->getOperand(1);
1536 Tmp3 = Node->getOperand(2);
1537 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1538
1539 // If we had to Expand the SetCC operands into a SELECT node, then it may
1540 // not always be possible to return a true LHS & RHS. In this case, just
1541 // return the value we legalized, returned in the LHS
1542 if (Tmp2.Val == 0) {
1543 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001544 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001545 }
Nate Begeman987121a2005-08-23 04:29:48 +00001546
Chris Lattnerf263a232006-01-30 22:43:50 +00001547 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001548 default: assert(0 && "Cannot handle this action for SETCC yet!");
1549 case TargetLowering::Custom:
1550 isCustom = true;
1551 // FALLTHROUGH.
1552 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001553 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001554 if (isCustom) {
1555 Tmp3 = TLI.LowerOperation(Result, DAG);
1556 if (Tmp3.Val) Result = Tmp3;
1557 }
Nate Begeman987121a2005-08-23 04:29:48 +00001558 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001559 case TargetLowering::Promote: {
1560 // First step, figure out the appropriate operation to use.
1561 // Allow SETCC to not be supported for all legal data types
1562 // Mostly this targets FP
1563 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1564 MVT::ValueType OldVT = NewInTy;
1565
1566 // Scan for the appropriate larger type to use.
1567 while (1) {
1568 NewInTy = (MVT::ValueType)(NewInTy+1);
1569
1570 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1571 "Fell off of the edge of the integer world");
1572 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1573 "Fell off of the edge of the floating point world");
1574
1575 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001576 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001577 break;
1578 }
1579 if (MVT::isInteger(NewInTy))
1580 assert(0 && "Cannot promote Legal Integer SETCC yet");
1581 else {
1582 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1583 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1584 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001585 Tmp1 = LegalizeOp(Tmp1);
1586 Tmp2 = LegalizeOp(Tmp2);
1587 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001588 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001589 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001590 }
Nate Begeman987121a2005-08-23 04:29:48 +00001591 case TargetLowering::Expand:
1592 // Expand a setcc node into a select_cc of the same condition, lhs, and
1593 // rhs that selects between const 1 (true) and const 0 (false).
1594 MVT::ValueType VT = Node->getValueType(0);
1595 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1596 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1597 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001598 break;
1599 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001600 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001601 case ISD::MEMSET:
1602 case ISD::MEMCPY:
1603 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001604 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001605 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1606
1607 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1608 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1609 case Expand: assert(0 && "Cannot expand a byte!");
1610 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001611 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001612 break;
1613 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001614 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001615 break;
1616 }
1617 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001618 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001619 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001620
1621 SDOperand Tmp4;
1622 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001623 case Expand: {
1624 // Length is too big, just take the lo-part of the length.
1625 SDOperand HiPart;
1626 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1627 break;
1628 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001629 case Legal:
1630 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001631 break;
1632 case Promote:
1633 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001634 break;
1635 }
1636
1637 SDOperand Tmp5;
1638 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1639 case Expand: assert(0 && "Cannot expand this yet!");
1640 case Legal:
1641 Tmp5 = LegalizeOp(Node->getOperand(4));
1642 break;
1643 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001644 Tmp5 = PromoteOp(Node->getOperand(4));
1645 break;
1646 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001647
1648 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1649 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001650 case TargetLowering::Custom:
1651 isCustom = true;
1652 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001653 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001654 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001655 if (isCustom) {
1656 Tmp1 = TLI.LowerOperation(Result, DAG);
1657 if (Tmp1.Val) Result = Tmp1;
1658 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001659 break;
1660 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001661 // Otherwise, the target does not support this operation. Lower the
1662 // operation to an explicit libcall as appropriate.
1663 MVT::ValueType IntPtr = TLI.getPointerTy();
1664 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1665 std::vector<std::pair<SDOperand, const Type*> > Args;
1666
Reid Spencer6dced922005-01-12 14:53:45 +00001667 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001668 if (Node->getOpcode() == ISD::MEMSET) {
1669 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001670 // Extend the (previously legalized) ubyte argument to be an int value
1671 // for the call.
1672 if (Tmp3.getValueType() > MVT::i32)
1673 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1674 else
1675 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001676 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1677 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1678
1679 FnName = "memset";
1680 } else if (Node->getOpcode() == ISD::MEMCPY ||
1681 Node->getOpcode() == ISD::MEMMOVE) {
1682 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1683 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1684 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1685 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1686 } else {
1687 assert(0 && "Unknown op!");
1688 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001689
Chris Lattner85d70c62005-01-11 05:57:22 +00001690 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001691 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001692 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001693 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001694 break;
1695 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001696 }
1697 break;
1698 }
Chris Lattner5385db52005-05-09 20:23:03 +00001699
Chris Lattner4157c412005-04-02 04:00:59 +00001700 case ISD::SHL_PARTS:
1701 case ISD::SRA_PARTS:
1702 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001703 std::vector<SDOperand> Ops;
1704 bool Changed = false;
1705 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1706 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1707 Changed |= Ops.back() != Node->getOperand(i);
1708 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001709 if (Changed)
1710 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001711
Evan Cheng870e4f82006-01-09 18:31:59 +00001712 switch (TLI.getOperationAction(Node->getOpcode(),
1713 Node->getValueType(0))) {
1714 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001715 case TargetLowering::Legal: break;
1716 case TargetLowering::Custom:
1717 Tmp1 = TLI.LowerOperation(Result, DAG);
1718 if (Tmp1.Val) {
1719 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001720 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001721 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001722 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1723 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001724 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001725 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001726 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001727 return RetVal;
1728 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001729 break;
1730 }
1731
Chris Lattner13fe99c2005-04-02 05:00:07 +00001732 // Since these produce multiple values, make sure to remember that we
1733 // legalized all of them.
1734 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1735 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1736 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001737 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001738
1739 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001740 case ISD::ADD:
1741 case ISD::SUB:
1742 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001743 case ISD::MULHS:
1744 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001745 case ISD::UDIV:
1746 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001747 case ISD::AND:
1748 case ISD::OR:
1749 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001750 case ISD::SHL:
1751 case ISD::SRL:
1752 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001753 case ISD::FADD:
1754 case ISD::FSUB:
1755 case ISD::FMUL:
1756 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001757 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001758 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1759 case Expand: assert(0 && "Not possible");
1760 case Legal:
1761 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1762 break;
1763 case Promote:
1764 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1765 break;
1766 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001767
1768 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001769
Andrew Lenharth72594262005-12-24 23:42:32 +00001770 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001771 default: assert(0 && "Operation not supported");
1772 case TargetLowering::Legal: break;
1773 case TargetLowering::Custom:
1774 Tmp1 = TLI.LowerOperation(Result, DAG);
1775 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001776 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001777 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001778 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001779
1780 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1781 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1782 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1783 case Expand: assert(0 && "Not possible");
1784 case Legal:
1785 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1786 break;
1787 case Promote:
1788 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1789 break;
1790 }
1791
1792 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1793
1794 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1795 default: assert(0 && "Operation not supported");
1796 case TargetLowering::Custom:
1797 Tmp1 = TLI.LowerOperation(Result, DAG);
1798 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00001799 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001800 case TargetLowering::Legal: break;
1801 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00001802 // If this target supports fabs/fneg natively, do this efficiently.
1803 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1804 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1805 // Get the sign bit of the RHS.
1806 MVT::ValueType IVT =
1807 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1808 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1809 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1810 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1811 // Get the absolute value of the result.
1812 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1813 // Select between the nabs and abs value based on the sign bit of
1814 // the input.
1815 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1816 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1817 AbsVal),
1818 AbsVal);
1819 Result = LegalizeOp(Result);
1820 break;
1821 }
1822
1823 // Otherwise, do bitwise ops!
1824
1825 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001826 const char *FnName;
1827 if (Node->getValueType(0) == MVT::f32) {
1828 FnName = "copysignf";
1829 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1830 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1831 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1832 } else {
1833 FnName = "copysign";
1834 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1835 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1836 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1837 }
1838 SDOperand Dummy;
1839 Result = ExpandLibCall(FnName, Node, Dummy);
1840 break;
1841 }
1842 break;
1843
Nate Begeman5965bd12006-02-17 05:43:56 +00001844 case ISD::ADDC:
1845 case ISD::SUBC:
1846 Tmp1 = LegalizeOp(Node->getOperand(0));
1847 Tmp2 = LegalizeOp(Node->getOperand(1));
1848 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1849 // Since this produces two values, make sure to remember that we legalized
1850 // both of them.
1851 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1852 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1853 return Result;
1854 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001855
Nate Begeman5965bd12006-02-17 05:43:56 +00001856 case ISD::ADDE:
1857 case ISD::SUBE:
1858 Tmp1 = LegalizeOp(Node->getOperand(0));
1859 Tmp2 = LegalizeOp(Node->getOperand(1));
1860 Tmp3 = LegalizeOp(Node->getOperand(2));
1861 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1862 // Since this produces two values, make sure to remember that we legalized
1863 // both of them.
1864 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1865 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1866 return Result;
1867 break;
1868
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001869 case ISD::BUILD_PAIR: {
1870 MVT::ValueType PairTy = Node->getValueType(0);
1871 // TODO: handle the case where the Lo and Hi operands are not of legal type
1872 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1873 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1874 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001875 case TargetLowering::Promote:
1876 case TargetLowering::Custom:
1877 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001878 case TargetLowering::Legal:
1879 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1880 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1881 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001882 case TargetLowering::Expand:
1883 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1884 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1885 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1886 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1887 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001888 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001889 break;
1890 }
1891 break;
1892 }
1893
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001894 case ISD::UREM:
1895 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001896 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001897 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1898 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001899
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001900 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001901 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1902 case TargetLowering::Custom:
1903 isCustom = true;
1904 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001905 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001907 if (isCustom) {
1908 Tmp1 = TLI.LowerOperation(Result, DAG);
1909 if (Tmp1.Val) Result = Tmp1;
1910 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001911 break;
Chris Lattner81914422005-08-03 20:31:37 +00001912 case TargetLowering::Expand:
1913 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001914 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00001915 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001916 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00001917 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1918 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1919 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1920 } else {
1921 // Floating point mod -> fmod libcall.
1922 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1923 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00001924 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001925 }
1926 break;
1927 }
1928 break;
Nate Begemane74795c2006-01-25 18:21:52 +00001929 case ISD::VAARG: {
1930 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1931 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1932
Chris Lattner364b89a2006-01-28 07:42:08 +00001933 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00001934 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1935 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001936 case TargetLowering::Custom:
1937 isCustom = true;
1938 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001939 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001940 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1941 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001942 Tmp1 = Result.getValue(1);
1943
1944 if (isCustom) {
1945 Tmp2 = TLI.LowerOperation(Result, DAG);
1946 if (Tmp2.Val) {
1947 Result = LegalizeOp(Tmp2);
1948 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1949 }
1950 }
Nate Begemane74795c2006-01-25 18:21:52 +00001951 break;
1952 case TargetLowering::Expand: {
1953 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1954 Node->getOperand(2));
1955 // Increment the pointer, VAList, to the next vaarg
1956 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1957 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1958 TLI.getPointerTy()));
1959 // Store the incremented VAList to the legalized pointer
1960 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1961 Node->getOperand(2));
1962 // Load the actual argument out of the pointer VAList
1963 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001964 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00001965 Result = LegalizeOp(Result);
1966 break;
1967 }
1968 }
1969 // Since VAARG produces two values, make sure to remember that we
1970 // legalized both of them.
1971 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001972 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1973 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00001974 }
1975
1976 case ISD::VACOPY:
1977 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1978 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1979 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1980
1981 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1982 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001983 case TargetLowering::Custom:
1984 isCustom = true;
1985 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001986 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001987 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1988 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001989 if (isCustom) {
1990 Tmp1 = TLI.LowerOperation(Result, DAG);
1991 if (Tmp1.Val) Result = Tmp1;
1992 }
Nate Begemane74795c2006-01-25 18:21:52 +00001993 break;
1994 case TargetLowering::Expand:
1995 // This defaults to loading a pointer from the input and storing it to the
1996 // output, returning the chain.
1997 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1998 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1999 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002000 break;
2001 }
2002 break;
2003
2004 case ISD::VAEND:
2005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2006 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2007
2008 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2009 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002010 case TargetLowering::Custom:
2011 isCustom = true;
2012 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002013 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002014 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002015 if (isCustom) {
2016 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2017 if (Tmp1.Val) Result = Tmp1;
2018 }
Nate Begemane74795c2006-01-25 18:21:52 +00002019 break;
2020 case TargetLowering::Expand:
2021 Result = Tmp1; // Default to a no-op, return the chain
2022 break;
2023 }
2024 break;
2025
2026 case ISD::VASTART:
2027 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2028 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2029
Chris Lattnerd02b0542006-01-28 10:58:55 +00002030 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2031
Nate Begemane74795c2006-01-25 18:21:52 +00002032 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2033 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002034 case TargetLowering::Legal: break;
2035 case TargetLowering::Custom:
2036 Tmp1 = TLI.LowerOperation(Result, DAG);
2037 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002038 break;
2039 }
2040 break;
2041
Nate Begeman1b8121b2006-01-11 21:21:00 +00002042 case ISD::ROTL:
2043 case ISD::ROTR:
2044 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2045 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002046
2047 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2048 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002049 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002050 break;
2051
Nate Begeman2fba8a32006-01-14 03:14:10 +00002052 case ISD::BSWAP:
2053 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2054 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002055 case TargetLowering::Custom:
2056 assert(0 && "Cannot custom legalize this yet!");
2057 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002058 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002059 break;
2060 case TargetLowering::Promote: {
2061 MVT::ValueType OVT = Tmp1.getValueType();
2062 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2063 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002064
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002065 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2066 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2067 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2068 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2069 break;
2070 }
2071 case TargetLowering::Expand:
2072 Result = ExpandBSWAP(Tmp1);
2073 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002074 }
2075 break;
2076
Andrew Lenharth5e177822005-05-03 17:19:30 +00002077 case ISD::CTPOP:
2078 case ISD::CTTZ:
2079 case ISD::CTLZ:
2080 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2081 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002082 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002083 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002084 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002085 break;
2086 case TargetLowering::Promote: {
2087 MVT::ValueType OVT = Tmp1.getValueType();
2088 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002089
2090 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002091 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2092 // Perform the larger operation, then subtract if needed.
2093 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002094 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002095 case ISD::CTPOP:
2096 Result = Tmp1;
2097 break;
2098 case ISD::CTTZ:
2099 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002100 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2101 DAG.getConstant(getSizeInBits(NVT), NVT),
2102 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002103 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002104 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2105 break;
2106 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002107 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002108 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2109 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002110 getSizeInBits(OVT), NVT));
2111 break;
2112 }
2113 break;
2114 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002115 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002116 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002117 break;
2118 }
2119 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002120
Chris Lattner13fe99c2005-04-02 05:00:07 +00002121 // Unary operators
2122 case ISD::FABS:
2123 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002124 case ISD::FSQRT:
2125 case ISD::FSIN:
2126 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002127 Tmp1 = LegalizeOp(Node->getOperand(0));
2128 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002129 case TargetLowering::Promote:
2130 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002131 isCustom = true;
2132 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002133 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002134 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002135 if (isCustom) {
2136 Tmp1 = TLI.LowerOperation(Result, DAG);
2137 if (Tmp1.Val) Result = Tmp1;
2138 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002139 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002140 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002141 switch (Node->getOpcode()) {
2142 default: assert(0 && "Unreachable!");
2143 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002144 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2145 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002146 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002147 break;
Chris Lattner80026402005-04-30 04:43:14 +00002148 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002149 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2150 MVT::ValueType VT = Node->getValueType(0);
2151 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002152 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002153 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2154 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002155 break;
2156 }
2157 case ISD::FSQRT:
2158 case ISD::FSIN:
2159 case ISD::FCOS: {
2160 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002161 const char *FnName = 0;
2162 switch(Node->getOpcode()) {
2163 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2164 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2165 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2166 default: assert(0 && "Unreachable!");
2167 }
Nate Begeman77558da2005-08-04 21:43:28 +00002168 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002169 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002170 break;
2171 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002172 }
2173 break;
2174 }
2175 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002176
2177 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002178 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002179 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002180 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002181 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2182 Node->getOperand(0).getValueType())) {
2183 default: assert(0 && "Unknown operation action!");
2184 case TargetLowering::Expand:
2185 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2186 break;
2187 case TargetLowering::Legal:
2188 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002189 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002190 break;
2191 }
2192 }
2193 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002194 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002195 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002196 case ISD::UINT_TO_FP: {
2197 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002198 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2199 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002200 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002201 Node->getOperand(0).getValueType())) {
2202 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002203 case TargetLowering::Custom:
2204 isCustom = true;
2205 // FALLTHROUGH
2206 case TargetLowering::Legal:
2207 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002208 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002209 if (isCustom) {
2210 Tmp1 = TLI.LowerOperation(Result, DAG);
2211 if (Tmp1.Val) Result = Tmp1;
2212 }
2213 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002214 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002215 Result = ExpandLegalINT_TO_FP(isSigned,
2216 LegalizeOp(Node->getOperand(0)),
2217 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002218 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002219 case TargetLowering::Promote:
2220 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2221 Node->getValueType(0),
2222 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002223 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002224 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002225 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002226 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002227 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2228 Node->getValueType(0), Node->getOperand(0));
2229 break;
2230 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002231 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002232 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002233 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2234 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002235 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002236 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2237 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002238 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002239 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2240 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002241 break;
2242 }
2243 break;
2244 }
2245 case ISD::TRUNCATE:
2246 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2247 case Legal:
2248 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002249 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002250 break;
2251 case Expand:
2252 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2253
2254 // Since the result is legal, we should just be able to truncate the low
2255 // part of the source.
2256 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2257 break;
2258 case Promote:
2259 Result = PromoteOp(Node->getOperand(0));
2260 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2261 break;
2262 }
2263 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002264
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002265 case ISD::FP_TO_SINT:
2266 case ISD::FP_TO_UINT:
2267 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2268 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002269 Tmp1 = LegalizeOp(Node->getOperand(0));
2270
Chris Lattner44fe26f2005-07-29 00:11:56 +00002271 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2272 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002273 case TargetLowering::Custom:
2274 isCustom = true;
2275 // FALLTHROUGH
2276 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002277 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002278 if (isCustom) {
2279 Tmp1 = TLI.LowerOperation(Result, DAG);
2280 if (Tmp1.Val) Result = Tmp1;
2281 }
2282 break;
2283 case TargetLowering::Promote:
2284 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2285 Node->getOpcode() == ISD::FP_TO_SINT);
2286 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002287 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002288 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2289 SDOperand True, False;
2290 MVT::ValueType VT = Node->getOperand(0).getValueType();
2291 MVT::ValueType NVT = Node->getValueType(0);
2292 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2293 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2294 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2295 Node->getOperand(0), Tmp2, ISD::SETLT);
2296 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2297 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002298 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002299 Tmp2));
2300 False = DAG.getNode(ISD::XOR, NVT, False,
2301 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002302 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2303 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002304 } else {
2305 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2306 }
2307 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002308 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002309 break;
2310 case Expand:
2311 assert(0 && "Shouldn't need to expand other operators here!");
2312 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002313 Tmp1 = PromoteOp(Node->getOperand(0));
2314 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2315 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002316 break;
2317 }
2318 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002319
Chris Lattner7753f172005-09-02 00:18:10 +00002320 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002321 case ISD::ZERO_EXTEND:
2322 case ISD::SIGN_EXTEND:
2323 case ISD::FP_EXTEND:
2324 case ISD::FP_ROUND:
2325 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002326 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002327 case Legal:
2328 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002329 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002330 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002331 case Promote:
2332 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002333 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002334 Tmp1 = PromoteOp(Node->getOperand(0));
2335 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002336 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002337 case ISD::ZERO_EXTEND:
2338 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002339 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002340 Result = DAG.getZeroExtendInReg(Result,
2341 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002342 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002343 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002344 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002345 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002346 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002347 Result,
2348 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002349 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002350 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002351 Result = PromoteOp(Node->getOperand(0));
2352 if (Result.getValueType() != Op.getValueType())
2353 // Dynamically dead while we have only 2 FP types.
2354 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2355 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002356 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002357 Result = PromoteOp(Node->getOperand(0));
2358 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2359 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002360 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002361 }
2362 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002363 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002364 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002365 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002366 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002367
2368 // If this operation is not supported, convert it to a shl/shr or load/store
2369 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002370 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2371 default: assert(0 && "This action not supported for this op yet!");
2372 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002373 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002374 break;
2375 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002376 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002377 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002378 // NOTE: we could fall back on load/store here too for targets without
2379 // SAR. However, it is doubtful that any exist.
2380 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2381 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002382 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002383 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2384 Node->getOperand(0), ShiftCst);
2385 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2386 Result, ShiftCst);
2387 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2388 // The only way we can lower this is to turn it into a STORETRUNC,
2389 // EXTLOAD pair, targetting a temporary location (a stack slot).
2390
2391 // NOTE: there is a choice here between constantly creating new stack
2392 // slots and always reusing the same one. We currently always create
2393 // new ones, as reuse may inhibit scheduling.
2394 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2395 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2396 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2397 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002398 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002399 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2400 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2401 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002402 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002403 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002404 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2405 Result, StackSlot, DAG.getSrcValue(NULL),
2406 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002407 } else {
2408 assert(0 && "Unknown op");
2409 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002410 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002411 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002412 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002413 }
Chris Lattner99222f72005-01-15 07:15:18 +00002414 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002415
2416 // Make sure that the generated code is itself legal.
2417 if (Result != Op)
2418 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002419
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002420 // Note that LegalizeOp may be reentered even from single-use nodes, which
2421 // means that we always must cache transformed nodes.
2422 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002423 return Result;
2424}
2425
Chris Lattner4d978642005-01-15 22:16:26 +00002426/// PromoteOp - Given an operation that produces a value in an invalid type,
2427/// promote it to compute the value into a larger type. The produced value will
2428/// have the correct bits for the low portion of the register, but no guarantee
2429/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002430SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2431 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002432 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002433 assert(getTypeAction(VT) == Promote &&
2434 "Caller should expand or legalize operands that are not promotable!");
2435 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2436 "Cannot promote to smaller type!");
2437
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002438 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002439 SDOperand Result;
2440 SDNode *Node = Op.Val;
2441
Chris Lattner1a570f12005-09-02 20:32:45 +00002442 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2443 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002444
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002445 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002446 case ISD::CopyFromReg:
2447 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002448 default:
2449 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2450 assert(0 && "Do not know how to promote this operator!");
2451 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002452 case ISD::UNDEF:
2453 Result = DAG.getNode(ISD::UNDEF, NVT);
2454 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002455 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002456 if (VT != MVT::i1)
2457 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2458 else
2459 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002460 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2461 break;
2462 case ISD::ConstantFP:
2463 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2464 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2465 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002466
Chris Lattner2cb338d2005-01-18 02:59:52 +00002467 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002468 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002469 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2470 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002471 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002472
2473 case ISD::TRUNCATE:
2474 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2475 case Legal:
2476 Result = LegalizeOp(Node->getOperand(0));
2477 assert(Result.getValueType() >= NVT &&
2478 "This truncation doesn't make sense!");
2479 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2480 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2481 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002482 case Promote:
2483 // The truncation is not required, because we don't guarantee anything
2484 // about high bits anyway.
2485 Result = PromoteOp(Node->getOperand(0));
2486 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002487 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002488 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2489 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002490 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002491 }
2492 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002493 case ISD::SIGN_EXTEND:
2494 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002495 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002496 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2497 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2498 case Legal:
2499 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002500 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002501 break;
2502 case Promote:
2503 // Promote the reg if it's smaller.
2504 Result = PromoteOp(Node->getOperand(0));
2505 // The high bits are not guaranteed to be anything. Insert an extend.
2506 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002507 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002508 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002509 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002510 Result = DAG.getZeroExtendInReg(Result,
2511 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002512 break;
2513 }
2514 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002515 case ISD::BIT_CONVERT:
2516 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2517 Result = PromoteOp(Result);
2518 break;
2519
Chris Lattner4d978642005-01-15 22:16:26 +00002520 case ISD::FP_EXTEND:
2521 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2522 case ISD::FP_ROUND:
2523 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2524 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2525 case Promote: assert(0 && "Unreachable with 2 FP types!");
2526 case Legal:
2527 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002528 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002529 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002530 break;
2531 }
2532 break;
2533
2534 case ISD::SINT_TO_FP:
2535 case ISD::UINT_TO_FP:
2536 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2537 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002538 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002539 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002540 break;
2541
2542 case Promote:
2543 Result = PromoteOp(Node->getOperand(0));
2544 if (Node->getOpcode() == ISD::SINT_TO_FP)
2545 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002546 Result,
2547 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002548 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002549 Result = DAG.getZeroExtendInReg(Result,
2550 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002551 // No extra round required here.
2552 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002553 break;
2554 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002555 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2556 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002557 // Round if we cannot tolerate excess precision.
2558 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002559 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2560 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002561 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002562 }
Chris Lattner4d978642005-01-15 22:16:26 +00002563 break;
2564
Chris Lattner268d4572005-12-09 17:32:47 +00002565 case ISD::SIGN_EXTEND_INREG:
2566 Result = PromoteOp(Node->getOperand(0));
2567 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2568 Node->getOperand(1));
2569 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002570 case ISD::FP_TO_SINT:
2571 case ISD::FP_TO_UINT:
2572 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2573 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002574 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002575 break;
2576 case Promote:
2577 // The input result is prerounded, so we don't have to do anything
2578 // special.
2579 Tmp1 = PromoteOp(Node->getOperand(0));
2580 break;
2581 case Expand:
2582 assert(0 && "not implemented");
2583 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002584 // If we're promoting a UINT to a larger size, check to see if the new node
2585 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2586 // we can use that instead. This allows us to generate better code for
2587 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2588 // legal, such as PowerPC.
2589 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002590 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002591 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2592 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002593 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2594 } else {
2595 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2596 }
Chris Lattner4d978642005-01-15 22:16:26 +00002597 break;
2598
Chris Lattner13fe99c2005-04-02 05:00:07 +00002599 case ISD::FABS:
2600 case ISD::FNEG:
2601 Tmp1 = PromoteOp(Node->getOperand(0));
2602 assert(Tmp1.getValueType() == NVT);
2603 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2604 // NOTE: we do not have to do any extra rounding here for
2605 // NoExcessFPPrecision, because we know the input will have the appropriate
2606 // precision, and these operations don't modify precision at all.
2607 break;
2608
Chris Lattner9d6fa982005-04-28 21:44:33 +00002609 case ISD::FSQRT:
2610 case ISD::FSIN:
2611 case ISD::FCOS:
2612 Tmp1 = PromoteOp(Node->getOperand(0));
2613 assert(Tmp1.getValueType() == NVT);
2614 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002615 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002616 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2617 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002618 break;
2619
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002620 case ISD::AND:
2621 case ISD::OR:
2622 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002623 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002624 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002625 case ISD::MUL:
2626 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002627 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002628 // that too is okay if they are integer operations.
2629 Tmp1 = PromoteOp(Node->getOperand(0));
2630 Tmp2 = PromoteOp(Node->getOperand(1));
2631 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2632 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002633 break;
2634 case ISD::FADD:
2635 case ISD::FSUB:
2636 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002637 Tmp1 = PromoteOp(Node->getOperand(0));
2638 Tmp2 = PromoteOp(Node->getOperand(1));
2639 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2640 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2641
2642 // Floating point operations will give excess precision that we may not be
2643 // able to tolerate. If we DO allow excess precision, just leave it,
2644 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002645 // FIXME: Why would we need to round FP ops more than integer ones?
2646 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002647 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002648 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2649 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002650 break;
2651
Chris Lattner4d978642005-01-15 22:16:26 +00002652 case ISD::SDIV:
2653 case ISD::SREM:
2654 // These operators require that their input be sign extended.
2655 Tmp1 = PromoteOp(Node->getOperand(0));
2656 Tmp2 = PromoteOp(Node->getOperand(1));
2657 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002658 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2659 DAG.getValueType(VT));
2660 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2661 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002662 }
2663 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2664
2665 // Perform FP_ROUND: this is probably overly pessimistic.
2666 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002667 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2668 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002669 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002670 case ISD::FDIV:
2671 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002672 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002673 // These operators require that their input be fp extended.
2674 Tmp1 = PromoteOp(Node->getOperand(0));
2675 Tmp2 = PromoteOp(Node->getOperand(1));
2676 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2677
2678 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002679 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002680 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2681 DAG.getValueType(VT));
2682 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002683
2684 case ISD::UDIV:
2685 case ISD::UREM:
2686 // These operators require that their input be zero extended.
2687 Tmp1 = PromoteOp(Node->getOperand(0));
2688 Tmp2 = PromoteOp(Node->getOperand(1));
2689 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002690 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2691 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002692 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2693 break;
2694
2695 case ISD::SHL:
2696 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002697 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002698 break;
2699 case ISD::SRA:
2700 // The input value must be properly sign extended.
2701 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002702 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2703 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002704 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002705 break;
2706 case ISD::SRL:
2707 // The input value must be properly zero extended.
2708 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002709 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002710 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002711 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002712
2713 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002714 Tmp1 = Node->getOperand(0); // Get the chain.
2715 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002716 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2717 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2718 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2719 } else {
2720 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2721 Node->getOperand(2));
2722 // Increment the pointer, VAList, to the next vaarg
2723 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2724 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2725 TLI.getPointerTy()));
2726 // Store the incremented VAList to the legalized pointer
2727 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2728 Node->getOperand(2));
2729 // Load the actual argument out of the pointer VAList
2730 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2731 DAG.getSrcValue(0), VT);
2732 }
2733 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002734 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002735 break;
2736
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002737 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002738 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2739 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002740 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002741 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002742 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002743 case ISD::SEXTLOAD:
2744 case ISD::ZEXTLOAD:
2745 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002746 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2747 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002748 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002749 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002750 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002751 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002752 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002753 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2754 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002755 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002756 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002757 case ISD::SELECT_CC:
2758 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2759 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2760 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002761 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002762 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002763 case ISD::BSWAP:
2764 Tmp1 = Node->getOperand(0);
2765 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2766 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2767 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2768 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2769 TLI.getShiftAmountTy()));
2770 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002771 case ISD::CTPOP:
2772 case ISD::CTTZ:
2773 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002774 // Zero extend the argument
2775 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002776 // Perform the larger operation, then subtract if needed.
2777 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002778 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002779 case ISD::CTPOP:
2780 Result = Tmp1;
2781 break;
2782 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002783 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002784 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002785 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002786 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002787 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002788 break;
2789 case ISD::CTLZ:
2790 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002791 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2792 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002793 getSizeInBits(VT), NVT));
2794 break;
2795 }
2796 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002797 }
2798
2799 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002800
2801 // Make sure the result is itself legal.
2802 Result = LegalizeOp(Result);
2803
2804 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002805 AddPromotedOperand(Op, Result);
2806 return Result;
2807}
Chris Lattnerdc750592005-01-07 07:47:09 +00002808
Nate Begeman7e7f4392006-02-01 07:19:44 +00002809/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2810/// with condition CC on the current target. This usually involves legalizing
2811/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2812/// there may be no choice but to create a new SetCC node to represent the
2813/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2814/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2815void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2816 SDOperand &RHS,
2817 SDOperand &CC) {
2818 SDOperand Tmp1, Tmp2, Result;
2819
2820 switch (getTypeAction(LHS.getValueType())) {
2821 case Legal:
2822 Tmp1 = LegalizeOp(LHS); // LHS
2823 Tmp2 = LegalizeOp(RHS); // RHS
2824 break;
2825 case Promote:
2826 Tmp1 = PromoteOp(LHS); // LHS
2827 Tmp2 = PromoteOp(RHS); // RHS
2828
2829 // If this is an FP compare, the operands have already been extended.
2830 if (MVT::isInteger(LHS.getValueType())) {
2831 MVT::ValueType VT = LHS.getValueType();
2832 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2833
2834 // Otherwise, we have to insert explicit sign or zero extends. Note
2835 // that we could insert sign extends for ALL conditions, but zero extend
2836 // is cheaper on many machines (an AND instead of two shifts), so prefer
2837 // it.
2838 switch (cast<CondCodeSDNode>(CC)->get()) {
2839 default: assert(0 && "Unknown integer comparison!");
2840 case ISD::SETEQ:
2841 case ISD::SETNE:
2842 case ISD::SETUGE:
2843 case ISD::SETUGT:
2844 case ISD::SETULE:
2845 case ISD::SETULT:
2846 // ALL of these operations will work if we either sign or zero extend
2847 // the operands (including the unsigned comparisons!). Zero extend is
2848 // usually a simpler/cheaper operation, so prefer it.
2849 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2850 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2851 break;
2852 case ISD::SETGE:
2853 case ISD::SETGT:
2854 case ISD::SETLT:
2855 case ISD::SETLE:
2856 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2857 DAG.getValueType(VT));
2858 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2859 DAG.getValueType(VT));
2860 break;
2861 }
2862 }
2863 break;
2864 case Expand:
2865 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2866 ExpandOp(LHS, LHSLo, LHSHi);
2867 ExpandOp(RHS, RHSLo, RHSHi);
2868 switch (cast<CondCodeSDNode>(CC)->get()) {
2869 case ISD::SETEQ:
2870 case ISD::SETNE:
2871 if (RHSLo == RHSHi)
2872 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2873 if (RHSCST->isAllOnesValue()) {
2874 // Comparison to -1.
2875 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2876 Tmp2 = RHSLo;
2877 break;
2878 }
2879
2880 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2881 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2882 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2883 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2884 break;
2885 default:
2886 // If this is a comparison of the sign bit, just look at the top part.
2887 // X > -1, x < 0
2888 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2889 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2890 CST->getValue() == 0) || // X < 0
2891 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2892 CST->isAllOnesValue())) { // X > -1
2893 Tmp1 = LHSHi;
2894 Tmp2 = RHSHi;
2895 break;
2896 }
2897
2898 // FIXME: This generated code sucks.
2899 ISD::CondCode LowCC;
2900 switch (cast<CondCodeSDNode>(CC)->get()) {
2901 default: assert(0 && "Unknown integer setcc!");
2902 case ISD::SETLT:
2903 case ISD::SETULT: LowCC = ISD::SETULT; break;
2904 case ISD::SETGT:
2905 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2906 case ISD::SETLE:
2907 case ISD::SETULE: LowCC = ISD::SETULE; break;
2908 case ISD::SETGE:
2909 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2910 }
2911
2912 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2913 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2914 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2915
2916 // NOTE: on targets without efficient SELECT of bools, we can always use
2917 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2918 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2919 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2920 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2921 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2922 Result, Tmp1, Tmp2));
2923 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00002924 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00002925 }
2926 }
2927 LHS = Tmp1;
2928 RHS = Tmp2;
2929}
2930
Chris Lattner36e663d2005-12-23 00:16:34 +00002931/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002932/// The resultant code need not be legal. Note that SrcOp is the input operand
2933/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002934SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2935 SDOperand SrcOp) {
2936 // Create the stack frame object.
2937 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2938 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002939 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002940 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2941
2942 // Emit a store to the stack slot.
2943 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002944 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002945 // Result is a load from the stack slot.
2946 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2947}
2948
Chris Lattner4157c412005-04-02 04:00:59 +00002949void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2950 SDOperand Op, SDOperand Amt,
2951 SDOperand &Lo, SDOperand &Hi) {
2952 // Expand the subcomponents.
2953 SDOperand LHSL, LHSH;
2954 ExpandOp(Op, LHSL, LHSH);
2955
2956 std::vector<SDOperand> Ops;
2957 Ops.push_back(LHSL);
2958 Ops.push_back(LHSH);
2959 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002960 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002961 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002962 Hi = Lo.getValue(1);
2963}
2964
2965
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002966/// ExpandShift - Try to find a clever way to expand this shift operation out to
2967/// smaller elements. If we can't find a way that is more efficient than a
2968/// libcall on this target, return false. Otherwise, return true with the
2969/// low-parts expanded into Lo and Hi.
2970bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2971 SDOperand &Lo, SDOperand &Hi) {
2972 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2973 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002974
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002975 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002976 SDOperand ShAmt = LegalizeOp(Amt);
2977 MVT::ValueType ShTy = ShAmt.getValueType();
2978 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2979 unsigned NVTBits = MVT::getSizeInBits(NVT);
2980
2981 // Handle the case when Amt is an immediate. Other cases are currently broken
2982 // and are disabled.
2983 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2984 unsigned Cst = CN->getValue();
2985 // Expand the incoming operand to be shifted, so that we have its parts
2986 SDOperand InL, InH;
2987 ExpandOp(Op, InL, InH);
2988 switch(Opc) {
2989 case ISD::SHL:
2990 if (Cst > VTBits) {
2991 Lo = DAG.getConstant(0, NVT);
2992 Hi = DAG.getConstant(0, NVT);
2993 } else if (Cst > NVTBits) {
2994 Lo = DAG.getConstant(0, NVT);
2995 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002996 } else if (Cst == NVTBits) {
2997 Lo = DAG.getConstant(0, NVT);
2998 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002999 } else {
3000 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3001 Hi = DAG.getNode(ISD::OR, NVT,
3002 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3003 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3004 }
3005 return true;
3006 case ISD::SRL:
3007 if (Cst > VTBits) {
3008 Lo = DAG.getConstant(0, NVT);
3009 Hi = DAG.getConstant(0, NVT);
3010 } else if (Cst > NVTBits) {
3011 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3012 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003013 } else if (Cst == NVTBits) {
3014 Lo = InH;
3015 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003016 } else {
3017 Lo = DAG.getNode(ISD::OR, NVT,
3018 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3019 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3020 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3021 }
3022 return true;
3023 case ISD::SRA:
3024 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003025 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003026 DAG.getConstant(NVTBits-1, ShTy));
3027 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003028 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003029 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003030 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003031 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003032 } else if (Cst == NVTBits) {
3033 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003034 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003035 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003036 } else {
3037 Lo = DAG.getNode(ISD::OR, NVT,
3038 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3039 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3040 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3041 }
3042 return true;
3043 }
3044 }
Nate Begemanb0674922005-04-06 21:13:14 +00003045 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003046}
Chris Lattneraac464e2005-01-21 06:05:23 +00003047
Chris Lattner4add7e32005-01-23 04:42:50 +00003048
Chris Lattneraac464e2005-01-21 06:05:23 +00003049// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3050// does not fit into a register, return the lo part and set the hi part to the
3051// by-reg argument. If it does fit into a single register, return the result
3052// and leave the Hi part unset.
3053SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3054 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003055 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3056 // The input chain to this libcall is the entry node of the function.
3057 // Legalizing the call will automatically add the previous call to the
3058 // dependence.
3059 SDOperand InChain = DAG.getEntryNode();
3060
Chris Lattneraac464e2005-01-21 06:05:23 +00003061 TargetLowering::ArgListTy Args;
3062 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3063 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3064 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3065 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3066 }
3067 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003068
Chris Lattner06bbeb62005-05-11 19:02:11 +00003069 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003070 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003071 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003072 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3073 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003074
Chris Lattner462505f2006-02-13 09:18:02 +00003075 // Legalize the call sequence, starting with the chain. This will advance
3076 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3077 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3078 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003079 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003080 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003081 default: assert(0 && "Unknown thing");
3082 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003083 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003084 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003085 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003086 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003087 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003088 }
Chris Lattner63022662005-09-02 20:26:58 +00003089 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003090}
3091
Chris Lattner4add7e32005-01-23 04:42:50 +00003092
Chris Lattneraac464e2005-01-21 06:05:23 +00003093/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3094/// destination type is legal.
3095SDOperand SelectionDAGLegalize::
3096ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003097 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003098 assert(getTypeAction(Source.getValueType()) == Expand &&
3099 "This is not an expansion!");
3100 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3101
Chris Lattner06bbeb62005-05-11 19:02:11 +00003102 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003103 assert(Source.getValueType() == MVT::i64 &&
3104 "This only works for 64-bit -> FP");
3105 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3106 // incoming integer is set. To handle this, we dynamically test to see if
3107 // it is set, and, if so, add a fudge factor.
3108 SDOperand Lo, Hi;
3109 ExpandOp(Source, Lo, Hi);
3110
Chris Lattner2a4f7312005-05-13 04:45:13 +00003111 // If this is unsigned, and not supported, first perform the conversion to
3112 // signed, then adjust the result if the sign bit is set.
3113 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3114 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3115
Chris Lattnerd47675e2005-08-09 20:20:18 +00003116 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3117 DAG.getConstant(0, Hi.getValueType()),
3118 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003119 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3120 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3121 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003122 uint64_t FF = 0x5f800000ULL;
3123 if (TLI.isLittleEndian()) FF <<= 32;
3124 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003125
Chris Lattnerc30405e2005-08-26 17:15:30 +00003126 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003127 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3128 SDOperand FudgeInReg;
3129 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003130 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3131 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003132 else {
3133 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003134 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3135 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003136 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003137 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003138 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003139
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003140 // Check to see if the target has a custom way to lower this. If so, use it.
3141 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3142 default: assert(0 && "This action not implemented for this operation!");
3143 case TargetLowering::Legal:
3144 case TargetLowering::Expand:
3145 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003146 case TargetLowering::Custom: {
3147 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3148 Source), DAG);
3149 if (NV.Val)
3150 return LegalizeOp(NV);
3151 break; // The target decided this was legal after all
3152 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003153 }
3154
Chris Lattner153587e2005-05-12 07:00:44 +00003155 // Expand the source, then glue it back together for the call. We must expand
3156 // the source in case it is shared (this pass of legalize must traverse it).
3157 SDOperand SrcLo, SrcHi;
3158 ExpandOp(Source, SrcLo, SrcHi);
3159 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3160
Chris Lattner06bbeb62005-05-11 19:02:11 +00003161 const char *FnName = 0;
3162 if (DestTy == MVT::f32)
3163 FnName = "__floatdisf";
3164 else {
3165 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3166 FnName = "__floatdidf";
3167 }
Chris Lattner462505f2006-02-13 09:18:02 +00003168
3169 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3170 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003171 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003172}
Misha Brukman835702a2005-04-21 22:36:52 +00003173
Chris Lattner689bdcc2006-01-28 08:25:58 +00003174/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3175/// INT_TO_FP operation of the specified operand when the target requests that
3176/// we expand it. At this point, we know that the result and operand types are
3177/// legal for the target.
3178SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3179 SDOperand Op0,
3180 MVT::ValueType DestVT) {
3181 if (Op0.getValueType() == MVT::i32) {
3182 // simple 32-bit [signed|unsigned] integer to float/double expansion
3183
3184 // get the stack frame index of a 8 byte buffer
3185 MachineFunction &MF = DAG.getMachineFunction();
3186 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3187 // get address of 8 byte buffer
3188 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3189 // word offset constant for Hi/Lo address computation
3190 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3191 // set up Hi and Lo (into buffer) address based on endian
3192 SDOperand Hi, Lo;
3193 if (TLI.isLittleEndian()) {
3194 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3195 Lo = StackSlot;
3196 } else {
3197 Hi = StackSlot;
3198 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3199 }
3200 // if signed map to unsigned space
3201 SDOperand Op0Mapped;
3202 if (isSigned) {
3203 // constant used to invert sign bit (signed to unsigned mapping)
3204 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3205 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3206 } else {
3207 Op0Mapped = Op0;
3208 }
3209 // store the lo of the constructed double - based on integer input
3210 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3211 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3212 // initial hi portion of constructed double
3213 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3214 // store the hi of the constructed double - biased exponent
3215 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3216 InitialHi, Hi, DAG.getSrcValue(NULL));
3217 // load the constructed double
3218 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3219 DAG.getSrcValue(NULL));
3220 // FP constant to bias correct the final result
3221 SDOperand Bias = DAG.getConstantFP(isSigned ?
3222 BitsToDouble(0x4330000080000000ULL)
3223 : BitsToDouble(0x4330000000000000ULL),
3224 MVT::f64);
3225 // subtract the bias
3226 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3227 // final result
3228 SDOperand Result;
3229 // handle final rounding
3230 if (DestVT == MVT::f64) {
3231 // do nothing
3232 Result = Sub;
3233 } else {
3234 // if f32 then cast to f32
3235 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3236 }
3237 return Result;
3238 }
3239 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3240 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3241
3242 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3243 DAG.getConstant(0, Op0.getValueType()),
3244 ISD::SETLT);
3245 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3246 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3247 SignSet, Four, Zero);
3248
3249 // If the sign bit of the integer is set, the large number will be treated
3250 // as a negative number. To counteract this, the dynamic code adds an
3251 // offset depending on the data type.
3252 uint64_t FF;
3253 switch (Op0.getValueType()) {
3254 default: assert(0 && "Unsupported integer type!");
3255 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3256 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3257 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3258 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3259 }
3260 if (TLI.isLittleEndian()) FF <<= 32;
3261 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3262
3263 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3264 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3265 SDOperand FudgeInReg;
3266 if (DestVT == MVT::f32)
3267 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3268 DAG.getSrcValue(NULL));
3269 else {
3270 assert(DestVT == MVT::f64 && "Unexpected conversion");
3271 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3272 DAG.getEntryNode(), CPIdx,
3273 DAG.getSrcValue(NULL), MVT::f32));
3274 }
3275
3276 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3277}
3278
3279/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3280/// *INT_TO_FP operation of the specified operand when the target requests that
3281/// we promote it. At this point, we know that the result and operand types are
3282/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3283/// operation that takes a larger input.
3284SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3285 MVT::ValueType DestVT,
3286 bool isSigned) {
3287 // First step, figure out the appropriate *INT_TO_FP operation to use.
3288 MVT::ValueType NewInTy = LegalOp.getValueType();
3289
3290 unsigned OpToUse = 0;
3291
3292 // Scan for the appropriate larger type to use.
3293 while (1) {
3294 NewInTy = (MVT::ValueType)(NewInTy+1);
3295 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3296
3297 // If the target supports SINT_TO_FP of this type, use it.
3298 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3299 default: break;
3300 case TargetLowering::Legal:
3301 if (!TLI.isTypeLegal(NewInTy))
3302 break; // Can't use this datatype.
3303 // FALL THROUGH.
3304 case TargetLowering::Custom:
3305 OpToUse = ISD::SINT_TO_FP;
3306 break;
3307 }
3308 if (OpToUse) break;
3309 if (isSigned) continue;
3310
3311 // If the target supports UINT_TO_FP of this type, use it.
3312 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3313 default: break;
3314 case TargetLowering::Legal:
3315 if (!TLI.isTypeLegal(NewInTy))
3316 break; // Can't use this datatype.
3317 // FALL THROUGH.
3318 case TargetLowering::Custom:
3319 OpToUse = ISD::UINT_TO_FP;
3320 break;
3321 }
3322 if (OpToUse) break;
3323
3324 // Otherwise, try a larger type.
3325 }
3326
3327 // Okay, we found the operation and type to use. Zero extend our input to the
3328 // desired type then run the operation on it.
3329 return DAG.getNode(OpToUse, DestVT,
3330 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3331 NewInTy, LegalOp));
3332}
3333
3334/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3335/// FP_TO_*INT operation of the specified operand when the target requests that
3336/// we promote it. At this point, we know that the result and operand types are
3337/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3338/// operation that returns a larger result.
3339SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3340 MVT::ValueType DestVT,
3341 bool isSigned) {
3342 // First step, figure out the appropriate FP_TO*INT operation to use.
3343 MVT::ValueType NewOutTy = DestVT;
3344
3345 unsigned OpToUse = 0;
3346
3347 // Scan for the appropriate larger type to use.
3348 while (1) {
3349 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3350 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3351
3352 // If the target supports FP_TO_SINT returning this type, use it.
3353 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3354 default: break;
3355 case TargetLowering::Legal:
3356 if (!TLI.isTypeLegal(NewOutTy))
3357 break; // Can't use this datatype.
3358 // FALL THROUGH.
3359 case TargetLowering::Custom:
3360 OpToUse = ISD::FP_TO_SINT;
3361 break;
3362 }
3363 if (OpToUse) break;
3364
3365 // If the target supports FP_TO_UINT of this type, use it.
3366 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3367 default: break;
3368 case TargetLowering::Legal:
3369 if (!TLI.isTypeLegal(NewOutTy))
3370 break; // Can't use this datatype.
3371 // FALL THROUGH.
3372 case TargetLowering::Custom:
3373 OpToUse = ISD::FP_TO_UINT;
3374 break;
3375 }
3376 if (OpToUse) break;
3377
3378 // Otherwise, try a larger type.
3379 }
3380
3381 // Okay, we found the operation and type to use. Truncate the result of the
3382 // extended FP_TO_*INT operation to the desired size.
3383 return DAG.getNode(ISD::TRUNCATE, DestVT,
3384 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3385}
3386
3387/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3388///
3389SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3390 MVT::ValueType VT = Op.getValueType();
3391 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3392 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3393 switch (VT) {
3394 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3395 case MVT::i16:
3396 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3397 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3398 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3399 case MVT::i32:
3400 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3401 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3402 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3403 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3404 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3405 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3406 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3407 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3408 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3409 case MVT::i64:
3410 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3411 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3412 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3413 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3414 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3415 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3416 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3417 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3418 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3419 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3420 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3421 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3422 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3423 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3424 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3425 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3426 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3427 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3428 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3429 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3430 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3431 }
3432}
3433
3434/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3435///
3436SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3437 switch (Opc) {
3438 default: assert(0 && "Cannot expand this yet!");
3439 case ISD::CTPOP: {
3440 static const uint64_t mask[6] = {
3441 0x5555555555555555ULL, 0x3333333333333333ULL,
3442 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3443 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3444 };
3445 MVT::ValueType VT = Op.getValueType();
3446 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3447 unsigned len = getSizeInBits(VT);
3448 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3449 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3450 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3451 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3452 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3453 DAG.getNode(ISD::AND, VT,
3454 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3455 }
3456 return Op;
3457 }
3458 case ISD::CTLZ: {
3459 // for now, we do this:
3460 // x = x | (x >> 1);
3461 // x = x | (x >> 2);
3462 // ...
3463 // x = x | (x >>16);
3464 // x = x | (x >>32); // for 64-bit input
3465 // return popcount(~x);
3466 //
3467 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3468 MVT::ValueType VT = Op.getValueType();
3469 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3470 unsigned len = getSizeInBits(VT);
3471 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3472 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3473 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3474 }
3475 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3476 return DAG.getNode(ISD::CTPOP, VT, Op);
3477 }
3478 case ISD::CTTZ: {
3479 // for now, we use: { return popcount(~x & (x - 1)); }
3480 // unless the target has ctlz but not ctpop, in which case we use:
3481 // { return 32 - nlz(~x & (x-1)); }
3482 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3483 MVT::ValueType VT = Op.getValueType();
3484 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3485 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3486 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3487 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3488 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3489 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3490 TLI.isOperationLegal(ISD::CTLZ, VT))
3491 return DAG.getNode(ISD::SUB, VT,
3492 DAG.getConstant(getSizeInBits(VT), VT),
3493 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3494 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3495 }
3496 }
3497}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003498
3499
Chris Lattnerdc750592005-01-07 07:47:09 +00003500/// ExpandOp - Expand the specified SDOperand into its two component pieces
3501/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3502/// LegalizeNodes map is filled in for any results that are not expanded, the
3503/// ExpandedNodes map is filled in for any results that are expanded, and the
3504/// Lo/Hi values are returned.
3505void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3506 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003507 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003508 SDNode *Node = Op.Val;
3509 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003510 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3511 "Cannot expand FP values!");
3512 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003513 "Cannot expand to FP value or to larger int value!");
3514
Chris Lattner1a570f12005-09-02 20:32:45 +00003515 // See if we already expanded it.
3516 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3517 = ExpandedNodes.find(Op);
3518 if (I != ExpandedNodes.end()) {
3519 Lo = I->second.first;
3520 Hi = I->second.second;
3521 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003522 }
3523
Chris Lattnerdc750592005-01-07 07:47:09 +00003524 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003525 case ISD::CopyFromReg:
3526 assert(0 && "CopyFromReg must be legal!");
3527 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003528 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3529 assert(0 && "Do not know how to expand this operator!");
3530 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003531 case ISD::UNDEF:
3532 Lo = DAG.getNode(ISD::UNDEF, NVT);
3533 Hi = DAG.getNode(ISD::UNDEF, NVT);
3534 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003535 case ISD::Constant: {
3536 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3537 Lo = DAG.getConstant(Cst, NVT);
3538 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3539 break;
3540 }
Evan Chengb97aab42006-03-01 01:09:54 +00003541 case ISD::VConstant: {
3542 unsigned NumElements =
3543 cast<ConstantSDNode>(Node->getOperand(0))->getValue() / 2;
3544 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3545 MVT::ValueType TVT = (NumElements > 1)
3546 ? getVectorType(EVT, NumElements) : EVT;
3547 // If type of bisected vector is legal, turn it into a ConstantVec (which
3548 // will be lowered to a ConstantPool or something else). Otherwise, bisect
3549 // the VConstant, and return each half as a new VConstant.
3550 unsigned Opc = ISD::ConstantVec;
3551 std::vector<SDOperand> LoOps, HiOps;
3552 if (!(TVT != MVT::Other &&
3553 (!MVT::isVector(TVT) || TLI.isTypeLegal(TVT)))) {
3554 Opc = ISD::VConstant;
3555 TVT = MVT::Vector;
3556 SDOperand Num = DAG.getConstant(NumElements, MVT::i32);
3557 SDOperand Typ = DAG.getValueType(EVT);
3558 HiOps.push_back(Num);
3559 HiOps.push_back(Typ);
3560 LoOps.push_back(Num);
3561 LoOps.push_back(Typ);
3562 }
3563
3564 if (NumElements == 1) {
3565 Hi = Node->getOperand(2);
3566 Lo = Node->getOperand(3);
Nate Begemanae89d862005-12-07 19:48:11 +00003567 } else {
Nate Begemanae89d862005-12-07 19:48:11 +00003568 for (unsigned I = 0, E = NumElements; I < E; ++I) {
Evan Chengb97aab42006-03-01 01:09:54 +00003569 HiOps.push_back(Node->getOperand(I+2));
3570 LoOps.push_back(Node->getOperand(I+2+NumElements));
Nate Begemanae89d862005-12-07 19:48:11 +00003571 }
Evan Chengb97aab42006-03-01 01:09:54 +00003572 Hi = DAG.getNode(Opc, TVT, HiOps);
3573 Lo = DAG.getNode(Opc, TVT, LoOps);
Nate Begemanae89d862005-12-07 19:48:11 +00003574 }
3575 break;
3576 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003577
Chris Lattner32e08b72005-03-28 22:03:13 +00003578 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003579 // Return the operands.
3580 Lo = Node->getOperand(0);
3581 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003582 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003583
3584 case ISD::SIGN_EXTEND_INREG:
3585 ExpandOp(Node->getOperand(0), Lo, Hi);
3586 // Sign extend the lo-part.
3587 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3588 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3589 TLI.getShiftAmountTy()));
3590 // sext_inreg the low part if needed.
3591 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3592 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003593
Nate Begeman2fba8a32006-01-14 03:14:10 +00003594 case ISD::BSWAP: {
3595 ExpandOp(Node->getOperand(0), Lo, Hi);
3596 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3597 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3598 Lo = TempLo;
3599 break;
3600 }
3601
Chris Lattner55e9cde2005-05-11 04:51:16 +00003602 case ISD::CTPOP:
3603 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003604 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3605 DAG.getNode(ISD::CTPOP, NVT, Lo),
3606 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003607 Hi = DAG.getConstant(0, NVT);
3608 break;
3609
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003610 case ISD::CTLZ: {
3611 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003612 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003613 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3614 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003615 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3616 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003617 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3618 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3619
3620 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3621 Hi = DAG.getConstant(0, NVT);
3622 break;
3623 }
3624
3625 case ISD::CTTZ: {
3626 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003627 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003628 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3629 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003630 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3631 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003632 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3633 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3634
3635 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3636 Hi = DAG.getConstant(0, NVT);
3637 break;
3638 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003639
Nate Begemane74795c2006-01-25 18:21:52 +00003640 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003641 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3642 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003643 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3644 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3645
3646 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003647 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003648 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3649 if (!TLI.isLittleEndian())
3650 std::swap(Lo, Hi);
3651 break;
3652 }
3653
Chris Lattnerdc750592005-01-07 07:47:09 +00003654 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003655 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3656 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003657 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003658
3659 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003660 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003661 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3662 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003663 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003664 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003665
3666 // Build a factor node to remember that this load is independent of the
3667 // other one.
3668 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3669 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003670
Chris Lattnerdc750592005-01-07 07:47:09 +00003671 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003672 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003673 if (!TLI.isLittleEndian())
3674 std::swap(Lo, Hi);
3675 break;
3676 }
Nate Begemand37c1312005-11-22 18:16:00 +00003677 case ISD::VLOAD: {
Evan Chengb97aab42006-03-01 01:09:54 +00003678 SDOperand Ch = Node->getOperand(2); // Legalize the chain.
3679 SDOperand Ptr = Node->getOperand(3); // Legalize the pointer.
3680 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(0))->getValue();
3681 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3682 MVT::ValueType TVT = (NumElements/2 > 1)
3683 ? getVectorType(EVT, NumElements/2) : EVT;
Nate Begemand37c1312005-11-22 18:16:00 +00003684
Evan Chengb97aab42006-03-01 01:09:54 +00003685 // If type of split vector is legal, turn into a pair of scalar or
3686 // packed loads.
3687 if (TVT != MVT::Other &&
3688 (!MVT::isVector(TVT) ||
3689 (TLI.isTypeLegal(TVT) && TLI.isOperationLegal(ISD::LOAD, TVT)))) {
3690 Lo = DAG.getLoad(TVT, Ch, Ptr, Node->getOperand(4));
Nate Begemand37c1312005-11-22 18:16:00 +00003691 // Increment the pointer to the other half.
Evan Chengb97aab42006-03-01 01:09:54 +00003692 unsigned IncrementSize = MVT::getSizeInBits(TVT)/8;
Nate Begemand37c1312005-11-22 18:16:00 +00003693 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3694 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003695 // FIXME: This creates a bogus srcvalue!
Evan Chengb97aab42006-03-01 01:09:54 +00003696 Hi = DAG.getLoad(TVT, Ch, Ptr, Node->getOperand(4));
Nate Begemand37c1312005-11-22 18:16:00 +00003697 } else {
3698 NumElements /= 2; // Split the vector in half
3699 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3700 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3701 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3702 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003703 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003704 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3705 }
3706
3707 // Build a factor node to remember that this load is independent of the
3708 // other one.
3709 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3710 Hi.getValue(1));
3711
3712 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003713 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemand37c1312005-11-22 18:16:00 +00003714 if (!TLI.isLittleEndian())
3715 std::swap(Lo, Hi);
3716 break;
3717 }
3718 case ISD::VADD:
3719 case ISD::VSUB:
Evan Cheng3bf916d2006-03-03 07:01:07 +00003720 case ISD::VMUL:
3721 case ISD::VSDIV:
3722 case ISD::VUDIV:
3723 case ISD::VAND:
3724 case ISD::VOR:
3725 case ISD::VXOR: {
Evan Chengb97aab42006-03-01 01:09:54 +00003726 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(0))->getValue();
3727 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3728 MVT::ValueType TVT = (NumElements/2 > 1)
3729 ? getVectorType(EVT, NumElements/2) : EVT;
Nate Begemand37c1312005-11-22 18:16:00 +00003730 SDOperand LL, LH, RL, RH;
3731
Evan Chengb97aab42006-03-01 01:09:54 +00003732 ExpandOp(Node->getOperand(2), LL, LH);
3733 ExpandOp(Node->getOperand(3), RL, RH);
Nate Begemand37c1312005-11-22 18:16:00 +00003734
Evan Chengb97aab42006-03-01 01:09:54 +00003735 // If type of split vector is legal, turn into a pair of scalar / packed
3736 // ADD, SUB, or MUL.
3737 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3738 if (TVT != MVT::Other &&
3739 (!MVT::isVector(TVT) ||
3740 (TLI.isTypeLegal(TVT) && TLI.isOperationLegal(Opc, TVT)))) {
3741 Lo = DAG.getNode(Opc, TVT, LL, RL);
3742 Hi = DAG.getNode(Opc, TVT, LH, RH);
Nate Begemand37c1312005-11-22 18:16:00 +00003743 } else {
Evan Chengb97aab42006-03-01 01:09:54 +00003744 SDOperand Num = DAG.getConstant(NumElements/2, MVT::i32);
3745 SDOperand Typ = DAG.getValueType(EVT);
3746 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, Num, Typ, LL, RL);
3747 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, Num, Typ, LH, RH);
Nate Begemand37c1312005-11-22 18:16:00 +00003748 }
3749 break;
3750 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003751 case ISD::AND:
3752 case ISD::OR:
3753 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3754 SDOperand LL, LH, RL, RH;
3755 ExpandOp(Node->getOperand(0), LL, LH);
3756 ExpandOp(Node->getOperand(1), RL, RH);
3757 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3758 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3759 break;
3760 }
3761 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003762 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003763 ExpandOp(Node->getOperand(1), LL, LH);
3764 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003765 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3766 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003767 break;
3768 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003769 case ISD::SELECT_CC: {
3770 SDOperand TL, TH, FL, FH;
3771 ExpandOp(Node->getOperand(2), TL, TH);
3772 ExpandOp(Node->getOperand(3), FL, FH);
3773 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3774 Node->getOperand(1), TL, FL, Node->getOperand(4));
3775 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3776 Node->getOperand(1), TH, FH, Node->getOperand(4));
3777 break;
3778 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003779 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003780 SDOperand Chain = Node->getOperand(0);
3781 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003782 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3783
3784 if (EVT == NVT)
3785 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3786 else
3787 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3788 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003789
3790 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003791 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003792
Nate Begemanc3a89c52005-10-13 17:15:37 +00003793 // The high part is obtained by SRA'ing all but one of the bits of the lo
3794 // part.
3795 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3796 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3797 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003798 break;
3799 }
3800 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003801 SDOperand Chain = Node->getOperand(0);
3802 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003803 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3804
3805 if (EVT == NVT)
3806 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3807 else
3808 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3809 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003810
3811 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003812 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003813
Nate Begemanc3a89c52005-10-13 17:15:37 +00003814 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003815 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003816 break;
3817 }
3818 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003819 SDOperand Chain = Node->getOperand(0);
3820 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003821 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3822
3823 if (EVT == NVT)
3824 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3825 else
3826 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3827 EVT);
3828
3829 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003830 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003831
3832 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003833 Hi = DAG.getNode(ISD::UNDEF, NVT);
3834 break;
3835 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003836 case ISD::ANY_EXTEND:
3837 // The low part is any extension of the input (which degenerates to a copy).
3838 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3839 // The high part is undefined.
3840 Hi = DAG.getNode(ISD::UNDEF, NVT);
3841 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003842 case ISD::SIGN_EXTEND: {
3843 // The low part is just a sign extension of the input (which degenerates to
3844 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003845 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003846
Chris Lattnerdc750592005-01-07 07:47:09 +00003847 // The high part is obtained by SRA'ing all but one of the bits of the lo
3848 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003849 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003850 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3851 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003852 break;
3853 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003854 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003855 // The low part is just a zero extension of the input (which degenerates to
3856 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003857 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003858
Chris Lattnerdc750592005-01-07 07:47:09 +00003859 // The high part is just a zero.
3860 Hi = DAG.getConstant(0, NVT);
3861 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003862
3863 case ISD::BIT_CONVERT: {
3864 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3865 Node->getOperand(0));
3866 ExpandOp(Tmp, Lo, Hi);
3867 break;
3868 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003869
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003870 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00003871 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3872 TargetLowering::Custom &&
3873 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003874 Lo = TLI.LowerOperation(Op, DAG);
3875 assert(Lo.Val && "Node must be custom expanded!");
3876 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00003877 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003878 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003879 break;
3880
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003881 // These operators cannot be expanded directly, emit them as calls to
3882 // library functions.
3883 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003884 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003885 SDOperand Op;
3886 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3887 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003888 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3889 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00003890 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003891
Chris Lattner941d84a2005-07-30 01:40:57 +00003892 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3893
Chris Lattnerfe68d752005-07-29 00:33:32 +00003894 // Now that the custom expander is done, expand the result, which is still
3895 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003896 if (Op.Val) {
3897 ExpandOp(Op, Lo, Hi);
3898 break;
3899 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003900 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003901
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003902 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003903 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003904 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003905 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003906 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003907
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003908 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003909 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003910 SDOperand Op;
3911 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3912 case Expand: assert(0 && "cannot expand FP!");
3913 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3914 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3915 }
3916
3917 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3918
3919 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003920 if (Op.Val) {
3921 ExpandOp(Op, Lo, Hi);
3922 break;
3923 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003924 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003925
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003926 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003927 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003928 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003929 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003930 break;
3931
Evan Cheng870e4f82006-01-09 18:31:59 +00003932 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003933 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003934 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003935 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003936 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003937 Op = TLI.LowerOperation(Op, DAG);
3938 if (Op.Val) {
3939 // Now that the custom expander is done, expand the result, which is
3940 // still VT.
3941 ExpandOp(Op, Lo, Hi);
3942 break;
3943 }
3944 }
3945
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003946 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003947 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003948 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003949
3950 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003951 TargetLowering::LegalizeAction Action =
3952 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3953 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3954 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003955 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003956 break;
3957 }
3958
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003959 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003960 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003961 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003962 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003963
Evan Cheng870e4f82006-01-09 18:31:59 +00003964 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003965 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003966 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003967 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003968 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003969 Op = TLI.LowerOperation(Op, DAG);
3970 if (Op.Val) {
3971 // Now that the custom expander is done, expand the result, which is
3972 // still VT.
3973 ExpandOp(Op, Lo, Hi);
3974 break;
3975 }
3976 }
3977
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003978 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003979 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003980 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003981
3982 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003983 TargetLowering::LegalizeAction Action =
3984 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3985 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3986 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003987 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003988 break;
3989 }
3990
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003991 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003992 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003993 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003994 }
3995
3996 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003997 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003998 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003999 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004000 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004001 Op = TLI.LowerOperation(Op, DAG);
4002 if (Op.Val) {
4003 // Now that the custom expander is done, expand the result, which is
4004 // still VT.
4005 ExpandOp(Op, Lo, Hi);
4006 break;
4007 }
4008 }
4009
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004010 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004011 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004012 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004013
4014 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004015 TargetLowering::LegalizeAction Action =
4016 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4017 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4018 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004019 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004020 break;
4021 }
4022
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004023 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004024 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004025 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004026 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004027
Misha Brukman835702a2005-04-21 22:36:52 +00004028 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004029 case ISD::SUB: {
4030 // If the target wants to custom expand this, let them.
4031 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4032 TargetLowering::Custom) {
4033 Op = TLI.LowerOperation(Op, DAG);
4034 if (Op.Val) {
4035 ExpandOp(Op, Lo, Hi);
4036 break;
4037 }
4038 }
4039
4040 // Expand the subcomponents.
4041 SDOperand LHSL, LHSH, RHSL, RHSH;
4042 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4043 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004044 std::vector<MVT::ValueType> VTs;
4045 std::vector<SDOperand> LoOps, HiOps;
4046 VTs.push_back(LHSL.getValueType());
4047 VTs.push_back(MVT::Flag);
4048 LoOps.push_back(LHSL);
4049 LoOps.push_back(RHSL);
4050 HiOps.push_back(LHSH);
4051 HiOps.push_back(RHSH);
4052 if (Node->getOpcode() == ISD::ADD) {
4053 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4054 HiOps.push_back(Lo.getValue(1));
4055 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4056 } else {
4057 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4058 HiOps.push_back(Lo.getValue(1));
4059 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4060 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004061 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004062 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004063 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004064 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004065 SDOperand LL, LH, RL, RH;
4066 ExpandOp(Node->getOperand(0), LL, LH);
4067 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004068 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4069 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4070 // extended the sign bit of the low half through the upper half, and if so
4071 // emit a MULHS instead of the alternate sequence that is valid for any
4072 // i64 x i64 multiply.
4073 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4074 // is RH an extension of the sign bit of RL?
4075 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4076 RH.getOperand(1).getOpcode() == ISD::Constant &&
4077 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4078 // is LH an extension of the sign bit of LL?
4079 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4080 LH.getOperand(1).getOpcode() == ISD::Constant &&
4081 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4082 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4083 } else {
4084 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4085 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4086 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4087 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4088 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4089 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004090 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4091 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004092 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004093 }
4094 break;
4095 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004096 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4097 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4098 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4099 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004100 }
4101
Chris Lattnerac12f682005-12-21 18:02:52 +00004102 // Make sure the resultant values have been legalized themselves, unless this
4103 // is a type that requires multi-step expansion.
4104 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4105 Lo = LegalizeOp(Lo);
4106 Hi = LegalizeOp(Hi);
4107 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004108
4109 // Remember in a map if the values will be reused later.
4110 bool isNew =
4111 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4112 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004113}
4114
4115
4116// SelectionDAG::Legalize - This is the entry point for the file.
4117//
Chris Lattner4add7e32005-01-23 04:42:50 +00004118void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004119 /// run - This is the main entry point to this class.
4120 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004121 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004122}
4123