blob: a0c10a09f72393155b9c93ea1dc3397669de9ad6 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
Chris Lattner6831a812006-02-13 09:18:02 +000044 // Libcall insertion helpers.
45
46 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
47 /// legalized. We use this to ensure that calls are properly serialized
48 /// against each other, including inserted libcalls.
49 SDOperand LastCALLSEQ_END;
50
51 /// IsLegalizingCall - This member is used *only* for purposes of providing
52 /// helpful assertions that a libcall isn't created while another call is
53 /// being legalized (which could lead to non-serialized call sequences).
54 bool IsLegalizingCall;
55
Chris Lattner3e928bb2005-01-07 07:47:09 +000056 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000057 Legal, // The target natively supports this operation.
58 Promote, // This operation should be executed in a larger type.
59 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000060 };
Chris Lattner6831a812006-02-13 09:18:02 +000061
Chris Lattner3e928bb2005-01-07 07:47:09 +000062 /// ValueTypeActions - This is a bitvector that contains two bits for each
63 /// value type, where the two bits correspond to the LegalizeAction enum.
64 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000065 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000066
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
70 std::map<SDOperand, SDOperand> LegalizedNodes;
71
Chris Lattner03c85462005-01-15 05:21:40 +000072 /// PromotedNodes - For nodes that are below legal width, and that have more
73 /// than one use, this map indicates what promoted value to use. This allows
74 /// us to avoid promoting the same thing more than once.
75 std::map<SDOperand, SDOperand> PromotedNodes;
76
Chris Lattnerc7029802006-03-18 01:44:44 +000077 /// ExpandedNodes - For nodes that need to be expanded this map indicates
78 /// which which operands are the expanded version of the input. This allows
79 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
81
Chris Lattnerc7029802006-03-18 01:44:44 +000082 /// SplitNodes - For vector nodes that need to be split, this map indicates
83 /// which which operands are the split version of the input. This allows us
84 /// to avoid splitting the same node more than once.
85 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
86
87 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
88 /// concrete packed types, this contains the mapping of ones we have already
89 /// processed to the result.
90 std::map<SDOperand, SDOperand> PackedNodes;
91
Chris Lattner8afc48e2005-01-07 22:28:47 +000092 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +000093 LegalizedNodes.insert(std::make_pair(From, To));
94 // If someone requests legalization of the new node, return itself.
95 if (From != To)
96 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +000097 }
Chris Lattner03c85462005-01-15 05:21:40 +000098 void AddPromotedOperand(SDOperand From, SDOperand To) {
99 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
100 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000101 // If someone requests legalization of the new node, return itself.
102 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000103 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000104
Chris Lattner3e928bb2005-01-07 07:47:09 +0000105public:
106
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000107 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000108
Chris Lattner3e928bb2005-01-07 07:47:09 +0000109 /// getTypeAction - Return how we should legalize values of this type, either
110 /// it is already legal or we need to expand it into multiple registers of
111 /// smaller integer type, or we need to promote it to a larger type.
112 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000113 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000114 }
115
116 /// isTypeLegal - Return true if this type is legal on this target.
117 ///
118 bool isTypeLegal(MVT::ValueType VT) const {
119 return getTypeAction(VT) == Legal;
120 }
121
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122 void LegalizeDAG();
123
Chris Lattner456a93a2006-01-28 07:39:30 +0000124private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000125 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
126 /// appropriate for its type.
127 void HandleOp(SDOperand Op);
128
129 /// LegalizeOp - We know that the specified value has a legal type.
130 /// Recursively ensure that the operands have legal types, then return the
131 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000133
134 /// PromoteOp - Given an operation that produces a value in an invalid type,
135 /// promote it to compute the value into a larger type. The produced value
136 /// will have the correct bits for the low portion of the register, but no
137 /// guarantee is made about the top bits: it may be zero, sign-extended, or
138 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000139 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140
Chris Lattnerc7029802006-03-18 01:44:44 +0000141 /// ExpandOp - Expand the specified SDOperand into its two component pieces
142 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
143 /// the LegalizeNodes map is filled in for any results that are not expanded,
144 /// the ExpandedNodes map is filled in for any results that are expanded, and
145 /// the Lo/Hi values are returned. This applies to integer types and Vector
146 /// types.
147 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
148
149 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
150 /// two smaller values of MVT::Vector type.
151 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
152
153 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
154 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
155 /// this is called, we know that PackedVT is the right type for the result and
156 /// we know that this type is legal for the target.
157 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
158
Chris Lattner6831a812006-02-13 09:18:02 +0000159 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
160
Nate Begeman750ac1b2006-02-01 07:19:44 +0000161 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
162
Chris Lattner77e77a62005-01-21 06:05:23 +0000163 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
164 SDOperand &Hi);
165 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
166 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000167
Chris Lattner35481892005-12-23 00:16:34 +0000168 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskey6269ed12005-08-17 00:39:29 +0000169 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
170 SDOperand LegalOp,
171 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000172 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
173 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000174 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
175 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000176
Chris Lattner456a93a2006-01-28 07:39:30 +0000177 SDOperand ExpandBSWAP(SDOperand Op);
178 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000179 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
180 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000181 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
182 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000183
Chris Lattner3e928bb2005-01-07 07:47:09 +0000184 SDOperand getIntPtrConstant(uint64_t Val) {
185 return DAG.getConstant(Val, TLI.getPointerTy());
186 }
187};
188}
189
Chris Lattnerc7029802006-03-18 01:44:44 +0000190/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
191/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000192static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000193 switch (VecOp) {
194 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000195 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
196 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
197 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
198 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
199 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
200 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
201 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
202 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000203 }
204}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000205
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000206SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
207 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
208 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000209 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000210 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000211}
212
Chris Lattner32fca002005-10-06 01:20:27 +0000213/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
214/// not been visited yet and if all of its operands have already been visited.
215static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
216 std::map<SDNode*, unsigned> &Visited) {
217 if (++Visited[N] != N->getNumOperands())
218 return; // Haven't visited all operands yet
219
220 Order.push_back(N);
221
222 if (N->hasOneUse()) { // Tail recurse in common case.
223 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
224 return;
225 }
226
227 // Now that we have N in, add anything that uses it if all of their operands
228 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000229 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
230 ComputeTopDownOrdering(*UI, Order, Visited);
231}
232
Chris Lattner1618beb2005-07-29 00:11:56 +0000233
Chris Lattner3e928bb2005-01-07 07:47:09 +0000234void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000235 LastCALLSEQ_END = DAG.getEntryNode();
236 IsLegalizingCall = false;
237
Chris Lattnerab510a72005-10-02 17:49:46 +0000238 // The legalize process is inherently a bottom-up recursive process (users
239 // legalize their uses before themselves). Given infinite stack space, we
240 // could just start legalizing on the root and traverse the whole graph. In
241 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000242 // blocks. To avoid this problem, compute an ordering of the nodes where each
243 // node is only legalized after all of its operands are legalized.
244 std::map<SDNode*, unsigned> Visited;
245 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000246
Chris Lattner32fca002005-10-06 01:20:27 +0000247 // Compute ordering from all of the leaves in the graphs, those (like the
248 // entry node) that have no operands.
249 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
250 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000251 if (I->getNumOperands() == 0) {
252 Visited[I] = 0 - 1U;
253 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000254 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000255 }
256
Chris Lattnerde202b32005-11-09 23:47:37 +0000257 assert(Order.size() == Visited.size() &&
258 Order.size() ==
259 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000260 "Error: DAG is cyclic!");
261 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000262
Chris Lattnerc7029802006-03-18 01:44:44 +0000263 for (unsigned i = 0, e = Order.size(); i != e; ++i)
264 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000265
266 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000267 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000268 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
269 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270
271 ExpandedNodes.clear();
272 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000273 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000274 SplitNodes.clear();
275 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000276
277 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000278 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279}
280
Chris Lattner6831a812006-02-13 09:18:02 +0000281
282/// FindCallEndFromCallStart - Given a chained node that is part of a call
283/// sequence, find the CALLSEQ_END node that terminates the call sequence.
284static SDNode *FindCallEndFromCallStart(SDNode *Node) {
285 if (Node->getOpcode() == ISD::CALLSEQ_END)
286 return Node;
287 if (Node->use_empty())
288 return 0; // No CallSeqEnd
289
290 // The chain is usually at the end.
291 SDOperand TheChain(Node, Node->getNumValues()-1);
292 if (TheChain.getValueType() != MVT::Other) {
293 // Sometimes it's at the beginning.
294 TheChain = SDOperand(Node, 0);
295 if (TheChain.getValueType() != MVT::Other) {
296 // Otherwise, hunt for it.
297 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
298 if (Node->getValueType(i) == MVT::Other) {
299 TheChain = SDOperand(Node, i);
300 break;
301 }
302
303 // Otherwise, we walked into a node without a chain.
304 if (TheChain.getValueType() != MVT::Other)
305 return 0;
306 }
307 }
308
309 for (SDNode::use_iterator UI = Node->use_begin(),
310 E = Node->use_end(); UI != E; ++UI) {
311
312 // Make sure to only follow users of our token chain.
313 SDNode *User = *UI;
314 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
315 if (User->getOperand(i) == TheChain)
316 if (SDNode *Result = FindCallEndFromCallStart(User))
317 return Result;
318 }
319 return 0;
320}
321
322/// FindCallStartFromCallEnd - Given a chained node that is part of a call
323/// sequence, find the CALLSEQ_START node that initiates the call sequence.
324static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
325 assert(Node && "Didn't find callseq_start for a call??");
326 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
327
328 assert(Node->getOperand(0).getValueType() == MVT::Other &&
329 "Node doesn't have a token chain argument!");
330 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
331}
332
333/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
334/// see if any uses can reach Dest. If no dest operands can get to dest,
335/// legalize them, legalize ourself, and return false, otherwise, return true.
336bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
337 SDNode *Dest) {
338 if (N == Dest) return true; // N certainly leads to Dest :)
339
340 // If the first result of this node has been already legalized, then it cannot
341 // reach N.
342 switch (getTypeAction(N->getValueType(0))) {
343 case Legal:
344 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
345 break;
346 case Promote:
347 if (PromotedNodes.count(SDOperand(N, 0))) return false;
348 break;
349 case Expand:
350 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
351 break;
352 }
353
354 // Okay, this node has not already been legalized. Check and legalize all
355 // operands. If none lead to Dest, then we can legalize this node.
356 bool OperandsLeadToDest = false;
357 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
358 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
359 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
360
361 if (OperandsLeadToDest) return true;
362
363 // Okay, this node looks safe, legalize it and return false.
364 switch (getTypeAction(N->getValueType(0))) {
365 case Legal:
366 LegalizeOp(SDOperand(N, 0));
367 break;
368 case Promote:
369 PromoteOp(SDOperand(N, 0));
370 break;
371 case Expand: {
372 SDOperand X, Y;
373 ExpandOp(SDOperand(N, 0), X, Y);
374 break;
375 }
376 }
377 return false;
378}
379
Chris Lattnerc7029802006-03-18 01:44:44 +0000380/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
381/// appropriate for its type.
382void SelectionDAGLegalize::HandleOp(SDOperand Op) {
383 switch (getTypeAction(Op.getValueType())) {
384 default: assert(0 && "Bad type action!");
385 case Legal: LegalizeOp(Op); break;
386 case Promote: PromoteOp(Op); break;
387 case Expand:
388 if (Op.getValueType() != MVT::Vector) {
389 SDOperand X, Y;
390 ExpandOp(Op, X, Y);
391 } else {
392 SDNode *N = Op.Val;
393 unsigned NumOps = N->getNumOperands();
394 unsigned NumElements =
395 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
396 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
397 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
398 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
399 // In the common case, this is a legal vector type, convert it to the
400 // packed operation and type now.
401 PackVectorOp(Op, PackedVT);
402 } else if (NumElements == 1) {
403 // Otherwise, if this is a single element vector, convert it to a
404 // scalar operation.
405 PackVectorOp(Op, EVT);
406 } else {
407 // Otherwise, this is a multiple element vector that isn't supported.
408 // Split it in half and legalize both parts.
409 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000410 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000411 }
412 }
413 break;
414 }
415}
Chris Lattner6831a812006-02-13 09:18:02 +0000416
417
Chris Lattnerc7029802006-03-18 01:44:44 +0000418/// LegalizeOp - We know that the specified value has a legal type.
419/// Recursively ensure that the operands have legal types, then return the
420/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000421SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000422 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000423 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000424 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000425
Chris Lattner3e928bb2005-01-07 07:47:09 +0000426 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000427 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000428 if (Node->getNumValues() > 1) {
429 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000430 if (getTypeAction(Node->getValueType(i)) != Legal) {
431 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000432 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000433 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000434 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000435 }
436 }
437
Chris Lattner45982da2005-05-12 16:53:42 +0000438 // Note that LegalizeOp may be reentered even from single-use nodes, which
439 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000440 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
441 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000442
Nate Begeman9373a812005-08-10 20:51:12 +0000443 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000444 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000445 bool isCustom = false;
446
Chris Lattner3e928bb2005-01-07 07:47:09 +0000447 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000448 case ISD::FrameIndex:
449 case ISD::EntryToken:
450 case ISD::Register:
451 case ISD::BasicBlock:
452 case ISD::TargetFrameIndex:
453 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000454 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000455 case ISD::TargetConstantPool:
456 case ISD::TargetGlobalAddress:
457 case ISD::TargetExternalSymbol:
458 case ISD::VALUETYPE:
459 case ISD::SRCVALUE:
460 case ISD::STRING:
461 case ISD::CONDCODE:
462 // Primitives must all be legal.
463 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
464 "This must be legal!");
465 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000467 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
468 // If this is a target node, legalize it by legalizing the operands then
469 // passing it through.
470 std::vector<SDOperand> Ops;
471 bool Changed = false;
472 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
473 Ops.push_back(LegalizeOp(Node->getOperand(i)));
474 Changed = Changed || Node->getOperand(i) != Ops.back();
475 }
476 if (Changed)
477 if (Node->getNumValues() == 1)
478 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
479 else {
480 std::vector<MVT::ValueType> VTs(Node->value_begin(),
481 Node->value_end());
482 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
483 }
484
485 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
486 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
487 return Result.getValue(Op.ResNo);
488 }
489 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
491 assert(0 && "Do not know how to legalize this operator!");
492 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000494 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000495 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000496 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
497 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000498 case TargetLowering::Custom:
499 Tmp1 = TLI.LowerOperation(Op, DAG);
500 if (Tmp1.Val) Result = Tmp1;
501 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000502 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000503 break;
504 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000505 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000506 case ISD::AssertSext:
507 case ISD::AssertZext:
508 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000510 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000511 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000512 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000513 Result = Node->getOperand(Op.ResNo);
514 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000515 case ISD::CopyFromReg:
516 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000517 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000518 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000519 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000520 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000521 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000522 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000523 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
525 } else {
526 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
527 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000528 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
529 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000530 // Since CopyFromReg produces two values, make sure to remember that we
531 // legalized both of them.
532 AddLegalizedOperand(Op.getValue(0), Result);
533 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
534 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000535 case ISD::UNDEF: {
536 MVT::ValueType VT = Op.getValueType();
537 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000538 default: assert(0 && "This action is not supported yet!");
539 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000540 if (MVT::isInteger(VT))
541 Result = DAG.getConstant(0, VT);
542 else if (MVT::isFloatingPoint(VT))
543 Result = DAG.getConstantFP(0, VT);
544 else
545 assert(0 && "Unknown value type!");
546 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000547 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000548 break;
549 }
550 break;
551 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000552
553 case ISD::LOCATION:
554 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
555 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
556
557 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
558 case TargetLowering::Promote:
559 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000560 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000561 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000562 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
563 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
564
565 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
566 const std::string &FName =
567 cast<StringSDNode>(Node->getOperand(3))->getValue();
568 const std::string &DirName =
569 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000570 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000571
Jim Laskeye81aecb2005-12-21 20:51:37 +0000572 std::vector<SDOperand> Ops;
573 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000574 SDOperand LineOp = Node->getOperand(1);
575 SDOperand ColOp = Node->getOperand(2);
576
577 if (useDEBUG_LOC) {
578 Ops.push_back(LineOp); // line #
579 Ops.push_back(ColOp); // col #
580 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
581 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
582 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000583 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
584 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000585 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
586 Ops.push_back(DAG.getConstant(ID, MVT::i32));
587 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
588 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000589 } else {
590 Result = Tmp1; // chain
591 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000592 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000593 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000594 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000595 if (Tmp1 != Node->getOperand(0) ||
596 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000597 std::vector<SDOperand> Ops;
598 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000599 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
600 Ops.push_back(Node->getOperand(1)); // line # must be legal.
601 Ops.push_back(Node->getOperand(2)); // col # must be legal.
602 } else {
603 // Otherwise promote them.
604 Ops.push_back(PromoteOp(Node->getOperand(1)));
605 Ops.push_back(PromoteOp(Node->getOperand(2)));
606 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000607 Ops.push_back(Node->getOperand(3)); // filename must be legal.
608 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000609 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000610 }
611 break;
612 }
613 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000614
615 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000616 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000617 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000618 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000619 case TargetLowering::Legal:
620 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
621 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
622 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
623 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000625 break;
626 }
627 break;
628
629 case ISD::DEBUG_LABEL:
630 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
631 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000632 default: assert(0 && "This action is not supported yet!");
633 case TargetLowering::Legal:
634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
635 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000637 break;
638 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000639 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000640
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 case ISD::Constant:
642 // We know we don't need to expand constants here, constants only have one
643 // value and we check that it is fine above.
644
645 // FIXME: Maybe we should handle things like targets that don't support full
646 // 32-bit immediates?
647 break;
648 case ISD::ConstantFP: {
649 // Spill FP immediates to the constant pool if the target cannot directly
650 // codegen them. Targets often have some immediate values that can be
651 // efficiently generated into an FP register without a load. We explicitly
652 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000653 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
654
655 // Check to see if this FP immediate is already legal.
656 bool isLegal = false;
657 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
658 E = TLI.legal_fpimm_end(); I != E; ++I)
659 if (CFP->isExactlyValue(*I)) {
660 isLegal = true;
661 break;
662 }
663
Chris Lattner3181a772006-01-29 06:26:56 +0000664 // If this is a legal constant, turn it into a TargetConstantFP node.
665 if (isLegal) {
666 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
667 break;
668 }
669
670 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
671 default: assert(0 && "This action is not supported yet!");
672 case TargetLowering::Custom:
673 Tmp3 = TLI.LowerOperation(Result, DAG);
674 if (Tmp3.Val) {
675 Result = Tmp3;
676 break;
677 }
678 // FALLTHROUGH
679 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000680 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000681 bool Extend = false;
682
Chris Lattner456a93a2006-01-28 07:39:30 +0000683 // If a FP immediate is precise when represented as a float and if the
684 // target can do an extending load from float to double, we put it into
685 // the constant pool as a float, even if it's is statically typed as a
686 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000687 MVT::ValueType VT = CFP->getValueType(0);
688 bool isDouble = VT == MVT::f64;
689 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
690 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000691 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
692 // Only do this if the target has a native EXTLOAD instruction from
693 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000694 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000695 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
696 VT = MVT::f32;
697 Extend = true;
698 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000699
Chris Lattner456a93a2006-01-28 07:39:30 +0000700 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000701 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000702 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
703 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000704 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000705 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
706 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000707 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000708 }
709 break;
710 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000711 case ISD::TokenFactor:
712 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000713 Tmp1 = LegalizeOp(Node->getOperand(0));
714 Tmp2 = LegalizeOp(Node->getOperand(1));
715 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
716 } else if (Node->getNumOperands() == 3) {
717 Tmp1 = LegalizeOp(Node->getOperand(0));
718 Tmp2 = LegalizeOp(Node->getOperand(1));
719 Tmp3 = LegalizeOp(Node->getOperand(2));
720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000721 } else {
722 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000723 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000724 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
725 Ops.push_back(LegalizeOp(Node->getOperand(i)));
726 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000727 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000728 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000729
Chris Lattnerb2827b02006-03-19 00:52:58 +0000730 case ISD::BUILD_VECTOR:
731 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000732 default: assert(0 && "This action is not supported yet!");
733 case TargetLowering::Custom:
734 Tmp3 = TLI.LowerOperation(Result, DAG);
735 if (Tmp3.Val) {
736 Result = Tmp3;
737 break;
738 }
739 // FALLTHROUGH
740 case TargetLowering::Expand: {
741 // We assume that built vectors are not legal, and will be immediately
742 // spilled to memory. If the values are all constants, turn this into a
743 // load from the constant pool.
744 bool isConstant = true;
745 for (SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
746 I != E; ++I) {
747 if (!isa<ConstantFPSDNode>(I) && !isa<ConstantSDNode>(I) &&
748 I->getOpcode() != ISD::UNDEF) {
749 isConstant = false;
Chris Lattnerb2827b02006-03-19 00:52:58 +0000750 break;
751 }
752 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000753
754 // Create a ConstantPacked, and put it in the constant pool.
755 if (isConstant) {
756 MVT::ValueType VT = Node->getValueType(0);
757 const Type *OpNTy =
758 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
759 std::vector<Constant*> CV;
760 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
761 if (ConstantFPSDNode *V =
762 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
763 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
764 } else if (ConstantSDNode *V =
765 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
766 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
767 } else {
768 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
769 CV.push_back(UndefValue::get(OpNTy));
770 }
771 }
772 Constant *CP = ConstantPacked::get(CV);
773 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
774 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
775 DAG.getSrcValue(NULL));
776 break;
777 }
778
779 // Otherwise, this isn't a constant entry. Allocate a sufficiently
780 // aligned object on the stack, store each element into it, then load
781 // the result as a vector.
Chris Lattner5fcd0352006-03-19 04:18:56 +0000782 MVT::ValueType VT = Node->getValueType(0);
783 // Create the stack frame object.
784 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
785 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
786 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
787 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
788
789 // Emit a store of each element to the stack slot.
790 std::vector<SDOperand> Stores;
791 bool isLittleEndian = TLI.isLittleEndian();
792 unsigned TypeByteSize =
793 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
794 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
795 // Store (in the right endianness) the elements to memory.
796 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
797 unsigned Offset;
798 if (isLittleEndian)
799 Offset = TypeByteSize*i;
800 else
801 Offset = TypeByteSize*(e-i-1);
802
803 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
804 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
805
806 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
807 Node->getOperand(i), Idx,
808 DAG.getSrcValue(NULL)));
809 }
810 SDOperand StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
811
812 // Result is a load from the stack slot.
813 Result = DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
Chris Lattnerb2827b02006-03-19 00:52:58 +0000814 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000815 }
816 }
817 break;
818 case ISD::INSERT_VECTOR_ELT:
819 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
820 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
821 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
822 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
823
824 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
825 Node->getValueType(0))) {
826 default: assert(0 && "This action is not supported yet!");
827 case TargetLowering::Legal:
828 break;
829 case TargetLowering::Custom:
830 Tmp3 = TLI.LowerOperation(Result, DAG);
831 if (Tmp3.Val) {
832 Result = Tmp3;
833 break;
834 }
835 // FALLTHROUGH
836 case TargetLowering::Expand: {
837 // If the target doesn't support this, we have to spill the input vector
838 // to a temporary stack slot, update the element, then reload it. This is
839 // badness. We could also load the value into a vector register (either
840 // with a "move to register" or "extload into register" instruction, then
841 // permute it into place, if the idx is a constant and if the idx is
842 // supported by the target.
843 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
844 break;
845 }
846 }
847 break;
Chris Lattner6831a812006-02-13 09:18:02 +0000848 case ISD::CALLSEQ_START: {
849 SDNode *CallEnd = FindCallEndFromCallStart(Node);
850
851 // Recursively Legalize all of the inputs of the call end that do not lead
852 // to this call start. This ensures that any libcalls that need be inserted
853 // are inserted *before* the CALLSEQ_START.
854 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
855 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
856
857 // Now that we legalized all of the inputs (which may have inserted
858 // libcalls) create the new CALLSEQ_START node.
859 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
860
861 // Merge in the last call, to ensure that this call start after the last
862 // call ended.
863 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
864 Tmp1 = LegalizeOp(Tmp1);
865
866 // Do not try to legalize the target-specific arguments (#1+).
867 if (Tmp1 != Node->getOperand(0)) {
868 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
869 Ops[0] = Tmp1;
870 Result = DAG.UpdateNodeOperands(Result, Ops);
871 }
872
873 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000874 AddLegalizedOperand(Op.getValue(0), Result);
875 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
876 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
877
Chris Lattner6831a812006-02-13 09:18:02 +0000878 // Now that the callseq_start and all of the non-call nodes above this call
879 // sequence have been legalized, legalize the call itself. During this
880 // process, no libcalls can/will be inserted, guaranteeing that no calls
881 // can overlap.
882 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
883 SDOperand InCallSEQ = LastCALLSEQ_END;
884 // Note that we are selecting this call!
885 LastCALLSEQ_END = SDOperand(CallEnd, 0);
886 IsLegalizingCall = true;
887
888 // Legalize the call, starting from the CALLSEQ_END.
889 LegalizeOp(LastCALLSEQ_END);
890 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
891 return Result;
892 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000893 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000894 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
895 // will cause this node to be legalized as well as handling libcalls right.
896 if (LastCALLSEQ_END.Val != Node) {
897 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
898 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
899 assert(I != LegalizedNodes.end() &&
900 "Legalizing the call start should have legalized this node!");
901 return I->second;
902 }
903
904 // Otherwise, the call start has been legalized and everything is going
905 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000906 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000907 // Do not try to legalize the target-specific arguments (#1+), except for
908 // an optional flag input.
909 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
910 if (Tmp1 != Node->getOperand(0)) {
911 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
912 Ops[0] = Tmp1;
913 Result = DAG.UpdateNodeOperands(Result, Ops);
914 }
915 } else {
916 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
917 if (Tmp1 != Node->getOperand(0) ||
918 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
919 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
920 Ops[0] = Tmp1;
921 Ops.back() = Tmp2;
922 Result = DAG.UpdateNodeOperands(Result, Ops);
923 }
Chris Lattner6a542892006-01-24 05:48:21 +0000924 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000925 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000926 // This finishes up call legalization.
927 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000928
929 // If the CALLSEQ_END node has a flag, remember that we legalized it.
930 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
931 if (Node->getNumValues() == 2)
932 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
933 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000934 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
936 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
937 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000938 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000939
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000940 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000941 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000942 switch (TLI.getOperationAction(Node->getOpcode(),
943 Node->getValueType(0))) {
944 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000945 case TargetLowering::Expand: {
946 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
947 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
948 " not tell us which reg is the stack pointer!");
949 SDOperand Chain = Tmp1.getOperand(0);
950 SDOperand Size = Tmp2.getOperand(1);
951 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
952 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
953 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000954 Tmp1 = LegalizeOp(Tmp1);
955 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000956 break;
957 }
958 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000959 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
960 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000961 Tmp1 = LegalizeOp(Tmp3);
962 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000963 }
Chris Lattner903d2782006-01-15 08:54:32 +0000964 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000965 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000966 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000967 }
Chris Lattner903d2782006-01-15 08:54:32 +0000968 // Since this op produce two values, make sure to remember that we
969 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000970 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
971 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000972 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000973 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000974 case ISD::INLINEASM:
975 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
976 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000977 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000978 Tmp2 = Tmp3 = SDOperand(0, 0);
979 else
980 Tmp3 = LegalizeOp(Tmp2);
981
982 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
983 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
984 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000985 if (Tmp3.Val) Ops.back() = Tmp3;
986 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000987 }
988
989 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000990 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000991 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
992 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000993 case ISD::BR:
994 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000995 // Ensure that libcalls are emitted before a branch.
996 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
997 Tmp1 = LegalizeOp(Tmp1);
998 LastCALLSEQ_END = DAG.getEntryNode();
999
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001000 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001001 break;
1002
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001003 case ISD::BRCOND:
1004 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001005 // Ensure that libcalls are emitted before a return.
1006 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1007 Tmp1 = LegalizeOp(Tmp1);
1008 LastCALLSEQ_END = DAG.getEntryNode();
1009
Chris Lattner47e92232005-01-18 19:27:06 +00001010 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1011 case Expand: assert(0 && "It's impossible to expand bools");
1012 case Legal:
1013 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1014 break;
1015 case Promote:
1016 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1017 break;
1018 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001019
1020 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001021 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001022
1023 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1024 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001025 case TargetLowering::Legal: break;
1026 case TargetLowering::Custom:
1027 Tmp1 = TLI.LowerOperation(Result, DAG);
1028 if (Tmp1.Val) Result = Tmp1;
1029 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001030 case TargetLowering::Expand:
1031 // Expand brcond's setcc into its constituent parts and create a BR_CC
1032 // Node.
1033 if (Tmp2.getOpcode() == ISD::SETCC) {
1034 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1035 Tmp2.getOperand(0), Tmp2.getOperand(1),
1036 Node->getOperand(2));
1037 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001038 // Make sure the condition is either zero or one. It may have been
1039 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +00001040 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1041 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1042 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +00001043
Nate Begeman7cbd5252005-08-16 19:49:35 +00001044 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1045 DAG.getCondCode(ISD::SETNE), Tmp2,
1046 DAG.getConstant(0, Tmp2.getValueType()),
1047 Node->getOperand(2));
1048 }
1049 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001050 }
1051 break;
1052 case ISD::BR_CC:
1053 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001054 // Ensure that libcalls are emitted before a branch.
1055 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1056 Tmp1 = LegalizeOp(Tmp1);
1057 LastCALLSEQ_END = DAG.getEntryNode();
1058
Nate Begeman750ac1b2006-02-01 07:19:44 +00001059 Tmp2 = Node->getOperand(2); // LHS
1060 Tmp3 = Node->getOperand(3); // RHS
1061 Tmp4 = Node->getOperand(1); // CC
1062
1063 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1064
1065 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1066 // the LHS is a legal SETCC itself. In this case, we need to compare
1067 // the result against zero to select between true and false values.
1068 if (Tmp3.Val == 0) {
1069 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1070 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001071 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001072
1073 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1074 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001075
Chris Lattner181b7a32005-12-17 23:46:46 +00001076 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1077 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001078 case TargetLowering::Legal: break;
1079 case TargetLowering::Custom:
1080 Tmp4 = TLI.LowerOperation(Result, DAG);
1081 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001082 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001083 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001084 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001085 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001086 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1087 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001088
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001089 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001090 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1091 Tmp2 = Result.getValue(0);
1092 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001093
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001094 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1095 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001096 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001097 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001098 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001099 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001100 Tmp2 = LegalizeOp(Tmp1);
1101 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001102 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001103 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001104 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001105 // Since loads produce two values, make sure to remember that we
1106 // legalized both of them.
1107 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1108 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1109 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001110 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001111 case ISD::EXTLOAD:
1112 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001113 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001114 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1115 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001116
Chris Lattner5f056bf2005-07-10 01:55:33 +00001117 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001118 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001119 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001120 case TargetLowering::Promote:
1121 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001122 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1123 DAG.getValueType(MVT::i8));
1124 Tmp1 = Result.getValue(0);
1125 Tmp2 = Result.getValue(1);
1126 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001127 case TargetLowering::Custom:
1128 isCustom = true;
1129 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001130 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1132 Node->getOperand(3));
1133 Tmp1 = Result.getValue(0);
1134 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001135
1136 if (isCustom) {
1137 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1138 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001139 Tmp1 = LegalizeOp(Tmp3);
1140 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001141 }
1142 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001143 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001144 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001145 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001146 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1147 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001148 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001149 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1150 Tmp2 = LegalizeOp(Load.getValue(1));
1151 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001152 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001153 assert(Node->getOpcode() != ISD::EXTLOAD &&
1154 "EXTLOAD should always be supported!");
1155 // Turn the unsupported load into an EXTLOAD followed by an explicit
1156 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001157 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1158 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001159 SDOperand ValRes;
1160 if (Node->getOpcode() == ISD::SEXTLOAD)
1161 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001162 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001163 else
1164 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001165 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1166 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1167 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001168 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001169 // Since loads produce two values, make sure to remember that we legalized
1170 // both of them.
1171 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1172 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1173 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001174 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001175 case ISD::EXTRACT_ELEMENT: {
1176 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1177 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001178 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001179 case Legal:
1180 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1181 // 1 -> Hi
1182 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1183 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1184 TLI.getShiftAmountTy()));
1185 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1186 } else {
1187 // 0 -> Lo
1188 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1189 Node->getOperand(0));
1190 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001191 break;
1192 case Expand:
1193 // Get both the low and high parts.
1194 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1195 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1196 Result = Tmp2; // 1 -> Hi
1197 else
1198 Result = Tmp1; // 0 -> Lo
1199 break;
1200 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001201 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001202 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001203
1204 case ISD::CopyToReg:
1205 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001206
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001207 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001208 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001209 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001210 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001211 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001213 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001214 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001215 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001216 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1218 Tmp3);
1219 } else {
1220 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001221 }
1222
1223 // Since this produces two values, make sure to remember that we legalized
1224 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001225 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001226 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001227 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001228 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001229 break;
1230
1231 case ISD::RET:
1232 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001233
1234 // Ensure that libcalls are emitted before a return.
1235 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1236 Tmp1 = LegalizeOp(Tmp1);
1237 LastCALLSEQ_END = DAG.getEntryNode();
1238
Chris Lattner3e928bb2005-01-07 07:47:09 +00001239 switch (Node->getNumOperands()) {
1240 case 2: // ret val
1241 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1242 case Legal:
1243 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001244 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001245 break;
1246 case Expand: {
1247 SDOperand Lo, Hi;
1248 ExpandOp(Node->getOperand(1), Lo, Hi);
1249 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001250 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001251 }
1252 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001253 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001254 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1255 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001256 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001257 }
1258 break;
1259 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001260 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001261 break;
1262 default: { // ret <values>
1263 std::vector<SDOperand> NewValues;
1264 NewValues.push_back(Tmp1);
1265 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1266 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1267 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001268 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001269 break;
1270 case Expand: {
1271 SDOperand Lo, Hi;
1272 ExpandOp(Node->getOperand(i), Lo, Hi);
1273 NewValues.push_back(Lo);
1274 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001275 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001276 }
1277 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001278 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001279 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001280
1281 if (NewValues.size() == Node->getNumOperands())
1282 Result = DAG.UpdateNodeOperands(Result, NewValues);
1283 else
1284 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001285 break;
1286 }
1287 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001288
Chris Lattner6862dbc2006-01-29 21:02:23 +00001289 if (Result.getOpcode() == ISD::RET) {
1290 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1291 default: assert(0 && "This action is not supported yet!");
1292 case TargetLowering::Legal: break;
1293 case TargetLowering::Custom:
1294 Tmp1 = TLI.LowerOperation(Result, DAG);
1295 if (Tmp1.Val) Result = Tmp1;
1296 break;
1297 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001298 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001299 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001300 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001301 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1302 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1303
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001304 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001305 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001306 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001307 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001308 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001309 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001310 } else {
1311 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001312 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001313 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001314 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1315 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001316 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001317 }
1318
Chris Lattner3e928bb2005-01-07 07:47:09 +00001319 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1320 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001321 Tmp3 = LegalizeOp(Node->getOperand(1));
1322 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1323 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001324
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001325 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001326 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1327 default: assert(0 && "This action is not supported yet!");
1328 case TargetLowering::Legal: break;
1329 case TargetLowering::Custom:
1330 Tmp1 = TLI.LowerOperation(Result, DAG);
1331 if (Tmp1.Val) Result = Tmp1;
1332 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001333 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001334 break;
1335 }
1336 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001337 // Truncate the value and store the result.
1338 Tmp3 = PromoteOp(Node->getOperand(1));
1339 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001340 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001341 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001342 break;
1343
Chris Lattner3e928bb2005-01-07 07:47:09 +00001344 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001345 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001346 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001347
1348 // If this is a vector type, then we have to calculate the increment as
1349 // the product of the element size in bytes, and the number of elements
1350 // in the high half of the vector.
1351 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1352 SDNode *InVal = Node->getOperand(1).Val;
1353 unsigned NumElems =
1354 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1355 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1356
1357 // Figure out if there is a Packed type corresponding to this Vector
1358 // type. If so, convert to the packed type.
1359 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1360 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1361 // Turn this into a normal store of the packed type.
1362 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1364 Node->getOperand(3));
1365 break;
1366 } else if (NumElems == 1) {
1367 // Turn this into a normal store of the scalar type.
1368 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1369 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1370 Node->getOperand(3));
1371 break;
1372 } else {
1373 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1374 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1375 }
1376 } else {
1377 ExpandOp(Node->getOperand(1), Lo, Hi);
1378 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1379 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001380
1381 if (!TLI.isLittleEndian())
1382 std::swap(Lo, Hi);
1383
Chris Lattneredb1add2005-05-11 04:51:16 +00001384 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1385 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001386 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1387 getIntPtrConstant(IncrementSize));
1388 assert(isTypeLegal(Tmp2.getValueType()) &&
1389 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001390 // FIXME: This sets the srcvalue of both halves to be the same, which is
1391 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001392 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1393 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001394 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1395 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001396 }
1397 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001398 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001399 case ISD::PCMARKER:
1400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001401 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001402 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001403 case ISD::STACKSAVE:
1404 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001405 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1406 Tmp1 = Result.getValue(0);
1407 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001408
Chris Lattner140d53c2006-01-13 02:50:02 +00001409 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1410 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001411 case TargetLowering::Legal: break;
1412 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001413 Tmp3 = TLI.LowerOperation(Result, DAG);
1414 if (Tmp3.Val) {
1415 Tmp1 = LegalizeOp(Tmp3);
1416 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001417 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001418 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001419 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001420 // Expand to CopyFromReg if the target set
1421 // StackPointerRegisterToSaveRestore.
1422 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001423 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001424 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001425 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001426 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001427 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1428 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001429 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001430 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001431 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001432
1433 // Since stacksave produce two values, make sure to remember that we
1434 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001435 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1436 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1437 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001438
Chris Lattner140d53c2006-01-13 02:50:02 +00001439 case ISD::STACKRESTORE:
1440 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1441 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001443
1444 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1445 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001446 case TargetLowering::Legal: break;
1447 case TargetLowering::Custom:
1448 Tmp1 = TLI.LowerOperation(Result, DAG);
1449 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001450 break;
1451 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001452 // Expand to CopyToReg if the target set
1453 // StackPointerRegisterToSaveRestore.
1454 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1455 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1456 } else {
1457 Result = Tmp1;
1458 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001459 break;
1460 }
1461 break;
1462
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001463 case ISD::READCYCLECOUNTER:
1464 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001465 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001466
1467 // Since rdcc produce two values, make sure to remember that we legalized
1468 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001469 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001470 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001471 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001472
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001473 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001474 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1475 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1476
Chris Lattner456a93a2006-01-28 07:39:30 +00001477 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1478 "Cannot handle illegal TRUNCSTORE yet!");
1479 Tmp2 = LegalizeOp(Node->getOperand(1));
1480
1481 // The only promote case we handle is TRUNCSTORE:i1 X into
1482 // -> TRUNCSTORE:i8 (and X, 1)
1483 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1484 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1485 TargetLowering::Promote) {
1486 // Promote the bool to a mask then store.
1487 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1488 DAG.getConstant(1, Tmp2.getValueType()));
1489 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1490 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001491
Chris Lattner456a93a2006-01-28 07:39:30 +00001492 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1493 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001494 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1495 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001496 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001497
Chris Lattner456a93a2006-01-28 07:39:30 +00001498 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1499 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1500 default: assert(0 && "This action is not supported yet!");
1501 case TargetLowering::Legal: break;
1502 case TargetLowering::Custom:
1503 Tmp1 = TLI.LowerOperation(Result, DAG);
1504 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001505 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001506 }
1507 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001508 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001509 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001510 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1511 case Expand: assert(0 && "It's impossible to expand bools");
1512 case Legal:
1513 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1514 break;
1515 case Promote:
1516 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1517 break;
1518 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001519 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001520 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001521
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001522 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001523
Nate Begemanb942a3d2005-08-23 04:29:48 +00001524 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001525 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001526 case TargetLowering::Legal: break;
1527 case TargetLowering::Custom: {
1528 Tmp1 = TLI.LowerOperation(Result, DAG);
1529 if (Tmp1.Val) Result = Tmp1;
1530 break;
1531 }
Nate Begeman9373a812005-08-10 20:51:12 +00001532 case TargetLowering::Expand:
1533 if (Tmp1.getOpcode() == ISD::SETCC) {
1534 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1535 Tmp2, Tmp3,
1536 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1537 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001538 // Make sure the condition is either zero or one. It may have been
1539 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001540 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1541 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1542 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001543 Result = DAG.getSelectCC(Tmp1,
1544 DAG.getConstant(0, Tmp1.getValueType()),
1545 Tmp2, Tmp3, ISD::SETNE);
1546 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001547 break;
1548 case TargetLowering::Promote: {
1549 MVT::ValueType NVT =
1550 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1551 unsigned ExtOp, TruncOp;
1552 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001553 ExtOp = ISD::ANY_EXTEND;
1554 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001555 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001556 ExtOp = ISD::FP_EXTEND;
1557 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001558 }
1559 // Promote each of the values to the new type.
1560 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1561 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1562 // Perform the larger operation, then round down.
1563 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1564 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1565 break;
1566 }
1567 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001568 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001569 case ISD::SELECT_CC: {
1570 Tmp1 = Node->getOperand(0); // LHS
1571 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001572 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1573 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001574 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001575
Nate Begeman750ac1b2006-02-01 07:19:44 +00001576 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1577
1578 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1579 // the LHS is a legal SETCC itself. In this case, we need to compare
1580 // the result against zero to select between true and false values.
1581 if (Tmp2.Val == 0) {
1582 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1583 CC = DAG.getCondCode(ISD::SETNE);
1584 }
1585 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1586
1587 // Everything is legal, see if we should expand this op or something.
1588 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1589 default: assert(0 && "This action is not supported yet!");
1590 case TargetLowering::Legal: break;
1591 case TargetLowering::Custom:
1592 Tmp1 = TLI.LowerOperation(Result, DAG);
1593 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001594 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001595 }
1596 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001597 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001598 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001599 Tmp1 = Node->getOperand(0);
1600 Tmp2 = Node->getOperand(1);
1601 Tmp3 = Node->getOperand(2);
1602 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1603
1604 // If we had to Expand the SetCC operands into a SELECT node, then it may
1605 // not always be possible to return a true LHS & RHS. In this case, just
1606 // return the value we legalized, returned in the LHS
1607 if (Tmp2.Val == 0) {
1608 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001609 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001610 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001611
Chris Lattner73e142f2006-01-30 22:43:50 +00001612 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001613 default: assert(0 && "Cannot handle this action for SETCC yet!");
1614 case TargetLowering::Custom:
1615 isCustom = true;
1616 // FALLTHROUGH.
1617 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001618 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001619 if (isCustom) {
1620 Tmp3 = TLI.LowerOperation(Result, DAG);
1621 if (Tmp3.Val) Result = Tmp3;
1622 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001623 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001624 case TargetLowering::Promote: {
1625 // First step, figure out the appropriate operation to use.
1626 // Allow SETCC to not be supported for all legal data types
1627 // Mostly this targets FP
1628 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1629 MVT::ValueType OldVT = NewInTy;
1630
1631 // Scan for the appropriate larger type to use.
1632 while (1) {
1633 NewInTy = (MVT::ValueType)(NewInTy+1);
1634
1635 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1636 "Fell off of the edge of the integer world");
1637 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1638 "Fell off of the edge of the floating point world");
1639
1640 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001641 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001642 break;
1643 }
1644 if (MVT::isInteger(NewInTy))
1645 assert(0 && "Cannot promote Legal Integer SETCC yet");
1646 else {
1647 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1648 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1649 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001650 Tmp1 = LegalizeOp(Tmp1);
1651 Tmp2 = LegalizeOp(Tmp2);
1652 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001653 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001654 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001655 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001656 case TargetLowering::Expand:
1657 // Expand a setcc node into a select_cc of the same condition, lhs, and
1658 // rhs that selects between const 1 (true) and const 0 (false).
1659 MVT::ValueType VT = Node->getValueType(0);
1660 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1661 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1662 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001663 break;
1664 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001665 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001666 case ISD::MEMSET:
1667 case ISD::MEMCPY:
1668 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001669 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001670 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1671
1672 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1673 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1674 case Expand: assert(0 && "Cannot expand a byte!");
1675 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001676 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001677 break;
1678 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001679 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001680 break;
1681 }
1682 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001683 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001684 }
Chris Lattner272455b2005-02-02 03:44:41 +00001685
1686 SDOperand Tmp4;
1687 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001688 case Expand: {
1689 // Length is too big, just take the lo-part of the length.
1690 SDOperand HiPart;
1691 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1692 break;
1693 }
Chris Lattnere5605212005-01-28 22:29:18 +00001694 case Legal:
1695 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001696 break;
1697 case Promote:
1698 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001699 break;
1700 }
1701
1702 SDOperand Tmp5;
1703 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1704 case Expand: assert(0 && "Cannot expand this yet!");
1705 case Legal:
1706 Tmp5 = LegalizeOp(Node->getOperand(4));
1707 break;
1708 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001709 Tmp5 = PromoteOp(Node->getOperand(4));
1710 break;
1711 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001712
1713 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1714 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001715 case TargetLowering::Custom:
1716 isCustom = true;
1717 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001718 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001720 if (isCustom) {
1721 Tmp1 = TLI.LowerOperation(Result, DAG);
1722 if (Tmp1.Val) Result = Tmp1;
1723 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001724 break;
1725 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001726 // Otherwise, the target does not support this operation. Lower the
1727 // operation to an explicit libcall as appropriate.
1728 MVT::ValueType IntPtr = TLI.getPointerTy();
1729 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1730 std::vector<std::pair<SDOperand, const Type*> > Args;
1731
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001732 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001733 if (Node->getOpcode() == ISD::MEMSET) {
1734 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001735 // Extend the (previously legalized) ubyte argument to be an int value
1736 // for the call.
1737 if (Tmp3.getValueType() > MVT::i32)
1738 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1739 else
1740 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001741 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1742 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1743
1744 FnName = "memset";
1745 } else if (Node->getOpcode() == ISD::MEMCPY ||
1746 Node->getOpcode() == ISD::MEMMOVE) {
1747 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1748 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1749 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1750 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1751 } else {
1752 assert(0 && "Unknown op!");
1753 }
Chris Lattner45982da2005-05-12 16:53:42 +00001754
Chris Lattnere1bd8222005-01-11 05:57:22 +00001755 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001756 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001757 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001758 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001759 break;
1760 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001761 }
1762 break;
1763 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001764
Chris Lattner5b359c62005-04-02 04:00:59 +00001765 case ISD::SHL_PARTS:
1766 case ISD::SRA_PARTS:
1767 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001768 std::vector<SDOperand> Ops;
1769 bool Changed = false;
1770 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1771 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1772 Changed |= Ops.back() != Node->getOperand(i);
1773 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001774 if (Changed)
1775 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001776
Evan Cheng05a2d562006-01-09 18:31:59 +00001777 switch (TLI.getOperationAction(Node->getOpcode(),
1778 Node->getValueType(0))) {
1779 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001780 case TargetLowering::Legal: break;
1781 case TargetLowering::Custom:
1782 Tmp1 = TLI.LowerOperation(Result, DAG);
1783 if (Tmp1.Val) {
1784 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001785 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001786 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001787 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1788 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001789 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001790 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001791 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001792 return RetVal;
1793 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001794 break;
1795 }
1796
Chris Lattner2c8086f2005-04-02 05:00:07 +00001797 // Since these produce multiple values, make sure to remember that we
1798 // legalized all of them.
1799 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1800 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1801 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001802 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001803
1804 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001805 case ISD::ADD:
1806 case ISD::SUB:
1807 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001808 case ISD::MULHS:
1809 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001810 case ISD::UDIV:
1811 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001812 case ISD::AND:
1813 case ISD::OR:
1814 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001815 case ISD::SHL:
1816 case ISD::SRL:
1817 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001818 case ISD::FADD:
1819 case ISD::FSUB:
1820 case ISD::FMUL:
1821 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001822 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001823 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1824 case Expand: assert(0 && "Not possible");
1825 case Legal:
1826 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1827 break;
1828 case Promote:
1829 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1830 break;
1831 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001832
1833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001834
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001835 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001836 default: assert(0 && "Operation not supported");
1837 case TargetLowering::Legal: break;
1838 case TargetLowering::Custom:
1839 Tmp1 = TLI.LowerOperation(Result, DAG);
1840 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001841 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001842 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001843 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001844
1845 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1846 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1847 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1848 case Expand: assert(0 && "Not possible");
1849 case Legal:
1850 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1851 break;
1852 case Promote:
1853 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1854 break;
1855 }
1856
1857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1858
1859 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1860 default: assert(0 && "Operation not supported");
1861 case TargetLowering::Custom:
1862 Tmp1 = TLI.LowerOperation(Result, DAG);
1863 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001864 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001865 case TargetLowering::Legal: break;
1866 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001867 // If this target supports fabs/fneg natively, do this efficiently.
1868 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1869 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1870 // Get the sign bit of the RHS.
1871 MVT::ValueType IVT =
1872 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1873 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1874 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1875 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1876 // Get the absolute value of the result.
1877 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1878 // Select between the nabs and abs value based on the sign bit of
1879 // the input.
1880 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1881 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1882 AbsVal),
1883 AbsVal);
1884 Result = LegalizeOp(Result);
1885 break;
1886 }
1887
1888 // Otherwise, do bitwise ops!
1889
1890 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001891 const char *FnName;
1892 if (Node->getValueType(0) == MVT::f32) {
1893 FnName = "copysignf";
1894 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1895 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1896 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1897 } else {
1898 FnName = "copysign";
1899 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1900 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1901 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1902 }
1903 SDOperand Dummy;
1904 Result = ExpandLibCall(FnName, Node, Dummy);
1905 break;
1906 }
1907 break;
1908
Nate Begeman551bf3f2006-02-17 05:43:56 +00001909 case ISD::ADDC:
1910 case ISD::SUBC:
1911 Tmp1 = LegalizeOp(Node->getOperand(0));
1912 Tmp2 = LegalizeOp(Node->getOperand(1));
1913 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1914 // Since this produces two values, make sure to remember that we legalized
1915 // both of them.
1916 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1917 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1918 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001919
Nate Begeman551bf3f2006-02-17 05:43:56 +00001920 case ISD::ADDE:
1921 case ISD::SUBE:
1922 Tmp1 = LegalizeOp(Node->getOperand(0));
1923 Tmp2 = LegalizeOp(Node->getOperand(1));
1924 Tmp3 = LegalizeOp(Node->getOperand(2));
1925 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1926 // Since this produces two values, make sure to remember that we legalized
1927 // both of them.
1928 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1929 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1930 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001931
Nate Begeman419f8b62005-10-18 00:27:41 +00001932 case ISD::BUILD_PAIR: {
1933 MVT::ValueType PairTy = Node->getValueType(0);
1934 // TODO: handle the case where the Lo and Hi operands are not of legal type
1935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1936 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1937 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001938 case TargetLowering::Promote:
1939 case TargetLowering::Custom:
1940 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001941 case TargetLowering::Legal:
1942 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1943 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1944 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001945 case TargetLowering::Expand:
1946 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1947 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1948 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1949 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1950 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001951 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001952 break;
1953 }
1954 break;
1955 }
1956
Nate Begemanc105e192005-04-06 00:23:54 +00001957 case ISD::UREM:
1958 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001959 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001960 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1961 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001962
Nate Begemanc105e192005-04-06 00:23:54 +00001963 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001964 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1965 case TargetLowering::Custom:
1966 isCustom = true;
1967 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001968 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001969 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001970 if (isCustom) {
1971 Tmp1 = TLI.LowerOperation(Result, DAG);
1972 if (Tmp1.Val) Result = Tmp1;
1973 }
Nate Begemanc105e192005-04-06 00:23:54 +00001974 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001975 case TargetLowering::Expand:
1976 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001977 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001978 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001979 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001980 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1981 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1982 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1983 } else {
1984 // Floating point mod -> fmod libcall.
1985 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1986 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001987 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001988 }
1989 break;
1990 }
1991 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001992 case ISD::VAARG: {
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1994 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1995
Chris Lattner5c62f332006-01-28 07:42:08 +00001996 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001997 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1998 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001999 case TargetLowering::Custom:
2000 isCustom = true;
2001 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002002 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002003 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2004 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002005 Tmp1 = Result.getValue(1);
2006
2007 if (isCustom) {
2008 Tmp2 = TLI.LowerOperation(Result, DAG);
2009 if (Tmp2.Val) {
2010 Result = LegalizeOp(Tmp2);
2011 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2012 }
2013 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002014 break;
2015 case TargetLowering::Expand: {
2016 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2017 Node->getOperand(2));
2018 // Increment the pointer, VAList, to the next vaarg
2019 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2020 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2021 TLI.getPointerTy()));
2022 // Store the incremented VAList to the legalized pointer
2023 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2024 Node->getOperand(2));
2025 // Load the actual argument out of the pointer VAList
2026 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002027 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002028 Result = LegalizeOp(Result);
2029 break;
2030 }
2031 }
2032 // Since VAARG produces two values, make sure to remember that we
2033 // legalized both of them.
2034 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002035 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2036 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002037 }
2038
2039 case ISD::VACOPY:
2040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2041 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2042 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2043
2044 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2045 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002046 case TargetLowering::Custom:
2047 isCustom = true;
2048 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002049 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002050 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2051 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002052 if (isCustom) {
2053 Tmp1 = TLI.LowerOperation(Result, DAG);
2054 if (Tmp1.Val) Result = Tmp1;
2055 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002056 break;
2057 case TargetLowering::Expand:
2058 // This defaults to loading a pointer from the input and storing it to the
2059 // output, returning the chain.
2060 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2061 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2062 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002063 break;
2064 }
2065 break;
2066
2067 case ISD::VAEND:
2068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2069 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2070
2071 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2072 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002073 case TargetLowering::Custom:
2074 isCustom = true;
2075 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002076 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002077 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002078 if (isCustom) {
2079 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2080 if (Tmp1.Val) Result = Tmp1;
2081 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002082 break;
2083 case TargetLowering::Expand:
2084 Result = Tmp1; // Default to a no-op, return the chain
2085 break;
2086 }
2087 break;
2088
2089 case ISD::VASTART:
2090 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2091 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2092
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002093 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2094
Nate Begemanacc398c2006-01-25 18:21:52 +00002095 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2096 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002097 case TargetLowering::Legal: break;
2098 case TargetLowering::Custom:
2099 Tmp1 = TLI.LowerOperation(Result, DAG);
2100 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002101 break;
2102 }
2103 break;
2104
Nate Begeman35ef9132006-01-11 21:21:00 +00002105 case ISD::ROTL:
2106 case ISD::ROTR:
2107 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2108 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002109
2110 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2111 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002112 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002113 break;
2114
Nate Begemand88fc032006-01-14 03:14:10 +00002115 case ISD::BSWAP:
2116 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2117 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002118 case TargetLowering::Custom:
2119 assert(0 && "Cannot custom legalize this yet!");
2120 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002121 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002122 break;
2123 case TargetLowering::Promote: {
2124 MVT::ValueType OVT = Tmp1.getValueType();
2125 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2126 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002127
Chris Lattner456a93a2006-01-28 07:39:30 +00002128 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2129 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2130 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2131 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2132 break;
2133 }
2134 case TargetLowering::Expand:
2135 Result = ExpandBSWAP(Tmp1);
2136 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002137 }
2138 break;
2139
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002140 case ISD::CTPOP:
2141 case ISD::CTTZ:
2142 case ISD::CTLZ:
2143 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2144 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002145 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002146 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002147 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002148 break;
2149 case TargetLowering::Promote: {
2150 MVT::ValueType OVT = Tmp1.getValueType();
2151 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002152
2153 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002154 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2155 // Perform the larger operation, then subtract if needed.
2156 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002157 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002158 case ISD::CTPOP:
2159 Result = Tmp1;
2160 break;
2161 case ISD::CTTZ:
2162 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002163 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2164 DAG.getConstant(getSizeInBits(NVT), NVT),
2165 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002166 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002167 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2168 break;
2169 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002170 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002171 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2172 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002173 getSizeInBits(OVT), NVT));
2174 break;
2175 }
2176 break;
2177 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002178 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002179 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002180 break;
2181 }
2182 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002183
Chris Lattner2c8086f2005-04-02 05:00:07 +00002184 // Unary operators
2185 case ISD::FABS:
2186 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002187 case ISD::FSQRT:
2188 case ISD::FSIN:
2189 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002190 Tmp1 = LegalizeOp(Node->getOperand(0));
2191 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002192 case TargetLowering::Promote:
2193 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002194 isCustom = true;
2195 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002196 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002197 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002198 if (isCustom) {
2199 Tmp1 = TLI.LowerOperation(Result, DAG);
2200 if (Tmp1.Val) Result = Tmp1;
2201 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002202 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002203 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002204 switch (Node->getOpcode()) {
2205 default: assert(0 && "Unreachable!");
2206 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002207 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2208 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002209 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002210 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002211 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002212 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2213 MVT::ValueType VT = Node->getValueType(0);
2214 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002215 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002216 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2217 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002218 break;
2219 }
2220 case ISD::FSQRT:
2221 case ISD::FSIN:
2222 case ISD::FCOS: {
2223 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002224 const char *FnName = 0;
2225 switch(Node->getOpcode()) {
2226 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2227 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2228 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2229 default: assert(0 && "Unreachable!");
2230 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002231 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002232 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002233 break;
2234 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002235 }
2236 break;
2237 }
2238 break;
Chris Lattner35481892005-12-23 00:16:34 +00002239
2240 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002241 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002242 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002243 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002244 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2245 Node->getOperand(0).getValueType())) {
2246 default: assert(0 && "Unknown operation action!");
2247 case TargetLowering::Expand:
2248 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2249 break;
2250 case TargetLowering::Legal:
2251 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002252 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002253 break;
2254 }
2255 }
2256 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002257 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002258 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002259 case ISD::UINT_TO_FP: {
2260 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002261 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2262 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002263 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002264 Node->getOperand(0).getValueType())) {
2265 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002266 case TargetLowering::Custom:
2267 isCustom = true;
2268 // FALLTHROUGH
2269 case TargetLowering::Legal:
2270 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002271 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002272 if (isCustom) {
2273 Tmp1 = TLI.LowerOperation(Result, DAG);
2274 if (Tmp1.Val) Result = Tmp1;
2275 }
2276 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002277 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002278 Result = ExpandLegalINT_TO_FP(isSigned,
2279 LegalizeOp(Node->getOperand(0)),
2280 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002281 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002282 case TargetLowering::Promote:
2283 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2284 Node->getValueType(0),
2285 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002286 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002287 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002288 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002289 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002290 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2291 Node->getValueType(0), Node->getOperand(0));
2292 break;
2293 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002294 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002295 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002296 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2297 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002298 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002299 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2300 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002301 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002302 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2303 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002304 break;
2305 }
2306 break;
2307 }
2308 case ISD::TRUNCATE:
2309 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2310 case Legal:
2311 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002312 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002313 break;
2314 case Expand:
2315 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2316
2317 // Since the result is legal, we should just be able to truncate the low
2318 // part of the source.
2319 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2320 break;
2321 case Promote:
2322 Result = PromoteOp(Node->getOperand(0));
2323 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2324 break;
2325 }
2326 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002327
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002328 case ISD::FP_TO_SINT:
2329 case ISD::FP_TO_UINT:
2330 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2331 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002332 Tmp1 = LegalizeOp(Node->getOperand(0));
2333
Chris Lattner1618beb2005-07-29 00:11:56 +00002334 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2335 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002336 case TargetLowering::Custom:
2337 isCustom = true;
2338 // FALLTHROUGH
2339 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002340 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002341 if (isCustom) {
2342 Tmp1 = TLI.LowerOperation(Result, DAG);
2343 if (Tmp1.Val) Result = Tmp1;
2344 }
2345 break;
2346 case TargetLowering::Promote:
2347 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2348 Node->getOpcode() == ISD::FP_TO_SINT);
2349 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002350 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002351 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2352 SDOperand True, False;
2353 MVT::ValueType VT = Node->getOperand(0).getValueType();
2354 MVT::ValueType NVT = Node->getValueType(0);
2355 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2356 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2357 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2358 Node->getOperand(0), Tmp2, ISD::SETLT);
2359 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2360 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002361 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002362 Tmp2));
2363 False = DAG.getNode(ISD::XOR, NVT, False,
2364 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002365 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2366 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002367 } else {
2368 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2369 }
2370 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002371 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002372 break;
2373 case Expand:
2374 assert(0 && "Shouldn't need to expand other operators here!");
2375 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002376 Tmp1 = PromoteOp(Node->getOperand(0));
2377 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2378 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002379 break;
2380 }
2381 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002382
Chris Lattner13c78e22005-09-02 00:18:10 +00002383 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002384 case ISD::ZERO_EXTEND:
2385 case ISD::SIGN_EXTEND:
2386 case ISD::FP_EXTEND:
2387 case ISD::FP_ROUND:
2388 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002389 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002390 case Legal:
2391 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002392 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002393 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002394 case Promote:
2395 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002396 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002397 Tmp1 = PromoteOp(Node->getOperand(0));
2398 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002399 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002400 case ISD::ZERO_EXTEND:
2401 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002402 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002403 Result = DAG.getZeroExtendInReg(Result,
2404 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002405 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002406 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002407 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002408 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002409 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002410 Result,
2411 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002412 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002413 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002414 Result = PromoteOp(Node->getOperand(0));
2415 if (Result.getValueType() != Op.getValueType())
2416 // Dynamically dead while we have only 2 FP types.
2417 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2418 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002419 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002420 Result = PromoteOp(Node->getOperand(0));
2421 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2422 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002423 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002424 }
2425 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002426 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002427 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002428 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002429 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002430
2431 // If this operation is not supported, convert it to a shl/shr or load/store
2432 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002433 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2434 default: assert(0 && "This action not supported for this op yet!");
2435 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002436 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002437 break;
2438 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002439 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002440 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002441 // NOTE: we could fall back on load/store here too for targets without
2442 // SAR. However, it is doubtful that any exist.
2443 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2444 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002445 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002446 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2447 Node->getOperand(0), ShiftCst);
2448 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2449 Result, ShiftCst);
2450 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2451 // The only way we can lower this is to turn it into a STORETRUNC,
2452 // EXTLOAD pair, targetting a temporary location (a stack slot).
2453
2454 // NOTE: there is a choice here between constantly creating new stack
2455 // slots and always reusing the same one. We currently always create
2456 // new ones, as reuse may inhibit scheduling.
2457 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2458 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2459 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2460 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002461 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002462 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2463 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2464 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002465 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002466 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002467 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2468 Result, StackSlot, DAG.getSrcValue(NULL),
2469 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002470 } else {
2471 assert(0 && "Unknown op");
2472 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002473 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002474 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002475 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002476 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002477 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002478
2479 // Make sure that the generated code is itself legal.
2480 if (Result != Op)
2481 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002482
Chris Lattner45982da2005-05-12 16:53:42 +00002483 // Note that LegalizeOp may be reentered even from single-use nodes, which
2484 // means that we always must cache transformed nodes.
2485 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002486 return Result;
2487}
2488
Chris Lattner8b6fa222005-01-15 22:16:26 +00002489/// PromoteOp - Given an operation that produces a value in an invalid type,
2490/// promote it to compute the value into a larger type. The produced value will
2491/// have the correct bits for the low portion of the register, but no guarantee
2492/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002493SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2494 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002495 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002496 assert(getTypeAction(VT) == Promote &&
2497 "Caller should expand or legalize operands that are not promotable!");
2498 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2499 "Cannot promote to smaller type!");
2500
Chris Lattner03c85462005-01-15 05:21:40 +00002501 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002502 SDOperand Result;
2503 SDNode *Node = Op.Val;
2504
Chris Lattner6fdcb252005-09-02 20:32:45 +00002505 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2506 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002507
Chris Lattner03c85462005-01-15 05:21:40 +00002508 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002509 case ISD::CopyFromReg:
2510 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002511 default:
2512 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2513 assert(0 && "Do not know how to promote this operator!");
2514 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002515 case ISD::UNDEF:
2516 Result = DAG.getNode(ISD::UNDEF, NVT);
2517 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002518 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002519 if (VT != MVT::i1)
2520 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2521 else
2522 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002523 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2524 break;
2525 case ISD::ConstantFP:
2526 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2527 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2528 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002529
Chris Lattner82fbfb62005-01-18 02:59:52 +00002530 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002531 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002532 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2533 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002534 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002535
2536 case ISD::TRUNCATE:
2537 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2538 case Legal:
2539 Result = LegalizeOp(Node->getOperand(0));
2540 assert(Result.getValueType() >= NVT &&
2541 "This truncation doesn't make sense!");
2542 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2543 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2544 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002545 case Promote:
2546 // The truncation is not required, because we don't guarantee anything
2547 // about high bits anyway.
2548 Result = PromoteOp(Node->getOperand(0));
2549 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002550 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002551 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2552 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002553 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002554 }
2555 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002556 case ISD::SIGN_EXTEND:
2557 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002558 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002559 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2560 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2561 case Legal:
2562 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002563 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002564 break;
2565 case Promote:
2566 // Promote the reg if it's smaller.
2567 Result = PromoteOp(Node->getOperand(0));
2568 // The high bits are not guaranteed to be anything. Insert an extend.
2569 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002570 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002571 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002572 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002573 Result = DAG.getZeroExtendInReg(Result,
2574 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002575 break;
2576 }
2577 break;
Chris Lattner35481892005-12-23 00:16:34 +00002578 case ISD::BIT_CONVERT:
2579 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2580 Result = PromoteOp(Result);
2581 break;
2582
Chris Lattner8b6fa222005-01-15 22:16:26 +00002583 case ISD::FP_EXTEND:
2584 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2585 case ISD::FP_ROUND:
2586 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2587 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2588 case Promote: assert(0 && "Unreachable with 2 FP types!");
2589 case Legal:
2590 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002591 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002592 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002593 break;
2594 }
2595 break;
2596
2597 case ISD::SINT_TO_FP:
2598 case ISD::UINT_TO_FP:
2599 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2600 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002601 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002602 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002603 break;
2604
2605 case Promote:
2606 Result = PromoteOp(Node->getOperand(0));
2607 if (Node->getOpcode() == ISD::SINT_TO_FP)
2608 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002609 Result,
2610 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002611 else
Chris Lattner23993562005-04-13 02:38:47 +00002612 Result = DAG.getZeroExtendInReg(Result,
2613 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002614 // No extra round required here.
2615 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002616 break;
2617 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002618 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2619 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002620 // Round if we cannot tolerate excess precision.
2621 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002622 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2623 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002624 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002625 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002626 break;
2627
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002628 case ISD::SIGN_EXTEND_INREG:
2629 Result = PromoteOp(Node->getOperand(0));
2630 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2631 Node->getOperand(1));
2632 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002633 case ISD::FP_TO_SINT:
2634 case ISD::FP_TO_UINT:
2635 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2636 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002637 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002638 break;
2639 case Promote:
2640 // The input result is prerounded, so we don't have to do anything
2641 // special.
2642 Tmp1 = PromoteOp(Node->getOperand(0));
2643 break;
2644 case Expand:
2645 assert(0 && "not implemented");
2646 }
Nate Begemand2558e32005-08-14 01:20:53 +00002647 // If we're promoting a UINT to a larger size, check to see if the new node
2648 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2649 // we can use that instead. This allows us to generate better code for
2650 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2651 // legal, such as PowerPC.
2652 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002653 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002654 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2655 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002656 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2657 } else {
2658 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2659 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002660 break;
2661
Chris Lattner2c8086f2005-04-02 05:00:07 +00002662 case ISD::FABS:
2663 case ISD::FNEG:
2664 Tmp1 = PromoteOp(Node->getOperand(0));
2665 assert(Tmp1.getValueType() == NVT);
2666 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2667 // NOTE: we do not have to do any extra rounding here for
2668 // NoExcessFPPrecision, because we know the input will have the appropriate
2669 // precision, and these operations don't modify precision at all.
2670 break;
2671
Chris Lattnerda6ba872005-04-28 21:44:33 +00002672 case ISD::FSQRT:
2673 case ISD::FSIN:
2674 case ISD::FCOS:
2675 Tmp1 = PromoteOp(Node->getOperand(0));
2676 assert(Tmp1.getValueType() == NVT);
2677 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002678 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002679 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2680 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002681 break;
2682
Chris Lattner03c85462005-01-15 05:21:40 +00002683 case ISD::AND:
2684 case ISD::OR:
2685 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002686 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002687 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002688 case ISD::MUL:
2689 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002690 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002691 // that too is okay if they are integer operations.
2692 Tmp1 = PromoteOp(Node->getOperand(0));
2693 Tmp2 = PromoteOp(Node->getOperand(1));
2694 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2695 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002696 break;
2697 case ISD::FADD:
2698 case ISD::FSUB:
2699 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002700 Tmp1 = PromoteOp(Node->getOperand(0));
2701 Tmp2 = PromoteOp(Node->getOperand(1));
2702 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2703 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2704
2705 // Floating point operations will give excess precision that we may not be
2706 // able to tolerate. If we DO allow excess precision, just leave it,
2707 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002708 // FIXME: Why would we need to round FP ops more than integer ones?
2709 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002710 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002711 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2712 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002713 break;
2714
Chris Lattner8b6fa222005-01-15 22:16:26 +00002715 case ISD::SDIV:
2716 case ISD::SREM:
2717 // These operators require that their input be sign extended.
2718 Tmp1 = PromoteOp(Node->getOperand(0));
2719 Tmp2 = PromoteOp(Node->getOperand(1));
2720 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002721 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2722 DAG.getValueType(VT));
2723 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2724 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002725 }
2726 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2727
2728 // Perform FP_ROUND: this is probably overly pessimistic.
2729 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002730 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2731 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002732 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002733 case ISD::FDIV:
2734 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002735 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002736 // These operators require that their input be fp extended.
2737 Tmp1 = PromoteOp(Node->getOperand(0));
2738 Tmp2 = PromoteOp(Node->getOperand(1));
2739 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2740
2741 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002742 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002743 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2744 DAG.getValueType(VT));
2745 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002746
2747 case ISD::UDIV:
2748 case ISD::UREM:
2749 // These operators require that their input be zero extended.
2750 Tmp1 = PromoteOp(Node->getOperand(0));
2751 Tmp2 = PromoteOp(Node->getOperand(1));
2752 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002753 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2754 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002755 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2756 break;
2757
2758 case ISD::SHL:
2759 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002760 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002761 break;
2762 case ISD::SRA:
2763 // The input value must be properly sign extended.
2764 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002765 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2766 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002767 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002768 break;
2769 case ISD::SRL:
2770 // The input value must be properly zero extended.
2771 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002772 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002773 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002774 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002775
2776 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002777 Tmp1 = Node->getOperand(0); // Get the chain.
2778 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002779 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2780 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2781 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2782 } else {
2783 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2784 Node->getOperand(2));
2785 // Increment the pointer, VAList, to the next vaarg
2786 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2787 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2788 TLI.getPointerTy()));
2789 // Store the incremented VAList to the legalized pointer
2790 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2791 Node->getOperand(2));
2792 // Load the actual argument out of the pointer VAList
2793 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2794 DAG.getSrcValue(0), VT);
2795 }
2796 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002797 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002798 break;
2799
Chris Lattner03c85462005-01-15 05:21:40 +00002800 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002801 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2802 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002803 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002804 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002805 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002806 case ISD::SEXTLOAD:
2807 case ISD::ZEXTLOAD:
2808 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002809 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2810 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002811 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002812 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002813 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002814 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002815 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002816 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2817 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002818 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002819 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002820 case ISD::SELECT_CC:
2821 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2822 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2823 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002824 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002825 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002826 case ISD::BSWAP:
2827 Tmp1 = Node->getOperand(0);
2828 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2829 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2830 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2831 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2832 TLI.getShiftAmountTy()));
2833 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002834 case ISD::CTPOP:
2835 case ISD::CTTZ:
2836 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002837 // Zero extend the argument
2838 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002839 // Perform the larger operation, then subtract if needed.
2840 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002841 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002842 case ISD::CTPOP:
2843 Result = Tmp1;
2844 break;
2845 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002846 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002847 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002848 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002849 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002850 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002851 break;
2852 case ISD::CTLZ:
2853 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002854 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2855 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002856 getSizeInBits(VT), NVT));
2857 break;
2858 }
2859 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002860 }
2861
2862 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002863
2864 // Make sure the result is itself legal.
2865 Result = LegalizeOp(Result);
2866
2867 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002868 AddPromotedOperand(Op, Result);
2869 return Result;
2870}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002871
Nate Begeman750ac1b2006-02-01 07:19:44 +00002872/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2873/// with condition CC on the current target. This usually involves legalizing
2874/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2875/// there may be no choice but to create a new SetCC node to represent the
2876/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2877/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2878void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2879 SDOperand &RHS,
2880 SDOperand &CC) {
2881 SDOperand Tmp1, Tmp2, Result;
2882
2883 switch (getTypeAction(LHS.getValueType())) {
2884 case Legal:
2885 Tmp1 = LegalizeOp(LHS); // LHS
2886 Tmp2 = LegalizeOp(RHS); // RHS
2887 break;
2888 case Promote:
2889 Tmp1 = PromoteOp(LHS); // LHS
2890 Tmp2 = PromoteOp(RHS); // RHS
2891
2892 // If this is an FP compare, the operands have already been extended.
2893 if (MVT::isInteger(LHS.getValueType())) {
2894 MVT::ValueType VT = LHS.getValueType();
2895 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2896
2897 // Otherwise, we have to insert explicit sign or zero extends. Note
2898 // that we could insert sign extends for ALL conditions, but zero extend
2899 // is cheaper on many machines (an AND instead of two shifts), so prefer
2900 // it.
2901 switch (cast<CondCodeSDNode>(CC)->get()) {
2902 default: assert(0 && "Unknown integer comparison!");
2903 case ISD::SETEQ:
2904 case ISD::SETNE:
2905 case ISD::SETUGE:
2906 case ISD::SETUGT:
2907 case ISD::SETULE:
2908 case ISD::SETULT:
2909 // ALL of these operations will work if we either sign or zero extend
2910 // the operands (including the unsigned comparisons!). Zero extend is
2911 // usually a simpler/cheaper operation, so prefer it.
2912 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2913 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2914 break;
2915 case ISD::SETGE:
2916 case ISD::SETGT:
2917 case ISD::SETLT:
2918 case ISD::SETLE:
2919 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2920 DAG.getValueType(VT));
2921 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2922 DAG.getValueType(VT));
2923 break;
2924 }
2925 }
2926 break;
2927 case Expand:
2928 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2929 ExpandOp(LHS, LHSLo, LHSHi);
2930 ExpandOp(RHS, RHSLo, RHSHi);
2931 switch (cast<CondCodeSDNode>(CC)->get()) {
2932 case ISD::SETEQ:
2933 case ISD::SETNE:
2934 if (RHSLo == RHSHi)
2935 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2936 if (RHSCST->isAllOnesValue()) {
2937 // Comparison to -1.
2938 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2939 Tmp2 = RHSLo;
2940 break;
2941 }
2942
2943 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2944 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2945 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2946 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2947 break;
2948 default:
2949 // If this is a comparison of the sign bit, just look at the top part.
2950 // X > -1, x < 0
2951 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2952 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2953 CST->getValue() == 0) || // X < 0
2954 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2955 CST->isAllOnesValue())) { // X > -1
2956 Tmp1 = LHSHi;
2957 Tmp2 = RHSHi;
2958 break;
2959 }
2960
2961 // FIXME: This generated code sucks.
2962 ISD::CondCode LowCC;
2963 switch (cast<CondCodeSDNode>(CC)->get()) {
2964 default: assert(0 && "Unknown integer setcc!");
2965 case ISD::SETLT:
2966 case ISD::SETULT: LowCC = ISD::SETULT; break;
2967 case ISD::SETGT:
2968 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2969 case ISD::SETLE:
2970 case ISD::SETULE: LowCC = ISD::SETULE; break;
2971 case ISD::SETGE:
2972 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2973 }
2974
2975 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2976 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2977 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2978
2979 // NOTE: on targets without efficient SELECT of bools, we can always use
2980 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2981 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2982 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2983 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2984 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2985 Result, Tmp1, Tmp2));
2986 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002987 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002988 }
2989 }
2990 LHS = Tmp1;
2991 RHS = Tmp2;
2992}
2993
Chris Lattner35481892005-12-23 00:16:34 +00002994/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002995/// The resultant code need not be legal. Note that SrcOp is the input operand
2996/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002997SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2998 SDOperand SrcOp) {
2999 // Create the stack frame object.
3000 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3001 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner232348d2005-12-23 00:52:30 +00003002 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner35481892005-12-23 00:16:34 +00003003 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3004
3005 // Emit a store to the stack slot.
3006 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00003007 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00003008 // Result is a load from the stack slot.
3009 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3010}
3011
Chris Lattner5b359c62005-04-02 04:00:59 +00003012void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3013 SDOperand Op, SDOperand Amt,
3014 SDOperand &Lo, SDOperand &Hi) {
3015 // Expand the subcomponents.
3016 SDOperand LHSL, LHSH;
3017 ExpandOp(Op, LHSL, LHSH);
3018
3019 std::vector<SDOperand> Ops;
3020 Ops.push_back(LHSL);
3021 Ops.push_back(LHSH);
3022 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003023 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003024 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003025 Hi = Lo.getValue(1);
3026}
3027
3028
Chris Lattnere34b3962005-01-19 04:19:40 +00003029/// ExpandShift - Try to find a clever way to expand this shift operation out to
3030/// smaller elements. If we can't find a way that is more efficient than a
3031/// libcall on this target, return false. Otherwise, return true with the
3032/// low-parts expanded into Lo and Hi.
3033bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3034 SDOperand &Lo, SDOperand &Hi) {
3035 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3036 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003037
Chris Lattnere34b3962005-01-19 04:19:40 +00003038 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003039 SDOperand ShAmt = LegalizeOp(Amt);
3040 MVT::ValueType ShTy = ShAmt.getValueType();
3041 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3042 unsigned NVTBits = MVT::getSizeInBits(NVT);
3043
3044 // Handle the case when Amt is an immediate. Other cases are currently broken
3045 // and are disabled.
3046 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3047 unsigned Cst = CN->getValue();
3048 // Expand the incoming operand to be shifted, so that we have its parts
3049 SDOperand InL, InH;
3050 ExpandOp(Op, InL, InH);
3051 switch(Opc) {
3052 case ISD::SHL:
3053 if (Cst > VTBits) {
3054 Lo = DAG.getConstant(0, NVT);
3055 Hi = DAG.getConstant(0, NVT);
3056 } else if (Cst > NVTBits) {
3057 Lo = DAG.getConstant(0, NVT);
3058 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003059 } else if (Cst == NVTBits) {
3060 Lo = DAG.getConstant(0, NVT);
3061 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003062 } else {
3063 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3064 Hi = DAG.getNode(ISD::OR, NVT,
3065 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3066 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3067 }
3068 return true;
3069 case ISD::SRL:
3070 if (Cst > VTBits) {
3071 Lo = DAG.getConstant(0, NVT);
3072 Hi = DAG.getConstant(0, NVT);
3073 } else if (Cst > NVTBits) {
3074 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3075 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003076 } else if (Cst == NVTBits) {
3077 Lo = InH;
3078 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003079 } else {
3080 Lo = DAG.getNode(ISD::OR, NVT,
3081 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3082 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3083 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3084 }
3085 return true;
3086 case ISD::SRA:
3087 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003088 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003089 DAG.getConstant(NVTBits-1, ShTy));
3090 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003091 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003092 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003093 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003094 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003095 } else if (Cst == NVTBits) {
3096 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003097 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003098 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003099 } else {
3100 Lo = DAG.getNode(ISD::OR, NVT,
3101 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3102 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3103 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3104 }
3105 return true;
3106 }
3107 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003108 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003109}
Chris Lattner77e77a62005-01-21 06:05:23 +00003110
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003111
Chris Lattner77e77a62005-01-21 06:05:23 +00003112// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3113// does not fit into a register, return the lo part and set the hi part to the
3114// by-reg argument. If it does fit into a single register, return the result
3115// and leave the Hi part unset.
3116SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3117 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003118 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3119 // The input chain to this libcall is the entry node of the function.
3120 // Legalizing the call will automatically add the previous call to the
3121 // dependence.
3122 SDOperand InChain = DAG.getEntryNode();
3123
Chris Lattner77e77a62005-01-21 06:05:23 +00003124 TargetLowering::ArgListTy Args;
3125 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3126 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3127 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3128 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3129 }
3130 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003131
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003132 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003133 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003134 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003135 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3136 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003137
Chris Lattner6831a812006-02-13 09:18:02 +00003138 // Legalize the call sequence, starting with the chain. This will advance
3139 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3140 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3141 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003142 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003143 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003144 default: assert(0 && "Unknown thing");
3145 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003146 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003147 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003148 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003149 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003150 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003151 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003152 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003153}
3154
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003155
Chris Lattner77e77a62005-01-21 06:05:23 +00003156/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3157/// destination type is legal.
3158SDOperand SelectionDAGLegalize::
3159ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003160 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003161 assert(getTypeAction(Source.getValueType()) == Expand &&
3162 "This is not an expansion!");
3163 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3164
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003165 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003166 assert(Source.getValueType() == MVT::i64 &&
3167 "This only works for 64-bit -> FP");
3168 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3169 // incoming integer is set. To handle this, we dynamically test to see if
3170 // it is set, and, if so, add a fudge factor.
3171 SDOperand Lo, Hi;
3172 ExpandOp(Source, Lo, Hi);
3173
Chris Lattner66de05b2005-05-13 04:45:13 +00003174 // If this is unsigned, and not supported, first perform the conversion to
3175 // signed, then adjust the result if the sign bit is set.
3176 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3177 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3178
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003179 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3180 DAG.getConstant(0, Hi.getValueType()),
3181 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003182 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3183 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3184 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003185 uint64_t FF = 0x5f800000ULL;
3186 if (TLI.isLittleEndian()) FF <<= 32;
3187 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003188
Chris Lattner5839bf22005-08-26 17:15:30 +00003189 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003190 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3191 SDOperand FudgeInReg;
3192 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003193 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3194 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003195 else {
3196 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003197 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3198 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003199 }
Chris Lattner473a9902005-09-29 06:44:39 +00003200 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003201 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003202
Chris Lattnera88a2602005-05-14 05:33:54 +00003203 // Check to see if the target has a custom way to lower this. If so, use it.
3204 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3205 default: assert(0 && "This action not implemented for this operation!");
3206 case TargetLowering::Legal:
3207 case TargetLowering::Expand:
3208 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003209 case TargetLowering::Custom: {
3210 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3211 Source), DAG);
3212 if (NV.Val)
3213 return LegalizeOp(NV);
3214 break; // The target decided this was legal after all
3215 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003216 }
3217
Chris Lattner13689e22005-05-12 07:00:44 +00003218 // Expand the source, then glue it back together for the call. We must expand
3219 // the source in case it is shared (this pass of legalize must traverse it).
3220 SDOperand SrcLo, SrcHi;
3221 ExpandOp(Source, SrcLo, SrcHi);
3222 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3223
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003224 const char *FnName = 0;
3225 if (DestTy == MVT::f32)
3226 FnName = "__floatdisf";
3227 else {
3228 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3229 FnName = "__floatdidf";
3230 }
Chris Lattner6831a812006-02-13 09:18:02 +00003231
3232 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3233 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003234 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003235}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003236
Chris Lattner22cde6a2006-01-28 08:25:58 +00003237/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3238/// INT_TO_FP operation of the specified operand when the target requests that
3239/// we expand it. At this point, we know that the result and operand types are
3240/// legal for the target.
3241SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3242 SDOperand Op0,
3243 MVT::ValueType DestVT) {
3244 if (Op0.getValueType() == MVT::i32) {
3245 // simple 32-bit [signed|unsigned] integer to float/double expansion
3246
3247 // get the stack frame index of a 8 byte buffer
3248 MachineFunction &MF = DAG.getMachineFunction();
3249 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3250 // get address of 8 byte buffer
3251 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3252 // word offset constant for Hi/Lo address computation
3253 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3254 // set up Hi and Lo (into buffer) address based on endian
3255 SDOperand Hi, Lo;
3256 if (TLI.isLittleEndian()) {
3257 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3258 Lo = StackSlot;
3259 } else {
3260 Hi = StackSlot;
3261 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3262 }
3263 // if signed map to unsigned space
3264 SDOperand Op0Mapped;
3265 if (isSigned) {
3266 // constant used to invert sign bit (signed to unsigned mapping)
3267 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3268 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3269 } else {
3270 Op0Mapped = Op0;
3271 }
3272 // store the lo of the constructed double - based on integer input
3273 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3274 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3275 // initial hi portion of constructed double
3276 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3277 // store the hi of the constructed double - biased exponent
3278 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3279 InitialHi, Hi, DAG.getSrcValue(NULL));
3280 // load the constructed double
3281 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3282 DAG.getSrcValue(NULL));
3283 // FP constant to bias correct the final result
3284 SDOperand Bias = DAG.getConstantFP(isSigned ?
3285 BitsToDouble(0x4330000080000000ULL)
3286 : BitsToDouble(0x4330000000000000ULL),
3287 MVT::f64);
3288 // subtract the bias
3289 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3290 // final result
3291 SDOperand Result;
3292 // handle final rounding
3293 if (DestVT == MVT::f64) {
3294 // do nothing
3295 Result = Sub;
3296 } else {
3297 // if f32 then cast to f32
3298 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3299 }
3300 return Result;
3301 }
3302 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3303 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3304
3305 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3306 DAG.getConstant(0, Op0.getValueType()),
3307 ISD::SETLT);
3308 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3309 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3310 SignSet, Four, Zero);
3311
3312 // If the sign bit of the integer is set, the large number will be treated
3313 // as a negative number. To counteract this, the dynamic code adds an
3314 // offset depending on the data type.
3315 uint64_t FF;
3316 switch (Op0.getValueType()) {
3317 default: assert(0 && "Unsupported integer type!");
3318 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3319 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3320 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3321 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3322 }
3323 if (TLI.isLittleEndian()) FF <<= 32;
3324 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3325
3326 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3327 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3328 SDOperand FudgeInReg;
3329 if (DestVT == MVT::f32)
3330 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3331 DAG.getSrcValue(NULL));
3332 else {
3333 assert(DestVT == MVT::f64 && "Unexpected conversion");
3334 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3335 DAG.getEntryNode(), CPIdx,
3336 DAG.getSrcValue(NULL), MVT::f32));
3337 }
3338
3339 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3340}
3341
3342/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3343/// *INT_TO_FP operation of the specified operand when the target requests that
3344/// we promote it. At this point, we know that the result and operand types are
3345/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3346/// operation that takes a larger input.
3347SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3348 MVT::ValueType DestVT,
3349 bool isSigned) {
3350 // First step, figure out the appropriate *INT_TO_FP operation to use.
3351 MVT::ValueType NewInTy = LegalOp.getValueType();
3352
3353 unsigned OpToUse = 0;
3354
3355 // Scan for the appropriate larger type to use.
3356 while (1) {
3357 NewInTy = (MVT::ValueType)(NewInTy+1);
3358 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3359
3360 // If the target supports SINT_TO_FP of this type, use it.
3361 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3362 default: break;
3363 case TargetLowering::Legal:
3364 if (!TLI.isTypeLegal(NewInTy))
3365 break; // Can't use this datatype.
3366 // FALL THROUGH.
3367 case TargetLowering::Custom:
3368 OpToUse = ISD::SINT_TO_FP;
3369 break;
3370 }
3371 if (OpToUse) break;
3372 if (isSigned) continue;
3373
3374 // If the target supports UINT_TO_FP of this type, use it.
3375 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3376 default: break;
3377 case TargetLowering::Legal:
3378 if (!TLI.isTypeLegal(NewInTy))
3379 break; // Can't use this datatype.
3380 // FALL THROUGH.
3381 case TargetLowering::Custom:
3382 OpToUse = ISD::UINT_TO_FP;
3383 break;
3384 }
3385 if (OpToUse) break;
3386
3387 // Otherwise, try a larger type.
3388 }
3389
3390 // Okay, we found the operation and type to use. Zero extend our input to the
3391 // desired type then run the operation on it.
3392 return DAG.getNode(OpToUse, DestVT,
3393 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3394 NewInTy, LegalOp));
3395}
3396
3397/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3398/// FP_TO_*INT operation of the specified operand when the target requests that
3399/// we promote it. At this point, we know that the result and operand types are
3400/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3401/// operation that returns a larger result.
3402SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3403 MVT::ValueType DestVT,
3404 bool isSigned) {
3405 // First step, figure out the appropriate FP_TO*INT operation to use.
3406 MVT::ValueType NewOutTy = DestVT;
3407
3408 unsigned OpToUse = 0;
3409
3410 // Scan for the appropriate larger type to use.
3411 while (1) {
3412 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3413 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3414
3415 // If the target supports FP_TO_SINT returning this type, use it.
3416 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3417 default: break;
3418 case TargetLowering::Legal:
3419 if (!TLI.isTypeLegal(NewOutTy))
3420 break; // Can't use this datatype.
3421 // FALL THROUGH.
3422 case TargetLowering::Custom:
3423 OpToUse = ISD::FP_TO_SINT;
3424 break;
3425 }
3426 if (OpToUse) break;
3427
3428 // If the target supports FP_TO_UINT of this type, use it.
3429 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3430 default: break;
3431 case TargetLowering::Legal:
3432 if (!TLI.isTypeLegal(NewOutTy))
3433 break; // Can't use this datatype.
3434 // FALL THROUGH.
3435 case TargetLowering::Custom:
3436 OpToUse = ISD::FP_TO_UINT;
3437 break;
3438 }
3439 if (OpToUse) break;
3440
3441 // Otherwise, try a larger type.
3442 }
3443
3444 // Okay, we found the operation and type to use. Truncate the result of the
3445 // extended FP_TO_*INT operation to the desired size.
3446 return DAG.getNode(ISD::TRUNCATE, DestVT,
3447 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3448}
3449
3450/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3451///
3452SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3453 MVT::ValueType VT = Op.getValueType();
3454 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3455 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3456 switch (VT) {
3457 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3458 case MVT::i16:
3459 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3460 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3461 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3462 case MVT::i32:
3463 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3464 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3465 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3466 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3467 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3468 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3469 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3470 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3471 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3472 case MVT::i64:
3473 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3474 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3475 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3476 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3477 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3478 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3479 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3480 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3481 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3482 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3483 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3484 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3485 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3486 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3487 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3488 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3489 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3490 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3491 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3492 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3493 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3494 }
3495}
3496
3497/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3498///
3499SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3500 switch (Opc) {
3501 default: assert(0 && "Cannot expand this yet!");
3502 case ISD::CTPOP: {
3503 static const uint64_t mask[6] = {
3504 0x5555555555555555ULL, 0x3333333333333333ULL,
3505 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3506 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3507 };
3508 MVT::ValueType VT = Op.getValueType();
3509 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3510 unsigned len = getSizeInBits(VT);
3511 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3512 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3513 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3514 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3515 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3516 DAG.getNode(ISD::AND, VT,
3517 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3518 }
3519 return Op;
3520 }
3521 case ISD::CTLZ: {
3522 // for now, we do this:
3523 // x = x | (x >> 1);
3524 // x = x | (x >> 2);
3525 // ...
3526 // x = x | (x >>16);
3527 // x = x | (x >>32); // for 64-bit input
3528 // return popcount(~x);
3529 //
3530 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3531 MVT::ValueType VT = Op.getValueType();
3532 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3533 unsigned len = getSizeInBits(VT);
3534 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3535 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3536 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3537 }
3538 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3539 return DAG.getNode(ISD::CTPOP, VT, Op);
3540 }
3541 case ISD::CTTZ: {
3542 // for now, we use: { return popcount(~x & (x - 1)); }
3543 // unless the target has ctlz but not ctpop, in which case we use:
3544 // { return 32 - nlz(~x & (x-1)); }
3545 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3546 MVT::ValueType VT = Op.getValueType();
3547 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3548 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3549 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3550 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3551 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3552 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3553 TLI.isOperationLegal(ISD::CTLZ, VT))
3554 return DAG.getNode(ISD::SUB, VT,
3555 DAG.getConstant(getSizeInBits(VT), VT),
3556 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3557 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3558 }
3559 }
3560}
Chris Lattnere34b3962005-01-19 04:19:40 +00003561
3562
Chris Lattner3e928bb2005-01-07 07:47:09 +00003563/// ExpandOp - Expand the specified SDOperand into its two component pieces
3564/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3565/// LegalizeNodes map is filled in for any results that are not expanded, the
3566/// ExpandedNodes map is filled in for any results that are expanded, and the
3567/// Lo/Hi values are returned.
3568void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3569 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003570 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003571 SDNode *Node = Op.Val;
3572 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003573 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3574 "Cannot expand FP values!");
3575 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003576 "Cannot expand to FP value or to larger int value!");
3577
Chris Lattner6fdcb252005-09-02 20:32:45 +00003578 // See if we already expanded it.
3579 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3580 = ExpandedNodes.find(Op);
3581 if (I != ExpandedNodes.end()) {
3582 Lo = I->second.first;
3583 Hi = I->second.second;
3584 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003585 }
3586
Chris Lattner3e928bb2005-01-07 07:47:09 +00003587 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003588 case ISD::CopyFromReg:
3589 assert(0 && "CopyFromReg must be legal!");
3590 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003591 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3592 assert(0 && "Do not know how to expand this operator!");
3593 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003594 case ISD::UNDEF:
3595 Lo = DAG.getNode(ISD::UNDEF, NVT);
3596 Hi = DAG.getNode(ISD::UNDEF, NVT);
3597 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003598 case ISD::Constant: {
3599 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3600 Lo = DAG.getConstant(Cst, NVT);
3601 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3602 break;
3603 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003604 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003605 // Return the operands.
3606 Lo = Node->getOperand(0);
3607 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003608 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003609
3610 case ISD::SIGN_EXTEND_INREG:
3611 ExpandOp(Node->getOperand(0), Lo, Hi);
3612 // Sign extend the lo-part.
3613 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3614 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3615 TLI.getShiftAmountTy()));
3616 // sext_inreg the low part if needed.
3617 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3618 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003619
Nate Begemand88fc032006-01-14 03:14:10 +00003620 case ISD::BSWAP: {
3621 ExpandOp(Node->getOperand(0), Lo, Hi);
3622 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3623 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3624 Lo = TempLo;
3625 break;
3626 }
3627
Chris Lattneredb1add2005-05-11 04:51:16 +00003628 case ISD::CTPOP:
3629 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003630 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3631 DAG.getNode(ISD::CTPOP, NVT, Lo),
3632 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003633 Hi = DAG.getConstant(0, NVT);
3634 break;
3635
Chris Lattner39a8f332005-05-12 19:05:01 +00003636 case ISD::CTLZ: {
3637 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003638 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003639 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3640 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003641 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3642 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003643 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3644 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3645
3646 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3647 Hi = DAG.getConstant(0, NVT);
3648 break;
3649 }
3650
3651 case ISD::CTTZ: {
3652 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003653 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003654 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3655 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003656 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3657 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003658 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3659 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3660
3661 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3662 Hi = DAG.getConstant(0, NVT);
3663 break;
3664 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003665
Nate Begemanacc398c2006-01-25 18:21:52 +00003666 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003667 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3668 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003669 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3670 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3671
3672 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003673 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003674 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3675 if (!TLI.isLittleEndian())
3676 std::swap(Lo, Hi);
3677 break;
3678 }
3679
Chris Lattner3e928bb2005-01-07 07:47:09 +00003680 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003681 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3682 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003683 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003684
3685 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003686 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003687 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3688 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003689 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003690 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003691
3692 // Build a factor node to remember that this load is independent of the
3693 // other one.
3694 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3695 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003696
Chris Lattner3e928bb2005-01-07 07:47:09 +00003697 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003698 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003699 if (!TLI.isLittleEndian())
3700 std::swap(Lo, Hi);
3701 break;
3702 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003703 case ISD::AND:
3704 case ISD::OR:
3705 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3706 SDOperand LL, LH, RL, RH;
3707 ExpandOp(Node->getOperand(0), LL, LH);
3708 ExpandOp(Node->getOperand(1), RL, RH);
3709 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3710 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3711 break;
3712 }
3713 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003714 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003715 ExpandOp(Node->getOperand(1), LL, LH);
3716 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003717 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3718 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003719 break;
3720 }
Nate Begeman9373a812005-08-10 20:51:12 +00003721 case ISD::SELECT_CC: {
3722 SDOperand TL, TH, FL, FH;
3723 ExpandOp(Node->getOperand(2), TL, TH);
3724 ExpandOp(Node->getOperand(3), FL, FH);
3725 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3726 Node->getOperand(1), TL, FL, Node->getOperand(4));
3727 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3728 Node->getOperand(1), TH, FH, Node->getOperand(4));
3729 break;
3730 }
Nate Begeman144ff662005-10-13 17:15:37 +00003731 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003732 SDOperand Chain = Node->getOperand(0);
3733 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003734 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3735
3736 if (EVT == NVT)
3737 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3738 else
3739 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3740 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003741
3742 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003743 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003744
Nate Begeman144ff662005-10-13 17:15:37 +00003745 // The high part is obtained by SRA'ing all but one of the bits of the lo
3746 // part.
3747 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3748 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3749 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003750 break;
3751 }
3752 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003753 SDOperand Chain = Node->getOperand(0);
3754 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003755 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3756
3757 if (EVT == NVT)
3758 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3759 else
3760 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3761 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003762
3763 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003764 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003765
Nate Begeman144ff662005-10-13 17:15:37 +00003766 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003767 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003768 break;
3769 }
3770 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003771 SDOperand Chain = Node->getOperand(0);
3772 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003773 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3774
3775 if (EVT == NVT)
3776 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3777 else
3778 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3779 EVT);
3780
3781 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003782 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003783
3784 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003785 Hi = DAG.getNode(ISD::UNDEF, NVT);
3786 break;
3787 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003788 case ISD::ANY_EXTEND:
3789 // The low part is any extension of the input (which degenerates to a copy).
3790 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3791 // The high part is undefined.
3792 Hi = DAG.getNode(ISD::UNDEF, NVT);
3793 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003794 case ISD::SIGN_EXTEND: {
3795 // The low part is just a sign extension of the input (which degenerates to
3796 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003797 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003798
Chris Lattner3e928bb2005-01-07 07:47:09 +00003799 // The high part is obtained by SRA'ing all but one of the bits of the lo
3800 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003801 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003802 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3803 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003804 break;
3805 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003806 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003807 // The low part is just a zero extension of the input (which degenerates to
3808 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003809 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003810
Chris Lattner3e928bb2005-01-07 07:47:09 +00003811 // The high part is just a zero.
3812 Hi = DAG.getConstant(0, NVT);
3813 break;
Chris Lattner35481892005-12-23 00:16:34 +00003814
3815 case ISD::BIT_CONVERT: {
3816 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3817 Node->getOperand(0));
3818 ExpandOp(Tmp, Lo, Hi);
3819 break;
3820 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003821
Chris Lattner8137c9e2006-01-28 05:07:51 +00003822 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003823 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3824 TargetLowering::Custom &&
3825 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003826 Lo = TLI.LowerOperation(Op, DAG);
3827 assert(Lo.Val && "Node must be custom expanded!");
3828 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003829 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003830 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003831 break;
3832
Chris Lattner4e6c7462005-01-08 19:27:05 +00003833 // These operators cannot be expanded directly, emit them as calls to
3834 // library functions.
3835 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003836 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003837 SDOperand Op;
3838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3839 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003840 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3841 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003842 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003843
Chris Lattnerf20d1832005-07-30 01:40:57 +00003844 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3845
Chris Lattner80a3e942005-07-29 00:33:32 +00003846 // Now that the custom expander is done, expand the result, which is still
3847 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003848 if (Op.Val) {
3849 ExpandOp(Op, Lo, Hi);
3850 break;
3851 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003852 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003853
Chris Lattner4e6c7462005-01-08 19:27:05 +00003854 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003855 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003856 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003857 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003858 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003859
Chris Lattner4e6c7462005-01-08 19:27:05 +00003860 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003861 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003862 SDOperand Op;
3863 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3864 case Expand: assert(0 && "cannot expand FP!");
3865 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3866 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3867 }
3868
3869 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3870
3871 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003872 if (Op.Val) {
3873 ExpandOp(Op, Lo, Hi);
3874 break;
3875 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003876 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003877
Chris Lattner4e6c7462005-01-08 19:27:05 +00003878 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003879 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003880 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003881 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003882 break;
3883
Evan Cheng05a2d562006-01-09 18:31:59 +00003884 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003885 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003886 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003887 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003888 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003889 Op = TLI.LowerOperation(Op, DAG);
3890 if (Op.Val) {
3891 // Now that the custom expander is done, expand the result, which is
3892 // still VT.
3893 ExpandOp(Op, Lo, Hi);
3894 break;
3895 }
3896 }
3897
Chris Lattnere34b3962005-01-19 04:19:40 +00003898 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003899 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003900 break;
Chris Lattner47599822005-04-02 03:38:53 +00003901
3902 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003903 TargetLowering::LegalizeAction Action =
3904 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3905 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3906 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003907 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003908 break;
3909 }
3910
Chris Lattnere34b3962005-01-19 04:19:40 +00003911 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003912 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003913 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003914 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003915
Evan Cheng05a2d562006-01-09 18:31:59 +00003916 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003917 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003918 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003919 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003920 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003921 Op = TLI.LowerOperation(Op, DAG);
3922 if (Op.Val) {
3923 // Now that the custom expander is done, expand the result, which is
3924 // still VT.
3925 ExpandOp(Op, Lo, Hi);
3926 break;
3927 }
3928 }
3929
Chris Lattnere34b3962005-01-19 04:19:40 +00003930 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003931 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003932 break;
Chris Lattner47599822005-04-02 03:38:53 +00003933
3934 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003935 TargetLowering::LegalizeAction Action =
3936 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3937 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3938 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003939 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003940 break;
3941 }
3942
Chris Lattnere34b3962005-01-19 04:19:40 +00003943 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003944 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003945 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003946 }
3947
3948 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003949 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003950 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003951 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003952 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003953 Op = TLI.LowerOperation(Op, DAG);
3954 if (Op.Val) {
3955 // Now that the custom expander is done, expand the result, which is
3956 // still VT.
3957 ExpandOp(Op, Lo, Hi);
3958 break;
3959 }
3960 }
3961
Chris Lattnere34b3962005-01-19 04:19:40 +00003962 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003963 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003964 break;
Chris Lattner47599822005-04-02 03:38:53 +00003965
3966 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003967 TargetLowering::LegalizeAction Action =
3968 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3969 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3970 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003971 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003972 break;
3973 }
3974
Chris Lattnere34b3962005-01-19 04:19:40 +00003975 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003976 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003977 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003978 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003979
Misha Brukmanedf128a2005-04-21 22:36:52 +00003980 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003981 case ISD::SUB: {
3982 // If the target wants to custom expand this, let them.
3983 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3984 TargetLowering::Custom) {
3985 Op = TLI.LowerOperation(Op, DAG);
3986 if (Op.Val) {
3987 ExpandOp(Op, Lo, Hi);
3988 break;
3989 }
3990 }
3991
3992 // Expand the subcomponents.
3993 SDOperand LHSL, LHSH, RHSL, RHSH;
3994 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3995 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00003996 std::vector<MVT::ValueType> VTs;
3997 std::vector<SDOperand> LoOps, HiOps;
3998 VTs.push_back(LHSL.getValueType());
3999 VTs.push_back(MVT::Flag);
4000 LoOps.push_back(LHSL);
4001 LoOps.push_back(RHSL);
4002 HiOps.push_back(LHSH);
4003 HiOps.push_back(RHSH);
4004 if (Node->getOpcode() == ISD::ADD) {
4005 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4006 HiOps.push_back(Lo.getValue(1));
4007 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4008 } else {
4009 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4010 HiOps.push_back(Lo.getValue(1));
4011 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4012 }
Chris Lattner84f67882005-01-20 18:52:28 +00004013 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004014 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004015 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004016 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004017 SDOperand LL, LH, RL, RH;
4018 ExpandOp(Node->getOperand(0), LL, LH);
4019 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004020 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4021 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4022 // extended the sign bit of the low half through the upper half, and if so
4023 // emit a MULHS instead of the alternate sequence that is valid for any
4024 // i64 x i64 multiply.
4025 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4026 // is RH an extension of the sign bit of RL?
4027 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4028 RH.getOperand(1).getOpcode() == ISD::Constant &&
4029 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4030 // is LH an extension of the sign bit of LL?
4031 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4032 LH.getOperand(1).getOpcode() == ISD::Constant &&
4033 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4034 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4035 } else {
4036 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4037 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4038 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4039 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4040 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4041 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004042 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4043 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004044 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004045 }
4046 break;
4047 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004048 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4049 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4050 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4051 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004052 }
4053
Chris Lattner83397362005-12-21 18:02:52 +00004054 // Make sure the resultant values have been legalized themselves, unless this
4055 // is a type that requires multi-step expansion.
4056 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4057 Lo = LegalizeOp(Lo);
4058 Hi = LegalizeOp(Hi);
4059 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004060
4061 // Remember in a map if the values will be reused later.
4062 bool isNew =
4063 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4064 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004065}
4066
Chris Lattnerc7029802006-03-18 01:44:44 +00004067/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4068/// two smaller values of MVT::Vector type.
4069void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4070 SDOperand &Hi) {
4071 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4072 SDNode *Node = Op.Val;
4073 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4074 assert(NumElements > 1 && "Cannot split a single element vector!");
4075 unsigned NewNumElts = NumElements/2;
4076 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4077 SDOperand TypeNode = *(Node->op_end()-1);
4078
4079 // See if we already split it.
4080 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4081 = SplitNodes.find(Op);
4082 if (I != SplitNodes.end()) {
4083 Lo = I->second.first;
4084 Hi = I->second.second;
4085 return;
4086 }
4087
4088 switch (Node->getOpcode()) {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004089 default: assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004090 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004091 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4092 LoOps.push_back(NewNumEltsNode);
4093 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004094 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004095
4096 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4097 HiOps.push_back(NewNumEltsNode);
4098 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004099 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004100 break;
4101 }
4102 case ISD::VADD:
4103 case ISD::VSUB:
4104 case ISD::VMUL:
4105 case ISD::VSDIV:
4106 case ISD::VUDIV:
4107 case ISD::VAND:
4108 case ISD::VOR:
4109 case ISD::VXOR: {
4110 SDOperand LL, LH, RL, RH;
4111 SplitVectorOp(Node->getOperand(0), LL, LH);
4112 SplitVectorOp(Node->getOperand(1), RL, RH);
4113
4114 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4115 NewNumEltsNode, TypeNode);
4116 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4117 NewNumEltsNode, TypeNode);
4118 break;
4119 }
4120 case ISD::VLOAD: {
4121 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4122 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4123 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4124
4125 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4126 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4127 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4128 getIntPtrConstant(IncrementSize));
4129 // FIXME: This creates a bogus srcvalue!
4130 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4131
4132 // Build a factor node to remember that this load is independent of the
4133 // other one.
4134 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4135 Hi.getValue(1));
4136
4137 // Remember that we legalized the chain.
4138 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4139 if (!TLI.isLittleEndian())
4140 std::swap(Lo, Hi);
4141 break;
4142 }
4143 }
4144
4145 // Remember in a map if the values will be reused later.
4146 bool isNew =
4147 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4148 assert(isNew && "Value already expanded?!?");
4149}
4150
4151
4152/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4153/// equivalent operation that returns a scalar (e.g. F32) or packed value
4154/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4155/// type for the result.
4156SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4157 MVT::ValueType NewVT) {
4158 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4159 SDNode *Node = Op.Val;
4160
4161 // See if we already packed it.
4162 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4163 if (I != PackedNodes.end()) return I->second;
4164
4165 SDOperand Result;
4166 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004167 default:
4168 Node->dump(); std::cerr << "\n";
4169 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004170 case ISD::VADD:
4171 case ISD::VSUB:
4172 case ISD::VMUL:
4173 case ISD::VSDIV:
4174 case ISD::VUDIV:
4175 case ISD::VAND:
4176 case ISD::VOR:
4177 case ISD::VXOR:
4178 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4179 NewVT,
4180 PackVectorOp(Node->getOperand(0), NewVT),
4181 PackVectorOp(Node->getOperand(1), NewVT));
4182 break;
4183 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004184 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4185 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004186
Chris Lattner4794a6b2006-03-19 00:07:49 +00004187 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004188
4189 // Remember that we legalized the chain.
4190 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4191 break;
4192 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004193 case ISD::VBUILD_VECTOR:
Chris Lattnerc7029802006-03-18 01:44:44 +00004194 if (!MVT::isVector(NewVT)) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004195 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004196 Result = Node->getOperand(0);
4197 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004198 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004199 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004200 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004201 }
4202 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004203 case ISD::VINSERT_VECTOR_ELT:
4204 if (!MVT::isVector(NewVT)) {
4205 // Returning a scalar? Must be the inserted element.
4206 Result = Node->getOperand(1);
4207 } else {
4208 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4209 PackVectorOp(Node->getOperand(0), NewVT),
4210 Node->getOperand(1), Node->getOperand(2));
4211 }
4212 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004213 }
4214
4215 if (TLI.isTypeLegal(NewVT))
4216 Result = LegalizeOp(Result);
4217 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4218 assert(isNew && "Value already packed?");
4219 return Result;
4220}
4221
Chris Lattner3e928bb2005-01-07 07:47:09 +00004222
4223// SelectionDAG::Legalize - This is the entry point for the file.
4224//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004225void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004226 /// run - This is the main entry point to this class.
4227 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004228 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004229}
4230