blob: b0a48eef6d76d03f7386d80938eaace6c6ca5f27 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Evan Cheng1d2e9952006-03-24 01:17:21 +000024#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
Chris Lattner462505f2006-02-13 09:18:02 +000044 // Libcall insertion helpers.
45
46 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
47 /// legalized. We use this to ensure that calls are properly serialized
48 /// against each other, including inserted libcalls.
49 SDOperand LastCALLSEQ_END;
50
51 /// IsLegalizingCall - This member is used *only* for purposes of providing
52 /// helpful assertions that a libcall isn't created while another call is
53 /// being legalized (which could lead to non-serialized call sequences).
54 bool IsLegalizingCall;
55
Chris Lattnerdc750592005-01-07 07:47:09 +000056 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000057 Legal, // The target natively supports this operation.
58 Promote, // This operation should be executed in a larger type.
59 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000060 };
Chris Lattner462505f2006-02-13 09:18:02 +000061
Chris Lattnerdc750592005-01-07 07:47:09 +000062 /// ValueTypeActions - This is a bitvector that contains two bits for each
63 /// value type, where the two bits correspond to the LegalizeAction enum.
64 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000065 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000066
Chris Lattnerdc750592005-01-07 07:47:09 +000067 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
70 std::map<SDOperand, SDOperand> LegalizedNodes;
71
Chris Lattner1f2c9d82005-01-15 05:21:40 +000072 /// PromotedNodes - For nodes that are below legal width, and that have more
73 /// than one use, this map indicates what promoted value to use. This allows
74 /// us to avoid promoting the same thing more than once.
75 std::map<SDOperand, SDOperand> PromotedNodes;
76
Chris Lattner32206f52006-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 Lattnerdc750592005-01-07 07:47:09 +000080 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
81
Chris Lattner32206f52006-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 Lattnerea4ca942005-01-07 22:28:47 +000092 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-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 Lattnerea4ca942005-01-07 22:28:47 +000097 }
Chris Lattner1f2c9d82005-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 Lattner2af3ee42005-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 Lattner1f2c9d82005-01-15 05:21:40 +0000103 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000104
Chris Lattnerdc750592005-01-07 07:47:09 +0000105public:
106
Chris Lattner4add7e32005-01-23 04:42:50 +0000107 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000108
Chris Lattnerdc750592005-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 Lattner2c748af2006-01-29 08:42:06 +0000113 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +0000122 void LegalizeDAG();
123
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000124private:
Chris Lattner32206f52006-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 Lattnerdc750592005-01-07 07:47:09 +0000132 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-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 Lattner1f2c9d82005-01-15 05:21:40 +0000139 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000140
Chris Lattner32206f52006-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 Lattner462505f2006-02-13 09:18:02 +0000159 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
160
Nate Begeman7e7f4392006-02-01 07:19:44 +0000161 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
162
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000163 SDOperand CreateStackTemporary(MVT::ValueType VT);
164
Chris Lattneraac464e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000169
Chris Lattner36e663d2005-12-23 00:16:34 +0000170 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000171 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000172 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand LegalOp,
174 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000175 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
176 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000177 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
178 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000179
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000180 SDOperand ExpandBSWAP(SDOperand Op);
181 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000182 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
183 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000184 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
185 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000186
Chris Lattnerdc750592005-01-07 07:47:09 +0000187 SDOperand getIntPtrConstant(uint64_t Val) {
188 return DAG.getConstant(Val, TLI.getPointerTy());
189 }
190};
191}
192
Chris Lattner32206f52006-03-18 01:44:44 +0000193/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
194/// specified vector opcode.
Chris Lattner301015a2005-11-19 05:51:46 +0000195static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000196 switch (VecOp) {
197 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3bf916d2006-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 Begemanb2e089c2005-11-19 00:36:38 +0000206 }
207}
Chris Lattnerdc750592005-01-07 07:47:09 +0000208
Chris Lattner4add7e32005-01-23 04:42:50 +0000209SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
210 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
211 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000212 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000213 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000214}
215
Chris Lattner4bbbb9e2005-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 Lattner4bbbb9e2005-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 Lattner44fe26f2005-07-29 00:11:56 +0000236
Chris Lattnerdc750592005-01-07 07:47:09 +0000237void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000238 LastCALLSEQ_END = DAG.getEntryNode();
239 IsLegalizingCall = false;
240
Chris Lattner9cfccfb2005-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 Lattner4bbbb9e2005-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 Lattner9cfccfb2005-10-02 17:49:46 +0000249
Chris Lattner4bbbb9e2005-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 Lattnerbf4f23322005-11-09 23:47:37 +0000254 if (I->getNumOperands() == 0) {
255 Visited[I] = 0 - 1U;
256 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000257 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000258 }
259
Chris Lattnerbf4f23322005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000263 "Error: DAG is cyclic!");
264 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000265
Chris Lattner32206f52006-03-18 01:44:44 +0000266 for (unsigned i = 0, e = Order.size(); i != e; ++i)
267 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000268
269 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000270 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000271 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
272 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000273
274 ExpandedNodes.clear();
275 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000276 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000277 SplitNodes.clear();
278 PackedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000279
280 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000281 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000282}
283
Chris Lattner462505f2006-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 Lattner32206f52006-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 Lattner93640542006-03-19 00:07:49 +0000413 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000414 }
415 }
416 break;
417 }
418}
Chris Lattner462505f2006-02-13 09:18:02 +0000419
420
Chris Lattner32206f52006-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 Lattnerdc750592005-01-07 07:47:09 +0000424SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000425 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000426 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000427 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000428
Chris Lattnerdc750592005-01-07 07:47:09 +0000429 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000430 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000431 if (Node->getNumValues() > 1) {
432 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000433 if (getTypeAction(Node->getValueType(i)) != Legal) {
434 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000435 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000436 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000437 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000438 }
439 }
440
Chris Lattnerb5a78e02005-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 Lattner85d70c62005-01-11 05:57:22 +0000443 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
444 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000445
Nate Begemane5b86d72005-08-10 20:51:12 +0000446 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000447 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000448 bool isCustom = false;
449
Chris Lattnerdc750592005-01-07 07:47:09 +0000450 switch (Node->getOpcode()) {
Chris Lattnereb637512006-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 Lattner758b0ac2006-01-29 06:26:56 +0000457 case ISD::TargetConstantFP:
Chris Lattnereb637512006-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 Lattnerdc750592005-01-07 07:47:09 +0000469 default:
Chris Lattner3eb86932005-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 Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +0000496 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000497 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000498 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-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 Lattnereb637512006-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 Lattner45ca1c02005-11-17 06:41:44 +0000505 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000506 break;
507 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000508 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000509 case ISD::AssertSext:
510 case ISD::AssertZext:
511 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000513 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000514 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000515 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000516 Result = Node->getOperand(Op.ResNo);
517 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000518 case ISD::CopyFromReg:
519 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000520 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000521 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000522 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000523 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000524 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000525 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000526 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-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 Lattnere3c67e92005-12-18 15:27:43 +0000531 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
532 }
Chris Lattnereb6614d2005-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 Begemancda9aa72005-04-01 22:34:39 +0000538 case ISD::UNDEF: {
539 MVT::ValueType VT = Op.getValueType();
540 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000541 default: assert(0 && "This action is not supported yet!");
542 case TargetLowering::Expand:
Nate Begemancda9aa72005-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 Begeman69d39432005-04-02 00:41:14 +0000550 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000551 break;
552 }
553 break;
554 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000555
556 case ISD::INTRINSIC: {
557 std::vector<SDOperand> Ops;
558 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
559 Ops.push_back(LegalizeOp(Node->getOperand(i)));
560 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner30ee7252006-03-26 09:12:51 +0000561
562 // Allow the target to custom lower its intrinsics if it wants to.
563 if (TLI.getOperationAction(ISD::INTRINSIC, MVT::Other) ==
564 TargetLowering::Custom) {
565 Tmp3 = TLI.LowerOperation(Result, DAG);
566 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000567 }
568
569 if (Result.Val->getNumValues() == 1) break;
570
571 // Must have return value and chain result.
572 assert(Result.Val->getNumValues() == 2 &&
573 "Cannot return more than two values!");
574
575 // Since loads produce two values, make sure to remember that we
576 // legalized both of them.
577 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
578 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
579 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000580 }
Chris Lattner435b4022005-11-29 06:21:05 +0000581
582 case ISD::LOCATION:
583 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
584 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
585
586 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
587 case TargetLowering::Promote:
588 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000589 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000590 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000591 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
592 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
593
594 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
595 const std::string &FName =
596 cast<StringSDNode>(Node->getOperand(3))->getValue();
597 const std::string &DirName =
598 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000599 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000600
Jim Laskey9e296be2005-12-21 20:51:37 +0000601 std::vector<SDOperand> Ops;
602 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000603 SDOperand LineOp = Node->getOperand(1);
604 SDOperand ColOp = Node->getOperand(2);
605
606 if (useDEBUG_LOC) {
607 Ops.push_back(LineOp); // line #
608 Ops.push_back(ColOp); // col #
609 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
610 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
611 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000612 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
613 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000614 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
615 Ops.push_back(DAG.getConstant(ID, MVT::i32));
616 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
617 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000618 } else {
619 Result = Tmp1; // chain
620 }
Chris Lattner435b4022005-11-29 06:21:05 +0000621 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000622 }
Chris Lattner435b4022005-11-29 06:21:05 +0000623 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000624 if (Tmp1 != Node->getOperand(0) ||
625 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000626 std::vector<SDOperand> Ops;
627 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000628 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
629 Ops.push_back(Node->getOperand(1)); // line # must be legal.
630 Ops.push_back(Node->getOperand(2)); // col # must be legal.
631 } else {
632 // Otherwise promote them.
633 Ops.push_back(PromoteOp(Node->getOperand(1)));
634 Ops.push_back(PromoteOp(Node->getOperand(2)));
635 }
Chris Lattner435b4022005-11-29 06:21:05 +0000636 Ops.push_back(Node->getOperand(3)); // filename must be legal.
637 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000638 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000639 }
640 break;
641 }
642 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000643
644 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000645 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000646 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000647 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000648 case TargetLowering::Legal:
649 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
650 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
651 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
652 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000653 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000654 break;
655 }
656 break;
657
658 case ISD::DEBUG_LABEL:
659 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
660 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000661 default: assert(0 && "This action is not supported yet!");
662 case TargetLowering::Legal:
663 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
664 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000666 break;
667 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000668 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000669
Chris Lattnerdc750592005-01-07 07:47:09 +0000670 case ISD::Constant:
671 // We know we don't need to expand constants here, constants only have one
672 // value and we check that it is fine above.
673
674 // FIXME: Maybe we should handle things like targets that don't support full
675 // 32-bit immediates?
676 break;
677 case ISD::ConstantFP: {
678 // Spill FP immediates to the constant pool if the target cannot directly
679 // codegen them. Targets often have some immediate values that can be
680 // efficiently generated into an FP register without a load. We explicitly
681 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000682 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
683
684 // Check to see if this FP immediate is already legal.
685 bool isLegal = false;
686 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
687 E = TLI.legal_fpimm_end(); I != E; ++I)
688 if (CFP->isExactlyValue(*I)) {
689 isLegal = true;
690 break;
691 }
692
Chris Lattner758b0ac2006-01-29 06:26:56 +0000693 // If this is a legal constant, turn it into a TargetConstantFP node.
694 if (isLegal) {
695 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
696 break;
697 }
698
699 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
700 default: assert(0 && "This action is not supported yet!");
701 case TargetLowering::Custom:
702 Tmp3 = TLI.LowerOperation(Result, DAG);
703 if (Tmp3.Val) {
704 Result = Tmp3;
705 break;
706 }
707 // FALLTHROUGH
708 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000709 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000710 bool Extend = false;
711
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000712 // If a FP immediate is precise when represented as a float and if the
713 // target can do an extending load from float to double, we put it into
714 // the constant pool as a float, even if it's is statically typed as a
715 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000716 MVT::ValueType VT = CFP->getValueType(0);
717 bool isDouble = VT == MVT::f64;
718 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
719 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000720 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
721 // Only do this if the target has a native EXTLOAD instruction from
722 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000723 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000724 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
725 VT = MVT::f32;
726 Extend = true;
727 }
Misha Brukman835702a2005-04-21 22:36:52 +0000728
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000729 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000730 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000731 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
732 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000733 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000734 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
735 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000736 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000737 }
738 break;
739 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000740 case ISD::TokenFactor:
741 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000742 Tmp1 = LegalizeOp(Node->getOperand(0));
743 Tmp2 = LegalizeOp(Node->getOperand(1));
744 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
745 } else if (Node->getNumOperands() == 3) {
746 Tmp1 = LegalizeOp(Node->getOperand(0));
747 Tmp2 = LegalizeOp(Node->getOperand(1));
748 Tmp3 = LegalizeOp(Node->getOperand(2));
749 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000750 } else {
751 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000752 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000753 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
754 Ops.push_back(LegalizeOp(Node->getOperand(i)));
755 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000756 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000757 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000758
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000759 case ISD::BUILD_VECTOR:
760 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000761 default: assert(0 && "This action is not supported yet!");
762 case TargetLowering::Custom:
763 Tmp3 = TLI.LowerOperation(Result, DAG);
764 if (Tmp3.Val) {
765 Result = Tmp3;
766 break;
767 }
768 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000769 case TargetLowering::Expand:
770 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000771 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000772 }
Chris Lattner29b23012006-03-19 01:17:20 +0000773 break;
774 case ISD::INSERT_VECTOR_ELT:
775 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
776 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
777 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
778 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
779
780 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
781 Node->getValueType(0))) {
782 default: assert(0 && "This action is not supported yet!");
783 case TargetLowering::Legal:
784 break;
785 case TargetLowering::Custom:
786 Tmp3 = TLI.LowerOperation(Result, DAG);
787 if (Tmp3.Val) {
788 Result = Tmp3;
789 break;
790 }
791 // FALLTHROUGH
792 case TargetLowering::Expand: {
793 // If the target doesn't support this, we have to spill the input vector
794 // to a temporary stack slot, update the element, then reload it. This is
795 // badness. We could also load the value into a vector register (either
796 // with a "move to register" or "extload into register" instruction, then
797 // permute it into place, if the idx is a constant and if the idx is
798 // supported by the target.
799 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
800 break;
801 }
802 }
803 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000804 case ISD::SCALAR_TO_VECTOR:
805 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
806 Result = DAG.UpdateNodeOperands(Result, Tmp1);
807 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
808 Node->getValueType(0))) {
809 default: assert(0 && "This action is not supported yet!");
810 case TargetLowering::Legal:
811 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000812 case TargetLowering::Custom:
813 Tmp3 = TLI.LowerOperation(Result, DAG);
814 if (Tmp3.Val) {
815 Result = Tmp3;
816 break;
817 }
818 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000819 case TargetLowering::Expand: {
820 // If the target doesn't support this, store the value to a temporary
821 // stack slot, then EXTLOAD the vector back out.
Chris Lattner79fb91c2006-03-19 06:47:21 +0000822 // TODO: If a target doesn't support this, create a stack slot for the
823 // whole vector, then store into it, then load the whole vector.
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000824 SDOperand StackPtr =
825 CreateStackTemporary(Node->getOperand(0).getValueType());
826 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
827 Node->getOperand(0), StackPtr,
828 DAG.getSrcValue(NULL));
829 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
830 DAG.getSrcValue(NULL),
831 Node->getOperand(0).getValueType());
832 break;
833 }
834 }
835 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000836 case ISD::VECTOR_SHUFFLE:
837 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
838 "vector shuffle should not be created if not legal!");
839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
840 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
842
843 // Allow targets to custom lower the SHUFFLEs they support.
844 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
845 == TargetLowering::Custom) {
846 Tmp1 = TLI.LowerOperation(Result, DAG);
847 if (Tmp1.Val) Result = Tmp1;
848 }
849 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000850
851 case ISD::EXTRACT_VECTOR_ELT:
852 Tmp1 = LegalizeOp(Node->getOperand(0));
853 Tmp2 = LegalizeOp(Node->getOperand(1));
854 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000855
856 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
857 Tmp1.getValueType())) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Legal:
860 break;
861 case TargetLowering::Custom:
862 Tmp3 = TLI.LowerOperation(Result, DAG);
863 if (Tmp3.Val) {
864 Result = Tmp3;
865 break;
866 }
867 // FALLTHROUGH
868 case TargetLowering::Expand: {
869 // If the target doesn't support this, store the value to a temporary
870 // stack slot, then LOAD the scalar element back out.
871 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
872 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
873 Tmp1, StackPtr, DAG.getSrcValue(NULL));
874
875 // Add the offset to the index.
876 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
877 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
878 DAG.getConstant(EltSize, Tmp2.getValueType()));
879 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
880
881 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
882 DAG.getSrcValue(NULL));
883 break;
884 }
885 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000886 break;
887
Chris Lattner5be43522006-03-22 00:12:37 +0000888 case ISD::VEXTRACT_VECTOR_ELT: {
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000889 // We know that operand #0 is the Vec vector. If the index is a constant
890 // or if the invec is a supported hardware type, we can use it. Otherwise,
891 // lower to a store then an indexed load.
892 Tmp1 = Node->getOperand(0);
893 Tmp2 = LegalizeOp(Node->getOperand(1));
894
895 SDNode *InVal = Tmp1.Val;
896 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
897 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
898
899 // Figure out if there is a Packed type corresponding to this Vector
900 // type. If so, convert to the packed type.
901 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
902 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
903 // Turn this into a packed extract_vector_elt operation.
904 Tmp1 = PackVectorOp(Tmp1, TVT);
905 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Node->getValueType(0),
906 Tmp1, Tmp2);
907 break;
908 } else if (NumElems == 1) {
909 // This must be an access of the only element.
910 Result = PackVectorOp(Tmp1, EVT);
911 break;
912 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Tmp2)) {
913 SDOperand Lo, Hi;
914 SplitVectorOp(Tmp1, Lo, Hi);
915 if (CIdx->getValue() < NumElems/2) {
916 Tmp1 = Lo;
917 } else {
918 Tmp1 = Hi;
919 Tmp2 = DAG.getConstant(CIdx->getValue() - NumElems/2,
920 Tmp2.getValueType());
921 }
922
923 // It's now an extract from the appropriate high or low part.
924 Result = LegalizeOp(DAG.UpdateNodeOperands(Result, Tmp1, Tmp2));
925 } else {
Chris Lattner5be43522006-03-22 00:12:37 +0000926 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000927 assert(0 && "unimp!");
928 }
929 break;
Chris Lattner5be43522006-03-22 00:12:37 +0000930 }
Chris Lattner21e68c82006-03-20 01:52:29 +0000931
Chris Lattner462505f2006-02-13 09:18:02 +0000932 case ISD::CALLSEQ_START: {
933 SDNode *CallEnd = FindCallEndFromCallStart(Node);
934
935 // Recursively Legalize all of the inputs of the call end that do not lead
936 // to this call start. This ensures that any libcalls that need be inserted
937 // are inserted *before* the CALLSEQ_START.
938 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
939 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
940
941 // Now that we legalized all of the inputs (which may have inserted
942 // libcalls) create the new CALLSEQ_START node.
943 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
944
945 // Merge in the last call, to ensure that this call start after the last
946 // call ended.
947 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
948 Tmp1 = LegalizeOp(Tmp1);
949
950 // Do not try to legalize the target-specific arguments (#1+).
951 if (Tmp1 != Node->getOperand(0)) {
952 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
953 Ops[0] = Tmp1;
954 Result = DAG.UpdateNodeOperands(Result, Ops);
955 }
956
957 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000958 AddLegalizedOperand(Op.getValue(0), Result);
959 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
960 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
961
Chris Lattner462505f2006-02-13 09:18:02 +0000962 // Now that the callseq_start and all of the non-call nodes above this call
963 // sequence have been legalized, legalize the call itself. During this
964 // process, no libcalls can/will be inserted, guaranteeing that no calls
965 // can overlap.
966 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
967 SDOperand InCallSEQ = LastCALLSEQ_END;
968 // Note that we are selecting this call!
969 LastCALLSEQ_END = SDOperand(CallEnd, 0);
970 IsLegalizingCall = true;
971
972 // Legalize the call, starting from the CALLSEQ_END.
973 LegalizeOp(LastCALLSEQ_END);
974 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
975 return Result;
976 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000977 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +0000978 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
979 // will cause this node to be legalized as well as handling libcalls right.
980 if (LastCALLSEQ_END.Val != Node) {
981 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
982 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
983 assert(I != LegalizedNodes.end() &&
984 "Legalizing the call start should have legalized this node!");
985 return I->second;
986 }
987
988 // Otherwise, the call start has been legalized and everything is going
989 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +0000990 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000991 // Do not try to legalize the target-specific arguments (#1+), except for
992 // an optional flag input.
993 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
994 if (Tmp1 != Node->getOperand(0)) {
995 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
996 Ops[0] = Tmp1;
997 Result = DAG.UpdateNodeOperands(Result, Ops);
998 }
999 } else {
1000 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1001 if (Tmp1 != Node->getOperand(0) ||
1002 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1003 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1004 Ops[0] = Tmp1;
1005 Ops.back() = Tmp2;
1006 Result = DAG.UpdateNodeOperands(Result, Ops);
1007 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001008 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001009 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001010 // This finishes up call legalization.
1011 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001012
1013 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1014 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1015 if (Node->getNumValues() == 2)
1016 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1017 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001018 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001019 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1020 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1021 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001022 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001023
Chris Lattnerd02b0542006-01-28 10:58:55 +00001024 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001025 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001026 switch (TLI.getOperationAction(Node->getOpcode(),
1027 Node->getValueType(0))) {
1028 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001029 case TargetLowering::Expand: {
1030 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1031 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1032 " not tell us which reg is the stack pointer!");
1033 SDOperand Chain = Tmp1.getOperand(0);
1034 SDOperand Size = Tmp2.getOperand(1);
1035 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1036 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1037 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001038 Tmp1 = LegalizeOp(Tmp1);
1039 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001040 break;
1041 }
1042 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001043 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1044 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001045 Tmp1 = LegalizeOp(Tmp3);
1046 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001047 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001048 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001049 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001050 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001051 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001052 // Since this op produce two values, make sure to remember that we
1053 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001054 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1055 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001056 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001057 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001058 case ISD::INLINEASM:
1059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1060 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001061 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001062 Tmp2 = Tmp3 = SDOperand(0, 0);
1063 else
1064 Tmp3 = LegalizeOp(Tmp2);
1065
1066 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1067 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1068 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001069 if (Tmp3.Val) Ops.back() = Tmp3;
1070 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001071 }
1072
1073 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001074 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001075 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1076 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001077 case ISD::BR:
1078 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001079 // Ensure that libcalls are emitted before a branch.
1080 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1081 Tmp1 = LegalizeOp(Tmp1);
1082 LastCALLSEQ_END = DAG.getEntryNode();
1083
Chris Lattnerd02b0542006-01-28 10:58:55 +00001084 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001085 break;
1086
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001087 case ISD::BRCOND:
1088 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001089 // Ensure that libcalls are emitted before a return.
1090 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1091 Tmp1 = LegalizeOp(Tmp1);
1092 LastCALLSEQ_END = DAG.getEntryNode();
1093
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001094 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1095 case Expand: assert(0 && "It's impossible to expand bools");
1096 case Legal:
1097 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1098 break;
1099 case Promote:
1100 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1101 break;
1102 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001103
1104 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001105 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001106
1107 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1108 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001109 case TargetLowering::Legal: break;
1110 case TargetLowering::Custom:
1111 Tmp1 = TLI.LowerOperation(Result, DAG);
1112 if (Tmp1.Val) Result = Tmp1;
1113 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001114 case TargetLowering::Expand:
1115 // Expand brcond's setcc into its constituent parts and create a BR_CC
1116 // Node.
1117 if (Tmp2.getOpcode() == ISD::SETCC) {
1118 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1119 Tmp2.getOperand(0), Tmp2.getOperand(1),
1120 Node->getOperand(2));
1121 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001122 // Make sure the condition is either zero or one. It may have been
1123 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001124 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1125 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1126 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001127
Nate Begeman371e4952005-08-16 19:49:35 +00001128 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1129 DAG.getCondCode(ISD::SETNE), Tmp2,
1130 DAG.getConstant(0, Tmp2.getValueType()),
1131 Node->getOperand(2));
1132 }
1133 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001134 }
1135 break;
1136 case ISD::BR_CC:
1137 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001138 // Ensure that libcalls are emitted before a branch.
1139 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1140 Tmp1 = LegalizeOp(Tmp1);
1141 LastCALLSEQ_END = DAG.getEntryNode();
1142
Nate Begeman7e7f4392006-02-01 07:19:44 +00001143 Tmp2 = Node->getOperand(2); // LHS
1144 Tmp3 = Node->getOperand(3); // RHS
1145 Tmp4 = Node->getOperand(1); // CC
1146
1147 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1148
1149 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1150 // the LHS is a legal SETCC itself. In this case, we need to compare
1151 // the result against zero to select between true and false values.
1152 if (Tmp3.Val == 0) {
1153 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1154 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001155 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001156
1157 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1158 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001159
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001160 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1161 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001162 case TargetLowering::Legal: break;
1163 case TargetLowering::Custom:
1164 Tmp4 = TLI.LowerOperation(Result, DAG);
1165 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001166 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001167 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001168 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001169 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001170 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1171 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001172
Evan Cheng31d15fa2005-12-23 07:29:34 +00001173 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001174 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1175 Tmp2 = Result.getValue(0);
1176 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001177
Evan Cheng31d15fa2005-12-23 07:29:34 +00001178 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1179 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001180 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001181 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001182 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001183 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001184 Tmp2 = LegalizeOp(Tmp1);
1185 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001186 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001187 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001188 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001189 // Since loads produce two values, make sure to remember that we
1190 // legalized both of them.
1191 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1192 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1193 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001194 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001195 case ISD::EXTLOAD:
1196 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001197 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001198 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1199 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001200
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001201 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001202 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001203 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001204 case TargetLowering::Promote:
1205 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001206 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1207 DAG.getValueType(MVT::i8));
1208 Tmp1 = Result.getValue(0);
1209 Tmp2 = Result.getValue(1);
1210 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001211 case TargetLowering::Custom:
1212 isCustom = true;
1213 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001214 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001215 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1216 Node->getOperand(3));
1217 Tmp1 = Result.getValue(0);
1218 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001219
1220 if (isCustom) {
1221 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1222 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001223 Tmp1 = LegalizeOp(Tmp3);
1224 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001225 }
1226 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001227 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001228 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001229 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001230 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1231 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001232 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001233 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1234 Tmp2 = LegalizeOp(Load.getValue(1));
1235 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001236 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001237 assert(Node->getOpcode() != ISD::EXTLOAD &&
1238 "EXTLOAD should always be supported!");
1239 // Turn the unsupported load into an EXTLOAD followed by an explicit
1240 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001241 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1242 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001243 SDOperand ValRes;
1244 if (Node->getOpcode() == ISD::SEXTLOAD)
1245 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001246 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001247 else
1248 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001249 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1250 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1251 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001252 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001253 // Since loads produce two values, make sure to remember that we legalized
1254 // both of them.
1255 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1256 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1257 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001258 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001259 case ISD::EXTRACT_ELEMENT: {
1260 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1261 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001262 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001263 case Legal:
1264 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1265 // 1 -> Hi
1266 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1267 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1268 TLI.getShiftAmountTy()));
1269 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1270 } else {
1271 // 0 -> Lo
1272 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1273 Node->getOperand(0));
1274 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001275 break;
1276 case Expand:
1277 // Get both the low and high parts.
1278 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1279 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1280 Result = Tmp2; // 1 -> Hi
1281 else
1282 Result = Tmp1; // 0 -> Lo
1283 break;
1284 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001285 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001286 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001287
1288 case ISD::CopyToReg:
1289 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001290
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001291 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001292 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001293 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001294 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001295 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001296 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001297 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001298 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001299 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001300 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1302 Tmp3);
1303 } else {
1304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001305 }
1306
1307 // Since this produces two values, make sure to remember that we legalized
1308 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001309 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001310 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001311 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001312 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001313 break;
1314
1315 case ISD::RET:
1316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001317
1318 // Ensure that libcalls are emitted before a return.
1319 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1320 Tmp1 = LegalizeOp(Tmp1);
1321 LastCALLSEQ_END = DAG.getEntryNode();
1322
Chris Lattnerdc750592005-01-07 07:47:09 +00001323 switch (Node->getNumOperands()) {
1324 case 2: // ret val
1325 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1326 case Legal:
1327 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001329 break;
1330 case Expand: {
1331 SDOperand Lo, Hi;
1332 ExpandOp(Node->getOperand(1), Lo, Hi);
1333 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001334 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001335 }
1336 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001337 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001338 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1339 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001340 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001341 }
1342 break;
1343 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001344 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001345 break;
1346 default: { // ret <values>
1347 std::vector<SDOperand> NewValues;
1348 NewValues.push_back(Tmp1);
1349 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1350 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1351 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001352 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001353 break;
1354 case Expand: {
1355 SDOperand Lo, Hi;
1356 ExpandOp(Node->getOperand(i), Lo, Hi);
1357 NewValues.push_back(Lo);
1358 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001359 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001360 }
1361 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001362 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001363 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001364
1365 if (NewValues.size() == Node->getNumOperands())
1366 Result = DAG.UpdateNodeOperands(Result, NewValues);
1367 else
1368 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001369 break;
1370 }
1371 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001372
Chris Lattner4d1ea712006-01-29 21:02:23 +00001373 if (Result.getOpcode() == ISD::RET) {
1374 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1375 default: assert(0 && "This action is not supported yet!");
1376 case TargetLowering::Legal: break;
1377 case TargetLowering::Custom:
1378 Tmp1 = TLI.LowerOperation(Result, DAG);
1379 if (Tmp1.Val) Result = Tmp1;
1380 break;
1381 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001382 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001383 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001384 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001385 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1386 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1387
Chris Lattnere69daaf2005-01-08 06:25:56 +00001388 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001389 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001390 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001391 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001392 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001393 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001394 } else {
1395 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001396 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001397 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001398 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1399 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001400 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001401 }
1402
Chris Lattnerdc750592005-01-07 07:47:09 +00001403 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1404 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001405 Tmp3 = LegalizeOp(Node->getOperand(1));
1406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1407 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001408
Chris Lattnerd02b0542006-01-28 10:58:55 +00001409 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001410 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1411 default: assert(0 && "This action is not supported yet!");
1412 case TargetLowering::Legal: break;
1413 case TargetLowering::Custom:
1414 Tmp1 = TLI.LowerOperation(Result, DAG);
1415 if (Tmp1.Val) Result = Tmp1;
1416 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001417 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001418 break;
1419 }
1420 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001421 // Truncate the value and store the result.
1422 Tmp3 = PromoteOp(Node->getOperand(1));
1423 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001424 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001425 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001426 break;
1427
Chris Lattnerdc750592005-01-07 07:47:09 +00001428 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001429 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001430 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001431
1432 // If this is a vector type, then we have to calculate the increment as
1433 // the product of the element size in bytes, and the number of elements
1434 // in the high half of the vector.
1435 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1436 SDNode *InVal = Node->getOperand(1).Val;
1437 unsigned NumElems =
1438 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1439 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1440
1441 // Figure out if there is a Packed type corresponding to this Vector
1442 // type. If so, convert to the packed type.
1443 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1444 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1445 // Turn this into a normal store of the packed type.
1446 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1447 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1448 Node->getOperand(3));
1449 break;
1450 } else if (NumElems == 1) {
1451 // Turn this into a normal store of the scalar type.
1452 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1454 Node->getOperand(3));
1455 break;
1456 } else {
1457 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1458 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1459 }
1460 } else {
1461 ExpandOp(Node->getOperand(1), Lo, Hi);
1462 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1463 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001464
1465 if (!TLI.isLittleEndian())
1466 std::swap(Lo, Hi);
1467
Chris Lattner55e9cde2005-05-11 04:51:16 +00001468 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1469 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001470 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1471 getIntPtrConstant(IncrementSize));
1472 assert(isTypeLegal(Tmp2.getValueType()) &&
1473 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001474 // FIXME: This sets the srcvalue of both halves to be the same, which is
1475 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001476 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1477 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001478 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1479 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001480 }
1481 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001482 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001483 case ISD::PCMARKER:
1484 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001485 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001486 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001487 case ISD::STACKSAVE:
1488 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001489 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1490 Tmp1 = Result.getValue(0);
1491 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001492
Chris Lattnerb3266452006-01-13 02:50:02 +00001493 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1494 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001495 case TargetLowering::Legal: break;
1496 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001497 Tmp3 = TLI.LowerOperation(Result, DAG);
1498 if (Tmp3.Val) {
1499 Tmp1 = LegalizeOp(Tmp3);
1500 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001501 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001502 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001503 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001504 // Expand to CopyFromReg if the target set
1505 // StackPointerRegisterToSaveRestore.
1506 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001507 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001508 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001509 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001510 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001511 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1512 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001513 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001514 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001515 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001516
1517 // Since stacksave produce two values, make sure to remember that we
1518 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001519 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1520 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1521 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001522
Chris Lattnerb3266452006-01-13 02:50:02 +00001523 case ISD::STACKRESTORE:
1524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1525 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001526 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001527
1528 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1529 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001530 case TargetLowering::Legal: break;
1531 case TargetLowering::Custom:
1532 Tmp1 = TLI.LowerOperation(Result, DAG);
1533 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001534 break;
1535 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001536 // Expand to CopyToReg if the target set
1537 // StackPointerRegisterToSaveRestore.
1538 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1539 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1540 } else {
1541 Result = Tmp1;
1542 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001543 break;
1544 }
1545 break;
1546
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001547 case ISD::READCYCLECOUNTER:
1548 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001549 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001550
1551 // Since rdcc produce two values, make sure to remember that we legalized
1552 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001553 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001554 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001555 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001556
Evan Cheng31d15fa2005-12-23 07:29:34 +00001557 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001558 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1559 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1560
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001561 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1562 "Cannot handle illegal TRUNCSTORE yet!");
1563 Tmp2 = LegalizeOp(Node->getOperand(1));
1564
1565 // The only promote case we handle is TRUNCSTORE:i1 X into
1566 // -> TRUNCSTORE:i8 (and X, 1)
1567 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1568 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1569 TargetLowering::Promote) {
1570 // Promote the bool to a mask then store.
1571 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1572 DAG.getConstant(1, Tmp2.getValueType()));
1573 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1574 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001575
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001576 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1577 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001578 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1579 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001580 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001581
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001582 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1583 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1584 default: assert(0 && "This action is not supported yet!");
1585 case TargetLowering::Legal: break;
1586 case TargetLowering::Custom:
1587 Tmp1 = TLI.LowerOperation(Result, DAG);
1588 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001589 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001590 }
1591 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001592 }
Chris Lattner39c67442005-01-14 22:08:15 +00001593 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001594 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1595 case Expand: assert(0 && "It's impossible to expand bools");
1596 case Legal:
1597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1598 break;
1599 case Promote:
1600 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1601 break;
1602 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001603 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001604 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001605
Chris Lattnerd02b0542006-01-28 10:58:55 +00001606 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001607
Nate Begeman987121a2005-08-23 04:29:48 +00001608 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001609 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001610 case TargetLowering::Legal: break;
1611 case TargetLowering::Custom: {
1612 Tmp1 = TLI.LowerOperation(Result, DAG);
1613 if (Tmp1.Val) Result = Tmp1;
1614 break;
1615 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001616 case TargetLowering::Expand:
1617 if (Tmp1.getOpcode() == ISD::SETCC) {
1618 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1619 Tmp2, Tmp3,
1620 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1621 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001622 // Make sure the condition is either zero or one. It may have been
1623 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001624 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1625 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1626 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001627 Result = DAG.getSelectCC(Tmp1,
1628 DAG.getConstant(0, Tmp1.getValueType()),
1629 Tmp2, Tmp3, ISD::SETNE);
1630 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001631 break;
1632 case TargetLowering::Promote: {
1633 MVT::ValueType NVT =
1634 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1635 unsigned ExtOp, TruncOp;
1636 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001637 ExtOp = ISD::ANY_EXTEND;
1638 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001639 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001640 ExtOp = ISD::FP_EXTEND;
1641 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001642 }
1643 // Promote each of the values to the new type.
1644 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1645 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1646 // Perform the larger operation, then round down.
1647 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1648 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1649 break;
1650 }
1651 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001652 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001653 case ISD::SELECT_CC: {
1654 Tmp1 = Node->getOperand(0); // LHS
1655 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001656 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1657 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001658 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001659
Nate Begeman7e7f4392006-02-01 07:19:44 +00001660 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1661
1662 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1663 // the LHS is a legal SETCC itself. In this case, we need to compare
1664 // the result against zero to select between true and false values.
1665 if (Tmp2.Val == 0) {
1666 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1667 CC = DAG.getCondCode(ISD::SETNE);
1668 }
1669 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1670
1671 // Everything is legal, see if we should expand this op or something.
1672 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1673 default: assert(0 && "This action is not supported yet!");
1674 case TargetLowering::Legal: break;
1675 case TargetLowering::Custom:
1676 Tmp1 = TLI.LowerOperation(Result, DAG);
1677 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001678 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001679 }
1680 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001681 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001682 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001683 Tmp1 = Node->getOperand(0);
1684 Tmp2 = Node->getOperand(1);
1685 Tmp3 = Node->getOperand(2);
1686 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1687
1688 // If we had to Expand the SetCC operands into a SELECT node, then it may
1689 // not always be possible to return a true LHS & RHS. In this case, just
1690 // return the value we legalized, returned in the LHS
1691 if (Tmp2.Val == 0) {
1692 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001693 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001694 }
Nate Begeman987121a2005-08-23 04:29:48 +00001695
Chris Lattnerf263a232006-01-30 22:43:50 +00001696 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001697 default: assert(0 && "Cannot handle this action for SETCC yet!");
1698 case TargetLowering::Custom:
1699 isCustom = true;
1700 // FALLTHROUGH.
1701 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001702 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001703 if (isCustom) {
1704 Tmp3 = TLI.LowerOperation(Result, DAG);
1705 if (Tmp3.Val) Result = Tmp3;
1706 }
Nate Begeman987121a2005-08-23 04:29:48 +00001707 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001708 case TargetLowering::Promote: {
1709 // First step, figure out the appropriate operation to use.
1710 // Allow SETCC to not be supported for all legal data types
1711 // Mostly this targets FP
1712 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1713 MVT::ValueType OldVT = NewInTy;
1714
1715 // Scan for the appropriate larger type to use.
1716 while (1) {
1717 NewInTy = (MVT::ValueType)(NewInTy+1);
1718
1719 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1720 "Fell off of the edge of the integer world");
1721 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1722 "Fell off of the edge of the floating point world");
1723
1724 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001725 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001726 break;
1727 }
1728 if (MVT::isInteger(NewInTy))
1729 assert(0 && "Cannot promote Legal Integer SETCC yet");
1730 else {
1731 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1732 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1733 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001734 Tmp1 = LegalizeOp(Tmp1);
1735 Tmp2 = LegalizeOp(Tmp2);
1736 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001737 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001738 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001739 }
Nate Begeman987121a2005-08-23 04:29:48 +00001740 case TargetLowering::Expand:
1741 // Expand a setcc node into a select_cc of the same condition, lhs, and
1742 // rhs that selects between const 1 (true) and const 0 (false).
1743 MVT::ValueType VT = Node->getValueType(0);
1744 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1745 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1746 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001747 break;
1748 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001749 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001750 case ISD::MEMSET:
1751 case ISD::MEMCPY:
1752 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001753 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001754 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1755
1756 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1757 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1758 case Expand: assert(0 && "Cannot expand a byte!");
1759 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001760 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001761 break;
1762 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001763 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001764 break;
1765 }
1766 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001767 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001768 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001769
1770 SDOperand Tmp4;
1771 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001772 case Expand: {
1773 // Length is too big, just take the lo-part of the length.
1774 SDOperand HiPart;
1775 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1776 break;
1777 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001778 case Legal:
1779 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001780 break;
1781 case Promote:
1782 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001783 break;
1784 }
1785
1786 SDOperand Tmp5;
1787 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1788 case Expand: assert(0 && "Cannot expand this yet!");
1789 case Legal:
1790 Tmp5 = LegalizeOp(Node->getOperand(4));
1791 break;
1792 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001793 Tmp5 = PromoteOp(Node->getOperand(4));
1794 break;
1795 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001796
1797 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1798 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001799 case TargetLowering::Custom:
1800 isCustom = true;
1801 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001802 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001803 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001804 if (isCustom) {
1805 Tmp1 = TLI.LowerOperation(Result, DAG);
1806 if (Tmp1.Val) Result = Tmp1;
1807 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001808 break;
1809 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001810 // Otherwise, the target does not support this operation. Lower the
1811 // operation to an explicit libcall as appropriate.
1812 MVT::ValueType IntPtr = TLI.getPointerTy();
1813 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1814 std::vector<std::pair<SDOperand, const Type*> > Args;
1815
Reid Spencer6dced922005-01-12 14:53:45 +00001816 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001817 if (Node->getOpcode() == ISD::MEMSET) {
1818 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001819 // Extend the (previously legalized) ubyte argument to be an int value
1820 // for the call.
1821 if (Tmp3.getValueType() > MVT::i32)
1822 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1823 else
1824 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001825 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1826 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1827
1828 FnName = "memset";
1829 } else if (Node->getOpcode() == ISD::MEMCPY ||
1830 Node->getOpcode() == ISD::MEMMOVE) {
1831 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1832 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1833 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1834 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1835 } else {
1836 assert(0 && "Unknown op!");
1837 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001838
Chris Lattner85d70c62005-01-11 05:57:22 +00001839 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001840 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001841 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001842 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001843 break;
1844 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001845 }
1846 break;
1847 }
Chris Lattner5385db52005-05-09 20:23:03 +00001848
Chris Lattner4157c412005-04-02 04:00:59 +00001849 case ISD::SHL_PARTS:
1850 case ISD::SRA_PARTS:
1851 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001852 std::vector<SDOperand> Ops;
1853 bool Changed = false;
1854 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1855 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1856 Changed |= Ops.back() != Node->getOperand(i);
1857 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001858 if (Changed)
1859 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001860
Evan Cheng870e4f82006-01-09 18:31:59 +00001861 switch (TLI.getOperationAction(Node->getOpcode(),
1862 Node->getValueType(0))) {
1863 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001864 case TargetLowering::Legal: break;
1865 case TargetLowering::Custom:
1866 Tmp1 = TLI.LowerOperation(Result, DAG);
1867 if (Tmp1.Val) {
1868 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001869 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001870 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001871 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1872 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001873 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001874 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001875 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001876 return RetVal;
1877 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001878 break;
1879 }
1880
Chris Lattner13fe99c2005-04-02 05:00:07 +00001881 // Since these produce multiple values, make sure to remember that we
1882 // legalized all of them.
1883 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1884 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1885 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001886 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001887
1888 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001889 case ISD::ADD:
1890 case ISD::SUB:
1891 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001892 case ISD::MULHS:
1893 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001894 case ISD::UDIV:
1895 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001896 case ISD::AND:
1897 case ISD::OR:
1898 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001899 case ISD::SHL:
1900 case ISD::SRL:
1901 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001902 case ISD::FADD:
1903 case ISD::FSUB:
1904 case ISD::FMUL:
1905 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001906 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001907 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1908 case Expand: assert(0 && "Not possible");
1909 case Legal:
1910 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1911 break;
1912 case Promote:
1913 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1914 break;
1915 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001916
1917 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001918
Andrew Lenharth72594262005-12-24 23:42:32 +00001919 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001920 default: assert(0 && "Operation not supported");
1921 case TargetLowering::Legal: break;
1922 case TargetLowering::Custom:
1923 Tmp1 = TLI.LowerOperation(Result, DAG);
1924 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001925 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001926 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001927 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001928
1929 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1930 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1931 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1932 case Expand: assert(0 && "Not possible");
1933 case Legal:
1934 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1935 break;
1936 case Promote:
1937 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1938 break;
1939 }
1940
1941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1942
1943 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1944 default: assert(0 && "Operation not supported");
1945 case TargetLowering::Custom:
1946 Tmp1 = TLI.LowerOperation(Result, DAG);
1947 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00001948 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001949 case TargetLowering::Legal: break;
1950 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00001951 // If this target supports fabs/fneg natively, do this efficiently.
1952 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1953 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1954 // Get the sign bit of the RHS.
1955 MVT::ValueType IVT =
1956 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1957 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1958 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1959 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1960 // Get the absolute value of the result.
1961 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1962 // Select between the nabs and abs value based on the sign bit of
1963 // the input.
1964 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1965 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1966 AbsVal),
1967 AbsVal);
1968 Result = LegalizeOp(Result);
1969 break;
1970 }
1971
1972 // Otherwise, do bitwise ops!
1973
1974 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001975 const char *FnName;
1976 if (Node->getValueType(0) == MVT::f32) {
1977 FnName = "copysignf";
1978 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1979 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1980 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1981 } else {
1982 FnName = "copysign";
1983 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1984 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1985 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1986 }
1987 SDOperand Dummy;
1988 Result = ExpandLibCall(FnName, Node, Dummy);
1989 break;
1990 }
1991 break;
1992
Nate Begeman5965bd12006-02-17 05:43:56 +00001993 case ISD::ADDC:
1994 case ISD::SUBC:
1995 Tmp1 = LegalizeOp(Node->getOperand(0));
1996 Tmp2 = LegalizeOp(Node->getOperand(1));
1997 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1998 // Since this produces two values, make sure to remember that we legalized
1999 // both of them.
2000 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2001 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2002 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002003
Nate Begeman5965bd12006-02-17 05:43:56 +00002004 case ISD::ADDE:
2005 case ISD::SUBE:
2006 Tmp1 = LegalizeOp(Node->getOperand(0));
2007 Tmp2 = LegalizeOp(Node->getOperand(1));
2008 Tmp3 = LegalizeOp(Node->getOperand(2));
2009 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2010 // Since this produces two values, make sure to remember that we legalized
2011 // both of them.
2012 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2013 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2014 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002015
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002016 case ISD::BUILD_PAIR: {
2017 MVT::ValueType PairTy = Node->getValueType(0);
2018 // TODO: handle the case where the Lo and Hi operands are not of legal type
2019 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2020 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2021 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002022 case TargetLowering::Promote:
2023 case TargetLowering::Custom:
2024 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002025 case TargetLowering::Legal:
2026 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2027 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2028 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002029 case TargetLowering::Expand:
2030 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2031 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2032 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2033 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2034 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002035 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002036 break;
2037 }
2038 break;
2039 }
2040
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002041 case ISD::UREM:
2042 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002043 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002044 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2045 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002046
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002047 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002048 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2049 case TargetLowering::Custom:
2050 isCustom = true;
2051 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002052 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002053 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002054 if (isCustom) {
2055 Tmp1 = TLI.LowerOperation(Result, DAG);
2056 if (Tmp1.Val) Result = Tmp1;
2057 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002058 break;
Chris Lattner81914422005-08-03 20:31:37 +00002059 case TargetLowering::Expand:
2060 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002061 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002062 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002063 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002064 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2065 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2066 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2067 } else {
2068 // Floating point mod -> fmod libcall.
2069 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2070 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002071 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002072 }
2073 break;
2074 }
2075 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002076 case ISD::VAARG: {
2077 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2078 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2079
Chris Lattner364b89a2006-01-28 07:42:08 +00002080 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002081 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2082 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002083 case TargetLowering::Custom:
2084 isCustom = true;
2085 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002086 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2088 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002089 Tmp1 = Result.getValue(1);
2090
2091 if (isCustom) {
2092 Tmp2 = TLI.LowerOperation(Result, DAG);
2093 if (Tmp2.Val) {
2094 Result = LegalizeOp(Tmp2);
2095 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2096 }
2097 }
Nate Begemane74795c2006-01-25 18:21:52 +00002098 break;
2099 case TargetLowering::Expand: {
2100 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2101 Node->getOperand(2));
2102 // Increment the pointer, VAList, to the next vaarg
2103 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2104 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2105 TLI.getPointerTy()));
2106 // Store the incremented VAList to the legalized pointer
2107 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2108 Node->getOperand(2));
2109 // Load the actual argument out of the pointer VAList
2110 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002111 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002112 Result = LegalizeOp(Result);
2113 break;
2114 }
2115 }
2116 // Since VAARG produces two values, make sure to remember that we
2117 // legalized both of them.
2118 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002119 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2120 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002121 }
2122
2123 case ISD::VACOPY:
2124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2125 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2126 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2127
2128 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2129 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002130 case TargetLowering::Custom:
2131 isCustom = true;
2132 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002133 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2135 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002136 if (isCustom) {
2137 Tmp1 = TLI.LowerOperation(Result, DAG);
2138 if (Tmp1.Val) Result = Tmp1;
2139 }
Nate Begemane74795c2006-01-25 18:21:52 +00002140 break;
2141 case TargetLowering::Expand:
2142 // This defaults to loading a pointer from the input and storing it to the
2143 // output, returning the chain.
2144 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2145 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2146 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002147 break;
2148 }
2149 break;
2150
2151 case ISD::VAEND:
2152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2153 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2154
2155 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2156 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002157 case TargetLowering::Custom:
2158 isCustom = true;
2159 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002160 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002161 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002162 if (isCustom) {
2163 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2164 if (Tmp1.Val) Result = Tmp1;
2165 }
Nate Begemane74795c2006-01-25 18:21:52 +00002166 break;
2167 case TargetLowering::Expand:
2168 Result = Tmp1; // Default to a no-op, return the chain
2169 break;
2170 }
2171 break;
2172
2173 case ISD::VASTART:
2174 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2175 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2176
Chris Lattnerd02b0542006-01-28 10:58:55 +00002177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2178
Nate Begemane74795c2006-01-25 18:21:52 +00002179 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2180 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002181 case TargetLowering::Legal: break;
2182 case TargetLowering::Custom:
2183 Tmp1 = TLI.LowerOperation(Result, DAG);
2184 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002185 break;
2186 }
2187 break;
2188
Nate Begeman1b8121b2006-01-11 21:21:00 +00002189 case ISD::ROTL:
2190 case ISD::ROTR:
2191 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2192 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002193
2194 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2195 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002196 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002197 break;
2198
Nate Begeman2fba8a32006-01-14 03:14:10 +00002199 case ISD::BSWAP:
2200 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2201 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002202 case TargetLowering::Custom:
2203 assert(0 && "Cannot custom legalize this yet!");
2204 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002205 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002206 break;
2207 case TargetLowering::Promote: {
2208 MVT::ValueType OVT = Tmp1.getValueType();
2209 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2210 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002211
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002212 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2213 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2214 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2215 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2216 break;
2217 }
2218 case TargetLowering::Expand:
2219 Result = ExpandBSWAP(Tmp1);
2220 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002221 }
2222 break;
2223
Andrew Lenharth5e177822005-05-03 17:19:30 +00002224 case ISD::CTPOP:
2225 case ISD::CTTZ:
2226 case ISD::CTLZ:
2227 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2228 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002229 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002230 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002231 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002232 break;
2233 case TargetLowering::Promote: {
2234 MVT::ValueType OVT = Tmp1.getValueType();
2235 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002236
2237 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002238 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2239 // Perform the larger operation, then subtract if needed.
2240 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002241 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002242 case ISD::CTPOP:
2243 Result = Tmp1;
2244 break;
2245 case ISD::CTTZ:
2246 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002247 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2248 DAG.getConstant(getSizeInBits(NVT), NVT),
2249 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002250 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002251 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2252 break;
2253 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002254 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002255 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2256 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002257 getSizeInBits(OVT), NVT));
2258 break;
2259 }
2260 break;
2261 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002262 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002263 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002264 break;
2265 }
2266 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002267
Chris Lattner13fe99c2005-04-02 05:00:07 +00002268 // Unary operators
2269 case ISD::FABS:
2270 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002271 case ISD::FSQRT:
2272 case ISD::FSIN:
2273 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002274 Tmp1 = LegalizeOp(Node->getOperand(0));
2275 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002276 case TargetLowering::Promote:
2277 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002278 isCustom = true;
2279 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002280 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002281 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002282 if (isCustom) {
2283 Tmp1 = TLI.LowerOperation(Result, DAG);
2284 if (Tmp1.Val) Result = Tmp1;
2285 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002286 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002287 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002288 switch (Node->getOpcode()) {
2289 default: assert(0 && "Unreachable!");
2290 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002291 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2292 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002293 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002294 break;
Chris Lattner80026402005-04-30 04:43:14 +00002295 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002296 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2297 MVT::ValueType VT = Node->getValueType(0);
2298 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002299 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002300 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2301 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002302 break;
2303 }
2304 case ISD::FSQRT:
2305 case ISD::FSIN:
2306 case ISD::FCOS: {
2307 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002308 const char *FnName = 0;
2309 switch(Node->getOpcode()) {
2310 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2311 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2312 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2313 default: assert(0 && "Unreachable!");
2314 }
Nate Begeman77558da2005-08-04 21:43:28 +00002315 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002316 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002317 break;
2318 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002319 }
2320 break;
2321 }
2322 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002323
2324 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002325 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002326 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002327 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002328 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2329 Node->getOperand(0).getValueType())) {
2330 default: assert(0 && "Unknown operation action!");
2331 case TargetLowering::Expand:
2332 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2333 break;
2334 case TargetLowering::Legal:
2335 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002336 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002337 break;
2338 }
2339 }
2340 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002341 case ISD::VBIT_CONVERT: {
2342 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2343 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2344
2345 // The input has to be a vector type, we have to either scalarize it, pack
2346 // it, or convert it based on whether the input vector type is legal.
2347 SDNode *InVal = Node->getOperand(0).Val;
2348 unsigned NumElems =
2349 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2350 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2351
2352 // Figure out if there is a Packed type corresponding to this Vector
2353 // type. If so, convert to the packed type.
2354 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2355 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2356 // Turn this into a bit convert of the packed input.
2357 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2358 PackVectorOp(Node->getOperand(0), TVT));
2359 break;
2360 } else if (NumElems == 1) {
2361 // Turn this into a bit convert of the scalar input.
2362 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2363 PackVectorOp(Node->getOperand(0), EVT));
2364 break;
2365 } else {
2366 // FIXME: UNIMP! Store then reload
2367 assert(0 && "Cast from unsupported vector type not implemented yet!");
2368 }
2369 }
2370
Chris Lattner13fe99c2005-04-02 05:00:07 +00002371 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002372 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002373 case ISD::UINT_TO_FP: {
2374 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002375 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2376 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002377 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002378 Node->getOperand(0).getValueType())) {
2379 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002380 case TargetLowering::Custom:
2381 isCustom = true;
2382 // FALLTHROUGH
2383 case TargetLowering::Legal:
2384 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002385 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002386 if (isCustom) {
2387 Tmp1 = TLI.LowerOperation(Result, DAG);
2388 if (Tmp1.Val) Result = Tmp1;
2389 }
2390 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002391 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002392 Result = ExpandLegalINT_TO_FP(isSigned,
2393 LegalizeOp(Node->getOperand(0)),
2394 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002395 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002396 case TargetLowering::Promote:
2397 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2398 Node->getValueType(0),
2399 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002400 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002401 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002402 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002403 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002404 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2405 Node->getValueType(0), Node->getOperand(0));
2406 break;
2407 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002408 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002409 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002410 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2411 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002412 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002413 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2414 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002415 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002416 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2417 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002418 break;
2419 }
2420 break;
2421 }
2422 case ISD::TRUNCATE:
2423 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2424 case Legal:
2425 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002426 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002427 break;
2428 case Expand:
2429 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2430
2431 // Since the result is legal, we should just be able to truncate the low
2432 // part of the source.
2433 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2434 break;
2435 case Promote:
2436 Result = PromoteOp(Node->getOperand(0));
2437 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2438 break;
2439 }
2440 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002441
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002442 case ISD::FP_TO_SINT:
2443 case ISD::FP_TO_UINT:
2444 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2445 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002446 Tmp1 = LegalizeOp(Node->getOperand(0));
2447
Chris Lattner44fe26f2005-07-29 00:11:56 +00002448 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2449 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002450 case TargetLowering::Custom:
2451 isCustom = true;
2452 // FALLTHROUGH
2453 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002454 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002455 if (isCustom) {
2456 Tmp1 = TLI.LowerOperation(Result, DAG);
2457 if (Tmp1.Val) Result = Tmp1;
2458 }
2459 break;
2460 case TargetLowering::Promote:
2461 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2462 Node->getOpcode() == ISD::FP_TO_SINT);
2463 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002464 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002465 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2466 SDOperand True, False;
2467 MVT::ValueType VT = Node->getOperand(0).getValueType();
2468 MVT::ValueType NVT = Node->getValueType(0);
2469 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2470 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2471 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2472 Node->getOperand(0), Tmp2, ISD::SETLT);
2473 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2474 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002475 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002476 Tmp2));
2477 False = DAG.getNode(ISD::XOR, NVT, False,
2478 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002479 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2480 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002481 } else {
2482 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2483 }
2484 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002485 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002486 break;
2487 case Expand:
2488 assert(0 && "Shouldn't need to expand other operators here!");
2489 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002490 Tmp1 = PromoteOp(Node->getOperand(0));
2491 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2492 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002493 break;
2494 }
2495 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002496
Chris Lattner7753f172005-09-02 00:18:10 +00002497 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002498 case ISD::ZERO_EXTEND:
2499 case ISD::SIGN_EXTEND:
2500 case ISD::FP_EXTEND:
2501 case ISD::FP_ROUND:
2502 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002503 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002504 case Legal:
2505 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002506 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002507 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002508 case Promote:
2509 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002510 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002511 Tmp1 = PromoteOp(Node->getOperand(0));
2512 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002513 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002514 case ISD::ZERO_EXTEND:
2515 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002516 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002517 Result = DAG.getZeroExtendInReg(Result,
2518 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002519 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002520 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002521 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002522 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002523 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002524 Result,
2525 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002526 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002527 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002528 Result = PromoteOp(Node->getOperand(0));
2529 if (Result.getValueType() != Op.getValueType())
2530 // Dynamically dead while we have only 2 FP types.
2531 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2532 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002533 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002534 Result = PromoteOp(Node->getOperand(0));
2535 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2536 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002537 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002538 }
2539 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002540 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002541 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002542 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002543 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002544
2545 // If this operation is not supported, convert it to a shl/shr or load/store
2546 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002547 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2548 default: assert(0 && "This action not supported for this op yet!");
2549 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002550 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002551 break;
2552 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002553 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002554 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002555 // NOTE: we could fall back on load/store here too for targets without
2556 // SAR. However, it is doubtful that any exist.
2557 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2558 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002559 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002560 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2561 Node->getOperand(0), ShiftCst);
2562 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2563 Result, ShiftCst);
2564 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2565 // The only way we can lower this is to turn it into a STORETRUNC,
2566 // EXTLOAD pair, targetting a temporary location (a stack slot).
2567
2568 // NOTE: there is a choice here between constantly creating new stack
2569 // slots and always reusing the same one. We currently always create
2570 // new ones, as reuse may inhibit scheduling.
2571 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2572 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2573 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2574 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002575 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002576 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2577 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2578 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002579 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002580 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002581 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2582 Result, StackSlot, DAG.getSrcValue(NULL),
2583 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002584 } else {
2585 assert(0 && "Unknown op");
2586 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002587 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002588 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002589 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002590 }
Chris Lattner99222f72005-01-15 07:15:18 +00002591 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002592
2593 // Make sure that the generated code is itself legal.
2594 if (Result != Op)
2595 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002596
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002597 // Note that LegalizeOp may be reentered even from single-use nodes, which
2598 // means that we always must cache transformed nodes.
2599 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002600 return Result;
2601}
2602
Chris Lattner4d978642005-01-15 22:16:26 +00002603/// PromoteOp - Given an operation that produces a value in an invalid type,
2604/// promote it to compute the value into a larger type. The produced value will
2605/// have the correct bits for the low portion of the register, but no guarantee
2606/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002607SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2608 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002609 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002610 assert(getTypeAction(VT) == Promote &&
2611 "Caller should expand or legalize operands that are not promotable!");
2612 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2613 "Cannot promote to smaller type!");
2614
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002615 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002616 SDOperand Result;
2617 SDNode *Node = Op.Val;
2618
Chris Lattner1a570f12005-09-02 20:32:45 +00002619 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2620 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002621
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002622 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002623 case ISD::CopyFromReg:
2624 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002625 default:
2626 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2627 assert(0 && "Do not know how to promote this operator!");
2628 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002629 case ISD::UNDEF:
2630 Result = DAG.getNode(ISD::UNDEF, NVT);
2631 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002632 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002633 if (VT != MVT::i1)
2634 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2635 else
2636 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002637 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2638 break;
2639 case ISD::ConstantFP:
2640 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2641 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2642 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002643
Chris Lattner2cb338d2005-01-18 02:59:52 +00002644 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002645 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002646 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2647 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002648 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002649
2650 case ISD::TRUNCATE:
2651 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2652 case Legal:
2653 Result = LegalizeOp(Node->getOperand(0));
2654 assert(Result.getValueType() >= NVT &&
2655 "This truncation doesn't make sense!");
2656 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2657 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2658 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002659 case Promote:
2660 // The truncation is not required, because we don't guarantee anything
2661 // about high bits anyway.
2662 Result = PromoteOp(Node->getOperand(0));
2663 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002664 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002665 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2666 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002667 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002668 }
2669 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002670 case ISD::SIGN_EXTEND:
2671 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002672 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002673 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2674 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2675 case Legal:
2676 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002677 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002678 break;
2679 case Promote:
2680 // Promote the reg if it's smaller.
2681 Result = PromoteOp(Node->getOperand(0));
2682 // The high bits are not guaranteed to be anything. Insert an extend.
2683 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002684 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002685 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002686 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002687 Result = DAG.getZeroExtendInReg(Result,
2688 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002689 break;
2690 }
2691 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002692 case ISD::BIT_CONVERT:
2693 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2694 Result = PromoteOp(Result);
2695 break;
2696
Chris Lattner4d978642005-01-15 22:16:26 +00002697 case ISD::FP_EXTEND:
2698 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2699 case ISD::FP_ROUND:
2700 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2701 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2702 case Promote: assert(0 && "Unreachable with 2 FP types!");
2703 case Legal:
2704 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002705 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002706 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002707 break;
2708 }
2709 break;
2710
2711 case ISD::SINT_TO_FP:
2712 case ISD::UINT_TO_FP:
2713 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2714 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002715 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002716 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002717 break;
2718
2719 case Promote:
2720 Result = PromoteOp(Node->getOperand(0));
2721 if (Node->getOpcode() == ISD::SINT_TO_FP)
2722 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002723 Result,
2724 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002725 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002726 Result = DAG.getZeroExtendInReg(Result,
2727 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002728 // No extra round required here.
2729 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002730 break;
2731 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002732 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2733 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002734 // Round if we cannot tolerate excess precision.
2735 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002736 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2737 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002738 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002739 }
Chris Lattner4d978642005-01-15 22:16:26 +00002740 break;
2741
Chris Lattner268d4572005-12-09 17:32:47 +00002742 case ISD::SIGN_EXTEND_INREG:
2743 Result = PromoteOp(Node->getOperand(0));
2744 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2745 Node->getOperand(1));
2746 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002747 case ISD::FP_TO_SINT:
2748 case ISD::FP_TO_UINT:
2749 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2750 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002751 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002752 break;
2753 case Promote:
2754 // The input result is prerounded, so we don't have to do anything
2755 // special.
2756 Tmp1 = PromoteOp(Node->getOperand(0));
2757 break;
2758 case Expand:
2759 assert(0 && "not implemented");
2760 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002761 // If we're promoting a UINT to a larger size, check to see if the new node
2762 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2763 // we can use that instead. This allows us to generate better code for
2764 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2765 // legal, such as PowerPC.
2766 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002767 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002768 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2769 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002770 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2771 } else {
2772 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2773 }
Chris Lattner4d978642005-01-15 22:16:26 +00002774 break;
2775
Chris Lattner13fe99c2005-04-02 05:00:07 +00002776 case ISD::FABS:
2777 case ISD::FNEG:
2778 Tmp1 = PromoteOp(Node->getOperand(0));
2779 assert(Tmp1.getValueType() == NVT);
2780 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2781 // NOTE: we do not have to do any extra rounding here for
2782 // NoExcessFPPrecision, because we know the input will have the appropriate
2783 // precision, and these operations don't modify precision at all.
2784 break;
2785
Chris Lattner9d6fa982005-04-28 21:44:33 +00002786 case ISD::FSQRT:
2787 case ISD::FSIN:
2788 case ISD::FCOS:
2789 Tmp1 = PromoteOp(Node->getOperand(0));
2790 assert(Tmp1.getValueType() == NVT);
2791 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002792 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002793 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2794 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002795 break;
2796
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002797 case ISD::AND:
2798 case ISD::OR:
2799 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002800 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002801 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002802 case ISD::MUL:
2803 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002804 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002805 // that too is okay if they are integer operations.
2806 Tmp1 = PromoteOp(Node->getOperand(0));
2807 Tmp2 = PromoteOp(Node->getOperand(1));
2808 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2809 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002810 break;
2811 case ISD::FADD:
2812 case ISD::FSUB:
2813 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002814 Tmp1 = PromoteOp(Node->getOperand(0));
2815 Tmp2 = PromoteOp(Node->getOperand(1));
2816 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2817 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2818
2819 // Floating point operations will give excess precision that we may not be
2820 // able to tolerate. If we DO allow excess precision, just leave it,
2821 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002822 // FIXME: Why would we need to round FP ops more than integer ones?
2823 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002824 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002825 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2826 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002827 break;
2828
Chris Lattner4d978642005-01-15 22:16:26 +00002829 case ISD::SDIV:
2830 case ISD::SREM:
2831 // These operators require that their input be sign extended.
2832 Tmp1 = PromoteOp(Node->getOperand(0));
2833 Tmp2 = PromoteOp(Node->getOperand(1));
2834 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002835 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2836 DAG.getValueType(VT));
2837 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2838 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002839 }
2840 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2841
2842 // Perform FP_ROUND: this is probably overly pessimistic.
2843 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002844 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2845 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002846 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002847 case ISD::FDIV:
2848 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002849 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002850 // These operators require that their input be fp extended.
2851 Tmp1 = PromoteOp(Node->getOperand(0));
2852 Tmp2 = PromoteOp(Node->getOperand(1));
2853 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2854
2855 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002856 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002857 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2858 DAG.getValueType(VT));
2859 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002860
2861 case ISD::UDIV:
2862 case ISD::UREM:
2863 // These operators require that their input be zero extended.
2864 Tmp1 = PromoteOp(Node->getOperand(0));
2865 Tmp2 = PromoteOp(Node->getOperand(1));
2866 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002867 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2868 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002869 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2870 break;
2871
2872 case ISD::SHL:
2873 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002874 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002875 break;
2876 case ISD::SRA:
2877 // The input value must be properly sign extended.
2878 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002879 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2880 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002881 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002882 break;
2883 case ISD::SRL:
2884 // The input value must be properly zero extended.
2885 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002886 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002887 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002888 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002889
2890 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002891 Tmp1 = Node->getOperand(0); // Get the chain.
2892 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002893 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2894 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2895 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2896 } else {
2897 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2898 Node->getOperand(2));
2899 // Increment the pointer, VAList, to the next vaarg
2900 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2901 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2902 TLI.getPointerTy()));
2903 // Store the incremented VAList to the legalized pointer
2904 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2905 Node->getOperand(2));
2906 // Load the actual argument out of the pointer VAList
2907 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2908 DAG.getSrcValue(0), VT);
2909 }
2910 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002911 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002912 break;
2913
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002914 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002915 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2916 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002917 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002918 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002919 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002920 case ISD::SEXTLOAD:
2921 case ISD::ZEXTLOAD:
2922 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002923 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2924 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002925 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002926 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002927 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002928 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002929 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002930 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2931 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002932 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002933 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002934 case ISD::SELECT_CC:
2935 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2936 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2937 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002938 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002939 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002940 case ISD::BSWAP:
2941 Tmp1 = Node->getOperand(0);
2942 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2943 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2944 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2945 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2946 TLI.getShiftAmountTy()));
2947 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002948 case ISD::CTPOP:
2949 case ISD::CTTZ:
2950 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002951 // Zero extend the argument
2952 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002953 // Perform the larger operation, then subtract if needed.
2954 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002955 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002956 case ISD::CTPOP:
2957 Result = Tmp1;
2958 break;
2959 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002960 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002961 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002962 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002963 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002964 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002965 break;
2966 case ISD::CTLZ:
2967 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002968 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2969 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002970 getSizeInBits(VT), NVT));
2971 break;
2972 }
2973 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002974 }
2975
2976 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002977
2978 // Make sure the result is itself legal.
2979 Result = LegalizeOp(Result);
2980
2981 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002982 AddPromotedOperand(Op, Result);
2983 return Result;
2984}
Chris Lattnerdc750592005-01-07 07:47:09 +00002985
Nate Begeman7e7f4392006-02-01 07:19:44 +00002986/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2987/// with condition CC on the current target. This usually involves legalizing
2988/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2989/// there may be no choice but to create a new SetCC node to represent the
2990/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2991/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2992void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2993 SDOperand &RHS,
2994 SDOperand &CC) {
2995 SDOperand Tmp1, Tmp2, Result;
2996
2997 switch (getTypeAction(LHS.getValueType())) {
2998 case Legal:
2999 Tmp1 = LegalizeOp(LHS); // LHS
3000 Tmp2 = LegalizeOp(RHS); // RHS
3001 break;
3002 case Promote:
3003 Tmp1 = PromoteOp(LHS); // LHS
3004 Tmp2 = PromoteOp(RHS); // RHS
3005
3006 // If this is an FP compare, the operands have already been extended.
3007 if (MVT::isInteger(LHS.getValueType())) {
3008 MVT::ValueType VT = LHS.getValueType();
3009 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3010
3011 // Otherwise, we have to insert explicit sign or zero extends. Note
3012 // that we could insert sign extends for ALL conditions, but zero extend
3013 // is cheaper on many machines (an AND instead of two shifts), so prefer
3014 // it.
3015 switch (cast<CondCodeSDNode>(CC)->get()) {
3016 default: assert(0 && "Unknown integer comparison!");
3017 case ISD::SETEQ:
3018 case ISD::SETNE:
3019 case ISD::SETUGE:
3020 case ISD::SETUGT:
3021 case ISD::SETULE:
3022 case ISD::SETULT:
3023 // ALL of these operations will work if we either sign or zero extend
3024 // the operands (including the unsigned comparisons!). Zero extend is
3025 // usually a simpler/cheaper operation, so prefer it.
3026 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3027 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3028 break;
3029 case ISD::SETGE:
3030 case ISD::SETGT:
3031 case ISD::SETLT:
3032 case ISD::SETLE:
3033 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3034 DAG.getValueType(VT));
3035 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3036 DAG.getValueType(VT));
3037 break;
3038 }
3039 }
3040 break;
3041 case Expand:
3042 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3043 ExpandOp(LHS, LHSLo, LHSHi);
3044 ExpandOp(RHS, RHSLo, RHSHi);
3045 switch (cast<CondCodeSDNode>(CC)->get()) {
3046 case ISD::SETEQ:
3047 case ISD::SETNE:
3048 if (RHSLo == RHSHi)
3049 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3050 if (RHSCST->isAllOnesValue()) {
3051 // Comparison to -1.
3052 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3053 Tmp2 = RHSLo;
3054 break;
3055 }
3056
3057 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3058 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3059 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3060 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3061 break;
3062 default:
3063 // If this is a comparison of the sign bit, just look at the top part.
3064 // X > -1, x < 0
3065 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3066 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3067 CST->getValue() == 0) || // X < 0
3068 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3069 CST->isAllOnesValue())) { // X > -1
3070 Tmp1 = LHSHi;
3071 Tmp2 = RHSHi;
3072 break;
3073 }
3074
3075 // FIXME: This generated code sucks.
3076 ISD::CondCode LowCC;
3077 switch (cast<CondCodeSDNode>(CC)->get()) {
3078 default: assert(0 && "Unknown integer setcc!");
3079 case ISD::SETLT:
3080 case ISD::SETULT: LowCC = ISD::SETULT; break;
3081 case ISD::SETGT:
3082 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3083 case ISD::SETLE:
3084 case ISD::SETULE: LowCC = ISD::SETULE; break;
3085 case ISD::SETGE:
3086 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3087 }
3088
3089 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3090 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3091 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3092
3093 // NOTE: on targets without efficient SELECT of bools, we can always use
3094 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3095 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3096 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3097 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3098 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3099 Result, Tmp1, Tmp2));
3100 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003101 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003102 }
3103 }
3104 LHS = Tmp1;
3105 RHS = Tmp2;
3106}
3107
Chris Lattner36e663d2005-12-23 00:16:34 +00003108/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003109/// The resultant code need not be legal. Note that SrcOp is the input operand
3110/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003111SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3112 SDOperand SrcOp) {
3113 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003114 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003115
3116 // Emit a store to the stack slot.
3117 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003118 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003119 // Result is a load from the stack slot.
3120 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3121}
3122
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003123/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3124/// support the operation, but do support the resultant packed vector type.
3125SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3126
3127 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003128 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003129 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003130 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003131 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003132 std::map<SDOperand, std::vector<unsigned> > Values;
3133 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003134 bool isConstant = true;
3135 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3136 SplatValue.getOpcode() != ISD::UNDEF)
3137 isConstant = false;
3138
Evan Cheng1d2e9952006-03-24 01:17:21 +00003139 for (unsigned i = 1; i < NumElems; ++i) {
3140 SDOperand V = Node->getOperand(i);
3141 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3142 if (I != Values.end())
3143 I->second.push_back(i);
3144 else
3145 Values[V].push_back(i);
3146 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003147 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003148 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003149 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003150
3151 // If this isn't a constant element or an undef, we can't use a constant
3152 // pool load.
3153 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3154 V.getOpcode() != ISD::UNDEF)
3155 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003156 }
3157
3158 if (isOnlyLowElement) {
3159 // If the low element is an undef too, then this whole things is an undef.
3160 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3161 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3162 // Otherwise, turn this into a scalar_to_vector node.
3163 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3164 Node->getOperand(0));
3165 }
3166
Chris Lattner77e271c2006-03-24 07:29:17 +00003167 // If all elements are constants, create a load from the constant pool.
3168 if (isConstant) {
3169 MVT::ValueType VT = Node->getValueType(0);
3170 const Type *OpNTy =
3171 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3172 std::vector<Constant*> CV;
3173 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3174 if (ConstantFPSDNode *V =
3175 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3176 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3177 } else if (ConstantSDNode *V =
3178 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3179 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3180 } else {
3181 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3182 CV.push_back(UndefValue::get(OpNTy));
3183 }
3184 }
3185 Constant *CP = ConstantPacked::get(CV);
3186 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3187 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3188 DAG.getSrcValue(NULL));
3189 }
3190
Chris Lattner21e68c82006-03-20 01:52:29 +00003191 if (SplatValue.Val) { // Splat of one value?
3192 // Build the shuffle constant vector: <0, 0, 0, 0>
3193 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003194 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003195 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003196 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner21e68c82006-03-20 01:52:29 +00003197 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3198
3199 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3200 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3201 // Get the splatted value into the low element of a vector register.
3202 SDOperand LowValVec =
3203 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3204
3205 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3206 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3207 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3208 SplatMask);
3209 }
3210 }
3211
Evan Cheng1d2e9952006-03-24 01:17:21 +00003212 // If there are only two unique elements, we may be able to turn this into a
3213 // vector shuffle.
3214 if (Values.size() == 2) {
3215 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3216 MVT::ValueType MaskVT =
3217 MVT::getIntVectorWithNumElements(NumElems);
3218 std::vector<SDOperand> MaskVec(NumElems);
3219 unsigned i = 0;
3220 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3221 E = Values.end(); I != E; ++I) {
3222 for (std::vector<unsigned>::iterator II = I->second.begin(),
3223 EE = I->second.end(); II != EE; ++II)
3224 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3225 i += NumElems;
3226 }
3227 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3228
3229 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Evan Cheng68d9bf22006-03-24 18:45:20 +00003230 if (TLI.isShuffleLegal(Node->getValueType(0), ShuffleMask) &&
3231 TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0))) {
Evan Cheng1d2e9952006-03-24 01:17:21 +00003232 std::vector<SDOperand> Ops;
3233 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3234 E = Values.end(); I != E; ++I) {
3235 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3236 I->first);
3237 Ops.push_back(Op);
3238 }
3239 Ops.push_back(ShuffleMask);
3240
3241 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3242 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3243 }
3244 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003245
3246 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3247 // aligned object on the stack, store each element into it, then load
3248 // the result as a vector.
3249 MVT::ValueType VT = Node->getValueType(0);
3250 // Create the stack frame object.
3251 SDOperand FIPtr = CreateStackTemporary(VT);
3252
3253 // Emit a store of each element to the stack slot.
3254 std::vector<SDOperand> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003255 unsigned TypeByteSize =
3256 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3257 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3258 // Store (in the right endianness) the elements to memory.
3259 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3260 // Ignore undef elements.
3261 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3262
Chris Lattner8fa445a2006-03-22 01:46:54 +00003263 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003264
3265 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3266 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3267
3268 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3269 Node->getOperand(i), Idx,
3270 DAG.getSrcValue(NULL)));
3271 }
3272
3273 SDOperand StoreChain;
3274 if (!Stores.empty()) // Not all undef elements?
3275 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3276 else
3277 StoreChain = DAG.getEntryNode();
3278
3279 // Result is a load from the stack slot.
3280 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3281}
3282
3283/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3284/// specified value type.
3285SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3286 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3287 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3288 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3289 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3290}
3291
Chris Lattner4157c412005-04-02 04:00:59 +00003292void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3293 SDOperand Op, SDOperand Amt,
3294 SDOperand &Lo, SDOperand &Hi) {
3295 // Expand the subcomponents.
3296 SDOperand LHSL, LHSH;
3297 ExpandOp(Op, LHSL, LHSH);
3298
3299 std::vector<SDOperand> Ops;
3300 Ops.push_back(LHSL);
3301 Ops.push_back(LHSH);
3302 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003303 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003304 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003305 Hi = Lo.getValue(1);
3306}
3307
3308
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003309/// ExpandShift - Try to find a clever way to expand this shift operation out to
3310/// smaller elements. If we can't find a way that is more efficient than a
3311/// libcall on this target, return false. Otherwise, return true with the
3312/// low-parts expanded into Lo and Hi.
3313bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3314 SDOperand &Lo, SDOperand &Hi) {
3315 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3316 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003317
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003318 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003319 SDOperand ShAmt = LegalizeOp(Amt);
3320 MVT::ValueType ShTy = ShAmt.getValueType();
3321 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3322 unsigned NVTBits = MVT::getSizeInBits(NVT);
3323
3324 // Handle the case when Amt is an immediate. Other cases are currently broken
3325 // and are disabled.
3326 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3327 unsigned Cst = CN->getValue();
3328 // Expand the incoming operand to be shifted, so that we have its parts
3329 SDOperand InL, InH;
3330 ExpandOp(Op, InL, InH);
3331 switch(Opc) {
3332 case ISD::SHL:
3333 if (Cst > VTBits) {
3334 Lo = DAG.getConstant(0, NVT);
3335 Hi = DAG.getConstant(0, NVT);
3336 } else if (Cst > NVTBits) {
3337 Lo = DAG.getConstant(0, NVT);
3338 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003339 } else if (Cst == NVTBits) {
3340 Lo = DAG.getConstant(0, NVT);
3341 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003342 } else {
3343 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3344 Hi = DAG.getNode(ISD::OR, NVT,
3345 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3346 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3347 }
3348 return true;
3349 case ISD::SRL:
3350 if (Cst > VTBits) {
3351 Lo = DAG.getConstant(0, NVT);
3352 Hi = DAG.getConstant(0, NVT);
3353 } else if (Cst > NVTBits) {
3354 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3355 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003356 } else if (Cst == NVTBits) {
3357 Lo = InH;
3358 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003359 } else {
3360 Lo = DAG.getNode(ISD::OR, NVT,
3361 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3362 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3363 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3364 }
3365 return true;
3366 case ISD::SRA:
3367 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003368 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003369 DAG.getConstant(NVTBits-1, ShTy));
3370 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003371 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003372 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003373 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003374 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003375 } else if (Cst == NVTBits) {
3376 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003377 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003378 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003379 } else {
3380 Lo = DAG.getNode(ISD::OR, NVT,
3381 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3382 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3383 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3384 }
3385 return true;
3386 }
3387 }
Nate Begemanb0674922005-04-06 21:13:14 +00003388 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003389}
Chris Lattneraac464e2005-01-21 06:05:23 +00003390
Chris Lattner4add7e32005-01-23 04:42:50 +00003391
Chris Lattneraac464e2005-01-21 06:05:23 +00003392// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3393// does not fit into a register, return the lo part and set the hi part to the
3394// by-reg argument. If it does fit into a single register, return the result
3395// and leave the Hi part unset.
3396SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3397 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003398 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3399 // The input chain to this libcall is the entry node of the function.
3400 // Legalizing the call will automatically add the previous call to the
3401 // dependence.
3402 SDOperand InChain = DAG.getEntryNode();
3403
Chris Lattneraac464e2005-01-21 06:05:23 +00003404 TargetLowering::ArgListTy Args;
3405 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3406 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3407 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3408 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3409 }
3410 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003411
Chris Lattner06bbeb62005-05-11 19:02:11 +00003412 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003413 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003414 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003415 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3416 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003417
Chris Lattner462505f2006-02-13 09:18:02 +00003418 // Legalize the call sequence, starting with the chain. This will advance
3419 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3420 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3421 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003422 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003423 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003424 default: assert(0 && "Unknown thing");
3425 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003426 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003427 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003428 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003429 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003430 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003431 }
Chris Lattner63022662005-09-02 20:26:58 +00003432 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003433}
3434
Chris Lattner4add7e32005-01-23 04:42:50 +00003435
Chris Lattneraac464e2005-01-21 06:05:23 +00003436/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3437/// destination type is legal.
3438SDOperand SelectionDAGLegalize::
3439ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003440 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003441 assert(getTypeAction(Source.getValueType()) == Expand &&
3442 "This is not an expansion!");
3443 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3444
Chris Lattner06bbeb62005-05-11 19:02:11 +00003445 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003446 assert(Source.getValueType() == MVT::i64 &&
3447 "This only works for 64-bit -> FP");
3448 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3449 // incoming integer is set. To handle this, we dynamically test to see if
3450 // it is set, and, if so, add a fudge factor.
3451 SDOperand Lo, Hi;
3452 ExpandOp(Source, Lo, Hi);
3453
Chris Lattner2a4f7312005-05-13 04:45:13 +00003454 // If this is unsigned, and not supported, first perform the conversion to
3455 // signed, then adjust the result if the sign bit is set.
3456 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3457 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3458
Chris Lattnerd47675e2005-08-09 20:20:18 +00003459 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3460 DAG.getConstant(0, Hi.getValueType()),
3461 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003462 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3463 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3464 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003465 uint64_t FF = 0x5f800000ULL;
3466 if (TLI.isLittleEndian()) FF <<= 32;
3467 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003468
Chris Lattnerc30405e2005-08-26 17:15:30 +00003469 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003470 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3471 SDOperand FudgeInReg;
3472 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003473 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3474 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003475 else {
3476 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003477 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3478 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003479 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003480 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003481 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003482
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003483 // Check to see if the target has a custom way to lower this. If so, use it.
3484 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3485 default: assert(0 && "This action not implemented for this operation!");
3486 case TargetLowering::Legal:
3487 case TargetLowering::Expand:
3488 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003489 case TargetLowering::Custom: {
3490 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3491 Source), DAG);
3492 if (NV.Val)
3493 return LegalizeOp(NV);
3494 break; // The target decided this was legal after all
3495 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003496 }
3497
Chris Lattner153587e2005-05-12 07:00:44 +00003498 // Expand the source, then glue it back together for the call. We must expand
3499 // the source in case it is shared (this pass of legalize must traverse it).
3500 SDOperand SrcLo, SrcHi;
3501 ExpandOp(Source, SrcLo, SrcHi);
3502 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3503
Chris Lattner06bbeb62005-05-11 19:02:11 +00003504 const char *FnName = 0;
3505 if (DestTy == MVT::f32)
3506 FnName = "__floatdisf";
3507 else {
3508 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3509 FnName = "__floatdidf";
3510 }
Chris Lattner462505f2006-02-13 09:18:02 +00003511
3512 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3513 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003514 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003515}
Misha Brukman835702a2005-04-21 22:36:52 +00003516
Chris Lattner689bdcc2006-01-28 08:25:58 +00003517/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3518/// INT_TO_FP operation of the specified operand when the target requests that
3519/// we expand it. At this point, we know that the result and operand types are
3520/// legal for the target.
3521SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3522 SDOperand Op0,
3523 MVT::ValueType DestVT) {
3524 if (Op0.getValueType() == MVT::i32) {
3525 // simple 32-bit [signed|unsigned] integer to float/double expansion
3526
3527 // get the stack frame index of a 8 byte buffer
3528 MachineFunction &MF = DAG.getMachineFunction();
3529 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3530 // get address of 8 byte buffer
3531 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3532 // word offset constant for Hi/Lo address computation
3533 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3534 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00003535 SDOperand Hi = StackSlot;
3536 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3537 if (TLI.isLittleEndian())
3538 std::swap(Hi, Lo);
3539
Chris Lattner689bdcc2006-01-28 08:25:58 +00003540 // if signed map to unsigned space
3541 SDOperand Op0Mapped;
3542 if (isSigned) {
3543 // constant used to invert sign bit (signed to unsigned mapping)
3544 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3545 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3546 } else {
3547 Op0Mapped = Op0;
3548 }
3549 // store the lo of the constructed double - based on integer input
3550 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3551 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3552 // initial hi portion of constructed double
3553 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3554 // store the hi of the constructed double - biased exponent
3555 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3556 InitialHi, Hi, DAG.getSrcValue(NULL));
3557 // load the constructed double
3558 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3559 DAG.getSrcValue(NULL));
3560 // FP constant to bias correct the final result
3561 SDOperand Bias = DAG.getConstantFP(isSigned ?
3562 BitsToDouble(0x4330000080000000ULL)
3563 : BitsToDouble(0x4330000000000000ULL),
3564 MVT::f64);
3565 // subtract the bias
3566 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3567 // final result
3568 SDOperand Result;
3569 // handle final rounding
3570 if (DestVT == MVT::f64) {
3571 // do nothing
3572 Result = Sub;
3573 } else {
3574 // if f32 then cast to f32
3575 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3576 }
3577 return Result;
3578 }
3579 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3580 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3581
3582 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3583 DAG.getConstant(0, Op0.getValueType()),
3584 ISD::SETLT);
3585 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3586 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3587 SignSet, Four, Zero);
3588
3589 // If the sign bit of the integer is set, the large number will be treated
3590 // as a negative number. To counteract this, the dynamic code adds an
3591 // offset depending on the data type.
3592 uint64_t FF;
3593 switch (Op0.getValueType()) {
3594 default: assert(0 && "Unsupported integer type!");
3595 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3596 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3597 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3598 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3599 }
3600 if (TLI.isLittleEndian()) FF <<= 32;
3601 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3602
3603 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3604 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3605 SDOperand FudgeInReg;
3606 if (DestVT == MVT::f32)
3607 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3608 DAG.getSrcValue(NULL));
3609 else {
3610 assert(DestVT == MVT::f64 && "Unexpected conversion");
3611 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3612 DAG.getEntryNode(), CPIdx,
3613 DAG.getSrcValue(NULL), MVT::f32));
3614 }
3615
3616 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3617}
3618
3619/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3620/// *INT_TO_FP operation of the specified operand when the target requests that
3621/// we promote it. At this point, we know that the result and operand types are
3622/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3623/// operation that takes a larger input.
3624SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3625 MVT::ValueType DestVT,
3626 bool isSigned) {
3627 // First step, figure out the appropriate *INT_TO_FP operation to use.
3628 MVT::ValueType NewInTy = LegalOp.getValueType();
3629
3630 unsigned OpToUse = 0;
3631
3632 // Scan for the appropriate larger type to use.
3633 while (1) {
3634 NewInTy = (MVT::ValueType)(NewInTy+1);
3635 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3636
3637 // If the target supports SINT_TO_FP of this type, use it.
3638 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3639 default: break;
3640 case TargetLowering::Legal:
3641 if (!TLI.isTypeLegal(NewInTy))
3642 break; // Can't use this datatype.
3643 // FALL THROUGH.
3644 case TargetLowering::Custom:
3645 OpToUse = ISD::SINT_TO_FP;
3646 break;
3647 }
3648 if (OpToUse) break;
3649 if (isSigned) continue;
3650
3651 // If the target supports UINT_TO_FP of this type, use it.
3652 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3653 default: break;
3654 case TargetLowering::Legal:
3655 if (!TLI.isTypeLegal(NewInTy))
3656 break; // Can't use this datatype.
3657 // FALL THROUGH.
3658 case TargetLowering::Custom:
3659 OpToUse = ISD::UINT_TO_FP;
3660 break;
3661 }
3662 if (OpToUse) break;
3663
3664 // Otherwise, try a larger type.
3665 }
3666
3667 // Okay, we found the operation and type to use. Zero extend our input to the
3668 // desired type then run the operation on it.
3669 return DAG.getNode(OpToUse, DestVT,
3670 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3671 NewInTy, LegalOp));
3672}
3673
3674/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3675/// FP_TO_*INT operation of the specified operand when the target requests that
3676/// we promote it. At this point, we know that the result and operand types are
3677/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3678/// operation that returns a larger result.
3679SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3680 MVT::ValueType DestVT,
3681 bool isSigned) {
3682 // First step, figure out the appropriate FP_TO*INT operation to use.
3683 MVT::ValueType NewOutTy = DestVT;
3684
3685 unsigned OpToUse = 0;
3686
3687 // Scan for the appropriate larger type to use.
3688 while (1) {
3689 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3690 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3691
3692 // If the target supports FP_TO_SINT returning this type, use it.
3693 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3694 default: break;
3695 case TargetLowering::Legal:
3696 if (!TLI.isTypeLegal(NewOutTy))
3697 break; // Can't use this datatype.
3698 // FALL THROUGH.
3699 case TargetLowering::Custom:
3700 OpToUse = ISD::FP_TO_SINT;
3701 break;
3702 }
3703 if (OpToUse) break;
3704
3705 // If the target supports FP_TO_UINT of this type, use it.
3706 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3707 default: break;
3708 case TargetLowering::Legal:
3709 if (!TLI.isTypeLegal(NewOutTy))
3710 break; // Can't use this datatype.
3711 // FALL THROUGH.
3712 case TargetLowering::Custom:
3713 OpToUse = ISD::FP_TO_UINT;
3714 break;
3715 }
3716 if (OpToUse) break;
3717
3718 // Otherwise, try a larger type.
3719 }
3720
3721 // Okay, we found the operation and type to use. Truncate the result of the
3722 // extended FP_TO_*INT operation to the desired size.
3723 return DAG.getNode(ISD::TRUNCATE, DestVT,
3724 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3725}
3726
3727/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3728///
3729SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3730 MVT::ValueType VT = Op.getValueType();
3731 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3732 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3733 switch (VT) {
3734 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3735 case MVT::i16:
3736 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3737 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3738 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3739 case MVT::i32:
3740 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3741 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3742 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3743 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3744 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3745 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3746 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3747 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3748 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3749 case MVT::i64:
3750 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3751 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3752 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3753 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3754 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3755 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3756 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3757 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3758 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3759 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3760 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3761 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3762 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3763 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3764 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3765 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3766 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3767 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3768 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3769 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3770 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3771 }
3772}
3773
3774/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3775///
3776SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3777 switch (Opc) {
3778 default: assert(0 && "Cannot expand this yet!");
3779 case ISD::CTPOP: {
3780 static const uint64_t mask[6] = {
3781 0x5555555555555555ULL, 0x3333333333333333ULL,
3782 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3783 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3784 };
3785 MVT::ValueType VT = Op.getValueType();
3786 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3787 unsigned len = getSizeInBits(VT);
3788 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3789 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3790 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3791 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3792 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3793 DAG.getNode(ISD::AND, VT,
3794 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3795 }
3796 return Op;
3797 }
3798 case ISD::CTLZ: {
3799 // for now, we do this:
3800 // x = x | (x >> 1);
3801 // x = x | (x >> 2);
3802 // ...
3803 // x = x | (x >>16);
3804 // x = x | (x >>32); // for 64-bit input
3805 // return popcount(~x);
3806 //
3807 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3808 MVT::ValueType VT = Op.getValueType();
3809 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3810 unsigned len = getSizeInBits(VT);
3811 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3812 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3813 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3814 }
3815 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3816 return DAG.getNode(ISD::CTPOP, VT, Op);
3817 }
3818 case ISD::CTTZ: {
3819 // for now, we use: { return popcount(~x & (x - 1)); }
3820 // unless the target has ctlz but not ctpop, in which case we use:
3821 // { return 32 - nlz(~x & (x-1)); }
3822 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3823 MVT::ValueType VT = Op.getValueType();
3824 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3825 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3826 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3827 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3828 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3829 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3830 TLI.isOperationLegal(ISD::CTLZ, VT))
3831 return DAG.getNode(ISD::SUB, VT,
3832 DAG.getConstant(getSizeInBits(VT), VT),
3833 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3834 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3835 }
3836 }
3837}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003838
3839
Chris Lattnerdc750592005-01-07 07:47:09 +00003840/// ExpandOp - Expand the specified SDOperand into its two component pieces
3841/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3842/// LegalizeNodes map is filled in for any results that are not expanded, the
3843/// ExpandedNodes map is filled in for any results that are expanded, and the
3844/// Lo/Hi values are returned.
3845void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3846 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003847 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003848 SDNode *Node = Op.Val;
3849 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003850 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3851 "Cannot expand FP values!");
3852 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003853 "Cannot expand to FP value or to larger int value!");
3854
Chris Lattner1a570f12005-09-02 20:32:45 +00003855 // See if we already expanded it.
3856 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3857 = ExpandedNodes.find(Op);
3858 if (I != ExpandedNodes.end()) {
3859 Lo = I->second.first;
3860 Hi = I->second.second;
3861 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003862 }
3863
Chris Lattnerdc750592005-01-07 07:47:09 +00003864 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003865 case ISD::CopyFromReg:
3866 assert(0 && "CopyFromReg must be legal!");
3867 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003868 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3869 assert(0 && "Do not know how to expand this operator!");
3870 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003871 case ISD::UNDEF:
3872 Lo = DAG.getNode(ISD::UNDEF, NVT);
3873 Hi = DAG.getNode(ISD::UNDEF, NVT);
3874 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003875 case ISD::Constant: {
3876 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3877 Lo = DAG.getConstant(Cst, NVT);
3878 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3879 break;
3880 }
Chris Lattner32e08b72005-03-28 22:03:13 +00003881 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003882 // Return the operands.
3883 Lo = Node->getOperand(0);
3884 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003885 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003886
3887 case ISD::SIGN_EXTEND_INREG:
3888 ExpandOp(Node->getOperand(0), Lo, Hi);
3889 // Sign extend the lo-part.
3890 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3891 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3892 TLI.getShiftAmountTy()));
3893 // sext_inreg the low part if needed.
3894 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3895 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003896
Nate Begeman2fba8a32006-01-14 03:14:10 +00003897 case ISD::BSWAP: {
3898 ExpandOp(Node->getOperand(0), Lo, Hi);
3899 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3900 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3901 Lo = TempLo;
3902 break;
3903 }
3904
Chris Lattner55e9cde2005-05-11 04:51:16 +00003905 case ISD::CTPOP:
3906 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003907 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3908 DAG.getNode(ISD::CTPOP, NVT, Lo),
3909 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003910 Hi = DAG.getConstant(0, NVT);
3911 break;
3912
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003913 case ISD::CTLZ: {
3914 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003915 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003916 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3917 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003918 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3919 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003920 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3921 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3922
3923 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3924 Hi = DAG.getConstant(0, NVT);
3925 break;
3926 }
3927
3928 case ISD::CTTZ: {
3929 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003930 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003931 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3932 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003933 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3934 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003935 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3936 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3937
3938 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3939 Hi = DAG.getConstant(0, NVT);
3940 break;
3941 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003942
Nate Begemane74795c2006-01-25 18:21:52 +00003943 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003944 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3945 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003946 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3947 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3948
3949 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003950 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003951 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3952 if (!TLI.isLittleEndian())
3953 std::swap(Lo, Hi);
3954 break;
3955 }
3956
Chris Lattnerdc750592005-01-07 07:47:09 +00003957 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003958 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3959 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003960 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003961
3962 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003963 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003964 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3965 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003966 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003967 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003968
3969 // Build a factor node to remember that this load is independent of the
3970 // other one.
3971 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3972 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003973
Chris Lattnerdc750592005-01-07 07:47:09 +00003974 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003975 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003976 if (!TLI.isLittleEndian())
3977 std::swap(Lo, Hi);
3978 break;
3979 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003980 case ISD::AND:
3981 case ISD::OR:
3982 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3983 SDOperand LL, LH, RL, RH;
3984 ExpandOp(Node->getOperand(0), LL, LH);
3985 ExpandOp(Node->getOperand(1), RL, RH);
3986 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3987 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3988 break;
3989 }
3990 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003991 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003992 ExpandOp(Node->getOperand(1), LL, LH);
3993 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003994 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3995 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003996 break;
3997 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003998 case ISD::SELECT_CC: {
3999 SDOperand TL, TH, FL, FH;
4000 ExpandOp(Node->getOperand(2), TL, TH);
4001 ExpandOp(Node->getOperand(3), FL, FH);
4002 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4003 Node->getOperand(1), TL, FL, Node->getOperand(4));
4004 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4005 Node->getOperand(1), TH, FH, Node->getOperand(4));
4006 break;
4007 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00004008 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004009 SDOperand Chain = Node->getOperand(0);
4010 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004011 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4012
4013 if (EVT == NVT)
4014 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4015 else
4016 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4017 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004018
4019 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004020 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004021
Nate Begemanc3a89c52005-10-13 17:15:37 +00004022 // The high part is obtained by SRA'ing all but one of the bits of the lo
4023 // part.
4024 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4025 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4026 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00004027 break;
4028 }
4029 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004030 SDOperand Chain = Node->getOperand(0);
4031 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004032 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4033
4034 if (EVT == NVT)
4035 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4036 else
4037 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4038 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004039
4040 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004041 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004042
Nate Begemanc3a89c52005-10-13 17:15:37 +00004043 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004044 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004045 break;
4046 }
4047 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004048 SDOperand Chain = Node->getOperand(0);
4049 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00004050 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4051
4052 if (EVT == NVT)
4053 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4054 else
4055 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4056 EVT);
4057
4058 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004059 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004060
4061 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00004062 Hi = DAG.getNode(ISD::UNDEF, NVT);
4063 break;
4064 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004065 case ISD::ANY_EXTEND:
4066 // The low part is any extension of the input (which degenerates to a copy).
4067 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4068 // The high part is undefined.
4069 Hi = DAG.getNode(ISD::UNDEF, NVT);
4070 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004071 case ISD::SIGN_EXTEND: {
4072 // The low part is just a sign extension of the input (which degenerates to
4073 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004074 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004075
Chris Lattnerdc750592005-01-07 07:47:09 +00004076 // The high part is obtained by SRA'ing all but one of the bits of the lo
4077 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004078 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004079 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4080 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004081 break;
4082 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004083 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004084 // The low part is just a zero extension of the input (which degenerates to
4085 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004086 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004087
Chris Lattnerdc750592005-01-07 07:47:09 +00004088 // The high part is just a zero.
4089 Hi = DAG.getConstant(0, NVT);
4090 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004091
4092 case ISD::BIT_CONVERT: {
4093 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4094 Node->getOperand(0));
4095 ExpandOp(Tmp, Lo, Hi);
4096 break;
4097 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004098
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004099 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004100 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4101 TargetLowering::Custom &&
4102 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004103 Lo = TLI.LowerOperation(Op, DAG);
4104 assert(Lo.Val && "Node must be custom expanded!");
4105 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004106 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004107 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004108 break;
4109
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004110 // These operators cannot be expanded directly, emit them as calls to
4111 // library functions.
4112 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004113 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004114 SDOperand Op;
4115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4116 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004117 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4118 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004119 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004120
Chris Lattner941d84a2005-07-30 01:40:57 +00004121 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4122
Chris Lattnerfe68d752005-07-29 00:33:32 +00004123 // Now that the custom expander is done, expand the result, which is still
4124 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004125 if (Op.Val) {
4126 ExpandOp(Op, Lo, Hi);
4127 break;
4128 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004129 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004130
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004131 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004132 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004133 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004134 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004135 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004136
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004137 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004138 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004139 SDOperand Op;
4140 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4141 case Expand: assert(0 && "cannot expand FP!");
4142 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4143 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4144 }
4145
4146 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4147
4148 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004149 if (Op.Val) {
4150 ExpandOp(Op, Lo, Hi);
4151 break;
4152 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004153 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004154
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004155 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004156 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004157 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004158 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004159 break;
4160
Evan Cheng870e4f82006-01-09 18:31:59 +00004161 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004162 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004163 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004164 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004165 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004166 Op = TLI.LowerOperation(Op, DAG);
4167 if (Op.Val) {
4168 // Now that the custom expander is done, expand the result, which is
4169 // still VT.
4170 ExpandOp(Op, Lo, Hi);
4171 break;
4172 }
4173 }
4174
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004175 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004176 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004177 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004178
4179 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004180 TargetLowering::LegalizeAction Action =
4181 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4182 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4183 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004184 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004185 break;
4186 }
4187
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004188 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004189 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004190 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004191 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004192
Evan Cheng870e4f82006-01-09 18:31:59 +00004193 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004194 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004195 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004196 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004197 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004198 Op = TLI.LowerOperation(Op, DAG);
4199 if (Op.Val) {
4200 // Now that the custom expander is done, expand the result, which is
4201 // still VT.
4202 ExpandOp(Op, Lo, Hi);
4203 break;
4204 }
4205 }
4206
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004207 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004208 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004209 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004210
4211 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004212 TargetLowering::LegalizeAction Action =
4213 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4214 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4215 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004216 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004217 break;
4218 }
4219
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004220 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004221 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004222 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004223 }
4224
4225 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004226 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004227 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004228 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004229 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004230 Op = TLI.LowerOperation(Op, DAG);
4231 if (Op.Val) {
4232 // Now that the custom expander is done, expand the result, which is
4233 // still VT.
4234 ExpandOp(Op, Lo, Hi);
4235 break;
4236 }
4237 }
4238
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004239 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004240 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004241 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004242
4243 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004244 TargetLowering::LegalizeAction Action =
4245 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4246 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4247 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004248 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004249 break;
4250 }
4251
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004252 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004253 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004254 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004255 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004256
Misha Brukman835702a2005-04-21 22:36:52 +00004257 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004258 case ISD::SUB: {
4259 // If the target wants to custom expand this, let them.
4260 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4261 TargetLowering::Custom) {
4262 Op = TLI.LowerOperation(Op, DAG);
4263 if (Op.Val) {
4264 ExpandOp(Op, Lo, Hi);
4265 break;
4266 }
4267 }
4268
4269 // Expand the subcomponents.
4270 SDOperand LHSL, LHSH, RHSL, RHSH;
4271 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4272 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004273 std::vector<MVT::ValueType> VTs;
4274 std::vector<SDOperand> LoOps, HiOps;
4275 VTs.push_back(LHSL.getValueType());
4276 VTs.push_back(MVT::Flag);
4277 LoOps.push_back(LHSL);
4278 LoOps.push_back(RHSL);
4279 HiOps.push_back(LHSH);
4280 HiOps.push_back(RHSH);
4281 if (Node->getOpcode() == ISD::ADD) {
4282 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4283 HiOps.push_back(Lo.getValue(1));
4284 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4285 } else {
4286 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4287 HiOps.push_back(Lo.getValue(1));
4288 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4289 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004290 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004291 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004292 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004293 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004294 SDOperand LL, LH, RL, RH;
4295 ExpandOp(Node->getOperand(0), LL, LH);
4296 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004297 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4298 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4299 // extended the sign bit of the low half through the upper half, and if so
4300 // emit a MULHS instead of the alternate sequence that is valid for any
4301 // i64 x i64 multiply.
4302 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4303 // is RH an extension of the sign bit of RL?
4304 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4305 RH.getOperand(1).getOpcode() == ISD::Constant &&
4306 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4307 // is LH an extension of the sign bit of LL?
4308 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4309 LH.getOperand(1).getOpcode() == ISD::Constant &&
4310 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4311 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4312 } else {
4313 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4314 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4315 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4316 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4317 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4318 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004319 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4320 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004321 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004322 }
4323 break;
4324 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004325 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4326 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4327 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4328 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004329 }
4330
Chris Lattnerac12f682005-12-21 18:02:52 +00004331 // Make sure the resultant values have been legalized themselves, unless this
4332 // is a type that requires multi-step expansion.
4333 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4334 Lo = LegalizeOp(Lo);
4335 Hi = LegalizeOp(Hi);
4336 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004337
4338 // Remember in a map if the values will be reused later.
4339 bool isNew =
4340 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4341 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004342}
4343
Chris Lattner32206f52006-03-18 01:44:44 +00004344/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4345/// two smaller values of MVT::Vector type.
4346void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4347 SDOperand &Hi) {
4348 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4349 SDNode *Node = Op.Val;
4350 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4351 assert(NumElements > 1 && "Cannot split a single element vector!");
4352 unsigned NewNumElts = NumElements/2;
4353 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4354 SDOperand TypeNode = *(Node->op_end()-1);
4355
4356 // See if we already split it.
4357 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4358 = SplitNodes.find(Op);
4359 if (I != SplitNodes.end()) {
4360 Lo = I->second.first;
4361 Hi = I->second.second;
4362 return;
4363 }
4364
4365 switch (Node->getOpcode()) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004366 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004367 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004368 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4369 LoOps.push_back(NewNumEltsNode);
4370 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004371 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004372
4373 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4374 HiOps.push_back(NewNumEltsNode);
4375 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004376 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004377 break;
4378 }
4379 case ISD::VADD:
4380 case ISD::VSUB:
4381 case ISD::VMUL:
4382 case ISD::VSDIV:
4383 case ISD::VUDIV:
4384 case ISD::VAND:
4385 case ISD::VOR:
4386 case ISD::VXOR: {
4387 SDOperand LL, LH, RL, RH;
4388 SplitVectorOp(Node->getOperand(0), LL, LH);
4389 SplitVectorOp(Node->getOperand(1), RL, RH);
4390
4391 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4392 NewNumEltsNode, TypeNode);
4393 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4394 NewNumEltsNode, TypeNode);
4395 break;
4396 }
4397 case ISD::VLOAD: {
4398 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4399 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4400 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4401
4402 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4403 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4404 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4405 getIntPtrConstant(IncrementSize));
4406 // FIXME: This creates a bogus srcvalue!
4407 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4408
4409 // Build a factor node to remember that this load is independent of the
4410 // other one.
4411 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4412 Hi.getValue(1));
4413
4414 // Remember that we legalized the chain.
4415 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4416 if (!TLI.isLittleEndian())
4417 std::swap(Lo, Hi);
4418 break;
4419 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004420 case ISD::VBIT_CONVERT: {
4421 // We know the result is a vector. The input may be either a vector or a
4422 // scalar value.
4423 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4424 // Lower to a store/load. FIXME: this could be improved probably.
4425 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4426
4427 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4428 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4429 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4430 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4431 SplitVectorOp(St, Lo, Hi);
4432 } else {
4433 // If the input is a vector type, we have to either scalarize it, pack it
4434 // or convert it based on whether the input vector type is legal.
4435 SDNode *InVal = Node->getOperand(0).Val;
4436 unsigned NumElems =
4437 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4438 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4439
4440 // If the input is from a single element vector, scalarize the vector,
4441 // then treat like a scalar.
4442 if (NumElems == 1) {
4443 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4444 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4445 Op.getOperand(1), Op.getOperand(2));
4446 SplitVectorOp(Scalar, Lo, Hi);
4447 } else {
4448 // Split the input vector.
4449 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4450
4451 // Convert each of the pieces now.
4452 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4453 NewNumEltsNode, TypeNode);
4454 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4455 NewNumEltsNode, TypeNode);
4456 }
4457 break;
4458 }
4459 }
Chris Lattner32206f52006-03-18 01:44:44 +00004460 }
4461
4462 // Remember in a map if the values will be reused later.
4463 bool isNew =
4464 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4465 assert(isNew && "Value already expanded?!?");
4466}
4467
4468
4469/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4470/// equivalent operation that returns a scalar (e.g. F32) or packed value
4471/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4472/// type for the result.
4473SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4474 MVT::ValueType NewVT) {
Chris Lattner672a42d2006-03-21 19:20:37 +00004475 // FIXME: THIS IS A TEMPORARY HACK
4476 if (Op.getValueType() == NewVT) return Op;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00004477
Chris Lattner32206f52006-03-18 01:44:44 +00004478 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4479 SDNode *Node = Op.Val;
4480
4481 // See if we already packed it.
4482 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4483 if (I != PackedNodes.end()) return I->second;
4484
4485 SDOperand Result;
4486 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004487 default:
4488 Node->dump(); std::cerr << "\n";
4489 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004490 case ISD::VADD:
4491 case ISD::VSUB:
4492 case ISD::VMUL:
4493 case ISD::VSDIV:
4494 case ISD::VUDIV:
4495 case ISD::VAND:
4496 case ISD::VOR:
4497 case ISD::VXOR:
4498 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4499 NewVT,
4500 PackVectorOp(Node->getOperand(0), NewVT),
4501 PackVectorOp(Node->getOperand(1), NewVT));
4502 break;
4503 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004504 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4505 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004506
Chris Lattner93640542006-03-19 00:07:49 +00004507 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004508
4509 // Remember that we legalized the chain.
4510 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4511 break;
4512 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004513 case ISD::VBUILD_VECTOR:
Chris Lattner32206f52006-03-18 01:44:44 +00004514 if (!MVT::isVector(NewVT)) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004515 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004516 Result = Node->getOperand(0);
4517 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004518 // Returning a BUILD_VECTOR?
Chris Lattner32206f52006-03-18 01:44:44 +00004519 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004520 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattner32206f52006-03-18 01:44:44 +00004521 }
4522 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004523 case ISD::VINSERT_VECTOR_ELT:
4524 if (!MVT::isVector(NewVT)) {
4525 // Returning a scalar? Must be the inserted element.
4526 Result = Node->getOperand(1);
4527 } else {
4528 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4529 PackVectorOp(Node->getOperand(0), NewVT),
4530 Node->getOperand(1), Node->getOperand(2));
4531 }
4532 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00004533 case ISD::VBIT_CONVERT:
4534 if (Op.getOperand(0).getValueType() != MVT::Vector)
4535 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4536 else {
4537 // If the input is a vector type, we have to either scalarize it, pack it
4538 // or convert it based on whether the input vector type is legal.
4539 SDNode *InVal = Node->getOperand(0).Val;
4540 unsigned NumElems =
4541 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4542 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4543
4544 // Figure out if there is a Packed type corresponding to this Vector
4545 // type. If so, convert to the packed type.
4546 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4547 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4548 // Turn this into a bit convert of the packed input.
4549 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4550 PackVectorOp(Node->getOperand(0), TVT));
4551 break;
4552 } else if (NumElems == 1) {
4553 // Turn this into a bit convert of the scalar input.
4554 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4555 PackVectorOp(Node->getOperand(0), EVT));
4556 break;
4557 } else {
4558 // FIXME: UNIMP!
4559 assert(0 && "Cast from unsupported vector type not implemented yet!");
4560 }
4561 }
Chris Lattner32206f52006-03-18 01:44:44 +00004562 }
4563
4564 if (TLI.isTypeLegal(NewVT))
4565 Result = LegalizeOp(Result);
4566 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4567 assert(isNew && "Value already packed?");
4568 return Result;
4569}
4570
Chris Lattnerdc750592005-01-07 07:47:09 +00004571
4572// SelectionDAG::Legalize - This is the entry point for the file.
4573//
Chris Lattner4add7e32005-01-23 04:42:50 +00004574void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004575 /// run - This is the main entry point to this class.
4576 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004577 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004578}
4579