blob: 370772faab111fd2c56793389a34961fde246430 [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 Lattnerce872152006-03-19 06:31:19 +0000163 SDOperand CreateStackTemporary(MVT::ValueType VT);
164
Chris Lattner77e77a62005-01-21 06:05:23 +0000165 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
166 SDOperand &Hi);
167 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
168 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000169
Chris Lattner35481892005-12-23 00:16:34 +0000170 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000171 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000172 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand LegalOp,
174 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000175 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
176 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000177 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
178 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000179
Chris Lattner456a93a2006-01-28 07:39:30 +0000180 SDOperand ExpandBSWAP(SDOperand Op);
181 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000182 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
183 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000184 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
185 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000186
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187 SDOperand getIntPtrConstant(uint64_t Val) {
188 return DAG.getConstant(Val, TLI.getPointerTy());
189 }
190};
191}
192
Chris Lattnerc7029802006-03-18 01:44:44 +0000193/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
194/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000195static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000196 switch (VecOp) {
197 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000198 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
199 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
200 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
201 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
202 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
203 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
204 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
205 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000206 }
207}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000208
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000209SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
210 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
211 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000212 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000213 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000214}
215
Chris Lattner32fca002005-10-06 01:20:27 +0000216/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
217/// not been visited yet and if all of its operands have already been visited.
218static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
219 std::map<SDNode*, unsigned> &Visited) {
220 if (++Visited[N] != N->getNumOperands())
221 return; // Haven't visited all operands yet
222
223 Order.push_back(N);
224
225 if (N->hasOneUse()) { // Tail recurse in common case.
226 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
227 return;
228 }
229
230 // Now that we have N in, add anything that uses it if all of their operands
231 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000232 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
233 ComputeTopDownOrdering(*UI, Order, Visited);
234}
235
Chris Lattner1618beb2005-07-29 00:11:56 +0000236
Chris Lattner3e928bb2005-01-07 07:47:09 +0000237void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000238 LastCALLSEQ_END = DAG.getEntryNode();
239 IsLegalizingCall = false;
240
Chris Lattnerab510a72005-10-02 17:49:46 +0000241 // The legalize process is inherently a bottom-up recursive process (users
242 // legalize their uses before themselves). Given infinite stack space, we
243 // could just start legalizing on the root and traverse the whole graph. In
244 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000245 // blocks. To avoid this problem, compute an ordering of the nodes where each
246 // node is only legalized after all of its operands are legalized.
247 std::map<SDNode*, unsigned> Visited;
248 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000249
Chris Lattner32fca002005-10-06 01:20:27 +0000250 // Compute ordering from all of the leaves in the graphs, those (like the
251 // entry node) that have no operands.
252 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
253 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000254 if (I->getNumOperands() == 0) {
255 Visited[I] = 0 - 1U;
256 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000257 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000258 }
259
Chris Lattnerde202b32005-11-09 23:47:37 +0000260 assert(Order.size() == Visited.size() &&
261 Order.size() ==
262 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000263 "Error: DAG is cyclic!");
264 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000265
Chris Lattnerc7029802006-03-18 01:44:44 +0000266 for (unsigned i = 0, e = Order.size(); i != e; ++i)
267 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000268
269 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000271 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
272 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273
274 ExpandedNodes.clear();
275 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000276 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000277 SplitNodes.clear();
278 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279
280 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000281 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000282}
283
Chris Lattner6831a812006-02-13 09:18:02 +0000284
285/// FindCallEndFromCallStart - Given a chained node that is part of a call
286/// sequence, find the CALLSEQ_END node that terminates the call sequence.
287static SDNode *FindCallEndFromCallStart(SDNode *Node) {
288 if (Node->getOpcode() == ISD::CALLSEQ_END)
289 return Node;
290 if (Node->use_empty())
291 return 0; // No CallSeqEnd
292
293 // The chain is usually at the end.
294 SDOperand TheChain(Node, Node->getNumValues()-1);
295 if (TheChain.getValueType() != MVT::Other) {
296 // Sometimes it's at the beginning.
297 TheChain = SDOperand(Node, 0);
298 if (TheChain.getValueType() != MVT::Other) {
299 // Otherwise, hunt for it.
300 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
301 if (Node->getValueType(i) == MVT::Other) {
302 TheChain = SDOperand(Node, i);
303 break;
304 }
305
306 // Otherwise, we walked into a node without a chain.
307 if (TheChain.getValueType() != MVT::Other)
308 return 0;
309 }
310 }
311
312 for (SDNode::use_iterator UI = Node->use_begin(),
313 E = Node->use_end(); UI != E; ++UI) {
314
315 // Make sure to only follow users of our token chain.
316 SDNode *User = *UI;
317 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
318 if (User->getOperand(i) == TheChain)
319 if (SDNode *Result = FindCallEndFromCallStart(User))
320 return Result;
321 }
322 return 0;
323}
324
325/// FindCallStartFromCallEnd - Given a chained node that is part of a call
326/// sequence, find the CALLSEQ_START node that initiates the call sequence.
327static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
328 assert(Node && "Didn't find callseq_start for a call??");
329 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
330
331 assert(Node->getOperand(0).getValueType() == MVT::Other &&
332 "Node doesn't have a token chain argument!");
333 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
334}
335
336/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
337/// see if any uses can reach Dest. If no dest operands can get to dest,
338/// legalize them, legalize ourself, and return false, otherwise, return true.
339bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
340 SDNode *Dest) {
341 if (N == Dest) return true; // N certainly leads to Dest :)
342
343 // If the first result of this node has been already legalized, then it cannot
344 // reach N.
345 switch (getTypeAction(N->getValueType(0))) {
346 case Legal:
347 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
348 break;
349 case Promote:
350 if (PromotedNodes.count(SDOperand(N, 0))) return false;
351 break;
352 case Expand:
353 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
354 break;
355 }
356
357 // Okay, this node has not already been legalized. Check and legalize all
358 // operands. If none lead to Dest, then we can legalize this node.
359 bool OperandsLeadToDest = false;
360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
361 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
362 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
363
364 if (OperandsLeadToDest) return true;
365
366 // Okay, this node looks safe, legalize it and return false.
367 switch (getTypeAction(N->getValueType(0))) {
368 case Legal:
369 LegalizeOp(SDOperand(N, 0));
370 break;
371 case Promote:
372 PromoteOp(SDOperand(N, 0));
373 break;
374 case Expand: {
375 SDOperand X, Y;
376 ExpandOp(SDOperand(N, 0), X, Y);
377 break;
378 }
379 }
380 return false;
381}
382
Chris Lattnerc7029802006-03-18 01:44:44 +0000383/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
384/// appropriate for its type.
385void SelectionDAGLegalize::HandleOp(SDOperand Op) {
386 switch (getTypeAction(Op.getValueType())) {
387 default: assert(0 && "Bad type action!");
388 case Legal: LegalizeOp(Op); break;
389 case Promote: PromoteOp(Op); break;
390 case Expand:
391 if (Op.getValueType() != MVT::Vector) {
392 SDOperand X, Y;
393 ExpandOp(Op, X, Y);
394 } else {
395 SDNode *N = Op.Val;
396 unsigned NumOps = N->getNumOperands();
397 unsigned NumElements =
398 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
399 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
400 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
401 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
402 // In the common case, this is a legal vector type, convert it to the
403 // packed operation and type now.
404 PackVectorOp(Op, PackedVT);
405 } else if (NumElements == 1) {
406 // Otherwise, if this is a single element vector, convert it to a
407 // scalar operation.
408 PackVectorOp(Op, EVT);
409 } else {
410 // Otherwise, this is a multiple element vector that isn't supported.
411 // Split it in half and legalize both parts.
412 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000413 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000414 }
415 }
416 break;
417 }
418}
Chris Lattner6831a812006-02-13 09:18:02 +0000419
420
Chris Lattnerc7029802006-03-18 01:44:44 +0000421/// LegalizeOp - We know that the specified value has a legal type.
422/// Recursively ensure that the operands have legal types, then return the
423/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000424SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000425 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000426 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000427 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000428
Chris Lattner3e928bb2005-01-07 07:47:09 +0000429 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000430 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000431 if (Node->getNumValues() > 1) {
432 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000433 if (getTypeAction(Node->getValueType(i)) != Legal) {
434 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000435 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000436 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000437 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000438 }
439 }
440
Chris Lattner45982da2005-05-12 16:53:42 +0000441 // Note that LegalizeOp may be reentered even from single-use nodes, which
442 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000443 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
444 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000445
Nate Begeman9373a812005-08-10 20:51:12 +0000446 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000447 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000448 bool isCustom = false;
449
Chris Lattner3e928bb2005-01-07 07:47:09 +0000450 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000451 case ISD::FrameIndex:
452 case ISD::EntryToken:
453 case ISD::Register:
454 case ISD::BasicBlock:
455 case ISD::TargetFrameIndex:
456 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000457 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000458 case ISD::TargetConstantPool:
459 case ISD::TargetGlobalAddress:
460 case ISD::TargetExternalSymbol:
461 case ISD::VALUETYPE:
462 case ISD::SRCVALUE:
463 case ISD::STRING:
464 case ISD::CONDCODE:
465 // Primitives must all be legal.
466 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
467 "This must be legal!");
468 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000469 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000470 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
471 // If this is a target node, legalize it by legalizing the operands then
472 // passing it through.
473 std::vector<SDOperand> Ops;
474 bool Changed = false;
475 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
476 Ops.push_back(LegalizeOp(Node->getOperand(i)));
477 Changed = Changed || Node->getOperand(i) != Ops.back();
478 }
479 if (Changed)
480 if (Node->getNumValues() == 1)
481 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
482 else {
483 std::vector<MVT::ValueType> VTs(Node->value_begin(),
484 Node->value_end());
485 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
486 }
487
488 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
489 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
490 return Result.getValue(Op.ResNo);
491 }
492 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000493 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
494 assert(0 && "Do not know how to legalize this operator!");
495 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000496 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000497 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000498 case ISD::ConstantPool: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000499 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
500 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000501 case TargetLowering::Custom:
502 Tmp1 = TLI.LowerOperation(Op, DAG);
503 if (Tmp1.Val) Result = Tmp1;
504 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000505 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000506 break;
507 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000508 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000509 case ISD::AssertSext:
510 case ISD::AssertZext:
511 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000513 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000514 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000515 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000516 Result = Node->getOperand(Op.ResNo);
517 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000518 case ISD::CopyFromReg:
519 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000520 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000521 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000522 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000523 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000524 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000525 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000526 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
528 } else {
529 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
530 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000531 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
532 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000533 // Since CopyFromReg produces two values, make sure to remember that we
534 // legalized both of them.
535 AddLegalizedOperand(Op.getValue(0), Result);
536 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
537 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000538 case ISD::UNDEF: {
539 MVT::ValueType VT = Op.getValueType();
540 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000541 default: assert(0 && "This action is not supported yet!");
542 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000543 if (MVT::isInteger(VT))
544 Result = DAG.getConstant(0, VT);
545 else if (MVT::isFloatingPoint(VT))
546 Result = DAG.getConstantFP(0, VT);
547 else
548 assert(0 && "Unknown value type!");
549 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000550 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000551 break;
552 }
553 break;
554 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000555
556 case ISD::LOCATION:
557 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
558 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
559
560 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
561 case TargetLowering::Promote:
562 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000563 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000564 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000565 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
566 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
567
568 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
569 const std::string &FName =
570 cast<StringSDNode>(Node->getOperand(3))->getValue();
571 const std::string &DirName =
572 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000573 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000574
Jim Laskeye81aecb2005-12-21 20:51:37 +0000575 std::vector<SDOperand> Ops;
576 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000577 SDOperand LineOp = Node->getOperand(1);
578 SDOperand ColOp = Node->getOperand(2);
579
580 if (useDEBUG_LOC) {
581 Ops.push_back(LineOp); // line #
582 Ops.push_back(ColOp); // col #
583 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
584 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
585 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000586 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
587 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000588 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
589 Ops.push_back(DAG.getConstant(ID, MVT::i32));
590 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
591 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000592 } else {
593 Result = Tmp1; // chain
594 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000595 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000596 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000597 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000598 if (Tmp1 != Node->getOperand(0) ||
599 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner36ce6912005-11-29 06:21:05 +0000600 std::vector<SDOperand> Ops;
601 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000602 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
603 Ops.push_back(Node->getOperand(1)); // line # must be legal.
604 Ops.push_back(Node->getOperand(2)); // col # must be legal.
605 } else {
606 // Otherwise promote them.
607 Ops.push_back(PromoteOp(Node->getOperand(1)));
608 Ops.push_back(PromoteOp(Node->getOperand(2)));
609 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000610 Ops.push_back(Node->getOperand(3)); // filename must be legal.
611 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000612 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner36ce6912005-11-29 06:21:05 +0000613 }
614 break;
615 }
616 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000617
618 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000619 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000620 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000621 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000622 case TargetLowering::Legal:
623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
624 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
625 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
626 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000628 break;
629 }
630 break;
631
632 case ISD::DEBUG_LABEL:
633 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
634 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000635 default: assert(0 && "This action is not supported yet!");
636 case TargetLowering::Legal:
637 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
638 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000640 break;
641 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000642 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000643
Chris Lattner3e928bb2005-01-07 07:47:09 +0000644 case ISD::Constant:
645 // We know we don't need to expand constants here, constants only have one
646 // value and we check that it is fine above.
647
648 // FIXME: Maybe we should handle things like targets that don't support full
649 // 32-bit immediates?
650 break;
651 case ISD::ConstantFP: {
652 // Spill FP immediates to the constant pool if the target cannot directly
653 // codegen them. Targets often have some immediate values that can be
654 // efficiently generated into an FP register without a load. We explicitly
655 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000656 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
657
658 // Check to see if this FP immediate is already legal.
659 bool isLegal = false;
660 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
661 E = TLI.legal_fpimm_end(); I != E; ++I)
662 if (CFP->isExactlyValue(*I)) {
663 isLegal = true;
664 break;
665 }
666
Chris Lattner3181a772006-01-29 06:26:56 +0000667 // If this is a legal constant, turn it into a TargetConstantFP node.
668 if (isLegal) {
669 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
670 break;
671 }
672
673 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
674 default: assert(0 && "This action is not supported yet!");
675 case TargetLowering::Custom:
676 Tmp3 = TLI.LowerOperation(Result, DAG);
677 if (Tmp3.Val) {
678 Result = Tmp3;
679 break;
680 }
681 // FALLTHROUGH
682 case TargetLowering::Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000683 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000684 bool Extend = false;
685
Chris Lattner456a93a2006-01-28 07:39:30 +0000686 // If a FP immediate is precise when represented as a float and if the
687 // target can do an extending load from float to double, we put it into
688 // the constant pool as a float, even if it's is statically typed as a
689 // double.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000690 MVT::ValueType VT = CFP->getValueType(0);
691 bool isDouble = VT == MVT::f64;
692 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
693 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000694 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
695 // Only do this if the target has a native EXTLOAD instruction from
696 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000697 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000698 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
699 VT = MVT::f32;
700 Extend = true;
701 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000702
Chris Lattner456a93a2006-01-28 07:39:30 +0000703 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000704 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000705 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
706 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000707 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000708 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
709 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000710 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000711 }
712 break;
713 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000714 case ISD::TokenFactor:
715 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000716 Tmp1 = LegalizeOp(Node->getOperand(0));
717 Tmp2 = LegalizeOp(Node->getOperand(1));
718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
719 } else if (Node->getNumOperands() == 3) {
720 Tmp1 = LegalizeOp(Node->getOperand(0));
721 Tmp2 = LegalizeOp(Node->getOperand(1));
722 Tmp3 = LegalizeOp(Node->getOperand(2));
723 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000724 } else {
725 std::vector<SDOperand> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000726 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000727 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
728 Ops.push_back(LegalizeOp(Node->getOperand(i)));
729 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000730 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000731 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000732
Chris Lattnerb2827b02006-03-19 00:52:58 +0000733 case ISD::BUILD_VECTOR:
734 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000735 default: assert(0 && "This action is not supported yet!");
736 case TargetLowering::Custom:
737 Tmp3 = TLI.LowerOperation(Result, DAG);
738 if (Tmp3.Val) {
739 Result = Tmp3;
740 break;
741 }
742 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000743 case TargetLowering::Expand:
744 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000745 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000746 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000747 break;
748 case ISD::INSERT_VECTOR_ELT:
749 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
750 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
751 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
752 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
753
754 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
755 Node->getValueType(0))) {
756 default: assert(0 && "This action is not supported yet!");
757 case TargetLowering::Legal:
758 break;
759 case TargetLowering::Custom:
760 Tmp3 = TLI.LowerOperation(Result, DAG);
761 if (Tmp3.Val) {
762 Result = Tmp3;
763 break;
764 }
765 // FALLTHROUGH
766 case TargetLowering::Expand: {
767 // If the target doesn't support this, we have to spill the input vector
768 // to a temporary stack slot, update the element, then reload it. This is
769 // badness. We could also load the value into a vector register (either
770 // with a "move to register" or "extload into register" instruction, then
771 // permute it into place, if the idx is a constant and if the idx is
772 // supported by the target.
773 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
774 break;
775 }
776 }
777 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000778 case ISD::SCALAR_TO_VECTOR:
779 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
780 Result = DAG.UpdateNodeOperands(Result, Tmp1);
781 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
782 Node->getValueType(0))) {
783 default: assert(0 && "This action is not supported yet!");
784 case TargetLowering::Legal:
785 break;
786 case TargetLowering::Expand: {
787 // If the target doesn't support this, store the value to a temporary
788 // stack slot, then EXTLOAD the vector back out.
789 SDOperand StackPtr =
790 CreateStackTemporary(Node->getOperand(0).getValueType());
791 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
792 Node->getOperand(0), StackPtr,
793 DAG.getSrcValue(NULL));
794 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
795 DAG.getSrcValue(NULL),
796 Node->getOperand(0).getValueType());
797 break;
798 }
799 }
800 break;
Chris Lattner6831a812006-02-13 09:18:02 +0000801 case ISD::CALLSEQ_START: {
802 SDNode *CallEnd = FindCallEndFromCallStart(Node);
803
804 // Recursively Legalize all of the inputs of the call end that do not lead
805 // to this call start. This ensures that any libcalls that need be inserted
806 // are inserted *before* the CALLSEQ_START.
807 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
808 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
809
810 // Now that we legalized all of the inputs (which may have inserted
811 // libcalls) create the new CALLSEQ_START node.
812 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
813
814 // Merge in the last call, to ensure that this call start after the last
815 // call ended.
816 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
817 Tmp1 = LegalizeOp(Tmp1);
818
819 // Do not try to legalize the target-specific arguments (#1+).
820 if (Tmp1 != Node->getOperand(0)) {
821 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
822 Ops[0] = Tmp1;
823 Result = DAG.UpdateNodeOperands(Result, Ops);
824 }
825
826 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +0000827 AddLegalizedOperand(Op.getValue(0), Result);
828 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
829 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
830
Chris Lattner6831a812006-02-13 09:18:02 +0000831 // Now that the callseq_start and all of the non-call nodes above this call
832 // sequence have been legalized, legalize the call itself. During this
833 // process, no libcalls can/will be inserted, guaranteeing that no calls
834 // can overlap.
835 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
836 SDOperand InCallSEQ = LastCALLSEQ_END;
837 // Note that we are selecting this call!
838 LastCALLSEQ_END = SDOperand(CallEnd, 0);
839 IsLegalizingCall = true;
840
841 // Legalize the call, starting from the CALLSEQ_END.
842 LegalizeOp(LastCALLSEQ_END);
843 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
844 return Result;
845 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000846 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +0000847 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
848 // will cause this node to be legalized as well as handling libcalls right.
849 if (LastCALLSEQ_END.Val != Node) {
850 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
851 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
852 assert(I != LegalizedNodes.end() &&
853 "Legalizing the call start should have legalized this node!");
854 return I->second;
855 }
856
857 // Otherwise, the call start has been legalized and everything is going
858 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000859 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +0000860 // Do not try to legalize the target-specific arguments (#1+), except for
861 // an optional flag input.
862 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
863 if (Tmp1 != Node->getOperand(0)) {
864 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
865 Ops[0] = Tmp1;
866 Result = DAG.UpdateNodeOperands(Result, Ops);
867 }
868 } else {
869 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
870 if (Tmp1 != Node->getOperand(0) ||
871 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
872 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
873 Ops[0] = Tmp1;
874 Ops.back() = Tmp2;
875 Result = DAG.UpdateNodeOperands(Result, Ops);
876 }
Chris Lattner6a542892006-01-24 05:48:21 +0000877 }
Chris Lattner4b653a02006-02-14 00:55:02 +0000878 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +0000879 // This finishes up call legalization.
880 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +0000881
882 // If the CALLSEQ_END node has a flag, remember that we legalized it.
883 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
884 if (Node->getNumValues() == 2)
885 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
886 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000887 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +0000888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
889 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
890 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000891 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000892
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000893 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +0000894 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +0000895 switch (TLI.getOperationAction(Node->getOpcode(),
896 Node->getValueType(0))) {
897 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +0000898 case TargetLowering::Expand: {
899 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
900 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
901 " not tell us which reg is the stack pointer!");
902 SDOperand Chain = Tmp1.getOperand(0);
903 SDOperand Size = Tmp2.getOperand(1);
904 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
905 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
906 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000907 Tmp1 = LegalizeOp(Tmp1);
908 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000909 break;
910 }
911 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +0000912 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
913 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000914 Tmp1 = LegalizeOp(Tmp3);
915 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +0000916 }
Chris Lattner903d2782006-01-15 08:54:32 +0000917 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000918 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +0000919 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000920 }
Chris Lattner903d2782006-01-15 08:54:32 +0000921 // Since this op produce two values, make sure to remember that we
922 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000923 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
924 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +0000925 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +0000926 }
Chris Lattnerce7518c2006-01-26 22:24:51 +0000927 case ISD::INLINEASM:
928 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
929 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000930 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattnerce7518c2006-01-26 22:24:51 +0000931 Tmp2 = Tmp3 = SDOperand(0, 0);
932 else
933 Tmp3 = LegalizeOp(Tmp2);
934
935 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
936 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
937 Ops[0] = Tmp1;
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000938 if (Tmp3.Val) Ops.back() = Tmp3;
939 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattnerce7518c2006-01-26 22:24:51 +0000940 }
941
942 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000943 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +0000944 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
945 return Result.getValue(Op.ResNo);
Chris Lattnerc7af1792005-01-07 22:12:08 +0000946 case ISD::BR:
947 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000948 // Ensure that libcalls are emitted before a branch.
949 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
950 Tmp1 = LegalizeOp(Tmp1);
951 LastCALLSEQ_END = DAG.getEntryNode();
952
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000953 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +0000954 break;
955
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000956 case ISD::BRCOND:
957 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +0000958 // Ensure that libcalls are emitted before a return.
959 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
960 Tmp1 = LegalizeOp(Tmp1);
961 LastCALLSEQ_END = DAG.getEntryNode();
962
Chris Lattner47e92232005-01-18 19:27:06 +0000963 switch (getTypeAction(Node->getOperand(1).getValueType())) {
964 case Expand: assert(0 && "It's impossible to expand bools");
965 case Legal:
966 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
967 break;
968 case Promote:
969 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
970 break;
971 }
Chris Lattner456a93a2006-01-28 07:39:30 +0000972
973 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000975
976 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
977 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +0000978 case TargetLowering::Legal: break;
979 case TargetLowering::Custom:
980 Tmp1 = TLI.LowerOperation(Result, DAG);
981 if (Tmp1.Val) Result = Tmp1;
982 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000983 case TargetLowering::Expand:
984 // Expand brcond's setcc into its constituent parts and create a BR_CC
985 // Node.
986 if (Tmp2.getOpcode() == ISD::SETCC) {
987 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
988 Tmp2.getOperand(0), Tmp2.getOperand(1),
989 Node->getOperand(2));
990 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000991 // Make sure the condition is either zero or one. It may have been
992 // promoted from something else.
Chris Lattner19c5c4c2006-01-31 05:04:52 +0000993 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
994 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
995 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner550b1e52005-08-21 18:03:09 +0000996
Nate Begeman7cbd5252005-08-16 19:49:35 +0000997 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
998 DAG.getCondCode(ISD::SETNE), Tmp2,
999 DAG.getConstant(0, Tmp2.getValueType()),
1000 Node->getOperand(2));
1001 }
1002 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001003 }
1004 break;
1005 case ISD::BR_CC:
1006 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001007 // Ensure that libcalls are emitted before a branch.
1008 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1009 Tmp1 = LegalizeOp(Tmp1);
1010 LastCALLSEQ_END = DAG.getEntryNode();
1011
Nate Begeman750ac1b2006-02-01 07:19:44 +00001012 Tmp2 = Node->getOperand(2); // LHS
1013 Tmp3 = Node->getOperand(3); // RHS
1014 Tmp4 = Node->getOperand(1); // CC
1015
1016 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1017
1018 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1019 // the LHS is a legal SETCC itself. In this case, we need to compare
1020 // the result against zero to select between true and false values.
1021 if (Tmp3.Val == 0) {
1022 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1023 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001024 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001025
1026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1027 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001028
Chris Lattner181b7a32005-12-17 23:46:46 +00001029 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1030 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001031 case TargetLowering::Legal: break;
1032 case TargetLowering::Custom:
1033 Tmp4 = TLI.LowerOperation(Result, DAG);
1034 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001035 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001036 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001037 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001038 case ISD::LOAD: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1040 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001041
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001042 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001043 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1044 Tmp2 = Result.getValue(0);
1045 Tmp3 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001046
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001047 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1048 default: assert(0 && "This action is not supported yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001049 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001050 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001051 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001052 if (Tmp1.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001053 Tmp2 = LegalizeOp(Tmp1);
1054 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001055 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001056 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001057 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001058 // Since loads produce two values, make sure to remember that we
1059 // legalized both of them.
1060 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1061 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1062 return Op.ResNo ? Tmp3 : Tmp2;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001063 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001064 case ISD::EXTLOAD:
1065 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +00001066 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001067 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1068 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +00001069
Chris Lattner5f056bf2005-07-10 01:55:33 +00001070 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +00001071 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +00001072 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +00001073 case TargetLowering::Promote:
1074 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1076 DAG.getValueType(MVT::i8));
1077 Tmp1 = Result.getValue(0);
1078 Tmp2 = Result.getValue(1);
1079 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001080 case TargetLowering::Custom:
1081 isCustom = true;
1082 // FALLTHROUGH
Chris Lattner01ff7212005-04-10 22:54:25 +00001083 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001084 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1085 Node->getOperand(3));
1086 Tmp1 = Result.getValue(0);
1087 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001088
1089 if (isCustom) {
1090 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1091 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001092 Tmp1 = LegalizeOp(Tmp3);
1093 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001094 }
1095 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001096 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001097 case TargetLowering::Expand:
Chris Lattner69a889e2005-12-20 00:53:54 +00001098 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001099 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1100 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +00001101 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001102 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1103 Tmp2 = LegalizeOp(Load.getValue(1));
1104 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001105 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001106 assert(Node->getOpcode() != ISD::EXTLOAD &&
1107 "EXTLOAD should always be supported!");
1108 // Turn the unsupported load into an EXTLOAD followed by an explicit
1109 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +00001110 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1111 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +00001112 SDOperand ValRes;
1113 if (Node->getOpcode() == ISD::SEXTLOAD)
1114 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001115 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +00001116 else
1117 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001118 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1119 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1120 break;
Chris Lattner01ff7212005-04-10 22:54:25 +00001121 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001122 // Since loads produce two values, make sure to remember that we legalized
1123 // both of them.
1124 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1125 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1126 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001127 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001128 case ISD::EXTRACT_ELEMENT: {
1129 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1130 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001131 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001132 case Legal:
1133 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1134 // 1 -> Hi
1135 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1136 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1137 TLI.getShiftAmountTy()));
1138 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1139 } else {
1140 // 0 -> Lo
1141 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1142 Node->getOperand(0));
1143 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001144 break;
1145 case Expand:
1146 // Get both the low and high parts.
1147 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1148 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1149 Result = Tmp2; // 1 -> Hi
1150 else
1151 Result = Tmp1; // 0 -> Lo
1152 break;
1153 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001154 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001155 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001156
1157 case ISD::CopyToReg:
1158 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001159
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001160 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001161 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001162 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001163 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001164 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001165 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001166 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001167 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001168 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001169 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001170 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1171 Tmp3);
1172 } else {
1173 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001174 }
1175
1176 // Since this produces two values, make sure to remember that we legalized
1177 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001178 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001179 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001180 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001181 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001182 break;
1183
1184 case ISD::RET:
1185 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001186
1187 // Ensure that libcalls are emitted before a return.
1188 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1189 Tmp1 = LegalizeOp(Tmp1);
1190 LastCALLSEQ_END = DAG.getEntryNode();
1191
Chris Lattner3e928bb2005-01-07 07:47:09 +00001192 switch (Node->getNumOperands()) {
1193 case 2: // ret val
1194 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1195 case Legal:
1196 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001198 break;
1199 case Expand: {
1200 SDOperand Lo, Hi;
1201 ExpandOp(Node->getOperand(1), Lo, Hi);
1202 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001203 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001204 }
1205 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001206 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1208 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001209 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001210 }
1211 break;
1212 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001213 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001214 break;
1215 default: { // ret <values>
1216 std::vector<SDOperand> NewValues;
1217 NewValues.push_back(Tmp1);
1218 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1219 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1220 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001221 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001222 break;
1223 case Expand: {
1224 SDOperand Lo, Hi;
1225 ExpandOp(Node->getOperand(i), Lo, Hi);
1226 NewValues.push_back(Lo);
1227 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001228 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001229 }
1230 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001231 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001232 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001233
1234 if (NewValues.size() == Node->getNumOperands())
1235 Result = DAG.UpdateNodeOperands(Result, NewValues);
1236 else
1237 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001238 break;
1239 }
1240 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001241
Chris Lattner6862dbc2006-01-29 21:02:23 +00001242 if (Result.getOpcode() == ISD::RET) {
1243 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1244 default: assert(0 && "This action is not supported yet!");
1245 case TargetLowering::Legal: break;
1246 case TargetLowering::Custom:
1247 Tmp1 = TLI.LowerOperation(Result, DAG);
1248 if (Tmp1.Val) Result = Tmp1;
1249 break;
1250 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001251 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001252 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001253 case ISD::STORE: {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001254 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1255 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1256
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001257 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner456a93a2006-01-28 07:39:30 +00001258 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner06ac6ab2006-03-15 22:19:18 +00001259 // FIXME: move this to the DAG Combiner!
Chris Lattner03c85462005-01-15 05:21:40 +00001260 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001261 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001262 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001263 } else {
1264 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001265 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001266 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001267 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1268 Node->getOperand(3));
Chris Lattner6a542892006-01-24 05:48:21 +00001269 break;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001270 }
1271
Chris Lattner3e928bb2005-01-07 07:47:09 +00001272 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1273 case Legal: {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001274 Tmp3 = LegalizeOp(Node->getOperand(1));
1275 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1276 Node->getOperand(3));
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001277
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001278 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner456a93a2006-01-28 07:39:30 +00001279 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1280 default: assert(0 && "This action is not supported yet!");
1281 case TargetLowering::Legal: break;
1282 case TargetLowering::Custom:
1283 Tmp1 = TLI.LowerOperation(Result, DAG);
1284 if (Tmp1.Val) Result = Tmp1;
1285 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001286 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001287 break;
1288 }
1289 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001290 // Truncate the value and store the result.
1291 Tmp3 = PromoteOp(Node->getOperand(1));
1292 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001293 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001294 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001295 break;
1296
Chris Lattner3e928bb2005-01-07 07:47:09 +00001297 case Expand:
Chris Lattnerc7029802006-03-18 01:44:44 +00001298 unsigned IncrementSize = 0;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001299 SDOperand Lo, Hi;
Chris Lattnerc7029802006-03-18 01:44:44 +00001300
1301 // If this is a vector type, then we have to calculate the increment as
1302 // the product of the element size in bytes, and the number of elements
1303 // in the high half of the vector.
1304 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1305 SDNode *InVal = Node->getOperand(1).Val;
1306 unsigned NumElems =
1307 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1308 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1309
1310 // Figure out if there is a Packed type corresponding to this Vector
1311 // type. If so, convert to the packed type.
1312 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1313 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1314 // Turn this into a normal store of the packed type.
1315 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1316 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1317 Node->getOperand(3));
1318 break;
1319 } else if (NumElems == 1) {
1320 // Turn this into a normal store of the scalar type.
1321 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1322 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1323 Node->getOperand(3));
1324 break;
1325 } else {
1326 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1327 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1328 }
1329 } else {
1330 ExpandOp(Node->getOperand(1), Lo, Hi);
1331 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1332 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001333
1334 if (!TLI.isLittleEndian())
1335 std::swap(Lo, Hi);
1336
Chris Lattneredb1add2005-05-11 04:51:16 +00001337 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1338 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001339 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1340 getIntPtrConstant(IncrementSize));
1341 assert(isTypeLegal(Tmp2.getValueType()) &&
1342 "Pointers must be legal!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001343 // FIXME: This sets the srcvalue of both halves to be the same, which is
1344 // wrong.
Chris Lattneredb1add2005-05-11 04:51:16 +00001345 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1346 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001347 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1348 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001349 }
1350 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001351 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001352 case ISD::PCMARKER:
1353 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001355 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001356 case ISD::STACKSAVE:
1357 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001358 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1359 Tmp1 = Result.getValue(0);
1360 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001361
Chris Lattner140d53c2006-01-13 02:50:02 +00001362 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1363 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001364 case TargetLowering::Legal: break;
1365 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001366 Tmp3 = TLI.LowerOperation(Result, DAG);
1367 if (Tmp3.Val) {
1368 Tmp1 = LegalizeOp(Tmp3);
1369 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001370 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001371 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001372 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001373 // Expand to CopyFromReg if the target set
1374 // StackPointerRegisterToSaveRestore.
1375 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001376 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001377 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001378 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001379 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001380 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1381 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001382 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001383 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001384 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001385
1386 // Since stacksave produce two values, make sure to remember that we
1387 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001388 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1389 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1390 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001391
Chris Lattner140d53c2006-01-13 02:50:02 +00001392 case ISD::STACKRESTORE:
1393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001395 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001396
1397 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1398 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001399 case TargetLowering::Legal: break;
1400 case TargetLowering::Custom:
1401 Tmp1 = TLI.LowerOperation(Result, DAG);
1402 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001403 break;
1404 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001405 // Expand to CopyToReg if the target set
1406 // StackPointerRegisterToSaveRestore.
1407 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1408 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1409 } else {
1410 Result = Tmp1;
1411 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001412 break;
1413 }
1414 break;
1415
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001416 case ISD::READCYCLECOUNTER:
1417 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001418 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001419
1420 // Since rdcc produce two values, make sure to remember that we legalized
1421 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001422 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001423 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001424 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001425
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001426 case ISD::TRUNCSTORE: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001427 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1428 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1429
Chris Lattner456a93a2006-01-28 07:39:30 +00001430 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1431 "Cannot handle illegal TRUNCSTORE yet!");
1432 Tmp2 = LegalizeOp(Node->getOperand(1));
1433
1434 // The only promote case we handle is TRUNCSTORE:i1 X into
1435 // -> TRUNCSTORE:i8 (and X, 1)
1436 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1437 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1438 TargetLowering::Promote) {
1439 // Promote the bool to a mask then store.
1440 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1441 DAG.getConstant(1, Tmp2.getValueType()));
1442 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1443 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner13d58e72005-09-10 00:20:18 +00001444
Chris Lattner456a93a2006-01-28 07:39:30 +00001445 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1446 Tmp3 != Node->getOperand(2)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001447 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1448 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001449 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001450
Chris Lattner456a93a2006-01-28 07:39:30 +00001451 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1452 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1453 default: assert(0 && "This action is not supported yet!");
1454 case TargetLowering::Legal: break;
1455 case TargetLowering::Custom:
1456 Tmp1 = TLI.LowerOperation(Result, DAG);
1457 if (Tmp1.Val) Result = Tmp1;
Chris Lattner0f69b292005-01-15 06:18:18 +00001458 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001459 }
1460 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001461 }
Chris Lattner2ee743f2005-01-14 22:08:15 +00001462 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001463 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1464 case Expand: assert(0 && "It's impossible to expand bools");
1465 case Legal:
1466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1467 break;
1468 case Promote:
1469 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1470 break;
1471 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001472 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001473 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001474
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001475 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001476
Nate Begemanb942a3d2005-08-23 04:29:48 +00001477 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001478 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001479 case TargetLowering::Legal: break;
1480 case TargetLowering::Custom: {
1481 Tmp1 = TLI.LowerOperation(Result, DAG);
1482 if (Tmp1.Val) Result = Tmp1;
1483 break;
1484 }
Nate Begeman9373a812005-08-10 20:51:12 +00001485 case TargetLowering::Expand:
1486 if (Tmp1.getOpcode() == ISD::SETCC) {
1487 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1488 Tmp2, Tmp3,
1489 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1490 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001491 // Make sure the condition is either zero or one. It may have been
1492 // promoted from something else.
Chris Lattner0e753d62006-01-30 04:22:28 +00001493 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1494 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1495 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001496 Result = DAG.getSelectCC(Tmp1,
1497 DAG.getConstant(0, Tmp1.getValueType()),
1498 Tmp2, Tmp3, ISD::SETNE);
1499 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001500 break;
1501 case TargetLowering::Promote: {
1502 MVT::ValueType NVT =
1503 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1504 unsigned ExtOp, TruncOp;
1505 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001506 ExtOp = ISD::ANY_EXTEND;
1507 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001508 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001509 ExtOp = ISD::FP_EXTEND;
1510 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001511 }
1512 // Promote each of the values to the new type.
1513 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1514 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1515 // Perform the larger operation, then round down.
1516 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1517 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1518 break;
1519 }
1520 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001521 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001522 case ISD::SELECT_CC: {
1523 Tmp1 = Node->getOperand(0); // LHS
1524 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00001525 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1526 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00001527 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00001528
Nate Begeman750ac1b2006-02-01 07:19:44 +00001529 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1530
1531 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1532 // the LHS is a legal SETCC itself. In this case, we need to compare
1533 // the result against zero to select between true and false values.
1534 if (Tmp2.Val == 0) {
1535 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1536 CC = DAG.getCondCode(ISD::SETNE);
1537 }
1538 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1539
1540 // Everything is legal, see if we should expand this op or something.
1541 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1542 default: assert(0 && "This action is not supported yet!");
1543 case TargetLowering::Legal: break;
1544 case TargetLowering::Custom:
1545 Tmp1 = TLI.LowerOperation(Result, DAG);
1546 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00001547 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001548 }
1549 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00001550 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001551 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00001552 Tmp1 = Node->getOperand(0);
1553 Tmp2 = Node->getOperand(1);
1554 Tmp3 = Node->getOperand(2);
1555 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1556
1557 // If we had to Expand the SetCC operands into a SELECT node, then it may
1558 // not always be possible to return a true LHS & RHS. In this case, just
1559 // return the value we legalized, returned in the LHS
1560 if (Tmp2.Val == 0) {
1561 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001562 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001563 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001564
Chris Lattner73e142f2006-01-30 22:43:50 +00001565 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001566 default: assert(0 && "Cannot handle this action for SETCC yet!");
1567 case TargetLowering::Custom:
1568 isCustom = true;
1569 // FALLTHROUGH.
1570 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001571 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00001572 if (isCustom) {
1573 Tmp3 = TLI.LowerOperation(Result, DAG);
1574 if (Tmp3.Val) Result = Tmp3;
1575 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001576 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001577 case TargetLowering::Promote: {
1578 // First step, figure out the appropriate operation to use.
1579 // Allow SETCC to not be supported for all legal data types
1580 // Mostly this targets FP
1581 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1582 MVT::ValueType OldVT = NewInTy;
1583
1584 // Scan for the appropriate larger type to use.
1585 while (1) {
1586 NewInTy = (MVT::ValueType)(NewInTy+1);
1587
1588 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1589 "Fell off of the edge of the integer world");
1590 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1591 "Fell off of the edge of the floating point world");
1592
1593 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00001594 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00001595 break;
1596 }
1597 if (MVT::isInteger(NewInTy))
1598 assert(0 && "Cannot promote Legal Integer SETCC yet");
1599 else {
1600 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1601 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1602 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001603 Tmp1 = LegalizeOp(Tmp1);
1604 Tmp2 = LegalizeOp(Tmp2);
1605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng433f8ac2006-01-17 19:47:13 +00001606 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001607 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00001608 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001609 case TargetLowering::Expand:
1610 // Expand a setcc node into a select_cc of the same condition, lhs, and
1611 // rhs that selects between const 1 (true) and const 0 (false).
1612 MVT::ValueType VT = Node->getValueType(0);
1613 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1614 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1615 Node->getOperand(2));
Nate Begemanb942a3d2005-08-23 04:29:48 +00001616 break;
1617 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001618 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001619 case ISD::MEMSET:
1620 case ISD::MEMCPY:
1621 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001622 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001623 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1624
1625 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1626 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1627 case Expand: assert(0 && "Cannot expand a byte!");
1628 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001629 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001630 break;
1631 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001632 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001633 break;
1634 }
1635 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001636 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001637 }
Chris Lattner272455b2005-02-02 03:44:41 +00001638
1639 SDOperand Tmp4;
1640 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001641 case Expand: {
1642 // Length is too big, just take the lo-part of the length.
1643 SDOperand HiPart;
1644 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1645 break;
1646 }
Chris Lattnere5605212005-01-28 22:29:18 +00001647 case Legal:
1648 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001649 break;
1650 case Promote:
1651 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001652 break;
1653 }
1654
1655 SDOperand Tmp5;
1656 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1657 case Expand: assert(0 && "Cannot expand this yet!");
1658 case Legal:
1659 Tmp5 = LegalizeOp(Node->getOperand(4));
1660 break;
1661 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001662 Tmp5 = PromoteOp(Node->getOperand(4));
1663 break;
1664 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001665
1666 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1667 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001668 case TargetLowering::Custom:
1669 isCustom = true;
1670 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001671 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00001673 if (isCustom) {
1674 Tmp1 = TLI.LowerOperation(Result, DAG);
1675 if (Tmp1.Val) Result = Tmp1;
1676 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001677 break;
1678 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001679 // Otherwise, the target does not support this operation. Lower the
1680 // operation to an explicit libcall as appropriate.
1681 MVT::ValueType IntPtr = TLI.getPointerTy();
1682 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1683 std::vector<std::pair<SDOperand, const Type*> > Args;
1684
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001685 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001686 if (Node->getOpcode() == ISD::MEMSET) {
1687 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattnerdca7abe2006-02-20 06:38:35 +00001688 // Extend the (previously legalized) ubyte argument to be an int value
1689 // for the call.
1690 if (Tmp3.getValueType() > MVT::i32)
1691 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1692 else
1693 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001694 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1695 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1696
1697 FnName = "memset";
1698 } else if (Node->getOpcode() == ISD::MEMCPY ||
1699 Node->getOpcode() == ISD::MEMMOVE) {
1700 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1701 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1702 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1703 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1704 } else {
1705 assert(0 && "Unknown op!");
1706 }
Chris Lattner45982da2005-05-12 16:53:42 +00001707
Chris Lattnere1bd8222005-01-11 05:57:22 +00001708 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001709 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001710 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00001711 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001712 break;
1713 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001714 }
1715 break;
1716 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001717
Chris Lattner5b359c62005-04-02 04:00:59 +00001718 case ISD::SHL_PARTS:
1719 case ISD::SRA_PARTS:
1720 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001721 std::vector<SDOperand> Ops;
1722 bool Changed = false;
1723 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1724 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1725 Changed |= Ops.back() != Node->getOperand(i);
1726 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001727 if (Changed)
1728 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001729
Evan Cheng05a2d562006-01-09 18:31:59 +00001730 switch (TLI.getOperationAction(Node->getOpcode(),
1731 Node->getValueType(0))) {
1732 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001733 case TargetLowering::Legal: break;
1734 case TargetLowering::Custom:
1735 Tmp1 = TLI.LowerOperation(Result, DAG);
1736 if (Tmp1.Val) {
1737 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00001738 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001739 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00001740 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1741 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00001742 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00001743 }
Chris Lattner269f8c02006-01-10 19:43:26 +00001744 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00001745 return RetVal;
1746 }
Evan Cheng05a2d562006-01-09 18:31:59 +00001747 break;
1748 }
1749
Chris Lattner2c8086f2005-04-02 05:00:07 +00001750 // Since these produce multiple values, make sure to remember that we
1751 // legalized all of them.
1752 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1753 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1754 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001755 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001756
1757 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001758 case ISD::ADD:
1759 case ISD::SUB:
1760 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001761 case ISD::MULHS:
1762 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001763 case ISD::UDIV:
1764 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001765 case ISD::AND:
1766 case ISD::OR:
1767 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001768 case ISD::SHL:
1769 case ISD::SRL:
1770 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001771 case ISD::FADD:
1772 case ISD::FSUB:
1773 case ISD::FMUL:
1774 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001775 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001776 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1777 case Expand: assert(0 && "Not possible");
1778 case Legal:
1779 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1780 break;
1781 case Promote:
1782 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1783 break;
1784 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001785
1786 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001787
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001788 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001789 default: assert(0 && "Operation not supported");
1790 case TargetLowering::Legal: break;
1791 case TargetLowering::Custom:
1792 Tmp1 = TLI.LowerOperation(Result, DAG);
1793 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00001794 break;
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00001795 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001796 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001797
1798 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1799 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1800 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1801 case Expand: assert(0 && "Not possible");
1802 case Legal:
1803 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1804 break;
1805 case Promote:
1806 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1807 break;
1808 }
1809
1810 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1811
1812 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1813 default: assert(0 && "Operation not supported");
1814 case TargetLowering::Custom:
1815 Tmp1 = TLI.LowerOperation(Result, DAG);
1816 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00001817 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001818 case TargetLowering::Legal: break;
1819 case TargetLowering::Expand:
Chris Lattner8f4191d2006-03-13 06:08:38 +00001820 // If this target supports fabs/fneg natively, do this efficiently.
1821 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1822 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1823 // Get the sign bit of the RHS.
1824 MVT::ValueType IVT =
1825 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1826 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1827 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1828 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1829 // Get the absolute value of the result.
1830 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1831 // Select between the nabs and abs value based on the sign bit of
1832 // the input.
1833 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1834 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1835 AbsVal),
1836 AbsVal);
1837 Result = LegalizeOp(Result);
1838 break;
1839 }
1840
1841 // Otherwise, do bitwise ops!
1842
1843 // copysign -> copysignf/copysign libcall.
Chris Lattnera09f8482006-03-05 05:09:38 +00001844 const char *FnName;
1845 if (Node->getValueType(0) == MVT::f32) {
1846 FnName = "copysignf";
1847 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1848 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1849 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1850 } else {
1851 FnName = "copysign";
1852 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1853 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1854 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1855 }
1856 SDOperand Dummy;
1857 Result = ExpandLibCall(FnName, Node, Dummy);
1858 break;
1859 }
1860 break;
1861
Nate Begeman551bf3f2006-02-17 05:43:56 +00001862 case ISD::ADDC:
1863 case ISD::SUBC:
1864 Tmp1 = LegalizeOp(Node->getOperand(0));
1865 Tmp2 = LegalizeOp(Node->getOperand(1));
1866 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1867 // Since this produces two values, make sure to remember that we legalized
1868 // both of them.
1869 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1870 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1871 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001872
Nate Begeman551bf3f2006-02-17 05:43:56 +00001873 case ISD::ADDE:
1874 case ISD::SUBE:
1875 Tmp1 = LegalizeOp(Node->getOperand(0));
1876 Tmp2 = LegalizeOp(Node->getOperand(1));
1877 Tmp3 = LegalizeOp(Node->getOperand(2));
1878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1879 // Since this produces two values, make sure to remember that we legalized
1880 // both of them.
1881 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1882 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1883 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00001884
Nate Begeman419f8b62005-10-18 00:27:41 +00001885 case ISD::BUILD_PAIR: {
1886 MVT::ValueType PairTy = Node->getValueType(0);
1887 // TODO: handle the case where the Lo and Hi operands are not of legal type
1888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1889 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1890 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001891 case TargetLowering::Promote:
1892 case TargetLowering::Custom:
1893 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00001894 case TargetLowering::Legal:
1895 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1896 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1897 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00001898 case TargetLowering::Expand:
1899 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1900 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1901 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1902 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1903 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00001904 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00001905 break;
1906 }
1907 break;
1908 }
1909
Nate Begemanc105e192005-04-06 00:23:54 +00001910 case ISD::UREM:
1911 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001912 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001913 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1914 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00001915
Nate Begemanc105e192005-04-06 00:23:54 +00001916 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001917 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1918 case TargetLowering::Custom:
1919 isCustom = true;
1920 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00001921 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001922 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00001923 if (isCustom) {
1924 Tmp1 = TLI.LowerOperation(Result, DAG);
1925 if (Tmp1.Val) Result = Tmp1;
1926 }
Nate Begemanc105e192005-04-06 00:23:54 +00001927 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001928 case TargetLowering::Expand:
1929 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001930 // X % Y -> X-X/Y*Y
Chris Lattner4c64dd72005-08-03 20:31:37 +00001931 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001932 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00001933 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1934 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1935 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1936 } else {
1937 // Floating point mod -> fmod libcall.
1938 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1939 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00001940 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001941 }
1942 break;
1943 }
1944 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00001945 case ISD::VAARG: {
1946 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1947 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1948
Chris Lattner5c62f332006-01-28 07:42:08 +00001949 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00001950 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1951 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001952 case TargetLowering::Custom:
1953 isCustom = true;
1954 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00001955 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1957 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00001958 Tmp1 = Result.getValue(1);
1959
1960 if (isCustom) {
1961 Tmp2 = TLI.LowerOperation(Result, DAG);
1962 if (Tmp2.Val) {
1963 Result = LegalizeOp(Tmp2);
1964 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1965 }
1966 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001967 break;
1968 case TargetLowering::Expand: {
1969 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1970 Node->getOperand(2));
1971 // Increment the pointer, VAList, to the next vaarg
1972 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1973 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1974 TLI.getPointerTy()));
1975 // Store the incremented VAList to the legalized pointer
1976 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1977 Node->getOperand(2));
1978 // Load the actual argument out of the pointer VAList
1979 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00001980 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00001981 Result = LegalizeOp(Result);
1982 break;
1983 }
1984 }
1985 // Since VAARG produces two values, make sure to remember that we
1986 // legalized both of them.
1987 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00001988 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1989 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00001990 }
1991
1992 case ISD::VACOPY:
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1994 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1995 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1996
1997 switch (TLI.getOperationAction(ISD::VACOPY, 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, Tmp3,
2004 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002005 if (isCustom) {
2006 Tmp1 = TLI.LowerOperation(Result, DAG);
2007 if (Tmp1.Val) Result = Tmp1;
2008 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002009 break;
2010 case TargetLowering::Expand:
2011 // This defaults to loading a pointer from the input and storing it to the
2012 // output, returning the chain.
2013 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2014 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2015 Node->getOperand(4));
Nate Begemanacc398c2006-01-25 18:21:52 +00002016 break;
2017 }
2018 break;
2019
2020 case ISD::VAEND:
2021 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2022 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2023
2024 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2025 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002026 case TargetLowering::Custom:
2027 isCustom = true;
2028 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002029 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002030 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002031 if (isCustom) {
2032 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2033 if (Tmp1.Val) Result = Tmp1;
2034 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002035 break;
2036 case TargetLowering::Expand:
2037 Result = Tmp1; // Default to a no-op, return the chain
2038 break;
2039 }
2040 break;
2041
2042 case ISD::VASTART:
2043 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2044 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2045
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002046 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2047
Nate Begemanacc398c2006-01-25 18:21:52 +00002048 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2049 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002050 case TargetLowering::Legal: break;
2051 case TargetLowering::Custom:
2052 Tmp1 = TLI.LowerOperation(Result, DAG);
2053 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002054 break;
2055 }
2056 break;
2057
Nate Begeman35ef9132006-01-11 21:21:00 +00002058 case ISD::ROTL:
2059 case ISD::ROTR:
2060 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2061 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002062
2063 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2064 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002065 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002066 break;
2067
Nate Begemand88fc032006-01-14 03:14:10 +00002068 case ISD::BSWAP:
2069 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2070 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002071 case TargetLowering::Custom:
2072 assert(0 && "Cannot custom legalize this yet!");
2073 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002074 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002075 break;
2076 case TargetLowering::Promote: {
2077 MVT::ValueType OVT = Tmp1.getValueType();
2078 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2079 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002080
Chris Lattner456a93a2006-01-28 07:39:30 +00002081 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2082 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2083 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2084 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2085 break;
2086 }
2087 case TargetLowering::Expand:
2088 Result = ExpandBSWAP(Tmp1);
2089 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002090 }
2091 break;
2092
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002093 case ISD::CTPOP:
2094 case ISD::CTTZ:
2095 case ISD::CTLZ:
2096 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2097 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002098 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002099 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002100 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002101 break;
2102 case TargetLowering::Promote: {
2103 MVT::ValueType OVT = Tmp1.getValueType();
2104 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002105
2106 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002107 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2108 // Perform the larger operation, then subtract if needed.
2109 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002110 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002111 case ISD::CTPOP:
2112 Result = Tmp1;
2113 break;
2114 case ISD::CTTZ:
2115 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002116 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2117 DAG.getConstant(getSizeInBits(NVT), NVT),
2118 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002119 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002120 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2121 break;
2122 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002123 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002124 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2125 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002126 getSizeInBits(OVT), NVT));
2127 break;
2128 }
2129 break;
2130 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002131 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002132 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002133 break;
2134 }
2135 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002136
Chris Lattner2c8086f2005-04-02 05:00:07 +00002137 // Unary operators
2138 case ISD::FABS:
2139 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002140 case ISD::FSQRT:
2141 case ISD::FSIN:
2142 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002143 Tmp1 = LegalizeOp(Node->getOperand(0));
2144 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002145 case TargetLowering::Promote:
2146 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002147 isCustom = true;
2148 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002149 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002150 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002151 if (isCustom) {
2152 Tmp1 = TLI.LowerOperation(Result, DAG);
2153 if (Tmp1.Val) Result = Tmp1;
2154 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002155 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002156 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002157 switch (Node->getOpcode()) {
2158 default: assert(0 && "Unreachable!");
2159 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002160 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2161 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002162 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002163 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002164 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002165 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2166 MVT::ValueType VT = Node->getValueType(0);
2167 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002168 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002169 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2170 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002171 break;
2172 }
2173 case ISD::FSQRT:
2174 case ISD::FSIN:
2175 case ISD::FCOS: {
2176 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002177 const char *FnName = 0;
2178 switch(Node->getOpcode()) {
2179 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2180 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2181 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2182 default: assert(0 && "Unreachable!");
2183 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002184 SDOperand Dummy;
Chris Lattner9c6b4b82006-01-28 04:28:26 +00002185 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002186 break;
2187 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002188 }
2189 break;
2190 }
2191 break;
Chris Lattner35481892005-12-23 00:16:34 +00002192
2193 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002194 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002195 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002196 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002197 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2198 Node->getOperand(0).getValueType())) {
2199 default: assert(0 && "Unknown operation action!");
2200 case TargetLowering::Expand:
2201 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2202 break;
2203 case TargetLowering::Legal:
2204 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002205 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002206 break;
2207 }
2208 }
2209 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002210 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002211 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002212 case ISD::UINT_TO_FP: {
2213 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002214 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2215 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002216 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002217 Node->getOperand(0).getValueType())) {
2218 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002219 case TargetLowering::Custom:
2220 isCustom = true;
2221 // FALLTHROUGH
2222 case TargetLowering::Legal:
2223 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002224 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002225 if (isCustom) {
2226 Tmp1 = TLI.LowerOperation(Result, DAG);
2227 if (Tmp1.Val) Result = Tmp1;
2228 }
2229 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002230 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002231 Result = ExpandLegalINT_TO_FP(isSigned,
2232 LegalizeOp(Node->getOperand(0)),
2233 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002234 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002235 case TargetLowering::Promote:
2236 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2237 Node->getValueType(0),
2238 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002239 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002240 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002241 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002242 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002243 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2244 Node->getValueType(0), Node->getOperand(0));
2245 break;
2246 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002247 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002248 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002249 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2250 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002251 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002252 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2253 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002254 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002255 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2256 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002257 break;
2258 }
2259 break;
2260 }
2261 case ISD::TRUNCATE:
2262 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2263 case Legal:
2264 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002265 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002266 break;
2267 case Expand:
2268 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2269
2270 // Since the result is legal, we should just be able to truncate the low
2271 // part of the source.
2272 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2273 break;
2274 case Promote:
2275 Result = PromoteOp(Node->getOperand(0));
2276 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2277 break;
2278 }
2279 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002280
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002281 case ISD::FP_TO_SINT:
2282 case ISD::FP_TO_UINT:
2283 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2284 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002285 Tmp1 = LegalizeOp(Node->getOperand(0));
2286
Chris Lattner1618beb2005-07-29 00:11:56 +00002287 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2288 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002289 case TargetLowering::Custom:
2290 isCustom = true;
2291 // FALLTHROUGH
2292 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002293 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002294 if (isCustom) {
2295 Tmp1 = TLI.LowerOperation(Result, DAG);
2296 if (Tmp1.Val) Result = Tmp1;
2297 }
2298 break;
2299 case TargetLowering::Promote:
2300 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2301 Node->getOpcode() == ISD::FP_TO_SINT);
2302 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002303 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002304 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2305 SDOperand True, False;
2306 MVT::ValueType VT = Node->getOperand(0).getValueType();
2307 MVT::ValueType NVT = Node->getValueType(0);
2308 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2309 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2310 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2311 Node->getOperand(0), Tmp2, ISD::SETLT);
2312 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2313 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002314 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002315 Tmp2));
2316 False = DAG.getNode(ISD::XOR, NVT, False,
2317 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002318 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2319 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002320 } else {
2321 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2322 }
2323 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002324 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002325 break;
2326 case Expand:
2327 assert(0 && "Shouldn't need to expand other operators here!");
2328 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002329 Tmp1 = PromoteOp(Node->getOperand(0));
2330 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2331 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002332 break;
2333 }
2334 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002335
Chris Lattner13c78e22005-09-02 00:18:10 +00002336 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002337 case ISD::ZERO_EXTEND:
2338 case ISD::SIGN_EXTEND:
2339 case ISD::FP_EXTEND:
2340 case ISD::FP_ROUND:
2341 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002342 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002343 case Legal:
2344 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002345 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002346 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002347 case Promote:
2348 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002349 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002350 Tmp1 = PromoteOp(Node->getOperand(0));
2351 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002352 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002353 case ISD::ZERO_EXTEND:
2354 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002355 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002356 Result = DAG.getZeroExtendInReg(Result,
2357 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002358 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002359 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002360 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002361 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002362 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002363 Result,
2364 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002365 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002366 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002367 Result = PromoteOp(Node->getOperand(0));
2368 if (Result.getValueType() != Op.getValueType())
2369 // Dynamically dead while we have only 2 FP types.
2370 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2371 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002372 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002373 Result = PromoteOp(Node->getOperand(0));
2374 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2375 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002376 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002377 }
2378 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002379 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002380 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002381 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002382 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002383
2384 // If this operation is not supported, convert it to a shl/shr or load/store
2385 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002386 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2387 default: assert(0 && "This action not supported for this op yet!");
2388 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002389 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002390 break;
2391 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002392 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002393 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002394 // NOTE: we could fall back on load/store here too for targets without
2395 // SAR. However, it is doubtful that any exist.
2396 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2397 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002398 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002399 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2400 Node->getOperand(0), ShiftCst);
2401 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2402 Result, ShiftCst);
2403 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2404 // The only way we can lower this is to turn it into a STORETRUNC,
2405 // EXTLOAD pair, targetting a temporary location (a stack slot).
2406
2407 // NOTE: there is a choice here between constantly creating new stack
2408 // slots and always reusing the same one. We currently always create
2409 // new ones, as reuse may inhibit scheduling.
2410 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2411 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2412 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2413 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002414 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002415 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2416 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2417 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002418 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002419 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002420 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2421 Result, StackSlot, DAG.getSrcValue(NULL),
2422 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002423 } else {
2424 assert(0 && "Unknown op");
2425 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002426 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002427 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002428 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002429 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002430 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002431
2432 // Make sure that the generated code is itself legal.
2433 if (Result != Op)
2434 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002435
Chris Lattner45982da2005-05-12 16:53:42 +00002436 // Note that LegalizeOp may be reentered even from single-use nodes, which
2437 // means that we always must cache transformed nodes.
2438 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002439 return Result;
2440}
2441
Chris Lattner8b6fa222005-01-15 22:16:26 +00002442/// PromoteOp - Given an operation that produces a value in an invalid type,
2443/// promote it to compute the value into a larger type. The produced value will
2444/// have the correct bits for the low portion of the register, but no guarantee
2445/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002446SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2447 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002448 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002449 assert(getTypeAction(VT) == Promote &&
2450 "Caller should expand or legalize operands that are not promotable!");
2451 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2452 "Cannot promote to smaller type!");
2453
Chris Lattner03c85462005-01-15 05:21:40 +00002454 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00002455 SDOperand Result;
2456 SDNode *Node = Op.Val;
2457
Chris Lattner6fdcb252005-09-02 20:32:45 +00002458 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2459 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002460
Chris Lattner03c85462005-01-15 05:21:40 +00002461 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002462 case ISD::CopyFromReg:
2463 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002464 default:
2465 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2466 assert(0 && "Do not know how to promote this operator!");
2467 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002468 case ISD::UNDEF:
2469 Result = DAG.getNode(ISD::UNDEF, NVT);
2470 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002471 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002472 if (VT != MVT::i1)
2473 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2474 else
2475 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002476 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2477 break;
2478 case ISD::ConstantFP:
2479 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2480 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2481 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002482
Chris Lattner82fbfb62005-01-18 02:59:52 +00002483 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002484 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002485 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2486 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002487 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002488
2489 case ISD::TRUNCATE:
2490 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2491 case Legal:
2492 Result = LegalizeOp(Node->getOperand(0));
2493 assert(Result.getValueType() >= NVT &&
2494 "This truncation doesn't make sense!");
2495 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2496 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2497 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002498 case Promote:
2499 // The truncation is not required, because we don't guarantee anything
2500 // about high bits anyway.
2501 Result = PromoteOp(Node->getOperand(0));
2502 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002503 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002504 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2505 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002506 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002507 }
2508 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002509 case ISD::SIGN_EXTEND:
2510 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002511 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002512 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2513 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2514 case Legal:
2515 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00002516 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002517 break;
2518 case Promote:
2519 // Promote the reg if it's smaller.
2520 Result = PromoteOp(Node->getOperand(0));
2521 // The high bits are not guaranteed to be anything. Insert an extend.
2522 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002523 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002524 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002525 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002526 Result = DAG.getZeroExtendInReg(Result,
2527 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002528 break;
2529 }
2530 break;
Chris Lattner35481892005-12-23 00:16:34 +00002531 case ISD::BIT_CONVERT:
2532 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2533 Result = PromoteOp(Result);
2534 break;
2535
Chris Lattner8b6fa222005-01-15 22:16:26 +00002536 case ISD::FP_EXTEND:
2537 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2538 case ISD::FP_ROUND:
2539 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2540 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2541 case Promote: assert(0 && "Unreachable with 2 FP types!");
2542 case Legal:
2543 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00002544 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00002545 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002546 break;
2547 }
2548 break;
2549
2550 case ISD::SINT_TO_FP:
2551 case ISD::UINT_TO_FP:
2552 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2553 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00002554 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00002555 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002556 break;
2557
2558 case Promote:
2559 Result = PromoteOp(Node->getOperand(0));
2560 if (Node->getOpcode() == ISD::SINT_TO_FP)
2561 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002562 Result,
2563 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002564 else
Chris Lattner23993562005-04-13 02:38:47 +00002565 Result = DAG.getZeroExtendInReg(Result,
2566 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002567 // No extra round required here.
2568 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002569 break;
2570 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002571 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2572 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002573 // Round if we cannot tolerate excess precision.
2574 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002575 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2576 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002577 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002578 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002579 break;
2580
Chris Lattner5e3c5b42005-12-09 17:32:47 +00002581 case ISD::SIGN_EXTEND_INREG:
2582 Result = PromoteOp(Node->getOperand(0));
2583 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2584 Node->getOperand(1));
2585 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002586 case ISD::FP_TO_SINT:
2587 case ISD::FP_TO_UINT:
2588 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2589 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00002590 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002591 break;
2592 case Promote:
2593 // The input result is prerounded, so we don't have to do anything
2594 // special.
2595 Tmp1 = PromoteOp(Node->getOperand(0));
2596 break;
2597 case Expand:
2598 assert(0 && "not implemented");
2599 }
Nate Begemand2558e32005-08-14 01:20:53 +00002600 // If we're promoting a UINT to a larger size, check to see if the new node
2601 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2602 // we can use that instead. This allows us to generate better code for
2603 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2604 // legal, such as PowerPC.
2605 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002606 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002607 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2608 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002609 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2610 } else {
2611 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2612 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002613 break;
2614
Chris Lattner2c8086f2005-04-02 05:00:07 +00002615 case ISD::FABS:
2616 case ISD::FNEG:
2617 Tmp1 = PromoteOp(Node->getOperand(0));
2618 assert(Tmp1.getValueType() == NVT);
2619 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2620 // NOTE: we do not have to do any extra rounding here for
2621 // NoExcessFPPrecision, because we know the input will have the appropriate
2622 // precision, and these operations don't modify precision at all.
2623 break;
2624
Chris Lattnerda6ba872005-04-28 21:44:33 +00002625 case ISD::FSQRT:
2626 case ISD::FSIN:
2627 case ISD::FCOS:
2628 Tmp1 = PromoteOp(Node->getOperand(0));
2629 assert(Tmp1.getValueType() == NVT);
2630 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002631 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002632 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2633 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002634 break;
2635
Chris Lattner03c85462005-01-15 05:21:40 +00002636 case ISD::AND:
2637 case ISD::OR:
2638 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002639 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002640 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002641 case ISD::MUL:
2642 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002643 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002644 // that too is okay if they are integer operations.
2645 Tmp1 = PromoteOp(Node->getOperand(0));
2646 Tmp2 = PromoteOp(Node->getOperand(1));
2647 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2648 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002649 break;
2650 case ISD::FADD:
2651 case ISD::FSUB:
2652 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00002653 Tmp1 = PromoteOp(Node->getOperand(0));
2654 Tmp2 = PromoteOp(Node->getOperand(1));
2655 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2656 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2657
2658 // Floating point operations will give excess precision that we may not be
2659 // able to tolerate. If we DO allow excess precision, just leave it,
2660 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002661 // FIXME: Why would we need to round FP ops more than integer ones?
2662 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002663 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002664 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2665 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002666 break;
2667
Chris Lattner8b6fa222005-01-15 22:16:26 +00002668 case ISD::SDIV:
2669 case ISD::SREM:
2670 // These operators require that their input be sign extended.
2671 Tmp1 = PromoteOp(Node->getOperand(0));
2672 Tmp2 = PromoteOp(Node->getOperand(1));
2673 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002674 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2675 DAG.getValueType(VT));
2676 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2677 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002678 }
2679 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2680
2681 // Perform FP_ROUND: this is probably overly pessimistic.
2682 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002683 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2684 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002685 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002686 case ISD::FDIV:
2687 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00002688 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00002689 // These operators require that their input be fp extended.
2690 Tmp1 = PromoteOp(Node->getOperand(0));
2691 Tmp2 = PromoteOp(Node->getOperand(1));
2692 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2693
2694 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00002695 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00002696 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2697 DAG.getValueType(VT));
2698 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002699
2700 case ISD::UDIV:
2701 case ISD::UREM:
2702 // These operators require that their input be zero extended.
2703 Tmp1 = PromoteOp(Node->getOperand(0));
2704 Tmp2 = PromoteOp(Node->getOperand(1));
2705 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002706 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2707 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002708 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2709 break;
2710
2711 case ISD::SHL:
2712 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002713 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002714 break;
2715 case ISD::SRA:
2716 // The input value must be properly sign extended.
2717 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002718 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2719 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002720 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002721 break;
2722 case ISD::SRL:
2723 // The input value must be properly zero extended.
2724 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002725 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00002726 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002727 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00002728
2729 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00002730 Tmp1 = Node->getOperand(0); // Get the chain.
2731 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00002732 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2733 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2734 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2735 } else {
2736 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2737 Node->getOperand(2));
2738 // Increment the pointer, VAList, to the next vaarg
2739 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2740 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2741 TLI.getPointerTy()));
2742 // Store the incremented VAList to the legalized pointer
2743 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2744 Node->getOperand(2));
2745 // Load the actual argument out of the pointer VAList
2746 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2747 DAG.getSrcValue(0), VT);
2748 }
2749 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002750 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00002751 break;
2752
Chris Lattner03c85462005-01-15 05:21:40 +00002753 case ISD::LOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002754 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2755 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002756 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002757 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00002758 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002759 case ISD::SEXTLOAD:
2760 case ISD::ZEXTLOAD:
2761 case ISD::EXTLOAD:
Chris Lattner456a93a2006-01-28 07:39:30 +00002762 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2763 Node->getOperand(1), Node->getOperand(2),
Chris Lattner8136cda2005-10-15 20:24:07 +00002764 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002765 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00002766 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002767 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002768 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00002769 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2770 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00002771 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00002772 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002773 case ISD::SELECT_CC:
2774 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2775 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2776 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00002777 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00002778 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002779 case ISD::BSWAP:
2780 Tmp1 = Node->getOperand(0);
2781 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2782 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2783 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2784 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2785 TLI.getShiftAmountTy()));
2786 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002787 case ISD::CTPOP:
2788 case ISD::CTTZ:
2789 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002790 // Zero extend the argument
2791 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002792 // Perform the larger operation, then subtract if needed.
2793 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002794 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002795 case ISD::CTPOP:
2796 Result = Tmp1;
2797 break;
2798 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002799 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002800 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002801 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002802 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00002803 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002804 break;
2805 case ISD::CTLZ:
2806 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002807 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2808 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002809 getSizeInBits(VT), NVT));
2810 break;
2811 }
2812 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002813 }
2814
2815 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002816
2817 // Make sure the result is itself legal.
2818 Result = LegalizeOp(Result);
2819
2820 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00002821 AddPromotedOperand(Op, Result);
2822 return Result;
2823}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002824
Nate Begeman750ac1b2006-02-01 07:19:44 +00002825/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2826/// with condition CC on the current target. This usually involves legalizing
2827/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2828/// there may be no choice but to create a new SetCC node to represent the
2829/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2830/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2831void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2832 SDOperand &RHS,
2833 SDOperand &CC) {
2834 SDOperand Tmp1, Tmp2, Result;
2835
2836 switch (getTypeAction(LHS.getValueType())) {
2837 case Legal:
2838 Tmp1 = LegalizeOp(LHS); // LHS
2839 Tmp2 = LegalizeOp(RHS); // RHS
2840 break;
2841 case Promote:
2842 Tmp1 = PromoteOp(LHS); // LHS
2843 Tmp2 = PromoteOp(RHS); // RHS
2844
2845 // If this is an FP compare, the operands have already been extended.
2846 if (MVT::isInteger(LHS.getValueType())) {
2847 MVT::ValueType VT = LHS.getValueType();
2848 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2849
2850 // Otherwise, we have to insert explicit sign or zero extends. Note
2851 // that we could insert sign extends for ALL conditions, but zero extend
2852 // is cheaper on many machines (an AND instead of two shifts), so prefer
2853 // it.
2854 switch (cast<CondCodeSDNode>(CC)->get()) {
2855 default: assert(0 && "Unknown integer comparison!");
2856 case ISD::SETEQ:
2857 case ISD::SETNE:
2858 case ISD::SETUGE:
2859 case ISD::SETUGT:
2860 case ISD::SETULE:
2861 case ISD::SETULT:
2862 // ALL of these operations will work if we either sign or zero extend
2863 // the operands (including the unsigned comparisons!). Zero extend is
2864 // usually a simpler/cheaper operation, so prefer it.
2865 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2866 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2867 break;
2868 case ISD::SETGE:
2869 case ISD::SETGT:
2870 case ISD::SETLT:
2871 case ISD::SETLE:
2872 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2873 DAG.getValueType(VT));
2874 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2875 DAG.getValueType(VT));
2876 break;
2877 }
2878 }
2879 break;
2880 case Expand:
2881 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2882 ExpandOp(LHS, LHSLo, LHSHi);
2883 ExpandOp(RHS, RHSLo, RHSHi);
2884 switch (cast<CondCodeSDNode>(CC)->get()) {
2885 case ISD::SETEQ:
2886 case ISD::SETNE:
2887 if (RHSLo == RHSHi)
2888 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2889 if (RHSCST->isAllOnesValue()) {
2890 // Comparison to -1.
2891 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2892 Tmp2 = RHSLo;
2893 break;
2894 }
2895
2896 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
2897 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
2898 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
2899 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2900 break;
2901 default:
2902 // If this is a comparison of the sign bit, just look at the top part.
2903 // X > -1, x < 0
2904 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
2905 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
2906 CST->getValue() == 0) || // X < 0
2907 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
2908 CST->isAllOnesValue())) { // X > -1
2909 Tmp1 = LHSHi;
2910 Tmp2 = RHSHi;
2911 break;
2912 }
2913
2914 // FIXME: This generated code sucks.
2915 ISD::CondCode LowCC;
2916 switch (cast<CondCodeSDNode>(CC)->get()) {
2917 default: assert(0 && "Unknown integer setcc!");
2918 case ISD::SETLT:
2919 case ISD::SETULT: LowCC = ISD::SETULT; break;
2920 case ISD::SETGT:
2921 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2922 case ISD::SETLE:
2923 case ISD::SETULE: LowCC = ISD::SETULE; break;
2924 case ISD::SETGE:
2925 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2926 }
2927
2928 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
2929 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
2930 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2931
2932 // NOTE: on targets without efficient SELECT of bools, we can always use
2933 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2934 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
2935 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
2936 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
2937 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2938 Result, Tmp1, Tmp2));
2939 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00002940 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00002941 }
2942 }
2943 LHS = Tmp1;
2944 RHS = Tmp2;
2945}
2946
Chris Lattner35481892005-12-23 00:16:34 +00002947/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00002948/// The resultant code need not be legal. Note that SrcOp is the input operand
2949/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00002950SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2951 SDOperand SrcOp) {
2952 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00002953 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00002954
2955 // Emit a store to the stack slot.
2956 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattnered7b5ba2005-12-23 00:50:25 +00002957 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner35481892005-12-23 00:16:34 +00002958 // Result is a load from the stack slot.
2959 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2960}
2961
Chris Lattnerce872152006-03-19 06:31:19 +00002962/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
2963/// support the operation, but do support the resultant packed vector type.
2964SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
2965
2966 // If the only non-undef value is the low element, turn this into a
2967 // SCALAR_TO_VECTOR node.
2968 bool isOnlyLowElement = true;
2969 for (SDNode::op_iterator I = Node->op_begin()+1, E = Node->op_end();
2970 I != E; ++I) {
2971 if (I->getOpcode() != ISD::UNDEF) {
2972 isOnlyLowElement = false;
2973 break;
2974 }
2975 }
2976
2977 if (isOnlyLowElement) {
2978 // If the low element is an undef too, then this whole things is an undef.
2979 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
2980 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2981 // Otherwise, turn this into a scalar_to_vector node.
2982 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
2983 Node->getOperand(0));
2984 }
2985
2986 // If the elements are all constants, turn this into a load from the constant
2987 // pool.
2988 bool isConstant = true;
2989 for (SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
2990 I != E; ++I) {
2991 if (!isa<ConstantFPSDNode>(I) && !isa<ConstantSDNode>(I) &&
2992 I->getOpcode() != ISD::UNDEF) {
2993 isConstant = false;
2994 break;
2995 }
2996 }
2997
2998 // Create a ConstantPacked, and put it in the constant pool.
2999 if (isConstant) {
3000 MVT::ValueType VT = Node->getValueType(0);
3001 const Type *OpNTy =
3002 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3003 std::vector<Constant*> CV;
3004 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3005 if (ConstantFPSDNode *V =
3006 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3007 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3008 } else if (ConstantSDNode *V =
3009 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3010 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3011 } else {
3012 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3013 CV.push_back(UndefValue::get(OpNTy));
3014 }
3015 }
3016 Constant *CP = ConstantPacked::get(CV);
3017 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3018 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3019 DAG.getSrcValue(NULL));
3020 }
3021
3022 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3023 // aligned object on the stack, store each element into it, then load
3024 // the result as a vector.
3025 MVT::ValueType VT = Node->getValueType(0);
3026 // Create the stack frame object.
3027 SDOperand FIPtr = CreateStackTemporary(VT);
3028
3029 // Emit a store of each element to the stack slot.
3030 std::vector<SDOperand> Stores;
3031 bool isLittleEndian = TLI.isLittleEndian();
3032 unsigned TypeByteSize =
3033 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3034 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3035 // Store (in the right endianness) the elements to memory.
3036 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3037 // Ignore undef elements.
3038 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3039
3040 unsigned Offset;
3041 if (isLittleEndian)
3042 Offset = TypeByteSize*i;
3043 else
3044 Offset = TypeByteSize*(e-i-1);
3045
3046 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3047 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3048
3049 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3050 Node->getOperand(i), Idx,
3051 DAG.getSrcValue(NULL)));
3052 }
3053
3054 SDOperand StoreChain;
3055 if (!Stores.empty()) // Not all undef elements?
3056 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3057 else
3058 StoreChain = DAG.getEntryNode();
3059
3060 // Result is a load from the stack slot.
3061 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3062}
3063
3064/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3065/// specified value type.
3066SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3067 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3068 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3069 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3070 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3071}
3072
Chris Lattner5b359c62005-04-02 04:00:59 +00003073void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3074 SDOperand Op, SDOperand Amt,
3075 SDOperand &Lo, SDOperand &Hi) {
3076 // Expand the subcomponents.
3077 SDOperand LHSL, LHSH;
3078 ExpandOp(Op, LHSL, LHSH);
3079
3080 std::vector<SDOperand> Ops;
3081 Ops.push_back(LHSL);
3082 Ops.push_back(LHSH);
3083 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00003084 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00003085 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00003086 Hi = Lo.getValue(1);
3087}
3088
3089
Chris Lattnere34b3962005-01-19 04:19:40 +00003090/// ExpandShift - Try to find a clever way to expand this shift operation out to
3091/// smaller elements. If we can't find a way that is more efficient than a
3092/// libcall on this target, return false. Otherwise, return true with the
3093/// low-parts expanded into Lo and Hi.
3094bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3095 SDOperand &Lo, SDOperand &Hi) {
3096 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3097 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003098
Chris Lattnere34b3962005-01-19 04:19:40 +00003099 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003100 SDOperand ShAmt = LegalizeOp(Amt);
3101 MVT::ValueType ShTy = ShAmt.getValueType();
3102 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3103 unsigned NVTBits = MVT::getSizeInBits(NVT);
3104
3105 // Handle the case when Amt is an immediate. Other cases are currently broken
3106 // and are disabled.
3107 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3108 unsigned Cst = CN->getValue();
3109 // Expand the incoming operand to be shifted, so that we have its parts
3110 SDOperand InL, InH;
3111 ExpandOp(Op, InL, InH);
3112 switch(Opc) {
3113 case ISD::SHL:
3114 if (Cst > VTBits) {
3115 Lo = DAG.getConstant(0, NVT);
3116 Hi = DAG.getConstant(0, NVT);
3117 } else if (Cst > NVTBits) {
3118 Lo = DAG.getConstant(0, NVT);
3119 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003120 } else if (Cst == NVTBits) {
3121 Lo = DAG.getConstant(0, NVT);
3122 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003123 } else {
3124 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3125 Hi = DAG.getNode(ISD::OR, NVT,
3126 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3127 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3128 }
3129 return true;
3130 case ISD::SRL:
3131 if (Cst > VTBits) {
3132 Lo = DAG.getConstant(0, NVT);
3133 Hi = DAG.getConstant(0, NVT);
3134 } else if (Cst > NVTBits) {
3135 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3136 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00003137 } else if (Cst == NVTBits) {
3138 Lo = InH;
3139 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003140 } else {
3141 Lo = DAG.getNode(ISD::OR, NVT,
3142 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3143 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3144 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3145 }
3146 return true;
3147 case ISD::SRA:
3148 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003149 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003150 DAG.getConstant(NVTBits-1, ShTy));
3151 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00003152 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003153 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003154 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003155 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003156 } else if (Cst == NVTBits) {
3157 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003158 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00003159 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003160 } else {
3161 Lo = DAG.getNode(ISD::OR, NVT,
3162 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3163 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3164 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3165 }
3166 return true;
3167 }
3168 }
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003169 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00003170}
Chris Lattner77e77a62005-01-21 06:05:23 +00003171
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003172
Chris Lattner77e77a62005-01-21 06:05:23 +00003173// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3174// does not fit into a register, return the lo part and set the hi part to the
3175// by-reg argument. If it does fit into a single register, return the result
3176// and leave the Hi part unset.
3177SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3178 SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00003179 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3180 // The input chain to this libcall is the entry node of the function.
3181 // Legalizing the call will automatically add the previous call to the
3182 // dependence.
3183 SDOperand InChain = DAG.getEntryNode();
3184
Chris Lattner77e77a62005-01-21 06:05:23 +00003185 TargetLowering::ArgListTy Args;
3186 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3187 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3188 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3189 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3190 }
3191 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00003192
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003193 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00003194 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003195 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00003196 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3197 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003198
Chris Lattner6831a812006-02-13 09:18:02 +00003199 // Legalize the call sequence, starting with the chain. This will advance
3200 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3201 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3202 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00003203 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003204 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00003205 default: assert(0 && "Unknown thing");
3206 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00003207 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00003208 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003209 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00003210 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00003211 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00003212 }
Chris Lattner99c25b82005-09-02 20:26:58 +00003213 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00003214}
3215
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003216
Chris Lattner77e77a62005-01-21 06:05:23 +00003217/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3218/// destination type is legal.
3219SDOperand SelectionDAGLegalize::
3220ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003221 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00003222 assert(getTypeAction(Source.getValueType()) == Expand &&
3223 "This is not an expansion!");
3224 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3225
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003226 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00003227 assert(Source.getValueType() == MVT::i64 &&
3228 "This only works for 64-bit -> FP");
3229 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3230 // incoming integer is set. To handle this, we dynamically test to see if
3231 // it is set, and, if so, add a fudge factor.
3232 SDOperand Lo, Hi;
3233 ExpandOp(Source, Lo, Hi);
3234
Chris Lattner66de05b2005-05-13 04:45:13 +00003235 // If this is unsigned, and not supported, first perform the conversion to
3236 // signed, then adjust the result if the sign bit is set.
3237 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3238 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3239
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003240 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3241 DAG.getConstant(0, Hi.getValueType()),
3242 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003243 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3244 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3245 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00003246 uint64_t FF = 0x5f800000ULL;
3247 if (TLI.isLittleEndian()) FF <<= 32;
3248 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003249
Chris Lattner5839bf22005-08-26 17:15:30 +00003250 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00003251 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3252 SDOperand FudgeInReg;
3253 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00003254 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3255 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00003256 else {
3257 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00003258 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3259 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00003260 }
Chris Lattner473a9902005-09-29 06:44:39 +00003261 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00003262 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003263
Chris Lattnera88a2602005-05-14 05:33:54 +00003264 // Check to see if the target has a custom way to lower this. If so, use it.
3265 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3266 default: assert(0 && "This action not implemented for this operation!");
3267 case TargetLowering::Legal:
3268 case TargetLowering::Expand:
3269 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00003270 case TargetLowering::Custom: {
3271 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3272 Source), DAG);
3273 if (NV.Val)
3274 return LegalizeOp(NV);
3275 break; // The target decided this was legal after all
3276 }
Chris Lattnera88a2602005-05-14 05:33:54 +00003277 }
3278
Chris Lattner13689e22005-05-12 07:00:44 +00003279 // Expand the source, then glue it back together for the call. We must expand
3280 // the source in case it is shared (this pass of legalize must traverse it).
3281 SDOperand SrcLo, SrcHi;
3282 ExpandOp(Source, SrcLo, SrcHi);
3283 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3284
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003285 const char *FnName = 0;
3286 if (DestTy == MVT::f32)
3287 FnName = "__floatdisf";
3288 else {
3289 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3290 FnName = "__floatdidf";
3291 }
Chris Lattner6831a812006-02-13 09:18:02 +00003292
3293 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3294 SDOperand UnusedHiPart;
Chris Lattner25125692006-02-17 04:32:33 +00003295 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00003296}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003297
Chris Lattner22cde6a2006-01-28 08:25:58 +00003298/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3299/// INT_TO_FP operation of the specified operand when the target requests that
3300/// we expand it. At this point, we know that the result and operand types are
3301/// legal for the target.
3302SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3303 SDOperand Op0,
3304 MVT::ValueType DestVT) {
3305 if (Op0.getValueType() == MVT::i32) {
3306 // simple 32-bit [signed|unsigned] integer to float/double expansion
3307
3308 // get the stack frame index of a 8 byte buffer
3309 MachineFunction &MF = DAG.getMachineFunction();
3310 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3311 // get address of 8 byte buffer
3312 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3313 // word offset constant for Hi/Lo address computation
3314 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3315 // set up Hi and Lo (into buffer) address based on endian
3316 SDOperand Hi, Lo;
3317 if (TLI.isLittleEndian()) {
3318 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3319 Lo = StackSlot;
3320 } else {
3321 Hi = StackSlot;
3322 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3323 }
3324 // if signed map to unsigned space
3325 SDOperand Op0Mapped;
3326 if (isSigned) {
3327 // constant used to invert sign bit (signed to unsigned mapping)
3328 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3329 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3330 } else {
3331 Op0Mapped = Op0;
3332 }
3333 // store the lo of the constructed double - based on integer input
3334 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3335 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3336 // initial hi portion of constructed double
3337 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3338 // store the hi of the constructed double - biased exponent
3339 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3340 InitialHi, Hi, DAG.getSrcValue(NULL));
3341 // load the constructed double
3342 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3343 DAG.getSrcValue(NULL));
3344 // FP constant to bias correct the final result
3345 SDOperand Bias = DAG.getConstantFP(isSigned ?
3346 BitsToDouble(0x4330000080000000ULL)
3347 : BitsToDouble(0x4330000000000000ULL),
3348 MVT::f64);
3349 // subtract the bias
3350 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3351 // final result
3352 SDOperand Result;
3353 // handle final rounding
3354 if (DestVT == MVT::f64) {
3355 // do nothing
3356 Result = Sub;
3357 } else {
3358 // if f32 then cast to f32
3359 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3360 }
3361 return Result;
3362 }
3363 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3364 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3365
3366 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3367 DAG.getConstant(0, Op0.getValueType()),
3368 ISD::SETLT);
3369 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3370 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3371 SignSet, Four, Zero);
3372
3373 // If the sign bit of the integer is set, the large number will be treated
3374 // as a negative number. To counteract this, the dynamic code adds an
3375 // offset depending on the data type.
3376 uint64_t FF;
3377 switch (Op0.getValueType()) {
3378 default: assert(0 && "Unsupported integer type!");
3379 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3380 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3381 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3382 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3383 }
3384 if (TLI.isLittleEndian()) FF <<= 32;
3385 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3386
3387 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3388 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3389 SDOperand FudgeInReg;
3390 if (DestVT == MVT::f32)
3391 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3392 DAG.getSrcValue(NULL));
3393 else {
3394 assert(DestVT == MVT::f64 && "Unexpected conversion");
3395 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3396 DAG.getEntryNode(), CPIdx,
3397 DAG.getSrcValue(NULL), MVT::f32));
3398 }
3399
3400 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3401}
3402
3403/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3404/// *INT_TO_FP operation of the specified operand when the target requests that
3405/// we promote it. At this point, we know that the result and operand types are
3406/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3407/// operation that takes a larger input.
3408SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3409 MVT::ValueType DestVT,
3410 bool isSigned) {
3411 // First step, figure out the appropriate *INT_TO_FP operation to use.
3412 MVT::ValueType NewInTy = LegalOp.getValueType();
3413
3414 unsigned OpToUse = 0;
3415
3416 // Scan for the appropriate larger type to use.
3417 while (1) {
3418 NewInTy = (MVT::ValueType)(NewInTy+1);
3419 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3420
3421 // If the target supports SINT_TO_FP of this type, use it.
3422 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3423 default: break;
3424 case TargetLowering::Legal:
3425 if (!TLI.isTypeLegal(NewInTy))
3426 break; // Can't use this datatype.
3427 // FALL THROUGH.
3428 case TargetLowering::Custom:
3429 OpToUse = ISD::SINT_TO_FP;
3430 break;
3431 }
3432 if (OpToUse) break;
3433 if (isSigned) continue;
3434
3435 // If the target supports UINT_TO_FP of this type, use it.
3436 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3437 default: break;
3438 case TargetLowering::Legal:
3439 if (!TLI.isTypeLegal(NewInTy))
3440 break; // Can't use this datatype.
3441 // FALL THROUGH.
3442 case TargetLowering::Custom:
3443 OpToUse = ISD::UINT_TO_FP;
3444 break;
3445 }
3446 if (OpToUse) break;
3447
3448 // Otherwise, try a larger type.
3449 }
3450
3451 // Okay, we found the operation and type to use. Zero extend our input to the
3452 // desired type then run the operation on it.
3453 return DAG.getNode(OpToUse, DestVT,
3454 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3455 NewInTy, LegalOp));
3456}
3457
3458/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3459/// FP_TO_*INT operation of the specified operand when the target requests that
3460/// we promote it. At this point, we know that the result and operand types are
3461/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3462/// operation that returns a larger result.
3463SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3464 MVT::ValueType DestVT,
3465 bool isSigned) {
3466 // First step, figure out the appropriate FP_TO*INT operation to use.
3467 MVT::ValueType NewOutTy = DestVT;
3468
3469 unsigned OpToUse = 0;
3470
3471 // Scan for the appropriate larger type to use.
3472 while (1) {
3473 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3474 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3475
3476 // If the target supports FP_TO_SINT returning this type, use it.
3477 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3478 default: break;
3479 case TargetLowering::Legal:
3480 if (!TLI.isTypeLegal(NewOutTy))
3481 break; // Can't use this datatype.
3482 // FALL THROUGH.
3483 case TargetLowering::Custom:
3484 OpToUse = ISD::FP_TO_SINT;
3485 break;
3486 }
3487 if (OpToUse) break;
3488
3489 // If the target supports FP_TO_UINT of this type, use it.
3490 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3491 default: break;
3492 case TargetLowering::Legal:
3493 if (!TLI.isTypeLegal(NewOutTy))
3494 break; // Can't use this datatype.
3495 // FALL THROUGH.
3496 case TargetLowering::Custom:
3497 OpToUse = ISD::FP_TO_UINT;
3498 break;
3499 }
3500 if (OpToUse) break;
3501
3502 // Otherwise, try a larger type.
3503 }
3504
3505 // Okay, we found the operation and type to use. Truncate the result of the
3506 // extended FP_TO_*INT operation to the desired size.
3507 return DAG.getNode(ISD::TRUNCATE, DestVT,
3508 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3509}
3510
3511/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3512///
3513SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3514 MVT::ValueType VT = Op.getValueType();
3515 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3516 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3517 switch (VT) {
3518 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3519 case MVT::i16:
3520 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3521 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3522 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3523 case MVT::i32:
3524 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3525 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3526 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3527 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3528 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3529 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3530 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3531 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3532 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3533 case MVT::i64:
3534 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3535 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3536 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3537 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3538 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3539 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3540 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3541 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3542 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3543 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3544 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3545 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3546 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3547 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3548 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3549 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3550 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3551 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3552 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3553 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3554 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3555 }
3556}
3557
3558/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3559///
3560SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3561 switch (Opc) {
3562 default: assert(0 && "Cannot expand this yet!");
3563 case ISD::CTPOP: {
3564 static const uint64_t mask[6] = {
3565 0x5555555555555555ULL, 0x3333333333333333ULL,
3566 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3567 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3568 };
3569 MVT::ValueType VT = Op.getValueType();
3570 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3571 unsigned len = getSizeInBits(VT);
3572 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3573 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3574 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3575 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3576 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3577 DAG.getNode(ISD::AND, VT,
3578 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3579 }
3580 return Op;
3581 }
3582 case ISD::CTLZ: {
3583 // for now, we do this:
3584 // x = x | (x >> 1);
3585 // x = x | (x >> 2);
3586 // ...
3587 // x = x | (x >>16);
3588 // x = x | (x >>32); // for 64-bit input
3589 // return popcount(~x);
3590 //
3591 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3592 MVT::ValueType VT = Op.getValueType();
3593 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3594 unsigned len = getSizeInBits(VT);
3595 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3596 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3597 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3598 }
3599 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3600 return DAG.getNode(ISD::CTPOP, VT, Op);
3601 }
3602 case ISD::CTTZ: {
3603 // for now, we use: { return popcount(~x & (x - 1)); }
3604 // unless the target has ctlz but not ctpop, in which case we use:
3605 // { return 32 - nlz(~x & (x-1)); }
3606 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3607 MVT::ValueType VT = Op.getValueType();
3608 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3609 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3610 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3611 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3612 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3613 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3614 TLI.isOperationLegal(ISD::CTLZ, VT))
3615 return DAG.getNode(ISD::SUB, VT,
3616 DAG.getConstant(getSizeInBits(VT), VT),
3617 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3618 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3619 }
3620 }
3621}
Chris Lattnere34b3962005-01-19 04:19:40 +00003622
3623
Chris Lattner3e928bb2005-01-07 07:47:09 +00003624/// ExpandOp - Expand the specified SDOperand into its two component pieces
3625/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3626/// LegalizeNodes map is filled in for any results that are not expanded, the
3627/// ExpandedNodes map is filled in for any results that are expanded, and the
3628/// Lo/Hi values are returned.
3629void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3630 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003631 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003632 SDNode *Node = Op.Val;
3633 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemanab48be32005-11-22 18:16:00 +00003634 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3635 "Cannot expand FP values!");
3636 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00003637 "Cannot expand to FP value or to larger int value!");
3638
Chris Lattner6fdcb252005-09-02 20:32:45 +00003639 // See if we already expanded it.
3640 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3641 = ExpandedNodes.find(Op);
3642 if (I != ExpandedNodes.end()) {
3643 Lo = I->second.first;
3644 Hi = I->second.second;
3645 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003646 }
3647
Chris Lattner3e928bb2005-01-07 07:47:09 +00003648 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003649 case ISD::CopyFromReg:
3650 assert(0 && "CopyFromReg must be legal!");
3651 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003652 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3653 assert(0 && "Do not know how to expand this operator!");
3654 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003655 case ISD::UNDEF:
3656 Lo = DAG.getNode(ISD::UNDEF, NVT);
3657 Hi = DAG.getNode(ISD::UNDEF, NVT);
3658 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003659 case ISD::Constant: {
3660 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3661 Lo = DAG.getConstant(Cst, NVT);
3662 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3663 break;
3664 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003665 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00003666 // Return the operands.
3667 Lo = Node->getOperand(0);
3668 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003669 break;
Chris Lattner58f79632005-12-12 22:27:43 +00003670
3671 case ISD::SIGN_EXTEND_INREG:
3672 ExpandOp(Node->getOperand(0), Lo, Hi);
3673 // Sign extend the lo-part.
3674 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3675 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3676 TLI.getShiftAmountTy()));
3677 // sext_inreg the low part if needed.
3678 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3679 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003680
Nate Begemand88fc032006-01-14 03:14:10 +00003681 case ISD::BSWAP: {
3682 ExpandOp(Node->getOperand(0), Lo, Hi);
3683 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3684 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3685 Lo = TempLo;
3686 break;
3687 }
3688
Chris Lattneredb1add2005-05-11 04:51:16 +00003689 case ISD::CTPOP:
3690 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003691 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3692 DAG.getNode(ISD::CTPOP, NVT, Lo),
3693 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003694 Hi = DAG.getConstant(0, NVT);
3695 break;
3696
Chris Lattner39a8f332005-05-12 19:05:01 +00003697 case ISD::CTLZ: {
3698 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003699 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003700 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3701 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003702 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3703 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003704 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3705 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3706
3707 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3708 Hi = DAG.getConstant(0, NVT);
3709 break;
3710 }
3711
3712 case ISD::CTTZ: {
3713 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003714 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003715 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3716 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003717 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3718 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003719 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3720 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3721
3722 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3723 Hi = DAG.getConstant(0, NVT);
3724 break;
3725 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003726
Nate Begemanacc398c2006-01-25 18:21:52 +00003727 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003728 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3729 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00003730 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3731 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3732
3733 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003734 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00003735 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3736 if (!TLI.isLittleEndian())
3737 std::swap(Lo, Hi);
3738 break;
3739 }
3740
Chris Lattner3e928bb2005-01-07 07:47:09 +00003741 case ISD::LOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003742 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3743 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003744 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003745
3746 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003747 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003748 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3749 getIntPtrConstant(IncrementSize));
Chris Lattner8137c9e2006-01-28 05:07:51 +00003750 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003751 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003752
3753 // Build a factor node to remember that this load is independent of the
3754 // other one.
3755 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3756 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003757
Chris Lattner3e928bb2005-01-07 07:47:09 +00003758 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003759 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003760 if (!TLI.isLittleEndian())
3761 std::swap(Lo, Hi);
3762 break;
3763 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003764 case ISD::AND:
3765 case ISD::OR:
3766 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3767 SDOperand LL, LH, RL, RH;
3768 ExpandOp(Node->getOperand(0), LL, LH);
3769 ExpandOp(Node->getOperand(1), RL, RH);
3770 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3771 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3772 break;
3773 }
3774 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00003775 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003776 ExpandOp(Node->getOperand(1), LL, LH);
3777 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner456a93a2006-01-28 07:39:30 +00003778 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3779 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003780 break;
3781 }
Nate Begeman9373a812005-08-10 20:51:12 +00003782 case ISD::SELECT_CC: {
3783 SDOperand TL, TH, FL, FH;
3784 ExpandOp(Node->getOperand(2), TL, TH);
3785 ExpandOp(Node->getOperand(3), FL, FH);
3786 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3787 Node->getOperand(1), TL, FL, Node->getOperand(4));
3788 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3789 Node->getOperand(1), TH, FH, Node->getOperand(4));
3790 break;
3791 }
Nate Begeman144ff662005-10-13 17:15:37 +00003792 case ISD::SEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003793 SDOperand Chain = Node->getOperand(0);
3794 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003795 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3796
3797 if (EVT == NVT)
3798 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3799 else
3800 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3801 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003802
3803 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003804 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003805
Nate Begeman144ff662005-10-13 17:15:37 +00003806 // The high part is obtained by SRA'ing all but one of the bits of the lo
3807 // part.
3808 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3809 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3810 TLI.getShiftAmountTy()));
Nate Begeman144ff662005-10-13 17:15:37 +00003811 break;
3812 }
3813 case ISD::ZEXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003814 SDOperand Chain = Node->getOperand(0);
3815 SDOperand Ptr = Node->getOperand(1);
Nate Begeman144ff662005-10-13 17:15:37 +00003816 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3817
3818 if (EVT == NVT)
3819 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3820 else
3821 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3822 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003823
3824 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003825 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003826
Nate Begeman144ff662005-10-13 17:15:37 +00003827 // The high part is just a zero.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003828 Hi = DAG.getConstant(0, NVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003829 break;
3830 }
3831 case ISD::EXTLOAD: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003832 SDOperand Chain = Node->getOperand(0);
3833 SDOperand Ptr = Node->getOperand(1);
Chris Lattner9ad84812005-10-13 21:44:47 +00003834 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3835
3836 if (EVT == NVT)
3837 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3838 else
3839 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3840 EVT);
3841
3842 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003843 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner9ad84812005-10-13 21:44:47 +00003844
3845 // The high part is undefined.
Chris Lattner13c78e22005-09-02 00:18:10 +00003846 Hi = DAG.getNode(ISD::UNDEF, NVT);
3847 break;
3848 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003849 case ISD::ANY_EXTEND:
3850 // The low part is any extension of the input (which degenerates to a copy).
3851 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3852 // The high part is undefined.
3853 Hi = DAG.getNode(ISD::UNDEF, NVT);
3854 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003855 case ISD::SIGN_EXTEND: {
3856 // The low part is just a sign extension of the input (which degenerates to
3857 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003858 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003859
Chris Lattner3e928bb2005-01-07 07:47:09 +00003860 // The high part is obtained by SRA'ing all but one of the bits of the lo
3861 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003862 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00003863 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3864 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003865 break;
3866 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00003867 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003868 // The low part is just a zero extension of the input (which degenerates to
3869 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00003870 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003871
Chris Lattner3e928bb2005-01-07 07:47:09 +00003872 // The high part is just a zero.
3873 Hi = DAG.getConstant(0, NVT);
3874 break;
Chris Lattner35481892005-12-23 00:16:34 +00003875
3876 case ISD::BIT_CONVERT: {
3877 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3878 Node->getOperand(0));
3879 ExpandOp(Tmp, Lo, Hi);
3880 break;
3881 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003882
Chris Lattner8137c9e2006-01-28 05:07:51 +00003883 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00003884 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3885 TargetLowering::Custom &&
3886 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003887 Lo = TLI.LowerOperation(Op, DAG);
3888 assert(Lo.Val && "Node must be custom expanded!");
3889 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00003890 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00003891 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003892 break;
3893
Chris Lattner4e6c7462005-01-08 19:27:05 +00003894 // These operators cannot be expanded directly, emit them as calls to
3895 // library functions.
3896 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003897 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003898 SDOperand Op;
3899 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3900 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00003901 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3902 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00003903 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003904
Chris Lattnerf20d1832005-07-30 01:40:57 +00003905 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3906
Chris Lattner80a3e942005-07-29 00:33:32 +00003907 // Now that the custom expander is done, expand the result, which is still
3908 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003909 if (Op.Val) {
3910 ExpandOp(Op, Lo, Hi);
3911 break;
3912 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003913 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003914
Chris Lattner4e6c7462005-01-08 19:27:05 +00003915 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003916 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003917 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003918 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003919 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003920
Chris Lattner4e6c7462005-01-08 19:27:05 +00003921 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003922 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003923 SDOperand Op;
3924 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3925 case Expand: assert(0 && "cannot expand FP!");
3926 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3927 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3928 }
3929
3930 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3931
3932 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00003933 if (Op.Val) {
3934 ExpandOp(Op, Lo, Hi);
3935 break;
3936 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003937 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003938
Chris Lattner4e6c7462005-01-08 19:27:05 +00003939 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003940 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003941 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003942 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003943 break;
3944
Evan Cheng05a2d562006-01-09 18:31:59 +00003945 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003946 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003947 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003948 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003949 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003950 Op = TLI.LowerOperation(Op, DAG);
3951 if (Op.Val) {
3952 // Now that the custom expander is done, expand the result, which is
3953 // still VT.
3954 ExpandOp(Op, Lo, Hi);
3955 break;
3956 }
3957 }
3958
Chris Lattnere34b3962005-01-19 04:19:40 +00003959 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003960 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003961 break;
Chris Lattner47599822005-04-02 03:38:53 +00003962
3963 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003964 TargetLowering::LegalizeAction Action =
3965 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3966 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3967 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00003968 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003969 break;
3970 }
3971
Chris Lattnere34b3962005-01-19 04:19:40 +00003972 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003973 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003974 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00003975 }
Chris Lattnere34b3962005-01-19 04:19:40 +00003976
Evan Cheng05a2d562006-01-09 18:31:59 +00003977 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00003978 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00003979 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00003980 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00003981 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00003982 Op = TLI.LowerOperation(Op, DAG);
3983 if (Op.Val) {
3984 // Now that the custom expander is done, expand the result, which is
3985 // still VT.
3986 ExpandOp(Op, Lo, Hi);
3987 break;
3988 }
3989 }
3990
Chris Lattnere34b3962005-01-19 04:19:40 +00003991 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00003992 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003993 break;
Chris Lattner47599822005-04-02 03:38:53 +00003994
3995 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00003996 TargetLowering::LegalizeAction Action =
3997 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3998 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3999 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004000 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004001 break;
4002 }
4003
Chris Lattnere34b3962005-01-19 04:19:40 +00004004 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004005 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004006 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004007 }
4008
4009 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004010 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004011 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004012 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004013 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004014 Op = TLI.LowerOperation(Op, DAG);
4015 if (Op.Val) {
4016 // Now that the custom expander is done, expand the result, which is
4017 // still VT.
4018 ExpandOp(Op, Lo, Hi);
4019 break;
4020 }
4021 }
4022
Chris Lattnere34b3962005-01-19 04:19:40 +00004023 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004024 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004025 break;
Chris Lattner47599822005-04-02 03:38:53 +00004026
4027 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004028 TargetLowering::LegalizeAction Action =
4029 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4030 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4031 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004032 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004033 break;
4034 }
4035
Chris Lattnere34b3962005-01-19 04:19:40 +00004036 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00004037 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004038 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004039 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004040
Misha Brukmanedf128a2005-04-21 22:36:52 +00004041 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004042 case ISD::SUB: {
4043 // If the target wants to custom expand this, let them.
4044 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4045 TargetLowering::Custom) {
4046 Op = TLI.LowerOperation(Op, DAG);
4047 if (Op.Val) {
4048 ExpandOp(Op, Lo, Hi);
4049 break;
4050 }
4051 }
4052
4053 // Expand the subcomponents.
4054 SDOperand LHSL, LHSH, RHSL, RHSH;
4055 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4056 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman551bf3f2006-02-17 05:43:56 +00004057 std::vector<MVT::ValueType> VTs;
4058 std::vector<SDOperand> LoOps, HiOps;
4059 VTs.push_back(LHSL.getValueType());
4060 VTs.push_back(MVT::Flag);
4061 LoOps.push_back(LHSL);
4062 LoOps.push_back(RHSL);
4063 HiOps.push_back(LHSH);
4064 HiOps.push_back(RHSH);
4065 if (Node->getOpcode() == ISD::ADD) {
4066 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4067 HiOps.push_back(Lo.getValue(1));
4068 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4069 } else {
4070 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4071 HiOps.push_back(Lo.getValue(1));
4072 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4073 }
Chris Lattner84f67882005-01-20 18:52:28 +00004074 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00004075 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004076 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004077 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00004078 SDOperand LL, LH, RL, RH;
4079 ExpandOp(Node->getOperand(0), LL, LH);
4080 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00004081 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4082 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4083 // extended the sign bit of the low half through the upper half, and if so
4084 // emit a MULHS instead of the alternate sequence that is valid for any
4085 // i64 x i64 multiply.
4086 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4087 // is RH an extension of the sign bit of RL?
4088 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4089 RH.getOperand(1).getOpcode() == ISD::Constant &&
4090 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4091 // is LH an extension of the sign bit of LL?
4092 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4093 LH.getOperand(1).getOpcode() == ISD::Constant &&
4094 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4095 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4096 } else {
4097 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4098 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4099 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4100 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4101 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4102 }
Nate Begemanc7c16572005-04-11 03:01:51 +00004103 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4104 } else {
Chris Lattner9c6b4b82006-01-28 04:28:26 +00004105 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00004106 }
4107 break;
4108 }
Chris Lattner77e77a62005-01-21 06:05:23 +00004109 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4110 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4111 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4112 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004113 }
4114
Chris Lattner83397362005-12-21 18:02:52 +00004115 // Make sure the resultant values have been legalized themselves, unless this
4116 // is a type that requires multi-step expansion.
4117 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4118 Lo = LegalizeOp(Lo);
4119 Hi = LegalizeOp(Hi);
4120 }
Evan Cheng05a2d562006-01-09 18:31:59 +00004121
4122 // Remember in a map if the values will be reused later.
4123 bool isNew =
4124 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4125 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00004126}
4127
Chris Lattnerc7029802006-03-18 01:44:44 +00004128/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4129/// two smaller values of MVT::Vector type.
4130void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4131 SDOperand &Hi) {
4132 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4133 SDNode *Node = Op.Val;
4134 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4135 assert(NumElements > 1 && "Cannot split a single element vector!");
4136 unsigned NewNumElts = NumElements/2;
4137 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4138 SDOperand TypeNode = *(Node->op_end()-1);
4139
4140 // See if we already split it.
4141 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4142 = SplitNodes.find(Op);
4143 if (I != SplitNodes.end()) {
4144 Lo = I->second.first;
4145 Hi = I->second.second;
4146 return;
4147 }
4148
4149 switch (Node->getOpcode()) {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004150 default: assert(0 && "Unknown vector operation!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00004151 case ISD::VBUILD_VECTOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00004152 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4153 LoOps.push_back(NewNumEltsNode);
4154 LoOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004155 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004156
4157 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4158 HiOps.push_back(NewNumEltsNode);
4159 HiOps.push_back(TypeNode);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004160 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattnerc7029802006-03-18 01:44:44 +00004161 break;
4162 }
4163 case ISD::VADD:
4164 case ISD::VSUB:
4165 case ISD::VMUL:
4166 case ISD::VSDIV:
4167 case ISD::VUDIV:
4168 case ISD::VAND:
4169 case ISD::VOR:
4170 case ISD::VXOR: {
4171 SDOperand LL, LH, RL, RH;
4172 SplitVectorOp(Node->getOperand(0), LL, LH);
4173 SplitVectorOp(Node->getOperand(1), RL, RH);
4174
4175 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4176 NewNumEltsNode, TypeNode);
4177 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4178 NewNumEltsNode, TypeNode);
4179 break;
4180 }
4181 case ISD::VLOAD: {
4182 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4183 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4184 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4185
4186 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4187 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4188 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4189 getIntPtrConstant(IncrementSize));
4190 // FIXME: This creates a bogus srcvalue!
4191 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4192
4193 // Build a factor node to remember that this load is independent of the
4194 // other one.
4195 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4196 Hi.getValue(1));
4197
4198 // Remember that we legalized the chain.
4199 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4200 if (!TLI.isLittleEndian())
4201 std::swap(Lo, Hi);
4202 break;
4203 }
4204 }
4205
4206 // Remember in a map if the values will be reused later.
4207 bool isNew =
4208 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4209 assert(isNew && "Value already expanded?!?");
4210}
4211
4212
4213/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4214/// equivalent operation that returns a scalar (e.g. F32) or packed value
4215/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4216/// type for the result.
4217SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4218 MVT::ValueType NewVT) {
4219 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4220 SDNode *Node = Op.Val;
4221
4222 // See if we already packed it.
4223 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4224 if (I != PackedNodes.end()) return I->second;
4225
4226 SDOperand Result;
4227 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00004228 default:
4229 Node->dump(); std::cerr << "\n";
4230 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00004231 case ISD::VADD:
4232 case ISD::VSUB:
4233 case ISD::VMUL:
4234 case ISD::VSDIV:
4235 case ISD::VUDIV:
4236 case ISD::VAND:
4237 case ISD::VOR:
4238 case ISD::VXOR:
4239 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4240 NewVT,
4241 PackVectorOp(Node->getOperand(0), NewVT),
4242 PackVectorOp(Node->getOperand(1), NewVT));
4243 break;
4244 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00004245 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4246 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00004247
Chris Lattner4794a6b2006-03-19 00:07:49 +00004248 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerc7029802006-03-18 01:44:44 +00004249
4250 // Remember that we legalized the chain.
4251 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4252 break;
4253 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00004254 case ISD::VBUILD_VECTOR:
Chris Lattnerc7029802006-03-18 01:44:44 +00004255 if (!MVT::isVector(NewVT)) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004256 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00004257 Result = Node->getOperand(0);
4258 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00004259 // Returning a BUILD_VECTOR?
Chris Lattnerc7029802006-03-18 01:44:44 +00004260 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerb2827b02006-03-19 00:52:58 +00004261 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattnerc7029802006-03-18 01:44:44 +00004262 }
4263 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00004264 case ISD::VINSERT_VECTOR_ELT:
4265 if (!MVT::isVector(NewVT)) {
4266 // Returning a scalar? Must be the inserted element.
4267 Result = Node->getOperand(1);
4268 } else {
4269 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4270 PackVectorOp(Node->getOperand(0), NewVT),
4271 Node->getOperand(1), Node->getOperand(2));
4272 }
4273 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00004274 }
4275
4276 if (TLI.isTypeLegal(NewVT))
4277 Result = LegalizeOp(Result);
4278 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4279 assert(isNew && "Value already packed?");
4280 return Result;
4281}
4282
Chris Lattner3e928bb2005-01-07 07:47:09 +00004283
4284// SelectionDAG::Legalize - This is the entry point for the file.
4285//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004286void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00004287 /// run - This is the main entry point to this class.
4288 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00004289 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004290}
4291