blob: 81139dcde178375a0ded2457d8a37b7aac21fc4c [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;
567 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000568 break;
569 }
Chris Lattner435b4022005-11-29 06:21:05 +0000570
571 case ISD::LOCATION:
572 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
574
575 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
576 case TargetLowering::Promote:
577 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000578 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000579 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000580 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
581 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
582
583 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
584 const std::string &FName =
585 cast<StringSDNode>(Node->getOperand(3))->getValue();
586 const std::string &DirName =
587 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000588 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000589
Jim Laskey9e296be2005-12-21 20:51:37 +0000590 std::vector<SDOperand> Ops;
591 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000592 SDOperand LineOp = Node->getOperand(1);
593 SDOperand ColOp = Node->getOperand(2);
594
595 if (useDEBUG_LOC) {
596 Ops.push_back(LineOp); // line #
597 Ops.push_back(ColOp); // col #
598 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
599 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
600 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000601 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
602 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000603 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
604 Ops.push_back(DAG.getConstant(ID, MVT::i32));
605 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
606 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000607 } else {
608 Result = Tmp1; // chain
609 }
Chris Lattner435b4022005-11-29 06:21:05 +0000610 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000611 }
Chris Lattner435b4022005-11-29 06:21:05 +0000612 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000613 if (Tmp1 != Node->getOperand(0) ||
614 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000615 std::vector<SDOperand> Ops;
616 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000617 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
618 Ops.push_back(Node->getOperand(1)); // line # must be legal.
619 Ops.push_back(Node->getOperand(2)); // col # must be legal.
620 } else {
621 // Otherwise promote them.
622 Ops.push_back(PromoteOp(Node->getOperand(1)));
623 Ops.push_back(PromoteOp(Node->getOperand(2)));
624 }
Chris Lattner435b4022005-11-29 06:21:05 +0000625 Ops.push_back(Node->getOperand(3)); // filename must be legal.
626 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000627 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000628 }
629 break;
630 }
631 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000632
633 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000634 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000635 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000636 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000637 case TargetLowering::Legal:
638 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
639 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
640 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
641 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000643 break;
644 }
645 break;
646
647 case ISD::DEBUG_LABEL:
648 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
649 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000650 default: assert(0 && "This action is not supported yet!");
651 case TargetLowering::Legal:
652 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
653 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000654 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000655 break;
656 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000657 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000658
Chris Lattnerdc750592005-01-07 07:47:09 +0000659 case ISD::Constant:
660 // We know we don't need to expand constants here, constants only have one
661 // value and we check that it is fine above.
662
663 // FIXME: Maybe we should handle things like targets that don't support full
664 // 32-bit immediates?
665 break;
666 case ISD::ConstantFP: {
667 // Spill FP immediates to the constant pool if the target cannot directly
668 // codegen them. Targets often have some immediate values that can be
669 // efficiently generated into an FP register without a load. We explicitly
670 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000671 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
672
673 // Check to see if this FP immediate is already legal.
674 bool isLegal = false;
675 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
676 E = TLI.legal_fpimm_end(); I != E; ++I)
677 if (CFP->isExactlyValue(*I)) {
678 isLegal = true;
679 break;
680 }
681
Chris Lattner758b0ac2006-01-29 06:26:56 +0000682 // If this is a legal constant, turn it into a TargetConstantFP node.
683 if (isLegal) {
684 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
685 break;
686 }
687
688 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
689 default: assert(0 && "This action is not supported yet!");
690 case TargetLowering::Custom:
691 Tmp3 = TLI.LowerOperation(Result, DAG);
692 if (Tmp3.Val) {
693 Result = Tmp3;
694 break;
695 }
696 // FALLTHROUGH
697 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000698 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000699 bool Extend = false;
700
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000701 // If a FP immediate is precise when represented as a float and if the
702 // target can do an extending load from float to double, we put it into
703 // the constant pool as a float, even if it's is statically typed as a
704 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000705 MVT::ValueType VT = CFP->getValueType(0);
706 bool isDouble = VT == MVT::f64;
707 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
708 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000709 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
710 // Only do this if the target has a native EXTLOAD instruction from
711 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000712 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000713 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
714 VT = MVT::f32;
715 Extend = true;
716 }
Misha Brukman835702a2005-04-21 22:36:52 +0000717
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000718 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000719 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000720 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
721 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000722 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000723 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
724 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000725 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000726 }
727 break;
728 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000729 case ISD::TokenFactor:
730 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000731 Tmp1 = LegalizeOp(Node->getOperand(0));
732 Tmp2 = LegalizeOp(Node->getOperand(1));
733 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
734 } else if (Node->getNumOperands() == 3) {
735 Tmp1 = LegalizeOp(Node->getOperand(0));
736 Tmp2 = LegalizeOp(Node->getOperand(1));
737 Tmp3 = LegalizeOp(Node->getOperand(2));
738 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000739 } else {
740 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000741 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000742 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
743 Ops.push_back(LegalizeOp(Node->getOperand(i)));
744 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000745 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000746 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000747
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000748 case ISD::BUILD_VECTOR:
749 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000750 default: assert(0 && "This action is not supported yet!");
751 case TargetLowering::Custom:
752 Tmp3 = TLI.LowerOperation(Result, DAG);
753 if (Tmp3.Val) {
754 Result = Tmp3;
755 break;
756 }
757 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000758 case TargetLowering::Expand:
759 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000760 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000761 }
Chris Lattner29b23012006-03-19 01:17:20 +0000762 break;
763 case ISD::INSERT_VECTOR_ELT:
764 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
765 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
766 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
768
769 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
770 Node->getValueType(0))) {
771 default: assert(0 && "This action is not supported yet!");
772 case TargetLowering::Legal:
773 break;
774 case TargetLowering::Custom:
775 Tmp3 = TLI.LowerOperation(Result, DAG);
776 if (Tmp3.Val) {
777 Result = Tmp3;
778 break;
779 }
780 // FALLTHROUGH
781 case TargetLowering::Expand: {
782 // If the target doesn't support this, we have to spill the input vector
783 // to a temporary stack slot, update the element, then reload it. This is
784 // badness. We could also load the value into a vector register (either
785 // with a "move to register" or "extload into register" instruction, then
786 // permute it into place, if the idx is a constant and if the idx is
787 // supported by the target.
788 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
789 break;
790 }
791 }
792 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000793 case ISD::SCALAR_TO_VECTOR:
794 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
795 Result = DAG.UpdateNodeOperands(Result, Tmp1);
796 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
797 Node->getValueType(0))) {
798 default: assert(0 && "This action is not supported yet!");
799 case TargetLowering::Legal:
800 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000801 case TargetLowering::Custom:
802 Tmp3 = TLI.LowerOperation(Result, DAG);
803 if (Tmp3.Val) {
804 Result = Tmp3;
805 break;
806 }
807 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000808 case TargetLowering::Expand: {
809 // If the target doesn't support this, store the value to a temporary
810 // stack slot, then EXTLOAD the vector back out.
Chris Lattner79fb91c2006-03-19 06:47:21 +0000811 // TODO: If a target doesn't support this, create a stack slot for the
812 // whole vector, then store into it, then load the whole vector.
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000813 SDOperand StackPtr =
814 CreateStackTemporary(Node->getOperand(0).getValueType());
815 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
816 Node->getOperand(0), StackPtr,
817 DAG.getSrcValue(NULL));
818 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
819 DAG.getSrcValue(NULL),
820 Node->getOperand(0).getValueType());
821 break;
822 }
823 }
824 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000825 case ISD::VECTOR_SHUFFLE:
826 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
827 "vector shuffle should not be created if not legal!");
828 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
829 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
830 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
831
832 // Allow targets to custom lower the SHUFFLEs they support.
833 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
834 == TargetLowering::Custom) {
835 Tmp1 = TLI.LowerOperation(Result, DAG);
836 if (Tmp1.Val) Result = Tmp1;
837 }
838 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000839
840 case ISD::EXTRACT_VECTOR_ELT:
841 Tmp1 = LegalizeOp(Node->getOperand(0));
842 Tmp2 = LegalizeOp(Node->getOperand(1));
843 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000844
845 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
846 Tmp1.getValueType())) {
847 default: assert(0 && "This action is not supported yet!");
848 case TargetLowering::Legal:
849 break;
850 case TargetLowering::Custom:
851 Tmp3 = TLI.LowerOperation(Result, DAG);
852 if (Tmp3.Val) {
853 Result = Tmp3;
854 break;
855 }
856 // FALLTHROUGH
857 case TargetLowering::Expand: {
858 // If the target doesn't support this, store the value to a temporary
859 // stack slot, then LOAD the scalar element back out.
860 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
861 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
862 Tmp1, StackPtr, DAG.getSrcValue(NULL));
863
864 // Add the offset to the index.
865 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
866 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
867 DAG.getConstant(EltSize, Tmp2.getValueType()));
868 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
869
870 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
871 DAG.getSrcValue(NULL));
872 break;
873 }
874 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000875 break;
876
Chris Lattner5be43522006-03-22 00:12:37 +0000877 case ISD::VEXTRACT_VECTOR_ELT: {
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000878 // We know that operand #0 is the Vec vector. If the index is a constant
879 // or if the invec is a supported hardware type, we can use it. Otherwise,
880 // lower to a store then an indexed load.
881 Tmp1 = Node->getOperand(0);
882 Tmp2 = LegalizeOp(Node->getOperand(1));
883
884 SDNode *InVal = Tmp1.Val;
885 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
886 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
887
888 // Figure out if there is a Packed type corresponding to this Vector
889 // type. If so, convert to the packed type.
890 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
891 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
892 // Turn this into a packed extract_vector_elt operation.
893 Tmp1 = PackVectorOp(Tmp1, TVT);
894 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Node->getValueType(0),
895 Tmp1, Tmp2);
896 break;
897 } else if (NumElems == 1) {
898 // This must be an access of the only element.
899 Result = PackVectorOp(Tmp1, EVT);
900 break;
901 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Tmp2)) {
902 SDOperand Lo, Hi;
903 SplitVectorOp(Tmp1, Lo, Hi);
904 if (CIdx->getValue() < NumElems/2) {
905 Tmp1 = Lo;
906 } else {
907 Tmp1 = Hi;
908 Tmp2 = DAG.getConstant(CIdx->getValue() - NumElems/2,
909 Tmp2.getValueType());
910 }
911
912 // It's now an extract from the appropriate high or low part.
913 Result = LegalizeOp(DAG.UpdateNodeOperands(Result, Tmp1, Tmp2));
914 } else {
Chris Lattner5be43522006-03-22 00:12:37 +0000915 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000916 assert(0 && "unimp!");
917 }
918 break;
Chris Lattner5be43522006-03-22 00:12:37 +0000919 }
Chris Lattner21e68c82006-03-20 01:52:29 +0000920
Chris Lattner462505f2006-02-13 09:18:02 +0000921 case ISD::CALLSEQ_START: {
922 SDNode *CallEnd = FindCallEndFromCallStart(Node);
923
924 // Recursively Legalize all of the inputs of the call end that do not lead
925 // to this call start. This ensures that any libcalls that need be inserted
926 // are inserted *before* the CALLSEQ_START.
927 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
928 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
929
930 // Now that we legalized all of the inputs (which may have inserted
931 // libcalls) create the new CALLSEQ_START node.
932 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
933
934 // Merge in the last call, to ensure that this call start after the last
935 // call ended.
936 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
937 Tmp1 = LegalizeOp(Tmp1);
938
939 // Do not try to legalize the target-specific arguments (#1+).
940 if (Tmp1 != Node->getOperand(0)) {
941 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
942 Ops[0] = Tmp1;
943 Result = DAG.UpdateNodeOperands(Result, Ops);
944 }
945
946 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000947 AddLegalizedOperand(Op.getValue(0), Result);
948 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
949 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
950
Chris Lattner462505f2006-02-13 09:18:02 +0000951 // Now that the callseq_start and all of the non-call nodes above this call
952 // sequence have been legalized, legalize the call itself. During this
953 // process, no libcalls can/will be inserted, guaranteeing that no calls
954 // can overlap.
955 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
956 SDOperand InCallSEQ = LastCALLSEQ_END;
957 // Note that we are selecting this call!
958 LastCALLSEQ_END = SDOperand(CallEnd, 0);
959 IsLegalizingCall = true;
960
961 // Legalize the call, starting from the CALLSEQ_END.
962 LegalizeOp(LastCALLSEQ_END);
963 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
964 return Result;
965 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000966 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +0000967 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
968 // will cause this node to be legalized as well as handling libcalls right.
969 if (LastCALLSEQ_END.Val != Node) {
970 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
971 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
972 assert(I != LegalizedNodes.end() &&
973 "Legalizing the call start should have legalized this node!");
974 return I->second;
975 }
976
977 // Otherwise, the call start has been legalized and everything is going
978 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +0000979 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000980 // Do not try to legalize the target-specific arguments (#1+), except for
981 // an optional flag input.
982 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
983 if (Tmp1 != Node->getOperand(0)) {
984 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
985 Ops[0] = Tmp1;
986 Result = DAG.UpdateNodeOperands(Result, Ops);
987 }
988 } else {
989 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
990 if (Tmp1 != Node->getOperand(0) ||
991 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
992 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
993 Ops[0] = Tmp1;
994 Ops.back() = Tmp2;
995 Result = DAG.UpdateNodeOperands(Result, Ops);
996 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +0000997 }
Chris Lattner8e2ee732006-02-14 00:55:02 +0000998 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +0000999 // This finishes up call legalization.
1000 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001001
1002 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1003 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1004 if (Node->getNumValues() == 2)
1005 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1006 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001007 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001008 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1009 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1010 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001012
Chris Lattnerd02b0542006-01-28 10:58:55 +00001013 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001014 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001015 switch (TLI.getOperationAction(Node->getOpcode(),
1016 Node->getValueType(0))) {
1017 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001018 case TargetLowering::Expand: {
1019 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1020 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1021 " not tell us which reg is the stack pointer!");
1022 SDOperand Chain = Tmp1.getOperand(0);
1023 SDOperand Size = Tmp2.getOperand(1);
1024 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1025 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1026 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001027 Tmp1 = LegalizeOp(Tmp1);
1028 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001029 break;
1030 }
1031 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001032 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1033 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001034 Tmp1 = LegalizeOp(Tmp3);
1035 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001036 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001037 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001038 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001039 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001040 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001041 // Since this op produce two values, make sure to remember that we
1042 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001043 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1044 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001045 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001046 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001047 case ISD::INLINEASM:
1048 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1049 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001050 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001051 Tmp2 = Tmp3 = SDOperand(0, 0);
1052 else
1053 Tmp3 = LegalizeOp(Tmp2);
1054
1055 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1056 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1057 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001058 if (Tmp3.Val) Ops.back() = Tmp3;
1059 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001060 }
1061
1062 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001063 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001064 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1065 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001066 case ISD::BR:
1067 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001068 // Ensure that libcalls are emitted before a branch.
1069 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1070 Tmp1 = LegalizeOp(Tmp1);
1071 LastCALLSEQ_END = DAG.getEntryNode();
1072
Chris Lattnerd02b0542006-01-28 10:58:55 +00001073 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001074 break;
1075
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001076 case ISD::BRCOND:
1077 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001078 // Ensure that libcalls are emitted before a return.
1079 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1080 Tmp1 = LegalizeOp(Tmp1);
1081 LastCALLSEQ_END = DAG.getEntryNode();
1082
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001083 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1084 case Expand: assert(0 && "It's impossible to expand bools");
1085 case Legal:
1086 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1087 break;
1088 case Promote:
1089 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1090 break;
1091 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001092
1093 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001094 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001095
1096 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1097 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001098 case TargetLowering::Legal: break;
1099 case TargetLowering::Custom:
1100 Tmp1 = TLI.LowerOperation(Result, DAG);
1101 if (Tmp1.Val) Result = Tmp1;
1102 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001103 case TargetLowering::Expand:
1104 // Expand brcond's setcc into its constituent parts and create a BR_CC
1105 // Node.
1106 if (Tmp2.getOpcode() == ISD::SETCC) {
1107 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1108 Tmp2.getOperand(0), Tmp2.getOperand(1),
1109 Node->getOperand(2));
1110 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001111 // Make sure the condition is either zero or one. It may have been
1112 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001113 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1114 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1115 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001116
Nate Begeman371e4952005-08-16 19:49:35 +00001117 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1118 DAG.getCondCode(ISD::SETNE), Tmp2,
1119 DAG.getConstant(0, Tmp2.getValueType()),
1120 Node->getOperand(2));
1121 }
1122 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001123 }
1124 break;
1125 case ISD::BR_CC:
1126 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001127 // Ensure that libcalls are emitted before a branch.
1128 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1129 Tmp1 = LegalizeOp(Tmp1);
1130 LastCALLSEQ_END = DAG.getEntryNode();
1131
Nate Begeman7e7f4392006-02-01 07:19:44 +00001132 Tmp2 = Node->getOperand(2); // LHS
1133 Tmp3 = Node->getOperand(3); // RHS
1134 Tmp4 = Node->getOperand(1); // CC
1135
1136 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1137
1138 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1139 // the LHS is a legal SETCC itself. In this case, we need to compare
1140 // the result against zero to select between true and false values.
1141 if (Tmp3.Val == 0) {
1142 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1143 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001144 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001145
1146 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1147 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001148
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001149 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1150 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001151 case TargetLowering::Legal: break;
1152 case TargetLowering::Custom:
1153 Tmp4 = TLI.LowerOperation(Result, DAG);
1154 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001155 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001156 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001157 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001158 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001159 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1160 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001161
Evan Cheng31d15fa2005-12-23 07:29:34 +00001162 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001163 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1164 Tmp2 = Result.getValue(0);
1165 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001166
Evan Cheng31d15fa2005-12-23 07:29:34 +00001167 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1168 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001169 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001170 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001171 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001172 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001173 Tmp2 = LegalizeOp(Tmp1);
1174 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001175 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001176 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001177 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001178 // Since loads produce two values, make sure to remember that we
1179 // legalized both of them.
1180 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1181 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1182 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001183 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001184 case ISD::EXTLOAD:
1185 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001186 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001187 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1188 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001189
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001190 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001191 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001192 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001193 case TargetLowering::Promote:
1194 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1196 DAG.getValueType(MVT::i8));
1197 Tmp1 = Result.getValue(0);
1198 Tmp2 = Result.getValue(1);
1199 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001200 case TargetLowering::Custom:
1201 isCustom = true;
1202 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001203 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001204 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1205 Node->getOperand(3));
1206 Tmp1 = Result.getValue(0);
1207 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001208
1209 if (isCustom) {
1210 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1211 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001212 Tmp1 = LegalizeOp(Tmp3);
1213 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001214 }
1215 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001216 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001217 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001218 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001219 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1220 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001221 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001222 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1223 Tmp2 = LegalizeOp(Load.getValue(1));
1224 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001225 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001226 assert(Node->getOpcode() != ISD::EXTLOAD &&
1227 "EXTLOAD should always be supported!");
1228 // Turn the unsupported load into an EXTLOAD followed by an explicit
1229 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001230 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1231 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001232 SDOperand ValRes;
1233 if (Node->getOpcode() == ISD::SEXTLOAD)
1234 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001235 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001236 else
1237 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001238 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1239 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1240 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001241 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001242 // Since loads produce two values, make sure to remember that we legalized
1243 // both of them.
1244 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1245 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1246 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001247 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001248 case ISD::EXTRACT_ELEMENT: {
1249 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1250 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001251 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001252 case Legal:
1253 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1254 // 1 -> Hi
1255 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1256 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1257 TLI.getShiftAmountTy()));
1258 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1259 } else {
1260 // 0 -> Lo
1261 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1262 Node->getOperand(0));
1263 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001264 break;
1265 case Expand:
1266 // Get both the low and high parts.
1267 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1268 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1269 Result = Tmp2; // 1 -> Hi
1270 else
1271 Result = Tmp1; // 0 -> Lo
1272 break;
1273 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001274 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001275 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001276
1277 case ISD::CopyToReg:
1278 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001279
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001280 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001281 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001282 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001283 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001284 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001285 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001286 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001287 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001288 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001289 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001290 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1291 Tmp3);
1292 } else {
1293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001294 }
1295
1296 // Since this produces two values, make sure to remember that we legalized
1297 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001298 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001299 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001300 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001301 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001302 break;
1303
1304 case ISD::RET:
1305 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001306
1307 // Ensure that libcalls are emitted before a return.
1308 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1309 Tmp1 = LegalizeOp(Tmp1);
1310 LastCALLSEQ_END = DAG.getEntryNode();
1311
Chris Lattnerdc750592005-01-07 07:47:09 +00001312 switch (Node->getNumOperands()) {
1313 case 2: // ret val
1314 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1315 case Legal:
1316 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001318 break;
1319 case Expand: {
1320 SDOperand Lo, Hi;
1321 ExpandOp(Node->getOperand(1), Lo, Hi);
1322 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001323 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001324 }
1325 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001326 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001327 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1328 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001329 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001330 }
1331 break;
1332 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001333 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001334 break;
1335 default: { // ret <values>
1336 std::vector<SDOperand> NewValues;
1337 NewValues.push_back(Tmp1);
1338 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1339 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1340 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001341 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001342 break;
1343 case Expand: {
1344 SDOperand Lo, Hi;
1345 ExpandOp(Node->getOperand(i), Lo, Hi);
1346 NewValues.push_back(Lo);
1347 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001348 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001349 }
1350 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001351 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001352 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001353
1354 if (NewValues.size() == Node->getNumOperands())
1355 Result = DAG.UpdateNodeOperands(Result, NewValues);
1356 else
1357 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001358 break;
1359 }
1360 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001361
Chris Lattner4d1ea712006-01-29 21:02:23 +00001362 if (Result.getOpcode() == ISD::RET) {
1363 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1364 default: assert(0 && "This action is not supported yet!");
1365 case TargetLowering::Legal: break;
1366 case TargetLowering::Custom:
1367 Tmp1 = TLI.LowerOperation(Result, DAG);
1368 if (Tmp1.Val) Result = Tmp1;
1369 break;
1370 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001371 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001372 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001373 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001374 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1375 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1376
Chris Lattnere69daaf2005-01-08 06:25:56 +00001377 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001378 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001379 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001380 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001381 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001382 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001383 } else {
1384 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001385 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001386 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001387 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1388 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001389 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001390 }
1391
Chris Lattnerdc750592005-01-07 07:47:09 +00001392 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1393 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001394 Tmp3 = LegalizeOp(Node->getOperand(1));
1395 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1396 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001397
Chris Lattnerd02b0542006-01-28 10:58:55 +00001398 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001399 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1400 default: assert(0 && "This action is not supported yet!");
1401 case TargetLowering::Legal: break;
1402 case TargetLowering::Custom:
1403 Tmp1 = TLI.LowerOperation(Result, DAG);
1404 if (Tmp1.Val) Result = Tmp1;
1405 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001406 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001407 break;
1408 }
1409 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001410 // Truncate the value and store the result.
1411 Tmp3 = PromoteOp(Node->getOperand(1));
1412 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001413 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001414 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001415 break;
1416
Chris Lattnerdc750592005-01-07 07:47:09 +00001417 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001418 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001419 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001420
1421 // If this is a vector type, then we have to calculate the increment as
1422 // the product of the element size in bytes, and the number of elements
1423 // in the high half of the vector.
1424 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1425 SDNode *InVal = Node->getOperand(1).Val;
1426 unsigned NumElems =
1427 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1428 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1429
1430 // Figure out if there is a Packed type corresponding to this Vector
1431 // type. If so, convert to the packed type.
1432 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1433 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1434 // Turn this into a normal store of the packed type.
1435 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1436 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1437 Node->getOperand(3));
1438 break;
1439 } else if (NumElems == 1) {
1440 // Turn this into a normal store of the scalar type.
1441 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1443 Node->getOperand(3));
1444 break;
1445 } else {
1446 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1447 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1448 }
1449 } else {
1450 ExpandOp(Node->getOperand(1), Lo, Hi);
1451 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1452 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001453
1454 if (!TLI.isLittleEndian())
1455 std::swap(Lo, Hi);
1456
Chris Lattner55e9cde2005-05-11 04:51:16 +00001457 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1458 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001459 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1460 getIntPtrConstant(IncrementSize));
1461 assert(isTypeLegal(Tmp2.getValueType()) &&
1462 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001463 // FIXME: This sets the srcvalue of both halves to be the same, which is
1464 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001465 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1466 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001467 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1468 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001469 }
1470 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001471 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001472 case ISD::PCMARKER:
1473 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001474 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001475 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001476 case ISD::STACKSAVE:
1477 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001478 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1479 Tmp1 = Result.getValue(0);
1480 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001481
Chris Lattnerb3266452006-01-13 02:50:02 +00001482 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1483 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001484 case TargetLowering::Legal: break;
1485 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001486 Tmp3 = TLI.LowerOperation(Result, DAG);
1487 if (Tmp3.Val) {
1488 Tmp1 = LegalizeOp(Tmp3);
1489 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001490 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001491 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001492 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001493 // Expand to CopyFromReg if the target set
1494 // StackPointerRegisterToSaveRestore.
1495 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001496 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001497 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001498 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001499 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001500 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1501 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001502 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001503 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001504 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001505
1506 // Since stacksave produce two values, make sure to remember that we
1507 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001508 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1509 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1510 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001511
Chris Lattnerb3266452006-01-13 02:50:02 +00001512 case ISD::STACKRESTORE:
1513 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1514 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001515 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001516
1517 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1518 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001519 case TargetLowering::Legal: break;
1520 case TargetLowering::Custom:
1521 Tmp1 = TLI.LowerOperation(Result, DAG);
1522 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001523 break;
1524 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001525 // Expand to CopyToReg if the target set
1526 // StackPointerRegisterToSaveRestore.
1527 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1528 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1529 } else {
1530 Result = Tmp1;
1531 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001532 break;
1533 }
1534 break;
1535
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001536 case ISD::READCYCLECOUNTER:
1537 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001538 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001539
1540 // Since rdcc produce two values, make sure to remember that we legalized
1541 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001542 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001543 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001544 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001545
Evan Cheng31d15fa2005-12-23 07:29:34 +00001546 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001547 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1548 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1549
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001550 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1551 "Cannot handle illegal TRUNCSTORE yet!");
1552 Tmp2 = LegalizeOp(Node->getOperand(1));
1553
1554 // The only promote case we handle is TRUNCSTORE:i1 X into
1555 // -> TRUNCSTORE:i8 (and X, 1)
1556 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1557 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1558 TargetLowering::Promote) {
1559 // Promote the bool to a mask then store.
1560 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1561 DAG.getConstant(1, Tmp2.getValueType()));
1562 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1563 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001564
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001565 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1566 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001567 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1568 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001569 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001570
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001571 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1572 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1573 default: assert(0 && "This action is not supported yet!");
1574 case TargetLowering::Legal: break;
1575 case TargetLowering::Custom:
1576 Tmp1 = TLI.LowerOperation(Result, DAG);
1577 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001578 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001579 }
1580 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001581 }
Chris Lattner39c67442005-01-14 22:08:15 +00001582 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001583 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1584 case Expand: assert(0 && "It's impossible to expand bools");
1585 case Legal:
1586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1587 break;
1588 case Promote:
1589 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1590 break;
1591 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001592 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001593 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001594
Chris Lattnerd02b0542006-01-28 10:58:55 +00001595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001596
Nate Begeman987121a2005-08-23 04:29:48 +00001597 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001598 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001599 case TargetLowering::Legal: break;
1600 case TargetLowering::Custom: {
1601 Tmp1 = TLI.LowerOperation(Result, DAG);
1602 if (Tmp1.Val) Result = Tmp1;
1603 break;
1604 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001605 case TargetLowering::Expand:
1606 if (Tmp1.getOpcode() == ISD::SETCC) {
1607 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1608 Tmp2, Tmp3,
1609 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1610 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001611 // Make sure the condition is either zero or one. It may have been
1612 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001613 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1614 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1615 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001616 Result = DAG.getSelectCC(Tmp1,
1617 DAG.getConstant(0, Tmp1.getValueType()),
1618 Tmp2, Tmp3, ISD::SETNE);
1619 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001620 break;
1621 case TargetLowering::Promote: {
1622 MVT::ValueType NVT =
1623 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1624 unsigned ExtOp, TruncOp;
1625 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001626 ExtOp = ISD::ANY_EXTEND;
1627 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001628 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001629 ExtOp = ISD::FP_EXTEND;
1630 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001631 }
1632 // Promote each of the values to the new type.
1633 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1634 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1635 // Perform the larger operation, then round down.
1636 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1637 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1638 break;
1639 }
1640 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001641 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001642 case ISD::SELECT_CC: {
1643 Tmp1 = Node->getOperand(0); // LHS
1644 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001645 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1646 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001647 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001648
Nate Begeman7e7f4392006-02-01 07:19:44 +00001649 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1650
1651 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1652 // the LHS is a legal SETCC itself. In this case, we need to compare
1653 // the result against zero to select between true and false values.
1654 if (Tmp2.Val == 0) {
1655 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1656 CC = DAG.getCondCode(ISD::SETNE);
1657 }
1658 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1659
1660 // Everything is legal, see if we should expand this op or something.
1661 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1662 default: assert(0 && "This action is not supported yet!");
1663 case TargetLowering::Legal: break;
1664 case TargetLowering::Custom:
1665 Tmp1 = TLI.LowerOperation(Result, DAG);
1666 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001667 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001668 }
1669 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001670 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001671 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001672 Tmp1 = Node->getOperand(0);
1673 Tmp2 = Node->getOperand(1);
1674 Tmp3 = Node->getOperand(2);
1675 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1676
1677 // If we had to Expand the SetCC operands into a SELECT node, then it may
1678 // not always be possible to return a true LHS & RHS. In this case, just
1679 // return the value we legalized, returned in the LHS
1680 if (Tmp2.Val == 0) {
1681 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001682 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001683 }
Nate Begeman987121a2005-08-23 04:29:48 +00001684
Chris Lattnerf263a232006-01-30 22:43:50 +00001685 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001686 default: assert(0 && "Cannot handle this action for SETCC yet!");
1687 case TargetLowering::Custom:
1688 isCustom = true;
1689 // FALLTHROUGH.
1690 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001691 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001692 if (isCustom) {
1693 Tmp3 = TLI.LowerOperation(Result, DAG);
1694 if (Tmp3.Val) Result = Tmp3;
1695 }
Nate Begeman987121a2005-08-23 04:29:48 +00001696 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001697 case TargetLowering::Promote: {
1698 // First step, figure out the appropriate operation to use.
1699 // Allow SETCC to not be supported for all legal data types
1700 // Mostly this targets FP
1701 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1702 MVT::ValueType OldVT = NewInTy;
1703
1704 // Scan for the appropriate larger type to use.
1705 while (1) {
1706 NewInTy = (MVT::ValueType)(NewInTy+1);
1707
1708 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1709 "Fell off of the edge of the integer world");
1710 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1711 "Fell off of the edge of the floating point world");
1712
1713 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001714 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001715 break;
1716 }
1717 if (MVT::isInteger(NewInTy))
1718 assert(0 && "Cannot promote Legal Integer SETCC yet");
1719 else {
1720 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1721 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1722 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001723 Tmp1 = LegalizeOp(Tmp1);
1724 Tmp2 = LegalizeOp(Tmp2);
1725 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001726 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001727 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001728 }
Nate Begeman987121a2005-08-23 04:29:48 +00001729 case TargetLowering::Expand:
1730 // Expand a setcc node into a select_cc of the same condition, lhs, and
1731 // rhs that selects between const 1 (true) and const 0 (false).
1732 MVT::ValueType VT = Node->getValueType(0);
1733 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1734 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1735 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001736 break;
1737 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001738 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001739 case ISD::MEMSET:
1740 case ISD::MEMCPY:
1741 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001742 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001743 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1744
1745 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1746 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1747 case Expand: assert(0 && "Cannot expand a byte!");
1748 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001749 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001750 break;
1751 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001752 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001753 break;
1754 }
1755 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001756 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001757 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001758
1759 SDOperand Tmp4;
1760 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001761 case Expand: {
1762 // Length is too big, just take the lo-part of the length.
1763 SDOperand HiPart;
1764 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1765 break;
1766 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001767 case Legal:
1768 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001769 break;
1770 case Promote:
1771 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001772 break;
1773 }
1774
1775 SDOperand Tmp5;
1776 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1777 case Expand: assert(0 && "Cannot expand this yet!");
1778 case Legal:
1779 Tmp5 = LegalizeOp(Node->getOperand(4));
1780 break;
1781 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001782 Tmp5 = PromoteOp(Node->getOperand(4));
1783 break;
1784 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001785
1786 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1787 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001788 case TargetLowering::Custom:
1789 isCustom = true;
1790 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001791 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001792 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001793 if (isCustom) {
1794 Tmp1 = TLI.LowerOperation(Result, DAG);
1795 if (Tmp1.Val) Result = Tmp1;
1796 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001797 break;
1798 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001799 // Otherwise, the target does not support this operation. Lower the
1800 // operation to an explicit libcall as appropriate.
1801 MVT::ValueType IntPtr = TLI.getPointerTy();
1802 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1803 std::vector<std::pair<SDOperand, const Type*> > Args;
1804
Reid Spencer6dced922005-01-12 14:53:45 +00001805 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001806 if (Node->getOpcode() == ISD::MEMSET) {
1807 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001808 // Extend the (previously legalized) ubyte argument to be an int value
1809 // for the call.
1810 if (Tmp3.getValueType() > MVT::i32)
1811 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1812 else
1813 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001814 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1815 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1816
1817 FnName = "memset";
1818 } else if (Node->getOpcode() == ISD::MEMCPY ||
1819 Node->getOpcode() == ISD::MEMMOVE) {
1820 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1821 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1822 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1823 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1824 } else {
1825 assert(0 && "Unknown op!");
1826 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001827
Chris Lattner85d70c62005-01-11 05:57:22 +00001828 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001829 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001830 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001831 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001832 break;
1833 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001834 }
1835 break;
1836 }
Chris Lattner5385db52005-05-09 20:23:03 +00001837
Chris Lattner4157c412005-04-02 04:00:59 +00001838 case ISD::SHL_PARTS:
1839 case ISD::SRA_PARTS:
1840 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001841 std::vector<SDOperand> Ops;
1842 bool Changed = false;
1843 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1844 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1845 Changed |= Ops.back() != Node->getOperand(i);
1846 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001847 if (Changed)
1848 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001849
Evan Cheng870e4f82006-01-09 18:31:59 +00001850 switch (TLI.getOperationAction(Node->getOpcode(),
1851 Node->getValueType(0))) {
1852 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001853 case TargetLowering::Legal: break;
1854 case TargetLowering::Custom:
1855 Tmp1 = TLI.LowerOperation(Result, DAG);
1856 if (Tmp1.Val) {
1857 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001858 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001859 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001860 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1861 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001862 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001863 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001864 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001865 return RetVal;
1866 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001867 break;
1868 }
1869
Chris Lattner13fe99c2005-04-02 05:00:07 +00001870 // Since these produce multiple values, make sure to remember that we
1871 // legalized all of them.
1872 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1873 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1874 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001875 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001876
1877 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001878 case ISD::ADD:
1879 case ISD::SUB:
1880 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001881 case ISD::MULHS:
1882 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001883 case ISD::UDIV:
1884 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001885 case ISD::AND:
1886 case ISD::OR:
1887 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001888 case ISD::SHL:
1889 case ISD::SRL:
1890 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001891 case ISD::FADD:
1892 case ISD::FSUB:
1893 case ISD::FMUL:
1894 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001895 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001896 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1897 case Expand: assert(0 && "Not possible");
1898 case Legal:
1899 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1900 break;
1901 case Promote:
1902 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1903 break;
1904 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001905
1906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001907
Andrew Lenharth72594262005-12-24 23:42:32 +00001908 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001909 default: assert(0 && "Operation not supported");
1910 case TargetLowering::Legal: break;
1911 case TargetLowering::Custom:
1912 Tmp1 = TLI.LowerOperation(Result, DAG);
1913 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001914 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001915 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001916 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001917
1918 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1919 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1920 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1921 case Expand: assert(0 && "Not possible");
1922 case Legal:
1923 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1924 break;
1925 case Promote:
1926 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1927 break;
1928 }
1929
1930 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1931
1932 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1933 default: assert(0 && "Operation not supported");
1934 case TargetLowering::Custom:
1935 Tmp1 = TLI.LowerOperation(Result, DAG);
1936 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00001937 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001938 case TargetLowering::Legal: break;
1939 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00001940 // If this target supports fabs/fneg natively, do this efficiently.
1941 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1942 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1943 // Get the sign bit of the RHS.
1944 MVT::ValueType IVT =
1945 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1946 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1947 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1948 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1949 // Get the absolute value of the result.
1950 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1951 // Select between the nabs and abs value based on the sign bit of
1952 // the input.
1953 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1954 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1955 AbsVal),
1956 AbsVal);
1957 Result = LegalizeOp(Result);
1958 break;
1959 }
1960
1961 // Otherwise, do bitwise ops!
1962
1963 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001964 const char *FnName;
1965 if (Node->getValueType(0) == MVT::f32) {
1966 FnName = "copysignf";
1967 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1968 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1969 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1970 } else {
1971 FnName = "copysign";
1972 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1973 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1974 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1975 }
1976 SDOperand Dummy;
1977 Result = ExpandLibCall(FnName, Node, Dummy);
1978 break;
1979 }
1980 break;
1981
Nate Begeman5965bd12006-02-17 05:43:56 +00001982 case ISD::ADDC:
1983 case ISD::SUBC:
1984 Tmp1 = LegalizeOp(Node->getOperand(0));
1985 Tmp2 = LegalizeOp(Node->getOperand(1));
1986 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1987 // Since this produces two values, make sure to remember that we legalized
1988 // both of them.
1989 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1990 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1991 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00001992
Nate Begeman5965bd12006-02-17 05:43:56 +00001993 case ISD::ADDE:
1994 case ISD::SUBE:
1995 Tmp1 = LegalizeOp(Node->getOperand(0));
1996 Tmp2 = LegalizeOp(Node->getOperand(1));
1997 Tmp3 = LegalizeOp(Node->getOperand(2));
1998 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1999 // Since this produces two values, make sure to remember that we legalized
2000 // both of them.
2001 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2002 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2003 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002004
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002005 case ISD::BUILD_PAIR: {
2006 MVT::ValueType PairTy = Node->getValueType(0);
2007 // TODO: handle the case where the Lo and Hi operands are not of legal type
2008 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2009 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2010 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002011 case TargetLowering::Promote:
2012 case TargetLowering::Custom:
2013 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002014 case TargetLowering::Legal:
2015 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2016 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2017 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002018 case TargetLowering::Expand:
2019 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2020 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2021 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2022 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2023 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002024 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002025 break;
2026 }
2027 break;
2028 }
2029
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002030 case ISD::UREM:
2031 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002032 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002033 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2034 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002035
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002036 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002037 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2038 case TargetLowering::Custom:
2039 isCustom = true;
2040 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002041 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002043 if (isCustom) {
2044 Tmp1 = TLI.LowerOperation(Result, DAG);
2045 if (Tmp1.Val) Result = Tmp1;
2046 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002047 break;
Chris Lattner81914422005-08-03 20:31:37 +00002048 case TargetLowering::Expand:
2049 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002050 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002051 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002052 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002053 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2054 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2055 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2056 } else {
2057 // Floating point mod -> fmod libcall.
2058 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2059 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002060 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002061 }
2062 break;
2063 }
2064 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002065 case ISD::VAARG: {
2066 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2067 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2068
Chris Lattner364b89a2006-01-28 07:42:08 +00002069 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002070 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2071 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002072 case TargetLowering::Custom:
2073 isCustom = true;
2074 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002075 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002076 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2077 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002078 Tmp1 = Result.getValue(1);
2079
2080 if (isCustom) {
2081 Tmp2 = TLI.LowerOperation(Result, DAG);
2082 if (Tmp2.Val) {
2083 Result = LegalizeOp(Tmp2);
2084 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2085 }
2086 }
Nate Begemane74795c2006-01-25 18:21:52 +00002087 break;
2088 case TargetLowering::Expand: {
2089 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2090 Node->getOperand(2));
2091 // Increment the pointer, VAList, to the next vaarg
2092 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2093 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2094 TLI.getPointerTy()));
2095 // Store the incremented VAList to the legalized pointer
2096 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2097 Node->getOperand(2));
2098 // Load the actual argument out of the pointer VAList
2099 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002100 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002101 Result = LegalizeOp(Result);
2102 break;
2103 }
2104 }
2105 // Since VAARG produces two values, make sure to remember that we
2106 // legalized both of them.
2107 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002108 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2109 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002110 }
2111
2112 case ISD::VACOPY:
2113 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2114 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2115 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2116
2117 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2118 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002119 case TargetLowering::Custom:
2120 isCustom = true;
2121 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002122 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002123 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2124 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002125 if (isCustom) {
2126 Tmp1 = TLI.LowerOperation(Result, DAG);
2127 if (Tmp1.Val) Result = Tmp1;
2128 }
Nate Begemane74795c2006-01-25 18:21:52 +00002129 break;
2130 case TargetLowering::Expand:
2131 // This defaults to loading a pointer from the input and storing it to the
2132 // output, returning the chain.
2133 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2134 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2135 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002136 break;
2137 }
2138 break;
2139
2140 case ISD::VAEND:
2141 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2142 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2143
2144 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2145 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002146 case TargetLowering::Custom:
2147 isCustom = true;
2148 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002149 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002151 if (isCustom) {
2152 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2153 if (Tmp1.Val) Result = Tmp1;
2154 }
Nate Begemane74795c2006-01-25 18:21:52 +00002155 break;
2156 case TargetLowering::Expand:
2157 Result = Tmp1; // Default to a no-op, return the chain
2158 break;
2159 }
2160 break;
2161
2162 case ISD::VASTART:
2163 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2164 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2165
Chris Lattnerd02b0542006-01-28 10:58:55 +00002166 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2167
Nate Begemane74795c2006-01-25 18:21:52 +00002168 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2169 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002170 case TargetLowering::Legal: break;
2171 case TargetLowering::Custom:
2172 Tmp1 = TLI.LowerOperation(Result, DAG);
2173 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002174 break;
2175 }
2176 break;
2177
Nate Begeman1b8121b2006-01-11 21:21:00 +00002178 case ISD::ROTL:
2179 case ISD::ROTR:
2180 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2181 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002182
2183 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2184 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002186 break;
2187
Nate Begeman2fba8a32006-01-14 03:14:10 +00002188 case ISD::BSWAP:
2189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2190 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002191 case TargetLowering::Custom:
2192 assert(0 && "Cannot custom legalize this yet!");
2193 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002194 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002195 break;
2196 case TargetLowering::Promote: {
2197 MVT::ValueType OVT = Tmp1.getValueType();
2198 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2199 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002200
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002201 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2202 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2203 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2204 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2205 break;
2206 }
2207 case TargetLowering::Expand:
2208 Result = ExpandBSWAP(Tmp1);
2209 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002210 }
2211 break;
2212
Andrew Lenharth5e177822005-05-03 17:19:30 +00002213 case ISD::CTPOP:
2214 case ISD::CTTZ:
2215 case ISD::CTLZ:
2216 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2217 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002218 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002219 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002220 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002221 break;
2222 case TargetLowering::Promote: {
2223 MVT::ValueType OVT = Tmp1.getValueType();
2224 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002225
2226 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002227 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2228 // Perform the larger operation, then subtract if needed.
2229 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002230 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002231 case ISD::CTPOP:
2232 Result = Tmp1;
2233 break;
2234 case ISD::CTTZ:
2235 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002236 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2237 DAG.getConstant(getSizeInBits(NVT), NVT),
2238 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002239 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002240 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2241 break;
2242 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002243 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002244 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2245 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002246 getSizeInBits(OVT), NVT));
2247 break;
2248 }
2249 break;
2250 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002251 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002252 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002253 break;
2254 }
2255 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002256
Chris Lattner13fe99c2005-04-02 05:00:07 +00002257 // Unary operators
2258 case ISD::FABS:
2259 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002260 case ISD::FSQRT:
2261 case ISD::FSIN:
2262 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002263 Tmp1 = LegalizeOp(Node->getOperand(0));
2264 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002265 case TargetLowering::Promote:
2266 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002267 isCustom = true;
2268 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002269 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002270 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002271 if (isCustom) {
2272 Tmp1 = TLI.LowerOperation(Result, DAG);
2273 if (Tmp1.Val) Result = Tmp1;
2274 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002275 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002276 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002277 switch (Node->getOpcode()) {
2278 default: assert(0 && "Unreachable!");
2279 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002280 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2281 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002282 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002283 break;
Chris Lattner80026402005-04-30 04:43:14 +00002284 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002285 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2286 MVT::ValueType VT = Node->getValueType(0);
2287 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002288 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002289 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2290 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002291 break;
2292 }
2293 case ISD::FSQRT:
2294 case ISD::FSIN:
2295 case ISD::FCOS: {
2296 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002297 const char *FnName = 0;
2298 switch(Node->getOpcode()) {
2299 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2300 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2301 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2302 default: assert(0 && "Unreachable!");
2303 }
Nate Begeman77558da2005-08-04 21:43:28 +00002304 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002305 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002306 break;
2307 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002308 }
2309 break;
2310 }
2311 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002312
2313 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002314 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002315 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002316 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002317 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2318 Node->getOperand(0).getValueType())) {
2319 default: assert(0 && "Unknown operation action!");
2320 case TargetLowering::Expand:
2321 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2322 break;
2323 case TargetLowering::Legal:
2324 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002325 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002326 break;
2327 }
2328 }
2329 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002330 case ISD::VBIT_CONVERT: {
2331 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2332 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2333
2334 // The input has to be a vector type, we have to either scalarize it, pack
2335 // it, or convert it based on whether the input vector type is legal.
2336 SDNode *InVal = Node->getOperand(0).Val;
2337 unsigned NumElems =
2338 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2339 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2340
2341 // Figure out if there is a Packed type corresponding to this Vector
2342 // type. If so, convert to the packed type.
2343 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2344 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2345 // Turn this into a bit convert of the packed input.
2346 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2347 PackVectorOp(Node->getOperand(0), TVT));
2348 break;
2349 } else if (NumElems == 1) {
2350 // Turn this into a bit convert of the scalar input.
2351 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2352 PackVectorOp(Node->getOperand(0), EVT));
2353 break;
2354 } else {
2355 // FIXME: UNIMP! Store then reload
2356 assert(0 && "Cast from unsupported vector type not implemented yet!");
2357 }
2358 }
2359
Chris Lattner13fe99c2005-04-02 05:00:07 +00002360 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002361 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002362 case ISD::UINT_TO_FP: {
2363 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002364 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2365 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002366 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002367 Node->getOperand(0).getValueType())) {
2368 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002369 case TargetLowering::Custom:
2370 isCustom = true;
2371 // FALLTHROUGH
2372 case TargetLowering::Legal:
2373 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002374 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002375 if (isCustom) {
2376 Tmp1 = TLI.LowerOperation(Result, DAG);
2377 if (Tmp1.Val) Result = Tmp1;
2378 }
2379 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002380 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002381 Result = ExpandLegalINT_TO_FP(isSigned,
2382 LegalizeOp(Node->getOperand(0)),
2383 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002384 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002385 case TargetLowering::Promote:
2386 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2387 Node->getValueType(0),
2388 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002389 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002390 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002391 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002392 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002393 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2394 Node->getValueType(0), Node->getOperand(0));
2395 break;
2396 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002397 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002398 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002399 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2400 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002401 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002402 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2403 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002404 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002405 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2406 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002407 break;
2408 }
2409 break;
2410 }
2411 case ISD::TRUNCATE:
2412 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2413 case Legal:
2414 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002415 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002416 break;
2417 case Expand:
2418 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2419
2420 // Since the result is legal, we should just be able to truncate the low
2421 // part of the source.
2422 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2423 break;
2424 case Promote:
2425 Result = PromoteOp(Node->getOperand(0));
2426 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2427 break;
2428 }
2429 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002430
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002431 case ISD::FP_TO_SINT:
2432 case ISD::FP_TO_UINT:
2433 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2434 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002435 Tmp1 = LegalizeOp(Node->getOperand(0));
2436
Chris Lattner44fe26f2005-07-29 00:11:56 +00002437 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2438 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002439 case TargetLowering::Custom:
2440 isCustom = true;
2441 // FALLTHROUGH
2442 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002443 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002444 if (isCustom) {
2445 Tmp1 = TLI.LowerOperation(Result, DAG);
2446 if (Tmp1.Val) Result = Tmp1;
2447 }
2448 break;
2449 case TargetLowering::Promote:
2450 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2451 Node->getOpcode() == ISD::FP_TO_SINT);
2452 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002453 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002454 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2455 SDOperand True, False;
2456 MVT::ValueType VT = Node->getOperand(0).getValueType();
2457 MVT::ValueType NVT = Node->getValueType(0);
2458 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2459 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2460 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2461 Node->getOperand(0), Tmp2, ISD::SETLT);
2462 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2463 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002464 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002465 Tmp2));
2466 False = DAG.getNode(ISD::XOR, NVT, False,
2467 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002468 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2469 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002470 } else {
2471 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2472 }
2473 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002474 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002475 break;
2476 case Expand:
2477 assert(0 && "Shouldn't need to expand other operators here!");
2478 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002479 Tmp1 = PromoteOp(Node->getOperand(0));
2480 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2481 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002482 break;
2483 }
2484 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002485
Chris Lattner7753f172005-09-02 00:18:10 +00002486 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002487 case ISD::ZERO_EXTEND:
2488 case ISD::SIGN_EXTEND:
2489 case ISD::FP_EXTEND:
2490 case ISD::FP_ROUND:
2491 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002492 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002493 case Legal:
2494 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002495 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002496 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002497 case Promote:
2498 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002499 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002500 Tmp1 = PromoteOp(Node->getOperand(0));
2501 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002502 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002503 case ISD::ZERO_EXTEND:
2504 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002505 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002506 Result = DAG.getZeroExtendInReg(Result,
2507 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002508 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002509 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002510 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002511 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002512 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002513 Result,
2514 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002515 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002516 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002517 Result = PromoteOp(Node->getOperand(0));
2518 if (Result.getValueType() != Op.getValueType())
2519 // Dynamically dead while we have only 2 FP types.
2520 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2521 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002522 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002523 Result = PromoteOp(Node->getOperand(0));
2524 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2525 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002526 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002527 }
2528 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002529 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002530 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002531 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002532 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002533
2534 // If this operation is not supported, convert it to a shl/shr or load/store
2535 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002536 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2537 default: assert(0 && "This action not supported for this op yet!");
2538 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002539 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002540 break;
2541 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002542 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002543 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002544 // NOTE: we could fall back on load/store here too for targets without
2545 // SAR. However, it is doubtful that any exist.
2546 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2547 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002548 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002549 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2550 Node->getOperand(0), ShiftCst);
2551 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2552 Result, ShiftCst);
2553 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2554 // The only way we can lower this is to turn it into a STORETRUNC,
2555 // EXTLOAD pair, targetting a temporary location (a stack slot).
2556
2557 // NOTE: there is a choice here between constantly creating new stack
2558 // slots and always reusing the same one. We currently always create
2559 // new ones, as reuse may inhibit scheduling.
2560 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2561 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2562 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2563 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002564 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002565 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2566 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2567 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002568 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002569 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002570 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2571 Result, StackSlot, DAG.getSrcValue(NULL),
2572 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002573 } else {
2574 assert(0 && "Unknown op");
2575 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002576 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002577 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002578 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002579 }
Chris Lattner99222f72005-01-15 07:15:18 +00002580 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002581
2582 // Make sure that the generated code is itself legal.
2583 if (Result != Op)
2584 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002585
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002586 // Note that LegalizeOp may be reentered even from single-use nodes, which
2587 // means that we always must cache transformed nodes.
2588 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002589 return Result;
2590}
2591
Chris Lattner4d978642005-01-15 22:16:26 +00002592/// PromoteOp - Given an operation that produces a value in an invalid type,
2593/// promote it to compute the value into a larger type. The produced value will
2594/// have the correct bits for the low portion of the register, but no guarantee
2595/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002596SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2597 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002598 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002599 assert(getTypeAction(VT) == Promote &&
2600 "Caller should expand or legalize operands that are not promotable!");
2601 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2602 "Cannot promote to smaller type!");
2603
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002604 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002605 SDOperand Result;
2606 SDNode *Node = Op.Val;
2607
Chris Lattner1a570f12005-09-02 20:32:45 +00002608 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2609 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002610
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002611 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002612 case ISD::CopyFromReg:
2613 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002614 default:
2615 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2616 assert(0 && "Do not know how to promote this operator!");
2617 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002618 case ISD::UNDEF:
2619 Result = DAG.getNode(ISD::UNDEF, NVT);
2620 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002621 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002622 if (VT != MVT::i1)
2623 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2624 else
2625 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002626 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2627 break;
2628 case ISD::ConstantFP:
2629 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2630 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2631 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002632
Chris Lattner2cb338d2005-01-18 02:59:52 +00002633 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002634 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002635 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2636 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002637 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002638
2639 case ISD::TRUNCATE:
2640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2641 case Legal:
2642 Result = LegalizeOp(Node->getOperand(0));
2643 assert(Result.getValueType() >= NVT &&
2644 "This truncation doesn't make sense!");
2645 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2646 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2647 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002648 case Promote:
2649 // The truncation is not required, because we don't guarantee anything
2650 // about high bits anyway.
2651 Result = PromoteOp(Node->getOperand(0));
2652 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002653 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002654 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2655 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002656 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002657 }
2658 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002659 case ISD::SIGN_EXTEND:
2660 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002661 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002662 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2663 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2664 case Legal:
2665 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002666 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002667 break;
2668 case Promote:
2669 // Promote the reg if it's smaller.
2670 Result = PromoteOp(Node->getOperand(0));
2671 // The high bits are not guaranteed to be anything. Insert an extend.
2672 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002673 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002674 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002675 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002676 Result = DAG.getZeroExtendInReg(Result,
2677 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002678 break;
2679 }
2680 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002681 case ISD::BIT_CONVERT:
2682 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2683 Result = PromoteOp(Result);
2684 break;
2685
Chris Lattner4d978642005-01-15 22:16:26 +00002686 case ISD::FP_EXTEND:
2687 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2688 case ISD::FP_ROUND:
2689 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2690 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2691 case Promote: assert(0 && "Unreachable with 2 FP types!");
2692 case Legal:
2693 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002694 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002695 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002696 break;
2697 }
2698 break;
2699
2700 case ISD::SINT_TO_FP:
2701 case ISD::UINT_TO_FP:
2702 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2703 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002704 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002705 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002706 break;
2707
2708 case Promote:
2709 Result = PromoteOp(Node->getOperand(0));
2710 if (Node->getOpcode() == ISD::SINT_TO_FP)
2711 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002712 Result,
2713 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002714 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002715 Result = DAG.getZeroExtendInReg(Result,
2716 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002717 // No extra round required here.
2718 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002719 break;
2720 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002721 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2722 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002723 // Round if we cannot tolerate excess precision.
2724 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002725 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2726 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002727 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002728 }
Chris Lattner4d978642005-01-15 22:16:26 +00002729 break;
2730
Chris Lattner268d4572005-12-09 17:32:47 +00002731 case ISD::SIGN_EXTEND_INREG:
2732 Result = PromoteOp(Node->getOperand(0));
2733 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2734 Node->getOperand(1));
2735 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002736 case ISD::FP_TO_SINT:
2737 case ISD::FP_TO_UINT:
2738 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2739 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002740 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002741 break;
2742 case Promote:
2743 // The input result is prerounded, so we don't have to do anything
2744 // special.
2745 Tmp1 = PromoteOp(Node->getOperand(0));
2746 break;
2747 case Expand:
2748 assert(0 && "not implemented");
2749 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002750 // If we're promoting a UINT to a larger size, check to see if the new node
2751 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2752 // we can use that instead. This allows us to generate better code for
2753 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2754 // legal, such as PowerPC.
2755 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002756 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002757 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2758 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002759 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2760 } else {
2761 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2762 }
Chris Lattner4d978642005-01-15 22:16:26 +00002763 break;
2764
Chris Lattner13fe99c2005-04-02 05:00:07 +00002765 case ISD::FABS:
2766 case ISD::FNEG:
2767 Tmp1 = PromoteOp(Node->getOperand(0));
2768 assert(Tmp1.getValueType() == NVT);
2769 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2770 // NOTE: we do not have to do any extra rounding here for
2771 // NoExcessFPPrecision, because we know the input will have the appropriate
2772 // precision, and these operations don't modify precision at all.
2773 break;
2774
Chris Lattner9d6fa982005-04-28 21:44:33 +00002775 case ISD::FSQRT:
2776 case ISD::FSIN:
2777 case ISD::FCOS:
2778 Tmp1 = PromoteOp(Node->getOperand(0));
2779 assert(Tmp1.getValueType() == NVT);
2780 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002781 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002782 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2783 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002784 break;
2785
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002786 case ISD::AND:
2787 case ISD::OR:
2788 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002789 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002790 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002791 case ISD::MUL:
2792 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002793 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002794 // that too is okay if they are integer operations.
2795 Tmp1 = PromoteOp(Node->getOperand(0));
2796 Tmp2 = PromoteOp(Node->getOperand(1));
2797 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2798 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002799 break;
2800 case ISD::FADD:
2801 case ISD::FSUB:
2802 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002803 Tmp1 = PromoteOp(Node->getOperand(0));
2804 Tmp2 = PromoteOp(Node->getOperand(1));
2805 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2806 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2807
2808 // Floating point operations will give excess precision that we may not be
2809 // able to tolerate. If we DO allow excess precision, just leave it,
2810 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002811 // FIXME: Why would we need to round FP ops more than integer ones?
2812 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002813 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002814 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2815 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002816 break;
2817
Chris Lattner4d978642005-01-15 22:16:26 +00002818 case ISD::SDIV:
2819 case ISD::SREM:
2820 // These operators require that their input be sign extended.
2821 Tmp1 = PromoteOp(Node->getOperand(0));
2822 Tmp2 = PromoteOp(Node->getOperand(1));
2823 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002824 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2825 DAG.getValueType(VT));
2826 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2827 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002828 }
2829 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2830
2831 // Perform FP_ROUND: this is probably overly pessimistic.
2832 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002833 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2834 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002835 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002836 case ISD::FDIV:
2837 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002838 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002839 // These operators require that their input be fp extended.
2840 Tmp1 = PromoteOp(Node->getOperand(0));
2841 Tmp2 = PromoteOp(Node->getOperand(1));
2842 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2843
2844 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002845 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002846 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2847 DAG.getValueType(VT));
2848 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002849
2850 case ISD::UDIV:
2851 case ISD::UREM:
2852 // These operators require that their input be zero extended.
2853 Tmp1 = PromoteOp(Node->getOperand(0));
2854 Tmp2 = PromoteOp(Node->getOperand(1));
2855 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002856 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2857 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002858 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2859 break;
2860
2861 case ISD::SHL:
2862 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002863 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002864 break;
2865 case ISD::SRA:
2866 // The input value must be properly sign extended.
2867 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002868 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2869 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002870 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002871 break;
2872 case ISD::SRL:
2873 // The input value must be properly zero extended.
2874 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002875 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002876 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002877 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002878
2879 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002880 Tmp1 = Node->getOperand(0); // Get the chain.
2881 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002882 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2883 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2884 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2885 } else {
2886 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2887 Node->getOperand(2));
2888 // Increment the pointer, VAList, to the next vaarg
2889 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2890 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2891 TLI.getPointerTy()));
2892 // Store the incremented VAList to the legalized pointer
2893 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2894 Node->getOperand(2));
2895 // Load the actual argument out of the pointer VAList
2896 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2897 DAG.getSrcValue(0), VT);
2898 }
2899 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002900 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002901 break;
2902
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002903 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002904 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2905 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002906 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002907 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002908 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002909 case ISD::SEXTLOAD:
2910 case ISD::ZEXTLOAD:
2911 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002912 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2913 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002914 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002915 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002916 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002917 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002918 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002919 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2920 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002921 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002922 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002923 case ISD::SELECT_CC:
2924 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2925 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2926 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002927 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002928 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002929 case ISD::BSWAP:
2930 Tmp1 = Node->getOperand(0);
2931 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2932 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2933 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2934 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2935 TLI.getShiftAmountTy()));
2936 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002937 case ISD::CTPOP:
2938 case ISD::CTTZ:
2939 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002940 // Zero extend the argument
2941 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002942 // Perform the larger operation, then subtract if needed.
2943 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002944 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002945 case ISD::CTPOP:
2946 Result = Tmp1;
2947 break;
2948 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002949 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002950 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002951 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002952 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002953 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002954 break;
2955 case ISD::CTLZ:
2956 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002957 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2958 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002959 getSizeInBits(VT), NVT));
2960 break;
2961 }
2962 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002963 }
2964
2965 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002966
2967 // Make sure the result is itself legal.
2968 Result = LegalizeOp(Result);
2969
2970 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002971 AddPromotedOperand(Op, Result);
2972 return Result;
2973}
Chris Lattnerdc750592005-01-07 07:47:09 +00002974
Nate Begeman7e7f4392006-02-01 07:19:44 +00002975/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2976/// with condition CC on the current target. This usually involves legalizing
2977/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2978/// there may be no choice but to create a new SetCC node to represent the
2979/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2980/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2981void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2982 SDOperand &RHS,
2983 SDOperand &CC) {
2984 SDOperand Tmp1, Tmp2, Result;
2985
2986 switch (getTypeAction(LHS.getValueType())) {
2987 case Legal:
2988 Tmp1 = LegalizeOp(LHS); // LHS
2989 Tmp2 = LegalizeOp(RHS); // RHS
2990 break;
2991 case Promote:
2992 Tmp1 = PromoteOp(LHS); // LHS
2993 Tmp2 = PromoteOp(RHS); // RHS
2994
2995 // If this is an FP compare, the operands have already been extended.
2996 if (MVT::isInteger(LHS.getValueType())) {
2997 MVT::ValueType VT = LHS.getValueType();
2998 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2999
3000 // Otherwise, we have to insert explicit sign or zero extends. Note
3001 // that we could insert sign extends for ALL conditions, but zero extend
3002 // is cheaper on many machines (an AND instead of two shifts), so prefer
3003 // it.
3004 switch (cast<CondCodeSDNode>(CC)->get()) {
3005 default: assert(0 && "Unknown integer comparison!");
3006 case ISD::SETEQ:
3007 case ISD::SETNE:
3008 case ISD::SETUGE:
3009 case ISD::SETUGT:
3010 case ISD::SETULE:
3011 case ISD::SETULT:
3012 // ALL of these operations will work if we either sign or zero extend
3013 // the operands (including the unsigned comparisons!). Zero extend is
3014 // usually a simpler/cheaper operation, so prefer it.
3015 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3016 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3017 break;
3018 case ISD::SETGE:
3019 case ISD::SETGT:
3020 case ISD::SETLT:
3021 case ISD::SETLE:
3022 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3023 DAG.getValueType(VT));
3024 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3025 DAG.getValueType(VT));
3026 break;
3027 }
3028 }
3029 break;
3030 case Expand:
3031 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3032 ExpandOp(LHS, LHSLo, LHSHi);
3033 ExpandOp(RHS, RHSLo, RHSHi);
3034 switch (cast<CondCodeSDNode>(CC)->get()) {
3035 case ISD::SETEQ:
3036 case ISD::SETNE:
3037 if (RHSLo == RHSHi)
3038 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3039 if (RHSCST->isAllOnesValue()) {
3040 // Comparison to -1.
3041 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3042 Tmp2 = RHSLo;
3043 break;
3044 }
3045
3046 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3047 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3048 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3049 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3050 break;
3051 default:
3052 // If this is a comparison of the sign bit, just look at the top part.
3053 // X > -1, x < 0
3054 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3055 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3056 CST->getValue() == 0) || // X < 0
3057 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3058 CST->isAllOnesValue())) { // X > -1
3059 Tmp1 = LHSHi;
3060 Tmp2 = RHSHi;
3061 break;
3062 }
3063
3064 // FIXME: This generated code sucks.
3065 ISD::CondCode LowCC;
3066 switch (cast<CondCodeSDNode>(CC)->get()) {
3067 default: assert(0 && "Unknown integer setcc!");
3068 case ISD::SETLT:
3069 case ISD::SETULT: LowCC = ISD::SETULT; break;
3070 case ISD::SETGT:
3071 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3072 case ISD::SETLE:
3073 case ISD::SETULE: LowCC = ISD::SETULE; break;
3074 case ISD::SETGE:
3075 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3076 }
3077
3078 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3079 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3080 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3081
3082 // NOTE: on targets without efficient SELECT of bools, we can always use
3083 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3084 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3085 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3086 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3087 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3088 Result, Tmp1, Tmp2));
3089 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003090 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003091 }
3092 }
3093 LHS = Tmp1;
3094 RHS = Tmp2;
3095}
3096
Chris Lattner36e663d2005-12-23 00:16:34 +00003097/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003098/// The resultant code need not be legal. Note that SrcOp is the input operand
3099/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003100SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3101 SDOperand SrcOp) {
3102 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003103 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003104
3105 // Emit a store to the stack slot.
3106 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003107 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003108 // Result is a load from the stack slot.
3109 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3110}
3111
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003112/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3113/// support the operation, but do support the resultant packed vector type.
3114SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3115
3116 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003117 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003118 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003119 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003120 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003121 std::map<SDOperand, std::vector<unsigned> > Values;
3122 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003123 bool isConstant = true;
3124 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3125 SplatValue.getOpcode() != ISD::UNDEF)
3126 isConstant = false;
3127
Evan Cheng1d2e9952006-03-24 01:17:21 +00003128 for (unsigned i = 1; i < NumElems; ++i) {
3129 SDOperand V = Node->getOperand(i);
3130 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3131 if (I != Values.end())
3132 I->second.push_back(i);
3133 else
3134 Values[V].push_back(i);
3135 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003136 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003137 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003138 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003139
3140 // If this isn't a constant element or an undef, we can't use a constant
3141 // pool load.
3142 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3143 V.getOpcode() != ISD::UNDEF)
3144 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003145 }
3146
3147 if (isOnlyLowElement) {
3148 // If the low element is an undef too, then this whole things is an undef.
3149 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3150 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3151 // Otherwise, turn this into a scalar_to_vector node.
3152 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3153 Node->getOperand(0));
3154 }
3155
Chris Lattner77e271c2006-03-24 07:29:17 +00003156 // If all elements are constants, create a load from the constant pool.
3157 if (isConstant) {
3158 MVT::ValueType VT = Node->getValueType(0);
3159 const Type *OpNTy =
3160 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3161 std::vector<Constant*> CV;
3162 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3163 if (ConstantFPSDNode *V =
3164 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3165 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3166 } else if (ConstantSDNode *V =
3167 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3168 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3169 } else {
3170 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3171 CV.push_back(UndefValue::get(OpNTy));
3172 }
3173 }
3174 Constant *CP = ConstantPacked::get(CV);
3175 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3176 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3177 DAG.getSrcValue(NULL));
3178 }
3179
Chris Lattner21e68c82006-03-20 01:52:29 +00003180 if (SplatValue.Val) { // Splat of one value?
3181 // Build the shuffle constant vector: <0, 0, 0, 0>
3182 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003183 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003184 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003185 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner21e68c82006-03-20 01:52:29 +00003186 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3187
3188 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3189 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3190 // Get the splatted value into the low element of a vector register.
3191 SDOperand LowValVec =
3192 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3193
3194 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3195 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3196 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3197 SplatMask);
3198 }
3199 }
3200
Evan Cheng1d2e9952006-03-24 01:17:21 +00003201 // If there are only two unique elements, we may be able to turn this into a
3202 // vector shuffle.
3203 if (Values.size() == 2) {
3204 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3205 MVT::ValueType MaskVT =
3206 MVT::getIntVectorWithNumElements(NumElems);
3207 std::vector<SDOperand> MaskVec(NumElems);
3208 unsigned i = 0;
3209 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3210 E = Values.end(); I != E; ++I) {
3211 for (std::vector<unsigned>::iterator II = I->second.begin(),
3212 EE = I->second.end(); II != EE; ++II)
3213 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3214 i += NumElems;
3215 }
3216 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3217
3218 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Evan Cheng68d9bf22006-03-24 18:45:20 +00003219 if (TLI.isShuffleLegal(Node->getValueType(0), ShuffleMask) &&
3220 TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0))) {
Evan Cheng1d2e9952006-03-24 01:17:21 +00003221 std::vector<SDOperand> Ops;
3222 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3223 E = Values.end(); I != E; ++I) {
3224 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3225 I->first);
3226 Ops.push_back(Op);
3227 }
3228 Ops.push_back(ShuffleMask);
3229
3230 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3231 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3232 }
3233 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003234
3235 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3236 // aligned object on the stack, store each element into it, then load
3237 // the result as a vector.
3238 MVT::ValueType VT = Node->getValueType(0);
3239 // Create the stack frame object.
3240 SDOperand FIPtr = CreateStackTemporary(VT);
3241
3242 // Emit a store of each element to the stack slot.
3243 std::vector<SDOperand> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003244 unsigned TypeByteSize =
3245 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3246 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3247 // Store (in the right endianness) the elements to memory.
3248 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3249 // Ignore undef elements.
3250 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3251
Chris Lattner8fa445a2006-03-22 01:46:54 +00003252 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003253
3254 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3255 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3256
3257 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3258 Node->getOperand(i), Idx,
3259 DAG.getSrcValue(NULL)));
3260 }
3261
3262 SDOperand StoreChain;
3263 if (!Stores.empty()) // Not all undef elements?
3264 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3265 else
3266 StoreChain = DAG.getEntryNode();
3267
3268 // Result is a load from the stack slot.
3269 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3270}
3271
3272/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3273/// specified value type.
3274SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3275 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3276 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3277 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3278 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3279}
3280
Chris Lattner4157c412005-04-02 04:00:59 +00003281void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3282 SDOperand Op, SDOperand Amt,
3283 SDOperand &Lo, SDOperand &Hi) {
3284 // Expand the subcomponents.
3285 SDOperand LHSL, LHSH;
3286 ExpandOp(Op, LHSL, LHSH);
3287
3288 std::vector<SDOperand> Ops;
3289 Ops.push_back(LHSL);
3290 Ops.push_back(LHSH);
3291 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003292 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003293 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003294 Hi = Lo.getValue(1);
3295}
3296
3297
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003298/// ExpandShift - Try to find a clever way to expand this shift operation out to
3299/// smaller elements. If we can't find a way that is more efficient than a
3300/// libcall on this target, return false. Otherwise, return true with the
3301/// low-parts expanded into Lo and Hi.
3302bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3303 SDOperand &Lo, SDOperand &Hi) {
3304 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3305 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003306
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003307 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003308 SDOperand ShAmt = LegalizeOp(Amt);
3309 MVT::ValueType ShTy = ShAmt.getValueType();
3310 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3311 unsigned NVTBits = MVT::getSizeInBits(NVT);
3312
3313 // Handle the case when Amt is an immediate. Other cases are currently broken
3314 // and are disabled.
3315 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3316 unsigned Cst = CN->getValue();
3317 // Expand the incoming operand to be shifted, so that we have its parts
3318 SDOperand InL, InH;
3319 ExpandOp(Op, InL, InH);
3320 switch(Opc) {
3321 case ISD::SHL:
3322 if (Cst > VTBits) {
3323 Lo = DAG.getConstant(0, NVT);
3324 Hi = DAG.getConstant(0, NVT);
3325 } else if (Cst > NVTBits) {
3326 Lo = DAG.getConstant(0, NVT);
3327 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003328 } else if (Cst == NVTBits) {
3329 Lo = DAG.getConstant(0, NVT);
3330 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003331 } else {
3332 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3333 Hi = DAG.getNode(ISD::OR, NVT,
3334 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3335 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3336 }
3337 return true;
3338 case ISD::SRL:
3339 if (Cst > VTBits) {
3340 Lo = DAG.getConstant(0, NVT);
3341 Hi = DAG.getConstant(0, NVT);
3342 } else if (Cst > NVTBits) {
3343 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3344 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003345 } else if (Cst == NVTBits) {
3346 Lo = InH;
3347 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003348 } else {
3349 Lo = DAG.getNode(ISD::OR, NVT,
3350 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3351 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3352 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3353 }
3354 return true;
3355 case ISD::SRA:
3356 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003357 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003358 DAG.getConstant(NVTBits-1, ShTy));
3359 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003360 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003361 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003362 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003363 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003364 } else if (Cst == NVTBits) {
3365 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003366 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003367 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003368 } else {
3369 Lo = DAG.getNode(ISD::OR, NVT,
3370 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3371 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3372 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3373 }
3374 return true;
3375 }
3376 }
Nate Begemanb0674922005-04-06 21:13:14 +00003377 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003378}
Chris Lattneraac464e2005-01-21 06:05:23 +00003379
Chris Lattner4add7e32005-01-23 04:42:50 +00003380
Chris Lattneraac464e2005-01-21 06:05:23 +00003381// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3382// does not fit into a register, return the lo part and set the hi part to the
3383// by-reg argument. If it does fit into a single register, return the result
3384// and leave the Hi part unset.
3385SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3386 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003387 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3388 // The input chain to this libcall is the entry node of the function.
3389 // Legalizing the call will automatically add the previous call to the
3390 // dependence.
3391 SDOperand InChain = DAG.getEntryNode();
3392
Chris Lattneraac464e2005-01-21 06:05:23 +00003393 TargetLowering::ArgListTy Args;
3394 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3395 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3396 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3397 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3398 }
3399 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003400
Chris Lattner06bbeb62005-05-11 19:02:11 +00003401 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003402 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003403 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003404 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3405 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003406
Chris Lattner462505f2006-02-13 09:18:02 +00003407 // Legalize the call sequence, starting with the chain. This will advance
3408 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3409 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3410 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003411 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003412 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003413 default: assert(0 && "Unknown thing");
3414 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003415 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003416 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003417 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003418 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003419 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003420 }
Chris Lattner63022662005-09-02 20:26:58 +00003421 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003422}
3423
Chris Lattner4add7e32005-01-23 04:42:50 +00003424
Chris Lattneraac464e2005-01-21 06:05:23 +00003425/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3426/// destination type is legal.
3427SDOperand SelectionDAGLegalize::
3428ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003429 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003430 assert(getTypeAction(Source.getValueType()) == Expand &&
3431 "This is not an expansion!");
3432 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3433
Chris Lattner06bbeb62005-05-11 19:02:11 +00003434 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003435 assert(Source.getValueType() == MVT::i64 &&
3436 "This only works for 64-bit -> FP");
3437 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3438 // incoming integer is set. To handle this, we dynamically test to see if
3439 // it is set, and, if so, add a fudge factor.
3440 SDOperand Lo, Hi;
3441 ExpandOp(Source, Lo, Hi);
3442
Chris Lattner2a4f7312005-05-13 04:45:13 +00003443 // If this is unsigned, and not supported, first perform the conversion to
3444 // signed, then adjust the result if the sign bit is set.
3445 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3446 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3447
Chris Lattnerd47675e2005-08-09 20:20:18 +00003448 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3449 DAG.getConstant(0, Hi.getValueType()),
3450 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003451 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3452 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3453 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003454 uint64_t FF = 0x5f800000ULL;
3455 if (TLI.isLittleEndian()) FF <<= 32;
3456 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003457
Chris Lattnerc30405e2005-08-26 17:15:30 +00003458 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003459 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3460 SDOperand FudgeInReg;
3461 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003462 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3463 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003464 else {
3465 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003466 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3467 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003468 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003469 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003470 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003471
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003472 // Check to see if the target has a custom way to lower this. If so, use it.
3473 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3474 default: assert(0 && "This action not implemented for this operation!");
3475 case TargetLowering::Legal:
3476 case TargetLowering::Expand:
3477 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003478 case TargetLowering::Custom: {
3479 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3480 Source), DAG);
3481 if (NV.Val)
3482 return LegalizeOp(NV);
3483 break; // The target decided this was legal after all
3484 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003485 }
3486
Chris Lattner153587e2005-05-12 07:00:44 +00003487 // Expand the source, then glue it back together for the call. We must expand
3488 // the source in case it is shared (this pass of legalize must traverse it).
3489 SDOperand SrcLo, SrcHi;
3490 ExpandOp(Source, SrcLo, SrcHi);
3491 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3492
Chris Lattner06bbeb62005-05-11 19:02:11 +00003493 const char *FnName = 0;
3494 if (DestTy == MVT::f32)
3495 FnName = "__floatdisf";
3496 else {
3497 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3498 FnName = "__floatdidf";
3499 }
Chris Lattner462505f2006-02-13 09:18:02 +00003500
3501 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3502 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003503 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003504}
Misha Brukman835702a2005-04-21 22:36:52 +00003505
Chris Lattner689bdcc2006-01-28 08:25:58 +00003506/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3507/// INT_TO_FP operation of the specified operand when the target requests that
3508/// we expand it. At this point, we know that the result and operand types are
3509/// legal for the target.
3510SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3511 SDOperand Op0,
3512 MVT::ValueType DestVT) {
3513 if (Op0.getValueType() == MVT::i32) {
3514 // simple 32-bit [signed|unsigned] integer to float/double expansion
3515
3516 // get the stack frame index of a 8 byte buffer
3517 MachineFunction &MF = DAG.getMachineFunction();
3518 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3519 // get address of 8 byte buffer
3520 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3521 // word offset constant for Hi/Lo address computation
3522 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3523 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00003524 SDOperand Hi = StackSlot;
3525 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3526 if (TLI.isLittleEndian())
3527 std::swap(Hi, Lo);
3528
Chris Lattner689bdcc2006-01-28 08:25:58 +00003529 // if signed map to unsigned space
3530 SDOperand Op0Mapped;
3531 if (isSigned) {
3532 // constant used to invert sign bit (signed to unsigned mapping)
3533 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3534 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3535 } else {
3536 Op0Mapped = Op0;
3537 }
3538 // store the lo of the constructed double - based on integer input
3539 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3540 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3541 // initial hi portion of constructed double
3542 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3543 // store the hi of the constructed double - biased exponent
3544 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3545 InitialHi, Hi, DAG.getSrcValue(NULL));
3546 // load the constructed double
3547 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3548 DAG.getSrcValue(NULL));
3549 // FP constant to bias correct the final result
3550 SDOperand Bias = DAG.getConstantFP(isSigned ?
3551 BitsToDouble(0x4330000080000000ULL)
3552 : BitsToDouble(0x4330000000000000ULL),
3553 MVT::f64);
3554 // subtract the bias
3555 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3556 // final result
3557 SDOperand Result;
3558 // handle final rounding
3559 if (DestVT == MVT::f64) {
3560 // do nothing
3561 Result = Sub;
3562 } else {
3563 // if f32 then cast to f32
3564 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3565 }
3566 return Result;
3567 }
3568 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3569 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3570
3571 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3572 DAG.getConstant(0, Op0.getValueType()),
3573 ISD::SETLT);
3574 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3575 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3576 SignSet, Four, Zero);
3577
3578 // If the sign bit of the integer is set, the large number will be treated
3579 // as a negative number. To counteract this, the dynamic code adds an
3580 // offset depending on the data type.
3581 uint64_t FF;
3582 switch (Op0.getValueType()) {
3583 default: assert(0 && "Unsupported integer type!");
3584 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3585 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3586 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3587 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3588 }
3589 if (TLI.isLittleEndian()) FF <<= 32;
3590 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3591
3592 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3593 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3594 SDOperand FudgeInReg;
3595 if (DestVT == MVT::f32)
3596 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3597 DAG.getSrcValue(NULL));
3598 else {
3599 assert(DestVT == MVT::f64 && "Unexpected conversion");
3600 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3601 DAG.getEntryNode(), CPIdx,
3602 DAG.getSrcValue(NULL), MVT::f32));
3603 }
3604
3605 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3606}
3607
3608/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3609/// *INT_TO_FP operation of the specified operand when the target requests that
3610/// we promote it. At this point, we know that the result and operand types are
3611/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3612/// operation that takes a larger input.
3613SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3614 MVT::ValueType DestVT,
3615 bool isSigned) {
3616 // First step, figure out the appropriate *INT_TO_FP operation to use.
3617 MVT::ValueType NewInTy = LegalOp.getValueType();
3618
3619 unsigned OpToUse = 0;
3620
3621 // Scan for the appropriate larger type to use.
3622 while (1) {
3623 NewInTy = (MVT::ValueType)(NewInTy+1);
3624 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3625
3626 // If the target supports SINT_TO_FP of this type, use it.
3627 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3628 default: break;
3629 case TargetLowering::Legal:
3630 if (!TLI.isTypeLegal(NewInTy))
3631 break; // Can't use this datatype.
3632 // FALL THROUGH.
3633 case TargetLowering::Custom:
3634 OpToUse = ISD::SINT_TO_FP;
3635 break;
3636 }
3637 if (OpToUse) break;
3638 if (isSigned) continue;
3639
3640 // If the target supports UINT_TO_FP of this type, use it.
3641 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3642 default: break;
3643 case TargetLowering::Legal:
3644 if (!TLI.isTypeLegal(NewInTy))
3645 break; // Can't use this datatype.
3646 // FALL THROUGH.
3647 case TargetLowering::Custom:
3648 OpToUse = ISD::UINT_TO_FP;
3649 break;
3650 }
3651 if (OpToUse) break;
3652
3653 // Otherwise, try a larger type.
3654 }
3655
3656 // Okay, we found the operation and type to use. Zero extend our input to the
3657 // desired type then run the operation on it.
3658 return DAG.getNode(OpToUse, DestVT,
3659 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3660 NewInTy, LegalOp));
3661}
3662
3663/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3664/// FP_TO_*INT operation of the specified operand when the target requests that
3665/// we promote it. At this point, we know that the result and operand types are
3666/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3667/// operation that returns a larger result.
3668SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3669 MVT::ValueType DestVT,
3670 bool isSigned) {
3671 // First step, figure out the appropriate FP_TO*INT operation to use.
3672 MVT::ValueType NewOutTy = DestVT;
3673
3674 unsigned OpToUse = 0;
3675
3676 // Scan for the appropriate larger type to use.
3677 while (1) {
3678 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3679 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3680
3681 // If the target supports FP_TO_SINT returning this type, use it.
3682 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3683 default: break;
3684 case TargetLowering::Legal:
3685 if (!TLI.isTypeLegal(NewOutTy))
3686 break; // Can't use this datatype.
3687 // FALL THROUGH.
3688 case TargetLowering::Custom:
3689 OpToUse = ISD::FP_TO_SINT;
3690 break;
3691 }
3692 if (OpToUse) break;
3693
3694 // If the target supports FP_TO_UINT of this type, use it.
3695 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3696 default: break;
3697 case TargetLowering::Legal:
3698 if (!TLI.isTypeLegal(NewOutTy))
3699 break; // Can't use this datatype.
3700 // FALL THROUGH.
3701 case TargetLowering::Custom:
3702 OpToUse = ISD::FP_TO_UINT;
3703 break;
3704 }
3705 if (OpToUse) break;
3706
3707 // Otherwise, try a larger type.
3708 }
3709
3710 // Okay, we found the operation and type to use. Truncate the result of the
3711 // extended FP_TO_*INT operation to the desired size.
3712 return DAG.getNode(ISD::TRUNCATE, DestVT,
3713 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3714}
3715
3716/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3717///
3718SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3719 MVT::ValueType VT = Op.getValueType();
3720 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3721 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3722 switch (VT) {
3723 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3724 case MVT::i16:
3725 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3726 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3727 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3728 case MVT::i32:
3729 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3730 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3731 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3732 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3733 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3734 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3735 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3736 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3737 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3738 case MVT::i64:
3739 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3740 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3741 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3742 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3743 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3744 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3745 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3746 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3747 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3748 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3749 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3750 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3751 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3752 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3753 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3754 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3755 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3756 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3757 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3758 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3759 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3760 }
3761}
3762
3763/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3764///
3765SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3766 switch (Opc) {
3767 default: assert(0 && "Cannot expand this yet!");
3768 case ISD::CTPOP: {
3769 static const uint64_t mask[6] = {
3770 0x5555555555555555ULL, 0x3333333333333333ULL,
3771 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3772 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3773 };
3774 MVT::ValueType VT = Op.getValueType();
3775 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3776 unsigned len = getSizeInBits(VT);
3777 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3778 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3779 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3780 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3781 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3782 DAG.getNode(ISD::AND, VT,
3783 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3784 }
3785 return Op;
3786 }
3787 case ISD::CTLZ: {
3788 // for now, we do this:
3789 // x = x | (x >> 1);
3790 // x = x | (x >> 2);
3791 // ...
3792 // x = x | (x >>16);
3793 // x = x | (x >>32); // for 64-bit input
3794 // return popcount(~x);
3795 //
3796 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3797 MVT::ValueType VT = Op.getValueType();
3798 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3799 unsigned len = getSizeInBits(VT);
3800 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3801 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3802 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3803 }
3804 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3805 return DAG.getNode(ISD::CTPOP, VT, Op);
3806 }
3807 case ISD::CTTZ: {
3808 // for now, we use: { return popcount(~x & (x - 1)); }
3809 // unless the target has ctlz but not ctpop, in which case we use:
3810 // { return 32 - nlz(~x & (x-1)); }
3811 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3812 MVT::ValueType VT = Op.getValueType();
3813 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3814 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3815 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3816 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3817 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3818 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3819 TLI.isOperationLegal(ISD::CTLZ, VT))
3820 return DAG.getNode(ISD::SUB, VT,
3821 DAG.getConstant(getSizeInBits(VT), VT),
3822 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3823 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3824 }
3825 }
3826}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003827
3828
Chris Lattnerdc750592005-01-07 07:47:09 +00003829/// ExpandOp - Expand the specified SDOperand into its two component pieces
3830/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3831/// LegalizeNodes map is filled in for any results that are not expanded, the
3832/// ExpandedNodes map is filled in for any results that are expanded, and the
3833/// Lo/Hi values are returned.
3834void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3835 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003836 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003837 SDNode *Node = Op.Val;
3838 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003839 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3840 "Cannot expand FP values!");
3841 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003842 "Cannot expand to FP value or to larger int value!");
3843
Chris Lattner1a570f12005-09-02 20:32:45 +00003844 // See if we already expanded it.
3845 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3846 = ExpandedNodes.find(Op);
3847 if (I != ExpandedNodes.end()) {
3848 Lo = I->second.first;
3849 Hi = I->second.second;
3850 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003851 }
3852
Chris Lattnerdc750592005-01-07 07:47:09 +00003853 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003854 case ISD::CopyFromReg:
3855 assert(0 && "CopyFromReg must be legal!");
3856 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003857 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3858 assert(0 && "Do not know how to expand this operator!");
3859 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003860 case ISD::UNDEF:
3861 Lo = DAG.getNode(ISD::UNDEF, NVT);
3862 Hi = DAG.getNode(ISD::UNDEF, NVT);
3863 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003864 case ISD::Constant: {
3865 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3866 Lo = DAG.getConstant(Cst, NVT);
3867 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3868 break;
3869 }
Chris Lattner32e08b72005-03-28 22:03:13 +00003870 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003871 // Return the operands.
3872 Lo = Node->getOperand(0);
3873 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003874 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003875
3876 case ISD::SIGN_EXTEND_INREG:
3877 ExpandOp(Node->getOperand(0), Lo, Hi);
3878 // Sign extend the lo-part.
3879 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3880 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3881 TLI.getShiftAmountTy()));
3882 // sext_inreg the low part if needed.
3883 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3884 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003885
Nate Begeman2fba8a32006-01-14 03:14:10 +00003886 case ISD::BSWAP: {
3887 ExpandOp(Node->getOperand(0), Lo, Hi);
3888 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3889 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3890 Lo = TempLo;
3891 break;
3892 }
3893
Chris Lattner55e9cde2005-05-11 04:51:16 +00003894 case ISD::CTPOP:
3895 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003896 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3897 DAG.getNode(ISD::CTPOP, NVT, Lo),
3898 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003899 Hi = DAG.getConstant(0, NVT);
3900 break;
3901
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003902 case ISD::CTLZ: {
3903 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003904 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003905 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3906 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003907 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3908 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003909 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3910 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3911
3912 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3913 Hi = DAG.getConstant(0, NVT);
3914 break;
3915 }
3916
3917 case ISD::CTTZ: {
3918 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003919 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003920 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3921 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003922 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3923 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003924 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3925 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3926
3927 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3928 Hi = DAG.getConstant(0, NVT);
3929 break;
3930 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003931
Nate Begemane74795c2006-01-25 18:21:52 +00003932 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003933 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3934 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003935 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3936 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3937
3938 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003939 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003940 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3941 if (!TLI.isLittleEndian())
3942 std::swap(Lo, Hi);
3943 break;
3944 }
3945
Chris Lattnerdc750592005-01-07 07:47:09 +00003946 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003947 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3948 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003949 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003950
3951 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003952 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003953 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3954 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003955 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003956 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003957
3958 // Build a factor node to remember that this load is independent of the
3959 // other one.
3960 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3961 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003962
Chris Lattnerdc750592005-01-07 07:47:09 +00003963 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003964 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003965 if (!TLI.isLittleEndian())
3966 std::swap(Lo, Hi);
3967 break;
3968 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003969 case ISD::AND:
3970 case ISD::OR:
3971 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3972 SDOperand LL, LH, RL, RH;
3973 ExpandOp(Node->getOperand(0), LL, LH);
3974 ExpandOp(Node->getOperand(1), RL, RH);
3975 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3976 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3977 break;
3978 }
3979 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003980 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003981 ExpandOp(Node->getOperand(1), LL, LH);
3982 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003983 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3984 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003985 break;
3986 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003987 case ISD::SELECT_CC: {
3988 SDOperand TL, TH, FL, FH;
3989 ExpandOp(Node->getOperand(2), TL, TH);
3990 ExpandOp(Node->getOperand(3), FL, FH);
3991 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3992 Node->getOperand(1), TL, FL, Node->getOperand(4));
3993 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3994 Node->getOperand(1), TH, FH, Node->getOperand(4));
3995 break;
3996 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003997 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003998 SDOperand Chain = Node->getOperand(0);
3999 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004000 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4001
4002 if (EVT == NVT)
4003 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4004 else
4005 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4006 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004007
4008 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004009 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004010
Nate Begemanc3a89c52005-10-13 17:15:37 +00004011 // The high part is obtained by SRA'ing all but one of the bits of the lo
4012 // part.
4013 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4014 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4015 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00004016 break;
4017 }
4018 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004019 SDOperand Chain = Node->getOperand(0);
4020 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004021 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4022
4023 if (EVT == NVT)
4024 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4025 else
4026 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4027 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004028
4029 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004030 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004031
Nate Begemanc3a89c52005-10-13 17:15:37 +00004032 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004033 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004034 break;
4035 }
4036 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004037 SDOperand Chain = Node->getOperand(0);
4038 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00004039 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4040
4041 if (EVT == NVT)
4042 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4043 else
4044 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4045 EVT);
4046
4047 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004048 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004049
4050 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00004051 Hi = DAG.getNode(ISD::UNDEF, NVT);
4052 break;
4053 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004054 case ISD::ANY_EXTEND:
4055 // The low part is any extension of the input (which degenerates to a copy).
4056 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4057 // The high part is undefined.
4058 Hi = DAG.getNode(ISD::UNDEF, NVT);
4059 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004060 case ISD::SIGN_EXTEND: {
4061 // The low part is just a sign extension of the input (which degenerates to
4062 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004063 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004064
Chris Lattnerdc750592005-01-07 07:47:09 +00004065 // The high part is obtained by SRA'ing all but one of the bits of the lo
4066 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004067 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004068 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4069 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004070 break;
4071 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004072 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004073 // The low part is just a zero extension of the input (which degenerates to
4074 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004075 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004076
Chris Lattnerdc750592005-01-07 07:47:09 +00004077 // The high part is just a zero.
4078 Hi = DAG.getConstant(0, NVT);
4079 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004080
4081 case ISD::BIT_CONVERT: {
4082 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4083 Node->getOperand(0));
4084 ExpandOp(Tmp, Lo, Hi);
4085 break;
4086 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004087
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004088 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004089 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4090 TargetLowering::Custom &&
4091 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004092 Lo = TLI.LowerOperation(Op, DAG);
4093 assert(Lo.Val && "Node must be custom expanded!");
4094 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004095 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004096 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004097 break;
4098
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004099 // These operators cannot be expanded directly, emit them as calls to
4100 // library functions.
4101 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004102 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004103 SDOperand Op;
4104 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4105 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004106 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4107 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004108 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004109
Chris Lattner941d84a2005-07-30 01:40:57 +00004110 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4111
Chris Lattnerfe68d752005-07-29 00:33:32 +00004112 // Now that the custom expander is done, expand the result, which is still
4113 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004114 if (Op.Val) {
4115 ExpandOp(Op, Lo, Hi);
4116 break;
4117 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004118 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004119
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004120 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004121 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004122 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004123 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004124 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004125
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004126 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004127 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004128 SDOperand Op;
4129 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4130 case Expand: assert(0 && "cannot expand FP!");
4131 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4132 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4133 }
4134
4135 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4136
4137 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004138 if (Op.Val) {
4139 ExpandOp(Op, Lo, Hi);
4140 break;
4141 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004142 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004143
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004144 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004145 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004146 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004147 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004148 break;
4149
Evan Cheng870e4f82006-01-09 18:31:59 +00004150 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004151 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004152 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004153 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004154 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004155 Op = TLI.LowerOperation(Op, DAG);
4156 if (Op.Val) {
4157 // Now that the custom expander is done, expand the result, which is
4158 // still VT.
4159 ExpandOp(Op, Lo, Hi);
4160 break;
4161 }
4162 }
4163
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004164 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004165 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004166 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004167
4168 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004169 TargetLowering::LegalizeAction Action =
4170 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4171 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4172 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004173 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004174 break;
4175 }
4176
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004177 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004178 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004179 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004180 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004181
Evan Cheng870e4f82006-01-09 18:31:59 +00004182 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004183 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004184 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004185 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004186 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004187 Op = TLI.LowerOperation(Op, DAG);
4188 if (Op.Val) {
4189 // Now that the custom expander is done, expand the result, which is
4190 // still VT.
4191 ExpandOp(Op, Lo, Hi);
4192 break;
4193 }
4194 }
4195
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004196 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004197 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004198 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004199
4200 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004201 TargetLowering::LegalizeAction Action =
4202 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4203 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4204 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004205 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004206 break;
4207 }
4208
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004209 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004210 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004211 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004212 }
4213
4214 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004215 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004216 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004217 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004218 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004219 Op = TLI.LowerOperation(Op, DAG);
4220 if (Op.Val) {
4221 // Now that the custom expander is done, expand the result, which is
4222 // still VT.
4223 ExpandOp(Op, Lo, Hi);
4224 break;
4225 }
4226 }
4227
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004228 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004229 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004230 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004231
4232 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004233 TargetLowering::LegalizeAction Action =
4234 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4235 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4236 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004237 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004238 break;
4239 }
4240
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004241 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004242 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004243 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004244 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004245
Misha Brukman835702a2005-04-21 22:36:52 +00004246 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004247 case ISD::SUB: {
4248 // If the target wants to custom expand this, let them.
4249 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4250 TargetLowering::Custom) {
4251 Op = TLI.LowerOperation(Op, DAG);
4252 if (Op.Val) {
4253 ExpandOp(Op, Lo, Hi);
4254 break;
4255 }
4256 }
4257
4258 // Expand the subcomponents.
4259 SDOperand LHSL, LHSH, RHSL, RHSH;
4260 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4261 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004262 std::vector<MVT::ValueType> VTs;
4263 std::vector<SDOperand> LoOps, HiOps;
4264 VTs.push_back(LHSL.getValueType());
4265 VTs.push_back(MVT::Flag);
4266 LoOps.push_back(LHSL);
4267 LoOps.push_back(RHSL);
4268 HiOps.push_back(LHSH);
4269 HiOps.push_back(RHSH);
4270 if (Node->getOpcode() == ISD::ADD) {
4271 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4272 HiOps.push_back(Lo.getValue(1));
4273 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4274 } else {
4275 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4276 HiOps.push_back(Lo.getValue(1));
4277 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4278 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004279 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004280 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004281 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004282 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004283 SDOperand LL, LH, RL, RH;
4284 ExpandOp(Node->getOperand(0), LL, LH);
4285 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004286 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4287 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4288 // extended the sign bit of the low half through the upper half, and if so
4289 // emit a MULHS instead of the alternate sequence that is valid for any
4290 // i64 x i64 multiply.
4291 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4292 // is RH an extension of the sign bit of RL?
4293 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4294 RH.getOperand(1).getOpcode() == ISD::Constant &&
4295 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4296 // is LH an extension of the sign bit of LL?
4297 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4298 LH.getOperand(1).getOpcode() == ISD::Constant &&
4299 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4300 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4301 } else {
4302 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4303 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4304 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4305 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4306 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4307 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004308 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4309 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004310 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004311 }
4312 break;
4313 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004314 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4315 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4316 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4317 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004318 }
4319
Chris Lattnerac12f682005-12-21 18:02:52 +00004320 // Make sure the resultant values have been legalized themselves, unless this
4321 // is a type that requires multi-step expansion.
4322 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4323 Lo = LegalizeOp(Lo);
4324 Hi = LegalizeOp(Hi);
4325 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004326
4327 // Remember in a map if the values will be reused later.
4328 bool isNew =
4329 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4330 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004331}
4332
Chris Lattner32206f52006-03-18 01:44:44 +00004333/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4334/// two smaller values of MVT::Vector type.
4335void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4336 SDOperand &Hi) {
4337 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4338 SDNode *Node = Op.Val;
4339 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4340 assert(NumElements > 1 && "Cannot split a single element vector!");
4341 unsigned NewNumElts = NumElements/2;
4342 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4343 SDOperand TypeNode = *(Node->op_end()-1);
4344
4345 // See if we already split it.
4346 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4347 = SplitNodes.find(Op);
4348 if (I != SplitNodes.end()) {
4349 Lo = I->second.first;
4350 Hi = I->second.second;
4351 return;
4352 }
4353
4354 switch (Node->getOpcode()) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004355 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004356 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004357 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4358 LoOps.push_back(NewNumEltsNode);
4359 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004360 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004361
4362 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4363 HiOps.push_back(NewNumEltsNode);
4364 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004365 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004366 break;
4367 }
4368 case ISD::VADD:
4369 case ISD::VSUB:
4370 case ISD::VMUL:
4371 case ISD::VSDIV:
4372 case ISD::VUDIV:
4373 case ISD::VAND:
4374 case ISD::VOR:
4375 case ISD::VXOR: {
4376 SDOperand LL, LH, RL, RH;
4377 SplitVectorOp(Node->getOperand(0), LL, LH);
4378 SplitVectorOp(Node->getOperand(1), RL, RH);
4379
4380 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4381 NewNumEltsNode, TypeNode);
4382 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4383 NewNumEltsNode, TypeNode);
4384 break;
4385 }
4386 case ISD::VLOAD: {
4387 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4388 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4389 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4390
4391 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4392 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4393 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4394 getIntPtrConstant(IncrementSize));
4395 // FIXME: This creates a bogus srcvalue!
4396 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4397
4398 // Build a factor node to remember that this load is independent of the
4399 // other one.
4400 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4401 Hi.getValue(1));
4402
4403 // Remember that we legalized the chain.
4404 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4405 if (!TLI.isLittleEndian())
4406 std::swap(Lo, Hi);
4407 break;
4408 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004409 case ISD::VBIT_CONVERT: {
4410 // We know the result is a vector. The input may be either a vector or a
4411 // scalar value.
4412 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4413 // Lower to a store/load. FIXME: this could be improved probably.
4414 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4415
4416 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4417 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4418 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4419 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4420 SplitVectorOp(St, Lo, Hi);
4421 } else {
4422 // If the input is a vector type, we have to either scalarize it, pack it
4423 // or convert it based on whether the input vector type is legal.
4424 SDNode *InVal = Node->getOperand(0).Val;
4425 unsigned NumElems =
4426 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4427 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4428
4429 // If the input is from a single element vector, scalarize the vector,
4430 // then treat like a scalar.
4431 if (NumElems == 1) {
4432 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4433 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4434 Op.getOperand(1), Op.getOperand(2));
4435 SplitVectorOp(Scalar, Lo, Hi);
4436 } else {
4437 // Split the input vector.
4438 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4439
4440 // Convert each of the pieces now.
4441 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4442 NewNumEltsNode, TypeNode);
4443 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4444 NewNumEltsNode, TypeNode);
4445 }
4446 break;
4447 }
4448 }
Chris Lattner32206f52006-03-18 01:44:44 +00004449 }
4450
4451 // Remember in a map if the values will be reused later.
4452 bool isNew =
4453 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4454 assert(isNew && "Value already expanded?!?");
4455}
4456
4457
4458/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4459/// equivalent operation that returns a scalar (e.g. F32) or packed value
4460/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4461/// type for the result.
4462SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4463 MVT::ValueType NewVT) {
Chris Lattner672a42d2006-03-21 19:20:37 +00004464 // FIXME: THIS IS A TEMPORARY HACK
4465 if (Op.getValueType() == NewVT) return Op;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00004466
Chris Lattner32206f52006-03-18 01:44:44 +00004467 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4468 SDNode *Node = Op.Val;
4469
4470 // See if we already packed it.
4471 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4472 if (I != PackedNodes.end()) return I->second;
4473
4474 SDOperand Result;
4475 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004476 default:
4477 Node->dump(); std::cerr << "\n";
4478 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004479 case ISD::VADD:
4480 case ISD::VSUB:
4481 case ISD::VMUL:
4482 case ISD::VSDIV:
4483 case ISD::VUDIV:
4484 case ISD::VAND:
4485 case ISD::VOR:
4486 case ISD::VXOR:
4487 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4488 NewVT,
4489 PackVectorOp(Node->getOperand(0), NewVT),
4490 PackVectorOp(Node->getOperand(1), NewVT));
4491 break;
4492 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004493 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4494 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004495
Chris Lattner93640542006-03-19 00:07:49 +00004496 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004497
4498 // Remember that we legalized the chain.
4499 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4500 break;
4501 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004502 case ISD::VBUILD_VECTOR:
Chris Lattner32206f52006-03-18 01:44:44 +00004503 if (!MVT::isVector(NewVT)) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004504 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004505 Result = Node->getOperand(0);
4506 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004507 // Returning a BUILD_VECTOR?
Chris Lattner32206f52006-03-18 01:44:44 +00004508 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004509 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattner32206f52006-03-18 01:44:44 +00004510 }
4511 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004512 case ISD::VINSERT_VECTOR_ELT:
4513 if (!MVT::isVector(NewVT)) {
4514 // Returning a scalar? Must be the inserted element.
4515 Result = Node->getOperand(1);
4516 } else {
4517 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4518 PackVectorOp(Node->getOperand(0), NewVT),
4519 Node->getOperand(1), Node->getOperand(2));
4520 }
4521 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00004522 case ISD::VBIT_CONVERT:
4523 if (Op.getOperand(0).getValueType() != MVT::Vector)
4524 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4525 else {
4526 // If the input is a vector type, we have to either scalarize it, pack it
4527 // or convert it based on whether the input vector type is legal.
4528 SDNode *InVal = Node->getOperand(0).Val;
4529 unsigned NumElems =
4530 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4531 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4532
4533 // Figure out if there is a Packed type corresponding to this Vector
4534 // type. If so, convert to the packed type.
4535 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4536 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4537 // Turn this into a bit convert of the packed input.
4538 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4539 PackVectorOp(Node->getOperand(0), TVT));
4540 break;
4541 } else if (NumElems == 1) {
4542 // Turn this into a bit convert of the scalar input.
4543 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4544 PackVectorOp(Node->getOperand(0), EVT));
4545 break;
4546 } else {
4547 // FIXME: UNIMP!
4548 assert(0 && "Cast from unsupported vector type not implemented yet!");
4549 }
4550 }
Chris Lattner32206f52006-03-18 01:44:44 +00004551 }
4552
4553 if (TLI.isTypeLegal(NewVT))
4554 Result = LegalizeOp(Result);
4555 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4556 assert(isNew && "Value already packed?");
4557 return Result;
4558}
4559
Chris Lattnerdc750592005-01-07 07:47:09 +00004560
4561// SelectionDAG::Legalize - This is the entry point for the file.
4562//
Chris Lattner4add7e32005-01-23 04:42:50 +00004563void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004564 /// run - This is the main entry point to this class.
4565 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004566 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004567}
4568