blob: 6acadc1349d83a5a85c1c0c2fc5316ffd53cd111 [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);
561 break;
562 }
Chris Lattner435b4022005-11-29 06:21:05 +0000563
564 case ISD::LOCATION:
565 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
566 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
567
568 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
569 case TargetLowering::Promote:
570 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000571 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000572 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000573 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
574 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
575
576 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
577 const std::string &FName =
578 cast<StringSDNode>(Node->getOperand(3))->getValue();
579 const std::string &DirName =
580 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000581 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000582
Jim Laskey9e296be2005-12-21 20:51:37 +0000583 std::vector<SDOperand> Ops;
584 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000585 SDOperand LineOp = Node->getOperand(1);
586 SDOperand ColOp = Node->getOperand(2);
587
588 if (useDEBUG_LOC) {
589 Ops.push_back(LineOp); // line #
590 Ops.push_back(ColOp); // col #
591 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
592 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
593 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000594 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
595 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000596 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
597 Ops.push_back(DAG.getConstant(ID, MVT::i32));
598 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
599 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000600 } else {
601 Result = Tmp1; // chain
602 }
Chris Lattner435b4022005-11-29 06:21:05 +0000603 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000604 }
Chris Lattner435b4022005-11-29 06:21:05 +0000605 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000606 if (Tmp1 != Node->getOperand(0) ||
607 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000608 std::vector<SDOperand> Ops;
609 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000610 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
611 Ops.push_back(Node->getOperand(1)); // line # must be legal.
612 Ops.push_back(Node->getOperand(2)); // col # must be legal.
613 } else {
614 // Otherwise promote them.
615 Ops.push_back(PromoteOp(Node->getOperand(1)));
616 Ops.push_back(PromoteOp(Node->getOperand(2)));
617 }
Chris Lattner435b4022005-11-29 06:21:05 +0000618 Ops.push_back(Node->getOperand(3)); // filename must be legal.
619 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000620 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000621 }
622 break;
623 }
624 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000625
626 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000627 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000628 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000629 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000630 case TargetLowering::Legal:
631 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
632 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
633 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
634 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000636 break;
637 }
638 break;
639
640 case ISD::DEBUG_LABEL:
641 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
642 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000643 default: assert(0 && "This action is not supported yet!");
644 case TargetLowering::Legal:
645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000648 break;
649 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000650 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000651
Chris Lattnerdc750592005-01-07 07:47:09 +0000652 case ISD::Constant:
653 // We know we don't need to expand constants here, constants only have one
654 // value and we check that it is fine above.
655
656 // FIXME: Maybe we should handle things like targets that don't support full
657 // 32-bit immediates?
658 break;
659 case ISD::ConstantFP: {
660 // Spill FP immediates to the constant pool if the target cannot directly
661 // codegen them. Targets often have some immediate values that can be
662 // efficiently generated into an FP register without a load. We explicitly
663 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000664 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
665
666 // Check to see if this FP immediate is already legal.
667 bool isLegal = false;
668 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
669 E = TLI.legal_fpimm_end(); I != E; ++I)
670 if (CFP->isExactlyValue(*I)) {
671 isLegal = true;
672 break;
673 }
674
Chris Lattner758b0ac2006-01-29 06:26:56 +0000675 // If this is a legal constant, turn it into a TargetConstantFP node.
676 if (isLegal) {
677 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
678 break;
679 }
680
681 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
682 default: assert(0 && "This action is not supported yet!");
683 case TargetLowering::Custom:
684 Tmp3 = TLI.LowerOperation(Result, DAG);
685 if (Tmp3.Val) {
686 Result = Tmp3;
687 break;
688 }
689 // FALLTHROUGH
690 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000691 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000692 bool Extend = false;
693
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000694 // If a FP immediate is precise when represented as a float and if the
695 // target can do an extending load from float to double, we put it into
696 // the constant pool as a float, even if it's is statically typed as a
697 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000698 MVT::ValueType VT = CFP->getValueType(0);
699 bool isDouble = VT == MVT::f64;
700 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
701 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000702 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
703 // Only do this if the target has a native EXTLOAD instruction from
704 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000705 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000706 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
707 VT = MVT::f32;
708 Extend = true;
709 }
Misha Brukman835702a2005-04-21 22:36:52 +0000710
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000711 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000712 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000713 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
714 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000715 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000716 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
717 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000718 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000719 }
720 break;
721 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000722 case ISD::TokenFactor:
723 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000724 Tmp1 = LegalizeOp(Node->getOperand(0));
725 Tmp2 = LegalizeOp(Node->getOperand(1));
726 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
727 } else if (Node->getNumOperands() == 3) {
728 Tmp1 = LegalizeOp(Node->getOperand(0));
729 Tmp2 = LegalizeOp(Node->getOperand(1));
730 Tmp3 = LegalizeOp(Node->getOperand(2));
731 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000732 } else {
733 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000734 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000735 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
736 Ops.push_back(LegalizeOp(Node->getOperand(i)));
737 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000738 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000739 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000740
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000741 case ISD::BUILD_VECTOR:
742 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000743 default: assert(0 && "This action is not supported yet!");
744 case TargetLowering::Custom:
745 Tmp3 = TLI.LowerOperation(Result, DAG);
746 if (Tmp3.Val) {
747 Result = Tmp3;
748 break;
749 }
750 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000751 case TargetLowering::Expand:
752 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000753 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000754 }
Chris Lattner29b23012006-03-19 01:17:20 +0000755 break;
756 case ISD::INSERT_VECTOR_ELT:
757 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
758 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
759 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
760 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
761
762 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
763 Node->getValueType(0))) {
764 default: assert(0 && "This action is not supported yet!");
765 case TargetLowering::Legal:
766 break;
767 case TargetLowering::Custom:
768 Tmp3 = TLI.LowerOperation(Result, DAG);
769 if (Tmp3.Val) {
770 Result = Tmp3;
771 break;
772 }
773 // FALLTHROUGH
774 case TargetLowering::Expand: {
775 // If the target doesn't support this, we have to spill the input vector
776 // to a temporary stack slot, update the element, then reload it. This is
777 // badness. We could also load the value into a vector register (either
778 // with a "move to register" or "extload into register" instruction, then
779 // permute it into place, if the idx is a constant and if the idx is
780 // supported by the target.
781 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
782 break;
783 }
784 }
785 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000786 case ISD::SCALAR_TO_VECTOR:
787 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
788 Result = DAG.UpdateNodeOperands(Result, Tmp1);
789 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
790 Node->getValueType(0))) {
791 default: assert(0 && "This action is not supported yet!");
792 case TargetLowering::Legal:
793 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000794 case TargetLowering::Custom:
795 Tmp3 = TLI.LowerOperation(Result, DAG);
796 if (Tmp3.Val) {
797 Result = Tmp3;
798 break;
799 }
800 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000801 case TargetLowering::Expand: {
802 // If the target doesn't support this, store the value to a temporary
803 // stack slot, then EXTLOAD the vector back out.
Chris Lattner79fb91c2006-03-19 06:47:21 +0000804 // TODO: If a target doesn't support this, create a stack slot for the
805 // whole vector, then store into it, then load the whole vector.
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000806 SDOperand StackPtr =
807 CreateStackTemporary(Node->getOperand(0).getValueType());
808 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
809 Node->getOperand(0), StackPtr,
810 DAG.getSrcValue(NULL));
811 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
812 DAG.getSrcValue(NULL),
813 Node->getOperand(0).getValueType());
814 break;
815 }
816 }
817 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000818 case ISD::VECTOR_SHUFFLE:
819 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
820 "vector shuffle should not be created if not legal!");
821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
822 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
823 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
824
825 // Allow targets to custom lower the SHUFFLEs they support.
826 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
827 == TargetLowering::Custom) {
828 Tmp1 = TLI.LowerOperation(Result, DAG);
829 if (Tmp1.Val) Result = Tmp1;
830 }
831 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000832
833 case ISD::EXTRACT_VECTOR_ELT:
834 Tmp1 = LegalizeOp(Node->getOperand(0));
835 Tmp2 = LegalizeOp(Node->getOperand(1));
836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000837
838 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
839 Tmp1.getValueType())) {
840 default: assert(0 && "This action is not supported yet!");
841 case TargetLowering::Legal:
842 break;
843 case TargetLowering::Custom:
844 Tmp3 = TLI.LowerOperation(Result, DAG);
845 if (Tmp3.Val) {
846 Result = Tmp3;
847 break;
848 }
849 // FALLTHROUGH
850 case TargetLowering::Expand: {
851 // If the target doesn't support this, store the value to a temporary
852 // stack slot, then LOAD the scalar element back out.
853 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
854 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
855 Tmp1, StackPtr, DAG.getSrcValue(NULL));
856
857 // Add the offset to the index.
858 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
859 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
860 DAG.getConstant(EltSize, Tmp2.getValueType()));
861 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
862
863 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
864 DAG.getSrcValue(NULL));
865 break;
866 }
867 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000868 break;
869
Chris Lattner5be43522006-03-22 00:12:37 +0000870 case ISD::VEXTRACT_VECTOR_ELT: {
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000871 // We know that operand #0 is the Vec vector. If the index is a constant
872 // or if the invec is a supported hardware type, we can use it. Otherwise,
873 // lower to a store then an indexed load.
874 Tmp1 = Node->getOperand(0);
875 Tmp2 = LegalizeOp(Node->getOperand(1));
876
877 SDNode *InVal = Tmp1.Val;
878 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
879 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
880
881 // Figure out if there is a Packed type corresponding to this Vector
882 // type. If so, convert to the packed type.
883 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
884 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
885 // Turn this into a packed extract_vector_elt operation.
886 Tmp1 = PackVectorOp(Tmp1, TVT);
887 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Node->getValueType(0),
888 Tmp1, Tmp2);
889 break;
890 } else if (NumElems == 1) {
891 // This must be an access of the only element.
892 Result = PackVectorOp(Tmp1, EVT);
893 break;
894 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Tmp2)) {
895 SDOperand Lo, Hi;
896 SplitVectorOp(Tmp1, Lo, Hi);
897 if (CIdx->getValue() < NumElems/2) {
898 Tmp1 = Lo;
899 } else {
900 Tmp1 = Hi;
901 Tmp2 = DAG.getConstant(CIdx->getValue() - NumElems/2,
902 Tmp2.getValueType());
903 }
904
905 // It's now an extract from the appropriate high or low part.
906 Result = LegalizeOp(DAG.UpdateNodeOperands(Result, Tmp1, Tmp2));
907 } else {
Chris Lattner5be43522006-03-22 00:12:37 +0000908 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000909 assert(0 && "unimp!");
910 }
911 break;
Chris Lattner5be43522006-03-22 00:12:37 +0000912 }
Chris Lattner21e68c82006-03-20 01:52:29 +0000913
Chris Lattner462505f2006-02-13 09:18:02 +0000914 case ISD::CALLSEQ_START: {
915 SDNode *CallEnd = FindCallEndFromCallStart(Node);
916
917 // Recursively Legalize all of the inputs of the call end that do not lead
918 // to this call start. This ensures that any libcalls that need be inserted
919 // are inserted *before* the CALLSEQ_START.
920 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
921 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
922
923 // Now that we legalized all of the inputs (which may have inserted
924 // libcalls) create the new CALLSEQ_START node.
925 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
926
927 // Merge in the last call, to ensure that this call start after the last
928 // call ended.
929 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
930 Tmp1 = LegalizeOp(Tmp1);
931
932 // Do not try to legalize the target-specific arguments (#1+).
933 if (Tmp1 != Node->getOperand(0)) {
934 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
935 Ops[0] = Tmp1;
936 Result = DAG.UpdateNodeOperands(Result, Ops);
937 }
938
939 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000940 AddLegalizedOperand(Op.getValue(0), Result);
941 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
942 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
943
Chris Lattner462505f2006-02-13 09:18:02 +0000944 // Now that the callseq_start and all of the non-call nodes above this call
945 // sequence have been legalized, legalize the call itself. During this
946 // process, no libcalls can/will be inserted, guaranteeing that no calls
947 // can overlap.
948 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
949 SDOperand InCallSEQ = LastCALLSEQ_END;
950 // Note that we are selecting this call!
951 LastCALLSEQ_END = SDOperand(CallEnd, 0);
952 IsLegalizingCall = true;
953
954 // Legalize the call, starting from the CALLSEQ_END.
955 LegalizeOp(LastCALLSEQ_END);
956 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
957 return Result;
958 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000959 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +0000960 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
961 // will cause this node to be legalized as well as handling libcalls right.
962 if (LastCALLSEQ_END.Val != Node) {
963 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
964 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
965 assert(I != LegalizedNodes.end() &&
966 "Legalizing the call start should have legalized this node!");
967 return I->second;
968 }
969
970 // Otherwise, the call start has been legalized and everything is going
971 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +0000972 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000973 // Do not try to legalize the target-specific arguments (#1+), except for
974 // an optional flag input.
975 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
976 if (Tmp1 != Node->getOperand(0)) {
977 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
978 Ops[0] = Tmp1;
979 Result = DAG.UpdateNodeOperands(Result, Ops);
980 }
981 } else {
982 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
983 if (Tmp1 != Node->getOperand(0) ||
984 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
985 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
986 Ops[0] = Tmp1;
987 Ops.back() = Tmp2;
988 Result = DAG.UpdateNodeOperands(Result, Ops);
989 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +0000990 }
Chris Lattner8e2ee732006-02-14 00:55:02 +0000991 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +0000992 // This finishes up call legalization.
993 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +0000994
995 // If the CALLSEQ_END node has a flag, remember that we legalized it.
996 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
997 if (Node->getNumValues() == 2)
998 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
999 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001000 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1002 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1003 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001005
Chris Lattnerd02b0542006-01-28 10:58:55 +00001006 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001007 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001008 switch (TLI.getOperationAction(Node->getOpcode(),
1009 Node->getValueType(0))) {
1010 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001011 case TargetLowering::Expand: {
1012 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1013 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1014 " not tell us which reg is the stack pointer!");
1015 SDOperand Chain = Tmp1.getOperand(0);
1016 SDOperand Size = Tmp2.getOperand(1);
1017 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1018 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1019 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001020 Tmp1 = LegalizeOp(Tmp1);
1021 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001022 break;
1023 }
1024 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001025 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1026 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001027 Tmp1 = LegalizeOp(Tmp3);
1028 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001029 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001030 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001031 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001032 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001033 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001034 // Since this op produce two values, make sure to remember that we
1035 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001036 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1037 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001038 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001039 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001040 case ISD::INLINEASM:
1041 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1042 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001043 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001044 Tmp2 = Tmp3 = SDOperand(0, 0);
1045 else
1046 Tmp3 = LegalizeOp(Tmp2);
1047
1048 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1049 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1050 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001051 if (Tmp3.Val) Ops.back() = Tmp3;
1052 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001053 }
1054
1055 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001056 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001057 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1058 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001059 case ISD::BR:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001061 // Ensure that libcalls are emitted before a branch.
1062 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1063 Tmp1 = LegalizeOp(Tmp1);
1064 LastCALLSEQ_END = DAG.getEntryNode();
1065
Chris Lattnerd02b0542006-01-28 10:58:55 +00001066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001067 break;
1068
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001069 case ISD::BRCOND:
1070 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001071 // Ensure that libcalls are emitted before a return.
1072 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1073 Tmp1 = LegalizeOp(Tmp1);
1074 LastCALLSEQ_END = DAG.getEntryNode();
1075
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001076 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1077 case Expand: assert(0 && "It's impossible to expand bools");
1078 case Legal:
1079 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1080 break;
1081 case Promote:
1082 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1083 break;
1084 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001085
1086 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001088
1089 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1090 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001091 case TargetLowering::Legal: break;
1092 case TargetLowering::Custom:
1093 Tmp1 = TLI.LowerOperation(Result, DAG);
1094 if (Tmp1.Val) Result = Tmp1;
1095 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001096 case TargetLowering::Expand:
1097 // Expand brcond's setcc into its constituent parts and create a BR_CC
1098 // Node.
1099 if (Tmp2.getOpcode() == ISD::SETCC) {
1100 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1101 Tmp2.getOperand(0), Tmp2.getOperand(1),
1102 Node->getOperand(2));
1103 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001104 // Make sure the condition is either zero or one. It may have been
1105 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001106 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1107 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1108 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001109
Nate Begeman371e4952005-08-16 19:49:35 +00001110 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1111 DAG.getCondCode(ISD::SETNE), Tmp2,
1112 DAG.getConstant(0, Tmp2.getValueType()),
1113 Node->getOperand(2));
1114 }
1115 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001116 }
1117 break;
1118 case ISD::BR_CC:
1119 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001120 // Ensure that libcalls are emitted before a branch.
1121 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1122 Tmp1 = LegalizeOp(Tmp1);
1123 LastCALLSEQ_END = DAG.getEntryNode();
1124
Nate Begeman7e7f4392006-02-01 07:19:44 +00001125 Tmp2 = Node->getOperand(2); // LHS
1126 Tmp3 = Node->getOperand(3); // RHS
1127 Tmp4 = Node->getOperand(1); // CC
1128
1129 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1130
1131 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1132 // the LHS is a legal SETCC itself. In this case, we need to compare
1133 // the result against zero to select between true and false values.
1134 if (Tmp3.Val == 0) {
1135 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1136 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001137 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001138
1139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1140 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001141
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001142 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1143 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001144 case TargetLowering::Legal: break;
1145 case TargetLowering::Custom:
1146 Tmp4 = TLI.LowerOperation(Result, DAG);
1147 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001148 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001149 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001150 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001151 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1153 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001154
Evan Cheng31d15fa2005-12-23 07:29:34 +00001155 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001156 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1157 Tmp2 = Result.getValue(0);
1158 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001159
Evan Cheng31d15fa2005-12-23 07:29:34 +00001160 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1161 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001162 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001163 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001164 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001165 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001166 Tmp2 = LegalizeOp(Tmp1);
1167 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001168 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001169 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001170 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001171 // Since loads produce two values, make sure to remember that we
1172 // legalized both of them.
1173 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1174 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1175 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001176 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001177 case ISD::EXTLOAD:
1178 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001179 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001180 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1181 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001182
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001183 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001184 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001185 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001186 case TargetLowering::Promote:
1187 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1189 DAG.getValueType(MVT::i8));
1190 Tmp1 = Result.getValue(0);
1191 Tmp2 = Result.getValue(1);
1192 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001193 case TargetLowering::Custom:
1194 isCustom = true;
1195 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001196 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1198 Node->getOperand(3));
1199 Tmp1 = Result.getValue(0);
1200 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001201
1202 if (isCustom) {
1203 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1204 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001205 Tmp1 = LegalizeOp(Tmp3);
1206 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001207 }
1208 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001209 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001210 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001211 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001212 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1213 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001214 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001215 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1216 Tmp2 = LegalizeOp(Load.getValue(1));
1217 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001218 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001219 assert(Node->getOpcode() != ISD::EXTLOAD &&
1220 "EXTLOAD should always be supported!");
1221 // Turn the unsupported load into an EXTLOAD followed by an explicit
1222 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001223 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1224 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001225 SDOperand ValRes;
1226 if (Node->getOpcode() == ISD::SEXTLOAD)
1227 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001228 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001229 else
1230 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001231 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1232 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1233 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001234 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001235 // Since loads produce two values, make sure to remember that we legalized
1236 // both of them.
1237 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1238 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1239 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001240 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001241 case ISD::EXTRACT_ELEMENT: {
1242 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1243 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001244 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001245 case Legal:
1246 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1247 // 1 -> Hi
1248 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1249 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1250 TLI.getShiftAmountTy()));
1251 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1252 } else {
1253 // 0 -> Lo
1254 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1255 Node->getOperand(0));
1256 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001257 break;
1258 case Expand:
1259 // Get both the low and high parts.
1260 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1261 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1262 Result = Tmp2; // 1 -> Hi
1263 else
1264 Result = Tmp1; // 0 -> Lo
1265 break;
1266 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001267 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001268 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001269
1270 case ISD::CopyToReg:
1271 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001272
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001273 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001274 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001275 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001276 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001277 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001278 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001279 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001280 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001281 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001282 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001283 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1284 Tmp3);
1285 } else {
1286 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001287 }
1288
1289 // Since this produces two values, make sure to remember that we legalized
1290 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001291 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001292 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001293 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001294 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001295 break;
1296
1297 case ISD::RET:
1298 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001299
1300 // Ensure that libcalls are emitted before a return.
1301 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1302 Tmp1 = LegalizeOp(Tmp1);
1303 LastCALLSEQ_END = DAG.getEntryNode();
1304
Chris Lattnerdc750592005-01-07 07:47:09 +00001305 switch (Node->getNumOperands()) {
1306 case 2: // ret val
1307 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1308 case Legal:
1309 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001310 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001311 break;
1312 case Expand: {
1313 SDOperand Lo, Hi;
1314 ExpandOp(Node->getOperand(1), Lo, Hi);
1315 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001316 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001317 }
1318 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001319 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1321 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001322 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001323 }
1324 break;
1325 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001326 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001327 break;
1328 default: { // ret <values>
1329 std::vector<SDOperand> NewValues;
1330 NewValues.push_back(Tmp1);
1331 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1332 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1333 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001334 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001335 break;
1336 case Expand: {
1337 SDOperand Lo, Hi;
1338 ExpandOp(Node->getOperand(i), Lo, Hi);
1339 NewValues.push_back(Lo);
1340 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001341 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001342 }
1343 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001344 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001345 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001346
1347 if (NewValues.size() == Node->getNumOperands())
1348 Result = DAG.UpdateNodeOperands(Result, NewValues);
1349 else
1350 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001351 break;
1352 }
1353 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001354
Chris Lattner4d1ea712006-01-29 21:02:23 +00001355 if (Result.getOpcode() == ISD::RET) {
1356 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1357 default: assert(0 && "This action is not supported yet!");
1358 case TargetLowering::Legal: break;
1359 case TargetLowering::Custom:
1360 Tmp1 = TLI.LowerOperation(Result, DAG);
1361 if (Tmp1.Val) Result = Tmp1;
1362 break;
1363 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001364 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001365 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001366 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1368 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1369
Chris Lattnere69daaf2005-01-08 06:25:56 +00001370 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001371 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001372 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001373 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001374 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001375 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001376 } else {
1377 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001378 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001379 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001380 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1381 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001382 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001383 }
1384
Chris Lattnerdc750592005-01-07 07:47:09 +00001385 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1386 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001387 Tmp3 = LegalizeOp(Node->getOperand(1));
1388 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1389 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001390
Chris Lattnerd02b0542006-01-28 10:58:55 +00001391 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001392 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1393 default: assert(0 && "This action is not supported yet!");
1394 case TargetLowering::Legal: break;
1395 case TargetLowering::Custom:
1396 Tmp1 = TLI.LowerOperation(Result, DAG);
1397 if (Tmp1.Val) Result = Tmp1;
1398 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001399 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001400 break;
1401 }
1402 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001403 // Truncate the value and store the result.
1404 Tmp3 = PromoteOp(Node->getOperand(1));
1405 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001406 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001407 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001408 break;
1409
Chris Lattnerdc750592005-01-07 07:47:09 +00001410 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001411 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001412 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001413
1414 // If this is a vector type, then we have to calculate the increment as
1415 // the product of the element size in bytes, and the number of elements
1416 // in the high half of the vector.
1417 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1418 SDNode *InVal = Node->getOperand(1).Val;
1419 unsigned NumElems =
1420 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1421 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1422
1423 // Figure out if there is a Packed type corresponding to this Vector
1424 // type. If so, convert to the packed type.
1425 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1426 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1427 // Turn this into a normal store of the packed type.
1428 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1429 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1430 Node->getOperand(3));
1431 break;
1432 } else if (NumElems == 1) {
1433 // Turn this into a normal store of the scalar type.
1434 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1435 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1436 Node->getOperand(3));
1437 break;
1438 } else {
1439 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1440 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1441 }
1442 } else {
1443 ExpandOp(Node->getOperand(1), Lo, Hi);
1444 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1445 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001446
1447 if (!TLI.isLittleEndian())
1448 std::swap(Lo, Hi);
1449
Chris Lattner55e9cde2005-05-11 04:51:16 +00001450 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1451 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001452 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1453 getIntPtrConstant(IncrementSize));
1454 assert(isTypeLegal(Tmp2.getValueType()) &&
1455 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001456 // FIXME: This sets the srcvalue of both halves to be the same, which is
1457 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001458 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1459 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001460 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1461 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001462 }
1463 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001464 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001465 case ISD::PCMARKER:
1466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001467 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001468 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001469 case ISD::STACKSAVE:
1470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001471 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1472 Tmp1 = Result.getValue(0);
1473 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001474
Chris Lattnerb3266452006-01-13 02:50:02 +00001475 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1476 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001477 case TargetLowering::Legal: break;
1478 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001479 Tmp3 = TLI.LowerOperation(Result, DAG);
1480 if (Tmp3.Val) {
1481 Tmp1 = LegalizeOp(Tmp3);
1482 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001483 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001484 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001485 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001486 // Expand to CopyFromReg if the target set
1487 // StackPointerRegisterToSaveRestore.
1488 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001489 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001490 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001491 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001492 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001493 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1494 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001495 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001496 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001497 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001498
1499 // Since stacksave produce two values, make sure to remember that we
1500 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001501 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1502 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1503 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001504
Chris Lattnerb3266452006-01-13 02:50:02 +00001505 case ISD::STACKRESTORE:
1506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1507 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001508 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001509
1510 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1511 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001512 case TargetLowering::Legal: break;
1513 case TargetLowering::Custom:
1514 Tmp1 = TLI.LowerOperation(Result, DAG);
1515 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001516 break;
1517 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001518 // Expand to CopyToReg if the target set
1519 // StackPointerRegisterToSaveRestore.
1520 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1521 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1522 } else {
1523 Result = Tmp1;
1524 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001525 break;
1526 }
1527 break;
1528
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001529 case ISD::READCYCLECOUNTER:
1530 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001531 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001532
1533 // Since rdcc produce two values, make sure to remember that we legalized
1534 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001535 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001536 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001537 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001538
Evan Cheng31d15fa2005-12-23 07:29:34 +00001539 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001540 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1541 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1542
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001543 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1544 "Cannot handle illegal TRUNCSTORE yet!");
1545 Tmp2 = LegalizeOp(Node->getOperand(1));
1546
1547 // The only promote case we handle is TRUNCSTORE:i1 X into
1548 // -> TRUNCSTORE:i8 (and X, 1)
1549 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1550 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1551 TargetLowering::Promote) {
1552 // Promote the bool to a mask then store.
1553 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1554 DAG.getConstant(1, Tmp2.getValueType()));
1555 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1556 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001557
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001558 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1559 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001560 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1561 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001562 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001563
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001564 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1565 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1566 default: assert(0 && "This action is not supported yet!");
1567 case TargetLowering::Legal: break;
1568 case TargetLowering::Custom:
1569 Tmp1 = TLI.LowerOperation(Result, DAG);
1570 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001571 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001572 }
1573 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001574 }
Chris Lattner39c67442005-01-14 22:08:15 +00001575 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001576 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1577 case Expand: assert(0 && "It's impossible to expand bools");
1578 case Legal:
1579 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1580 break;
1581 case Promote:
1582 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1583 break;
1584 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001585 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001586 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001587
Chris Lattnerd02b0542006-01-28 10:58:55 +00001588 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001589
Nate Begeman987121a2005-08-23 04:29:48 +00001590 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001591 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001592 case TargetLowering::Legal: break;
1593 case TargetLowering::Custom: {
1594 Tmp1 = TLI.LowerOperation(Result, DAG);
1595 if (Tmp1.Val) Result = Tmp1;
1596 break;
1597 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001598 case TargetLowering::Expand:
1599 if (Tmp1.getOpcode() == ISD::SETCC) {
1600 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1601 Tmp2, Tmp3,
1602 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1603 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001604 // Make sure the condition is either zero or one. It may have been
1605 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001606 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1607 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1608 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001609 Result = DAG.getSelectCC(Tmp1,
1610 DAG.getConstant(0, Tmp1.getValueType()),
1611 Tmp2, Tmp3, ISD::SETNE);
1612 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001613 break;
1614 case TargetLowering::Promote: {
1615 MVT::ValueType NVT =
1616 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1617 unsigned ExtOp, TruncOp;
1618 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001619 ExtOp = ISD::ANY_EXTEND;
1620 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001621 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001622 ExtOp = ISD::FP_EXTEND;
1623 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001624 }
1625 // Promote each of the values to the new type.
1626 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1627 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1628 // Perform the larger operation, then round down.
1629 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1630 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1631 break;
1632 }
1633 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001634 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001635 case ISD::SELECT_CC: {
1636 Tmp1 = Node->getOperand(0); // LHS
1637 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001638 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1639 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001640 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001641
Nate Begeman7e7f4392006-02-01 07:19:44 +00001642 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1643
1644 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1645 // the LHS is a legal SETCC itself. In this case, we need to compare
1646 // the result against zero to select between true and false values.
1647 if (Tmp2.Val == 0) {
1648 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1649 CC = DAG.getCondCode(ISD::SETNE);
1650 }
1651 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1652
1653 // Everything is legal, see if we should expand this op or something.
1654 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1655 default: assert(0 && "This action is not supported yet!");
1656 case TargetLowering::Legal: break;
1657 case TargetLowering::Custom:
1658 Tmp1 = TLI.LowerOperation(Result, DAG);
1659 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001660 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001661 }
1662 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001663 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001664 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001665 Tmp1 = Node->getOperand(0);
1666 Tmp2 = Node->getOperand(1);
1667 Tmp3 = Node->getOperand(2);
1668 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1669
1670 // If we had to Expand the SetCC operands into a SELECT node, then it may
1671 // not always be possible to return a true LHS & RHS. In this case, just
1672 // return the value we legalized, returned in the LHS
1673 if (Tmp2.Val == 0) {
1674 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001675 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001676 }
Nate Begeman987121a2005-08-23 04:29:48 +00001677
Chris Lattnerf263a232006-01-30 22:43:50 +00001678 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001679 default: assert(0 && "Cannot handle this action for SETCC yet!");
1680 case TargetLowering::Custom:
1681 isCustom = true;
1682 // FALLTHROUGH.
1683 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001684 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001685 if (isCustom) {
1686 Tmp3 = TLI.LowerOperation(Result, DAG);
1687 if (Tmp3.Val) Result = Tmp3;
1688 }
Nate Begeman987121a2005-08-23 04:29:48 +00001689 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001690 case TargetLowering::Promote: {
1691 // First step, figure out the appropriate operation to use.
1692 // Allow SETCC to not be supported for all legal data types
1693 // Mostly this targets FP
1694 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1695 MVT::ValueType OldVT = NewInTy;
1696
1697 // Scan for the appropriate larger type to use.
1698 while (1) {
1699 NewInTy = (MVT::ValueType)(NewInTy+1);
1700
1701 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1702 "Fell off of the edge of the integer world");
1703 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1704 "Fell off of the edge of the floating point world");
1705
1706 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001707 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001708 break;
1709 }
1710 if (MVT::isInteger(NewInTy))
1711 assert(0 && "Cannot promote Legal Integer SETCC yet");
1712 else {
1713 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1714 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1715 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001716 Tmp1 = LegalizeOp(Tmp1);
1717 Tmp2 = LegalizeOp(Tmp2);
1718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001719 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001720 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001721 }
Nate Begeman987121a2005-08-23 04:29:48 +00001722 case TargetLowering::Expand:
1723 // Expand a setcc node into a select_cc of the same condition, lhs, and
1724 // rhs that selects between const 1 (true) and const 0 (false).
1725 MVT::ValueType VT = Node->getValueType(0);
1726 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1727 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1728 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001729 break;
1730 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001731 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001732 case ISD::MEMSET:
1733 case ISD::MEMCPY:
1734 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001736 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1737
1738 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1739 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1740 case Expand: assert(0 && "Cannot expand a byte!");
1741 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001742 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001743 break;
1744 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001745 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001746 break;
1747 }
1748 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001749 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001750 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001751
1752 SDOperand Tmp4;
1753 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001754 case Expand: {
1755 // Length is too big, just take the lo-part of the length.
1756 SDOperand HiPart;
1757 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1758 break;
1759 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001760 case Legal:
1761 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001762 break;
1763 case Promote:
1764 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001765 break;
1766 }
1767
1768 SDOperand Tmp5;
1769 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1770 case Expand: assert(0 && "Cannot expand this yet!");
1771 case Legal:
1772 Tmp5 = LegalizeOp(Node->getOperand(4));
1773 break;
1774 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001775 Tmp5 = PromoteOp(Node->getOperand(4));
1776 break;
1777 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001778
1779 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1780 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001781 case TargetLowering::Custom:
1782 isCustom = true;
1783 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001784 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001785 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001786 if (isCustom) {
1787 Tmp1 = TLI.LowerOperation(Result, DAG);
1788 if (Tmp1.Val) Result = Tmp1;
1789 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001790 break;
1791 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001792 // Otherwise, the target does not support this operation. Lower the
1793 // operation to an explicit libcall as appropriate.
1794 MVT::ValueType IntPtr = TLI.getPointerTy();
1795 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1796 std::vector<std::pair<SDOperand, const Type*> > Args;
1797
Reid Spencer6dced922005-01-12 14:53:45 +00001798 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001799 if (Node->getOpcode() == ISD::MEMSET) {
1800 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001801 // Extend the (previously legalized) ubyte argument to be an int value
1802 // for the call.
1803 if (Tmp3.getValueType() > MVT::i32)
1804 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1805 else
1806 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001807 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1808 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1809
1810 FnName = "memset";
1811 } else if (Node->getOpcode() == ISD::MEMCPY ||
1812 Node->getOpcode() == ISD::MEMMOVE) {
1813 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1814 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1815 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1816 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1817 } else {
1818 assert(0 && "Unknown op!");
1819 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001820
Chris Lattner85d70c62005-01-11 05:57:22 +00001821 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001822 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001823 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001824 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001825 break;
1826 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001827 }
1828 break;
1829 }
Chris Lattner5385db52005-05-09 20:23:03 +00001830
Chris Lattner4157c412005-04-02 04:00:59 +00001831 case ISD::SHL_PARTS:
1832 case ISD::SRA_PARTS:
1833 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001834 std::vector<SDOperand> Ops;
1835 bool Changed = false;
1836 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1837 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1838 Changed |= Ops.back() != Node->getOperand(i);
1839 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001840 if (Changed)
1841 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001842
Evan Cheng870e4f82006-01-09 18:31:59 +00001843 switch (TLI.getOperationAction(Node->getOpcode(),
1844 Node->getValueType(0))) {
1845 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001846 case TargetLowering::Legal: break;
1847 case TargetLowering::Custom:
1848 Tmp1 = TLI.LowerOperation(Result, DAG);
1849 if (Tmp1.Val) {
1850 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001851 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001852 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001853 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1854 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001855 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001856 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001857 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001858 return RetVal;
1859 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001860 break;
1861 }
1862
Chris Lattner13fe99c2005-04-02 05:00:07 +00001863 // Since these produce multiple values, make sure to remember that we
1864 // legalized all of them.
1865 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1866 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1867 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001868 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001869
1870 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001871 case ISD::ADD:
1872 case ISD::SUB:
1873 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001874 case ISD::MULHS:
1875 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001876 case ISD::UDIV:
1877 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001878 case ISD::AND:
1879 case ISD::OR:
1880 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001881 case ISD::SHL:
1882 case ISD::SRL:
1883 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001884 case ISD::FADD:
1885 case ISD::FSUB:
1886 case ISD::FMUL:
1887 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001888 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001889 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1890 case Expand: assert(0 && "Not possible");
1891 case Legal:
1892 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1893 break;
1894 case Promote:
1895 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1896 break;
1897 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001898
1899 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001900
Andrew Lenharth72594262005-12-24 23:42:32 +00001901 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001902 default: assert(0 && "Operation not supported");
1903 case TargetLowering::Legal: break;
1904 case TargetLowering::Custom:
1905 Tmp1 = TLI.LowerOperation(Result, DAG);
1906 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001907 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001908 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001909 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001910
1911 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1912 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1913 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1914 case Expand: assert(0 && "Not possible");
1915 case Legal:
1916 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1917 break;
1918 case Promote:
1919 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1920 break;
1921 }
1922
1923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1924
1925 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1926 default: assert(0 && "Operation not supported");
1927 case TargetLowering::Custom:
1928 Tmp1 = TLI.LowerOperation(Result, DAG);
1929 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00001930 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001931 case TargetLowering::Legal: break;
1932 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00001933 // If this target supports fabs/fneg natively, do this efficiently.
1934 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1935 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1936 // Get the sign bit of the RHS.
1937 MVT::ValueType IVT =
1938 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1939 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1940 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1941 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1942 // Get the absolute value of the result.
1943 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1944 // Select between the nabs and abs value based on the sign bit of
1945 // the input.
1946 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1947 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1948 AbsVal),
1949 AbsVal);
1950 Result = LegalizeOp(Result);
1951 break;
1952 }
1953
1954 // Otherwise, do bitwise ops!
1955
1956 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001957 const char *FnName;
1958 if (Node->getValueType(0) == MVT::f32) {
1959 FnName = "copysignf";
1960 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1961 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1962 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1963 } else {
1964 FnName = "copysign";
1965 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1966 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1967 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1968 }
1969 SDOperand Dummy;
1970 Result = ExpandLibCall(FnName, Node, Dummy);
1971 break;
1972 }
1973 break;
1974
Nate Begeman5965bd12006-02-17 05:43:56 +00001975 case ISD::ADDC:
1976 case ISD::SUBC:
1977 Tmp1 = LegalizeOp(Node->getOperand(0));
1978 Tmp2 = LegalizeOp(Node->getOperand(1));
1979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1980 // Since this produces two values, make sure to remember that we legalized
1981 // both of them.
1982 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1983 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1984 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00001985
Nate Begeman5965bd12006-02-17 05:43:56 +00001986 case ISD::ADDE:
1987 case ISD::SUBE:
1988 Tmp1 = LegalizeOp(Node->getOperand(0));
1989 Tmp2 = LegalizeOp(Node->getOperand(1));
1990 Tmp3 = LegalizeOp(Node->getOperand(2));
1991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1992 // Since this produces two values, make sure to remember that we legalized
1993 // both of them.
1994 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1995 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1996 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00001997
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001998 case ISD::BUILD_PAIR: {
1999 MVT::ValueType PairTy = Node->getValueType(0);
2000 // TODO: handle the case where the Lo and Hi operands are not of legal type
2001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2002 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2003 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002004 case TargetLowering::Promote:
2005 case TargetLowering::Custom:
2006 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002007 case TargetLowering::Legal:
2008 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2009 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2010 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002011 case TargetLowering::Expand:
2012 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2013 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2014 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2015 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2016 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002017 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002018 break;
2019 }
2020 break;
2021 }
2022
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002023 case ISD::UREM:
2024 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002025 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002026 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2027 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002028
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002029 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002030 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2031 case TargetLowering::Custom:
2032 isCustom = true;
2033 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002034 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002035 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002036 if (isCustom) {
2037 Tmp1 = TLI.LowerOperation(Result, DAG);
2038 if (Tmp1.Val) Result = Tmp1;
2039 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002040 break;
Chris Lattner81914422005-08-03 20:31:37 +00002041 case TargetLowering::Expand:
2042 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002043 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002044 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002045 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002046 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2047 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2048 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2049 } else {
2050 // Floating point mod -> fmod libcall.
2051 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2052 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002053 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002054 }
2055 break;
2056 }
2057 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002058 case ISD::VAARG: {
2059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2060 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2061
Chris Lattner364b89a2006-01-28 07:42:08 +00002062 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002063 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2064 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002065 case TargetLowering::Custom:
2066 isCustom = true;
2067 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002068 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2070 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002071 Tmp1 = Result.getValue(1);
2072
2073 if (isCustom) {
2074 Tmp2 = TLI.LowerOperation(Result, DAG);
2075 if (Tmp2.Val) {
2076 Result = LegalizeOp(Tmp2);
2077 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2078 }
2079 }
Nate Begemane74795c2006-01-25 18:21:52 +00002080 break;
2081 case TargetLowering::Expand: {
2082 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2083 Node->getOperand(2));
2084 // Increment the pointer, VAList, to the next vaarg
2085 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2086 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2087 TLI.getPointerTy()));
2088 // Store the incremented VAList to the legalized pointer
2089 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2090 Node->getOperand(2));
2091 // Load the actual argument out of the pointer VAList
2092 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002093 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002094 Result = LegalizeOp(Result);
2095 break;
2096 }
2097 }
2098 // Since VAARG produces two values, make sure to remember that we
2099 // legalized both of them.
2100 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002101 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2102 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002103 }
2104
2105 case ISD::VACOPY:
2106 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2107 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2108 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2109
2110 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2111 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002112 case TargetLowering::Custom:
2113 isCustom = true;
2114 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002115 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002116 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2117 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002118 if (isCustom) {
2119 Tmp1 = TLI.LowerOperation(Result, DAG);
2120 if (Tmp1.Val) Result = Tmp1;
2121 }
Nate Begemane74795c2006-01-25 18:21:52 +00002122 break;
2123 case TargetLowering::Expand:
2124 // This defaults to loading a pointer from the input and storing it to the
2125 // output, returning the chain.
2126 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2127 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2128 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002129 break;
2130 }
2131 break;
2132
2133 case ISD::VAEND:
2134 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2135 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2136
2137 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2138 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002139 case TargetLowering::Custom:
2140 isCustom = true;
2141 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002142 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002144 if (isCustom) {
2145 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2146 if (Tmp1.Val) Result = Tmp1;
2147 }
Nate Begemane74795c2006-01-25 18:21:52 +00002148 break;
2149 case TargetLowering::Expand:
2150 Result = Tmp1; // Default to a no-op, return the chain
2151 break;
2152 }
2153 break;
2154
2155 case ISD::VASTART:
2156 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2157 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2158
Chris Lattnerd02b0542006-01-28 10:58:55 +00002159 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2160
Nate Begemane74795c2006-01-25 18:21:52 +00002161 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2162 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002163 case TargetLowering::Legal: break;
2164 case TargetLowering::Custom:
2165 Tmp1 = TLI.LowerOperation(Result, DAG);
2166 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002167 break;
2168 }
2169 break;
2170
Nate Begeman1b8121b2006-01-11 21:21:00 +00002171 case ISD::ROTL:
2172 case ISD::ROTR:
2173 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2174 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002175
2176 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2177 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002179 break;
2180
Nate Begeman2fba8a32006-01-14 03:14:10 +00002181 case ISD::BSWAP:
2182 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2183 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002184 case TargetLowering::Custom:
2185 assert(0 && "Cannot custom legalize this yet!");
2186 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002187 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002188 break;
2189 case TargetLowering::Promote: {
2190 MVT::ValueType OVT = Tmp1.getValueType();
2191 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2192 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002193
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002194 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2195 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2196 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2197 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2198 break;
2199 }
2200 case TargetLowering::Expand:
2201 Result = ExpandBSWAP(Tmp1);
2202 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002203 }
2204 break;
2205
Andrew Lenharth5e177822005-05-03 17:19:30 +00002206 case ISD::CTPOP:
2207 case ISD::CTTZ:
2208 case ISD::CTLZ:
2209 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2210 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002211 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002212 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002213 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002214 break;
2215 case TargetLowering::Promote: {
2216 MVT::ValueType OVT = Tmp1.getValueType();
2217 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002218
2219 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002220 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2221 // Perform the larger operation, then subtract if needed.
2222 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002223 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002224 case ISD::CTPOP:
2225 Result = Tmp1;
2226 break;
2227 case ISD::CTTZ:
2228 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002229 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2230 DAG.getConstant(getSizeInBits(NVT), NVT),
2231 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002232 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002233 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2234 break;
2235 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002236 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002237 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2238 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002239 getSizeInBits(OVT), NVT));
2240 break;
2241 }
2242 break;
2243 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002244 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002245 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002246 break;
2247 }
2248 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002249
Chris Lattner13fe99c2005-04-02 05:00:07 +00002250 // Unary operators
2251 case ISD::FABS:
2252 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002253 case ISD::FSQRT:
2254 case ISD::FSIN:
2255 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002256 Tmp1 = LegalizeOp(Node->getOperand(0));
2257 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002258 case TargetLowering::Promote:
2259 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002260 isCustom = true;
2261 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002262 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002263 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002264 if (isCustom) {
2265 Tmp1 = TLI.LowerOperation(Result, DAG);
2266 if (Tmp1.Val) Result = Tmp1;
2267 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002268 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002269 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002270 switch (Node->getOpcode()) {
2271 default: assert(0 && "Unreachable!");
2272 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002273 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2274 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002275 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002276 break;
Chris Lattner80026402005-04-30 04:43:14 +00002277 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002278 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2279 MVT::ValueType VT = Node->getValueType(0);
2280 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002281 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002282 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2283 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002284 break;
2285 }
2286 case ISD::FSQRT:
2287 case ISD::FSIN:
2288 case ISD::FCOS: {
2289 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002290 const char *FnName = 0;
2291 switch(Node->getOpcode()) {
2292 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2293 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2294 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2295 default: assert(0 && "Unreachable!");
2296 }
Nate Begeman77558da2005-08-04 21:43:28 +00002297 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002298 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002299 break;
2300 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002301 }
2302 break;
2303 }
2304 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002305
2306 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002307 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002308 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002309 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002310 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2311 Node->getOperand(0).getValueType())) {
2312 default: assert(0 && "Unknown operation action!");
2313 case TargetLowering::Expand:
2314 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2315 break;
2316 case TargetLowering::Legal:
2317 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002318 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002319 break;
2320 }
2321 }
2322 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002323 case ISD::VBIT_CONVERT: {
2324 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2325 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2326
2327 // The input has to be a vector type, we have to either scalarize it, pack
2328 // it, or convert it based on whether the input vector type is legal.
2329 SDNode *InVal = Node->getOperand(0).Val;
2330 unsigned NumElems =
2331 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2332 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2333
2334 // Figure out if there is a Packed type corresponding to this Vector
2335 // type. If so, convert to the packed type.
2336 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2337 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2338 // Turn this into a bit convert of the packed input.
2339 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2340 PackVectorOp(Node->getOperand(0), TVT));
2341 break;
2342 } else if (NumElems == 1) {
2343 // Turn this into a bit convert of the scalar input.
2344 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2345 PackVectorOp(Node->getOperand(0), EVT));
2346 break;
2347 } else {
2348 // FIXME: UNIMP! Store then reload
2349 assert(0 && "Cast from unsupported vector type not implemented yet!");
2350 }
2351 }
2352
Chris Lattner13fe99c2005-04-02 05:00:07 +00002353 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002354 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002355 case ISD::UINT_TO_FP: {
2356 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002357 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2358 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002359 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002360 Node->getOperand(0).getValueType())) {
2361 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002362 case TargetLowering::Custom:
2363 isCustom = true;
2364 // FALLTHROUGH
2365 case TargetLowering::Legal:
2366 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002367 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002368 if (isCustom) {
2369 Tmp1 = TLI.LowerOperation(Result, DAG);
2370 if (Tmp1.Val) Result = Tmp1;
2371 }
2372 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002373 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002374 Result = ExpandLegalINT_TO_FP(isSigned,
2375 LegalizeOp(Node->getOperand(0)),
2376 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002377 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002378 case TargetLowering::Promote:
2379 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2380 Node->getValueType(0),
2381 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002382 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002383 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002384 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002385 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002386 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2387 Node->getValueType(0), Node->getOperand(0));
2388 break;
2389 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002390 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002391 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002392 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2393 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002394 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002395 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2396 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002397 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002398 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2399 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002400 break;
2401 }
2402 break;
2403 }
2404 case ISD::TRUNCATE:
2405 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2406 case Legal:
2407 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002408 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002409 break;
2410 case Expand:
2411 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2412
2413 // Since the result is legal, we should just be able to truncate the low
2414 // part of the source.
2415 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2416 break;
2417 case Promote:
2418 Result = PromoteOp(Node->getOperand(0));
2419 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2420 break;
2421 }
2422 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002423
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002424 case ISD::FP_TO_SINT:
2425 case ISD::FP_TO_UINT:
2426 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2427 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002428 Tmp1 = LegalizeOp(Node->getOperand(0));
2429
Chris Lattner44fe26f2005-07-29 00:11:56 +00002430 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2431 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002432 case TargetLowering::Custom:
2433 isCustom = true;
2434 // FALLTHROUGH
2435 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002436 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002437 if (isCustom) {
2438 Tmp1 = TLI.LowerOperation(Result, DAG);
2439 if (Tmp1.Val) Result = Tmp1;
2440 }
2441 break;
2442 case TargetLowering::Promote:
2443 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2444 Node->getOpcode() == ISD::FP_TO_SINT);
2445 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002446 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002447 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2448 SDOperand True, False;
2449 MVT::ValueType VT = Node->getOperand(0).getValueType();
2450 MVT::ValueType NVT = Node->getValueType(0);
2451 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2452 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2453 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2454 Node->getOperand(0), Tmp2, ISD::SETLT);
2455 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2456 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002457 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002458 Tmp2));
2459 False = DAG.getNode(ISD::XOR, NVT, False,
2460 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002461 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2462 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002463 } else {
2464 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2465 }
2466 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002467 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002468 break;
2469 case Expand:
2470 assert(0 && "Shouldn't need to expand other operators here!");
2471 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002472 Tmp1 = PromoteOp(Node->getOperand(0));
2473 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2474 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002475 break;
2476 }
2477 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002478
Chris Lattner7753f172005-09-02 00:18:10 +00002479 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002480 case ISD::ZERO_EXTEND:
2481 case ISD::SIGN_EXTEND:
2482 case ISD::FP_EXTEND:
2483 case ISD::FP_ROUND:
2484 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002485 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002486 case Legal:
2487 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002488 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002489 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002490 case Promote:
2491 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002492 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002493 Tmp1 = PromoteOp(Node->getOperand(0));
2494 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002495 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002496 case ISD::ZERO_EXTEND:
2497 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002498 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002499 Result = DAG.getZeroExtendInReg(Result,
2500 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002501 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002502 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002503 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002504 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002505 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002506 Result,
2507 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002508 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002509 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002510 Result = PromoteOp(Node->getOperand(0));
2511 if (Result.getValueType() != Op.getValueType())
2512 // Dynamically dead while we have only 2 FP types.
2513 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2514 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002515 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002516 Result = PromoteOp(Node->getOperand(0));
2517 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2518 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002519 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002520 }
2521 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002522 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002523 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002524 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002525 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002526
2527 // If this operation is not supported, convert it to a shl/shr or load/store
2528 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002529 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2530 default: assert(0 && "This action not supported for this op yet!");
2531 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002532 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002533 break;
2534 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002535 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002536 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002537 // NOTE: we could fall back on load/store here too for targets without
2538 // SAR. However, it is doubtful that any exist.
2539 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2540 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002541 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002542 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2543 Node->getOperand(0), ShiftCst);
2544 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2545 Result, ShiftCst);
2546 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2547 // The only way we can lower this is to turn it into a STORETRUNC,
2548 // EXTLOAD pair, targetting a temporary location (a stack slot).
2549
2550 // NOTE: there is a choice here between constantly creating new stack
2551 // slots and always reusing the same one. We currently always create
2552 // new ones, as reuse may inhibit scheduling.
2553 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2554 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2555 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2556 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002557 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002558 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2559 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2560 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002561 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002562 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002563 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2564 Result, StackSlot, DAG.getSrcValue(NULL),
2565 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002566 } else {
2567 assert(0 && "Unknown op");
2568 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002569 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002570 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002571 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002572 }
Chris Lattner99222f72005-01-15 07:15:18 +00002573 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002574
2575 // Make sure that the generated code is itself legal.
2576 if (Result != Op)
2577 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002578
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002579 // Note that LegalizeOp may be reentered even from single-use nodes, which
2580 // means that we always must cache transformed nodes.
2581 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002582 return Result;
2583}
2584
Chris Lattner4d978642005-01-15 22:16:26 +00002585/// PromoteOp - Given an operation that produces a value in an invalid type,
2586/// promote it to compute the value into a larger type. The produced value will
2587/// have the correct bits for the low portion of the register, but no guarantee
2588/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002589SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2590 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002591 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002592 assert(getTypeAction(VT) == Promote &&
2593 "Caller should expand or legalize operands that are not promotable!");
2594 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2595 "Cannot promote to smaller type!");
2596
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002597 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002598 SDOperand Result;
2599 SDNode *Node = Op.Val;
2600
Chris Lattner1a570f12005-09-02 20:32:45 +00002601 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2602 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002603
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002604 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002605 case ISD::CopyFromReg:
2606 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002607 default:
2608 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2609 assert(0 && "Do not know how to promote this operator!");
2610 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002611 case ISD::UNDEF:
2612 Result = DAG.getNode(ISD::UNDEF, NVT);
2613 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002614 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002615 if (VT != MVT::i1)
2616 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2617 else
2618 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002619 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2620 break;
2621 case ISD::ConstantFP:
2622 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2623 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2624 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002625
Chris Lattner2cb338d2005-01-18 02:59:52 +00002626 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002627 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002628 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2629 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002630 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002631
2632 case ISD::TRUNCATE:
2633 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2634 case Legal:
2635 Result = LegalizeOp(Node->getOperand(0));
2636 assert(Result.getValueType() >= NVT &&
2637 "This truncation doesn't make sense!");
2638 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2639 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2640 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002641 case Promote:
2642 // The truncation is not required, because we don't guarantee anything
2643 // about high bits anyway.
2644 Result = PromoteOp(Node->getOperand(0));
2645 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002646 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002647 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2648 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002649 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002650 }
2651 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002652 case ISD::SIGN_EXTEND:
2653 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002654 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002655 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2656 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2657 case Legal:
2658 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002659 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002660 break;
2661 case Promote:
2662 // Promote the reg if it's smaller.
2663 Result = PromoteOp(Node->getOperand(0));
2664 // The high bits are not guaranteed to be anything. Insert an extend.
2665 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002666 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002667 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002668 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002669 Result = DAG.getZeroExtendInReg(Result,
2670 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002671 break;
2672 }
2673 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002674 case ISD::BIT_CONVERT:
2675 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2676 Result = PromoteOp(Result);
2677 break;
2678
Chris Lattner4d978642005-01-15 22:16:26 +00002679 case ISD::FP_EXTEND:
2680 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2681 case ISD::FP_ROUND:
2682 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2683 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2684 case Promote: assert(0 && "Unreachable with 2 FP types!");
2685 case Legal:
2686 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002687 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002688 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002689 break;
2690 }
2691 break;
2692
2693 case ISD::SINT_TO_FP:
2694 case ISD::UINT_TO_FP:
2695 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2696 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002697 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002698 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002699 break;
2700
2701 case Promote:
2702 Result = PromoteOp(Node->getOperand(0));
2703 if (Node->getOpcode() == ISD::SINT_TO_FP)
2704 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002705 Result,
2706 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002707 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002708 Result = DAG.getZeroExtendInReg(Result,
2709 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002710 // No extra round required here.
2711 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002712 break;
2713 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002714 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2715 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002716 // Round if we cannot tolerate excess precision.
2717 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002718 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2719 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002720 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002721 }
Chris Lattner4d978642005-01-15 22:16:26 +00002722 break;
2723
Chris Lattner268d4572005-12-09 17:32:47 +00002724 case ISD::SIGN_EXTEND_INREG:
2725 Result = PromoteOp(Node->getOperand(0));
2726 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2727 Node->getOperand(1));
2728 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002729 case ISD::FP_TO_SINT:
2730 case ISD::FP_TO_UINT:
2731 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2732 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002733 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002734 break;
2735 case Promote:
2736 // The input result is prerounded, so we don't have to do anything
2737 // special.
2738 Tmp1 = PromoteOp(Node->getOperand(0));
2739 break;
2740 case Expand:
2741 assert(0 && "not implemented");
2742 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002743 // If we're promoting a UINT to a larger size, check to see if the new node
2744 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2745 // we can use that instead. This allows us to generate better code for
2746 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2747 // legal, such as PowerPC.
2748 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002749 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002750 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2751 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002752 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2753 } else {
2754 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2755 }
Chris Lattner4d978642005-01-15 22:16:26 +00002756 break;
2757
Chris Lattner13fe99c2005-04-02 05:00:07 +00002758 case ISD::FABS:
2759 case ISD::FNEG:
2760 Tmp1 = PromoteOp(Node->getOperand(0));
2761 assert(Tmp1.getValueType() == NVT);
2762 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2763 // NOTE: we do not have to do any extra rounding here for
2764 // NoExcessFPPrecision, because we know the input will have the appropriate
2765 // precision, and these operations don't modify precision at all.
2766 break;
2767
Chris Lattner9d6fa982005-04-28 21:44:33 +00002768 case ISD::FSQRT:
2769 case ISD::FSIN:
2770 case ISD::FCOS:
2771 Tmp1 = PromoteOp(Node->getOperand(0));
2772 assert(Tmp1.getValueType() == NVT);
2773 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002774 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002775 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2776 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002777 break;
2778
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002779 case ISD::AND:
2780 case ISD::OR:
2781 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002782 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002783 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002784 case ISD::MUL:
2785 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002786 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002787 // that too is okay if they are integer operations.
2788 Tmp1 = PromoteOp(Node->getOperand(0));
2789 Tmp2 = PromoteOp(Node->getOperand(1));
2790 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2791 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002792 break;
2793 case ISD::FADD:
2794 case ISD::FSUB:
2795 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002796 Tmp1 = PromoteOp(Node->getOperand(0));
2797 Tmp2 = PromoteOp(Node->getOperand(1));
2798 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2799 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2800
2801 // Floating point operations will give excess precision that we may not be
2802 // able to tolerate. If we DO allow excess precision, just leave it,
2803 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002804 // FIXME: Why would we need to round FP ops more than integer ones?
2805 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002806 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002807 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2808 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002809 break;
2810
Chris Lattner4d978642005-01-15 22:16:26 +00002811 case ISD::SDIV:
2812 case ISD::SREM:
2813 // These operators require that their input be sign extended.
2814 Tmp1 = PromoteOp(Node->getOperand(0));
2815 Tmp2 = PromoteOp(Node->getOperand(1));
2816 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002817 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2818 DAG.getValueType(VT));
2819 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2820 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002821 }
2822 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2823
2824 // Perform FP_ROUND: this is probably overly pessimistic.
2825 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002826 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2827 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002828 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002829 case ISD::FDIV:
2830 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002831 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002832 // These operators require that their input be fp extended.
2833 Tmp1 = PromoteOp(Node->getOperand(0));
2834 Tmp2 = PromoteOp(Node->getOperand(1));
2835 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2836
2837 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002838 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002839 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2840 DAG.getValueType(VT));
2841 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002842
2843 case ISD::UDIV:
2844 case ISD::UREM:
2845 // These operators require that their input be zero extended.
2846 Tmp1 = PromoteOp(Node->getOperand(0));
2847 Tmp2 = PromoteOp(Node->getOperand(1));
2848 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002849 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2850 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002851 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2852 break;
2853
2854 case ISD::SHL:
2855 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002856 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002857 break;
2858 case ISD::SRA:
2859 // The input value must be properly sign extended.
2860 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002861 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2862 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002863 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002864 break;
2865 case ISD::SRL:
2866 // The input value must be properly zero extended.
2867 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002868 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002869 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002870 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002871
2872 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002873 Tmp1 = Node->getOperand(0); // Get the chain.
2874 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002875 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2876 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2877 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2878 } else {
2879 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2880 Node->getOperand(2));
2881 // Increment the pointer, VAList, to the next vaarg
2882 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2883 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2884 TLI.getPointerTy()));
2885 // Store the incremented VAList to the legalized pointer
2886 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2887 Node->getOperand(2));
2888 // Load the actual argument out of the pointer VAList
2889 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2890 DAG.getSrcValue(0), VT);
2891 }
2892 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002893 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002894 break;
2895
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002896 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002897 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2898 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002899 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002900 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002901 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002902 case ISD::SEXTLOAD:
2903 case ISD::ZEXTLOAD:
2904 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002905 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2906 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002907 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002908 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002909 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002910 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002911 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002912 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2913 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002914 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002915 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002916 case ISD::SELECT_CC:
2917 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2918 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2919 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002920 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002921 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002922 case ISD::BSWAP:
2923 Tmp1 = Node->getOperand(0);
2924 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2925 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2926 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2927 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2928 TLI.getShiftAmountTy()));
2929 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002930 case ISD::CTPOP:
2931 case ISD::CTTZ:
2932 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002933 // Zero extend the argument
2934 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002935 // Perform the larger operation, then subtract if needed.
2936 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002937 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002938 case ISD::CTPOP:
2939 Result = Tmp1;
2940 break;
2941 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002942 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002943 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002944 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002945 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002946 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002947 break;
2948 case ISD::CTLZ:
2949 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002950 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2951 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002952 getSizeInBits(VT), NVT));
2953 break;
2954 }
2955 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002956 }
2957
2958 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002959
2960 // Make sure the result is itself legal.
2961 Result = LegalizeOp(Result);
2962
2963 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002964 AddPromotedOperand(Op, Result);
2965 return Result;
2966}
Chris Lattnerdc750592005-01-07 07:47:09 +00002967
Nate Begeman7e7f4392006-02-01 07:19:44 +00002968/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2969/// with condition CC on the current target. This usually involves legalizing
2970/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2971/// there may be no choice but to create a new SetCC node to represent the
2972/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2973/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2974void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2975 SDOperand &RHS,
2976 SDOperand &CC) {
2977 SDOperand Tmp1, Tmp2, Result;
2978
2979 switch (getTypeAction(LHS.getValueType())) {
2980 case Legal:
2981 Tmp1 = LegalizeOp(LHS); // LHS
2982 Tmp2 = LegalizeOp(RHS); // RHS
2983 break;
2984 case Promote:
2985 Tmp1 = PromoteOp(LHS); // LHS
2986 Tmp2 = PromoteOp(RHS); // RHS
2987
2988 // If this is an FP compare, the operands have already been extended.
2989 if (MVT::isInteger(LHS.getValueType())) {
2990 MVT::ValueType VT = LHS.getValueType();
2991 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2992
2993 // Otherwise, we have to insert explicit sign or zero extends. Note
2994 // that we could insert sign extends for ALL conditions, but zero extend
2995 // is cheaper on many machines (an AND instead of two shifts), so prefer
2996 // it.
2997 switch (cast<CondCodeSDNode>(CC)->get()) {
2998 default: assert(0 && "Unknown integer comparison!");
2999 case ISD::SETEQ:
3000 case ISD::SETNE:
3001 case ISD::SETUGE:
3002 case ISD::SETUGT:
3003 case ISD::SETULE:
3004 case ISD::SETULT:
3005 // ALL of these operations will work if we either sign or zero extend
3006 // the operands (including the unsigned comparisons!). Zero extend is
3007 // usually a simpler/cheaper operation, so prefer it.
3008 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3009 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3010 break;
3011 case ISD::SETGE:
3012 case ISD::SETGT:
3013 case ISD::SETLT:
3014 case ISD::SETLE:
3015 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3016 DAG.getValueType(VT));
3017 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3018 DAG.getValueType(VT));
3019 break;
3020 }
3021 }
3022 break;
3023 case Expand:
3024 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3025 ExpandOp(LHS, LHSLo, LHSHi);
3026 ExpandOp(RHS, RHSLo, RHSHi);
3027 switch (cast<CondCodeSDNode>(CC)->get()) {
3028 case ISD::SETEQ:
3029 case ISD::SETNE:
3030 if (RHSLo == RHSHi)
3031 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3032 if (RHSCST->isAllOnesValue()) {
3033 // Comparison to -1.
3034 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3035 Tmp2 = RHSLo;
3036 break;
3037 }
3038
3039 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3040 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3041 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3042 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3043 break;
3044 default:
3045 // If this is a comparison of the sign bit, just look at the top part.
3046 // X > -1, x < 0
3047 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3048 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3049 CST->getValue() == 0) || // X < 0
3050 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3051 CST->isAllOnesValue())) { // X > -1
3052 Tmp1 = LHSHi;
3053 Tmp2 = RHSHi;
3054 break;
3055 }
3056
3057 // FIXME: This generated code sucks.
3058 ISD::CondCode LowCC;
3059 switch (cast<CondCodeSDNode>(CC)->get()) {
3060 default: assert(0 && "Unknown integer setcc!");
3061 case ISD::SETLT:
3062 case ISD::SETULT: LowCC = ISD::SETULT; break;
3063 case ISD::SETGT:
3064 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3065 case ISD::SETLE:
3066 case ISD::SETULE: LowCC = ISD::SETULE; break;
3067 case ISD::SETGE:
3068 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3069 }
3070
3071 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3072 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3073 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3074
3075 // NOTE: on targets without efficient SELECT of bools, we can always use
3076 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3077 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3078 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3079 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3080 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3081 Result, Tmp1, Tmp2));
3082 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003083 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003084 }
3085 }
3086 LHS = Tmp1;
3087 RHS = Tmp2;
3088}
3089
Chris Lattner36e663d2005-12-23 00:16:34 +00003090/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003091/// The resultant code need not be legal. Note that SrcOp is the input operand
3092/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003093SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3094 SDOperand SrcOp) {
3095 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003096 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003097
3098 // Emit a store to the stack slot.
3099 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003100 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003101 // Result is a load from the stack slot.
3102 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3103}
3104
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003105/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3106/// support the operation, but do support the resultant packed vector type.
3107SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3108
3109 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003110 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003111 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003112 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003113 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003114 std::map<SDOperand, std::vector<unsigned> > Values;
3115 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003116 bool isConstant = true;
3117 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3118 SplatValue.getOpcode() != ISD::UNDEF)
3119 isConstant = false;
3120
Evan Cheng1d2e9952006-03-24 01:17:21 +00003121 for (unsigned i = 1; i < NumElems; ++i) {
3122 SDOperand V = Node->getOperand(i);
3123 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3124 if (I != Values.end())
3125 I->second.push_back(i);
3126 else
3127 Values[V].push_back(i);
3128 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003129 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003130 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003131 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003132
3133 // If this isn't a constant element or an undef, we can't use a constant
3134 // pool load.
3135 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3136 V.getOpcode() != ISD::UNDEF)
3137 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003138 }
3139
3140 if (isOnlyLowElement) {
3141 // If the low element is an undef too, then this whole things is an undef.
3142 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3143 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3144 // Otherwise, turn this into a scalar_to_vector node.
3145 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3146 Node->getOperand(0));
3147 }
3148
Chris Lattner77e271c2006-03-24 07:29:17 +00003149 // If all elements are constants, create a load from the constant pool.
3150 if (isConstant) {
3151 MVT::ValueType VT = Node->getValueType(0);
3152 const Type *OpNTy =
3153 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3154 std::vector<Constant*> CV;
3155 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3156 if (ConstantFPSDNode *V =
3157 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3158 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3159 } else if (ConstantSDNode *V =
3160 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3161 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3162 } else {
3163 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3164 CV.push_back(UndefValue::get(OpNTy));
3165 }
3166 }
3167 Constant *CP = ConstantPacked::get(CV);
3168 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3169 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3170 DAG.getSrcValue(NULL));
3171 }
3172
Chris Lattner21e68c82006-03-20 01:52:29 +00003173 if (SplatValue.Val) { // Splat of one value?
3174 // Build the shuffle constant vector: <0, 0, 0, 0>
3175 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003176 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003177 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003178 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner21e68c82006-03-20 01:52:29 +00003179 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3180
3181 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3182 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3183 // Get the splatted value into the low element of a vector register.
3184 SDOperand LowValVec =
3185 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3186
3187 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3188 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3189 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3190 SplatMask);
3191 }
3192 }
3193
Evan Cheng1d2e9952006-03-24 01:17:21 +00003194 // If there are only two unique elements, we may be able to turn this into a
3195 // vector shuffle.
3196 if (Values.size() == 2) {
3197 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3198 MVT::ValueType MaskVT =
3199 MVT::getIntVectorWithNumElements(NumElems);
3200 std::vector<SDOperand> MaskVec(NumElems);
3201 unsigned i = 0;
3202 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3203 E = Values.end(); I != E; ++I) {
3204 for (std::vector<unsigned>::iterator II = I->second.begin(),
3205 EE = I->second.end(); II != EE; ++II)
3206 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3207 i += NumElems;
3208 }
3209 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3210
3211 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Evan Cheng68d9bf22006-03-24 18:45:20 +00003212 if (TLI.isShuffleLegal(Node->getValueType(0), ShuffleMask) &&
3213 TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0))) {
Evan Cheng1d2e9952006-03-24 01:17:21 +00003214 std::vector<SDOperand> Ops;
3215 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3216 E = Values.end(); I != E; ++I) {
3217 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3218 I->first);
3219 Ops.push_back(Op);
3220 }
3221 Ops.push_back(ShuffleMask);
3222
3223 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3224 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3225 }
3226 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003227
3228 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3229 // aligned object on the stack, store each element into it, then load
3230 // the result as a vector.
3231 MVT::ValueType VT = Node->getValueType(0);
3232 // Create the stack frame object.
3233 SDOperand FIPtr = CreateStackTemporary(VT);
3234
3235 // Emit a store of each element to the stack slot.
3236 std::vector<SDOperand> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003237 unsigned TypeByteSize =
3238 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3239 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3240 // Store (in the right endianness) the elements to memory.
3241 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3242 // Ignore undef elements.
3243 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3244
Chris Lattner8fa445a2006-03-22 01:46:54 +00003245 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003246
3247 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3248 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3249
3250 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3251 Node->getOperand(i), Idx,
3252 DAG.getSrcValue(NULL)));
3253 }
3254
3255 SDOperand StoreChain;
3256 if (!Stores.empty()) // Not all undef elements?
3257 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3258 else
3259 StoreChain = DAG.getEntryNode();
3260
3261 // Result is a load from the stack slot.
3262 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3263}
3264
3265/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3266/// specified value type.
3267SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3268 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3269 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3270 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3271 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3272}
3273
Chris Lattner4157c412005-04-02 04:00:59 +00003274void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3275 SDOperand Op, SDOperand Amt,
3276 SDOperand &Lo, SDOperand &Hi) {
3277 // Expand the subcomponents.
3278 SDOperand LHSL, LHSH;
3279 ExpandOp(Op, LHSL, LHSH);
3280
3281 std::vector<SDOperand> Ops;
3282 Ops.push_back(LHSL);
3283 Ops.push_back(LHSH);
3284 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003285 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003286 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003287 Hi = Lo.getValue(1);
3288}
3289
3290
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003291/// ExpandShift - Try to find a clever way to expand this shift operation out to
3292/// smaller elements. If we can't find a way that is more efficient than a
3293/// libcall on this target, return false. Otherwise, return true with the
3294/// low-parts expanded into Lo and Hi.
3295bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3296 SDOperand &Lo, SDOperand &Hi) {
3297 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3298 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003299
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003300 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003301 SDOperand ShAmt = LegalizeOp(Amt);
3302 MVT::ValueType ShTy = ShAmt.getValueType();
3303 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3304 unsigned NVTBits = MVT::getSizeInBits(NVT);
3305
3306 // Handle the case when Amt is an immediate. Other cases are currently broken
3307 // and are disabled.
3308 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3309 unsigned Cst = CN->getValue();
3310 // Expand the incoming operand to be shifted, so that we have its parts
3311 SDOperand InL, InH;
3312 ExpandOp(Op, InL, InH);
3313 switch(Opc) {
3314 case ISD::SHL:
3315 if (Cst > VTBits) {
3316 Lo = DAG.getConstant(0, NVT);
3317 Hi = DAG.getConstant(0, NVT);
3318 } else if (Cst > NVTBits) {
3319 Lo = DAG.getConstant(0, NVT);
3320 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003321 } else if (Cst == NVTBits) {
3322 Lo = DAG.getConstant(0, NVT);
3323 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003324 } else {
3325 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3326 Hi = DAG.getNode(ISD::OR, NVT,
3327 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3328 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3329 }
3330 return true;
3331 case ISD::SRL:
3332 if (Cst > VTBits) {
3333 Lo = DAG.getConstant(0, NVT);
3334 Hi = DAG.getConstant(0, NVT);
3335 } else if (Cst > NVTBits) {
3336 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3337 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003338 } else if (Cst == NVTBits) {
3339 Lo = InH;
3340 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003341 } else {
3342 Lo = DAG.getNode(ISD::OR, NVT,
3343 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3344 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3345 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3346 }
3347 return true;
3348 case ISD::SRA:
3349 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003350 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003351 DAG.getConstant(NVTBits-1, ShTy));
3352 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003353 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003354 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003355 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003356 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003357 } else if (Cst == NVTBits) {
3358 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003359 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003360 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003361 } else {
3362 Lo = DAG.getNode(ISD::OR, NVT,
3363 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3364 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3365 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3366 }
3367 return true;
3368 }
3369 }
Nate Begemanb0674922005-04-06 21:13:14 +00003370 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003371}
Chris Lattneraac464e2005-01-21 06:05:23 +00003372
Chris Lattner4add7e32005-01-23 04:42:50 +00003373
Chris Lattneraac464e2005-01-21 06:05:23 +00003374// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3375// does not fit into a register, return the lo part and set the hi part to the
3376// by-reg argument. If it does fit into a single register, return the result
3377// and leave the Hi part unset.
3378SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3379 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003380 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3381 // The input chain to this libcall is the entry node of the function.
3382 // Legalizing the call will automatically add the previous call to the
3383 // dependence.
3384 SDOperand InChain = DAG.getEntryNode();
3385
Chris Lattneraac464e2005-01-21 06:05:23 +00003386 TargetLowering::ArgListTy Args;
3387 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3388 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3389 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3390 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3391 }
3392 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003393
Chris Lattner06bbeb62005-05-11 19:02:11 +00003394 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003395 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003396 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003397 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3398 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003399
Chris Lattner462505f2006-02-13 09:18:02 +00003400 // Legalize the call sequence, starting with the chain. This will advance
3401 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3402 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3403 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003404 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003405 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003406 default: assert(0 && "Unknown thing");
3407 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003408 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003409 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003410 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003411 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003412 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003413 }
Chris Lattner63022662005-09-02 20:26:58 +00003414 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003415}
3416
Chris Lattner4add7e32005-01-23 04:42:50 +00003417
Chris Lattneraac464e2005-01-21 06:05:23 +00003418/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3419/// destination type is legal.
3420SDOperand SelectionDAGLegalize::
3421ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003422 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003423 assert(getTypeAction(Source.getValueType()) == Expand &&
3424 "This is not an expansion!");
3425 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3426
Chris Lattner06bbeb62005-05-11 19:02:11 +00003427 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003428 assert(Source.getValueType() == MVT::i64 &&
3429 "This only works for 64-bit -> FP");
3430 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3431 // incoming integer is set. To handle this, we dynamically test to see if
3432 // it is set, and, if so, add a fudge factor.
3433 SDOperand Lo, Hi;
3434 ExpandOp(Source, Lo, Hi);
3435
Chris Lattner2a4f7312005-05-13 04:45:13 +00003436 // If this is unsigned, and not supported, first perform the conversion to
3437 // signed, then adjust the result if the sign bit is set.
3438 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3439 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3440
Chris Lattnerd47675e2005-08-09 20:20:18 +00003441 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3442 DAG.getConstant(0, Hi.getValueType()),
3443 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003444 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3445 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3446 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003447 uint64_t FF = 0x5f800000ULL;
3448 if (TLI.isLittleEndian()) FF <<= 32;
3449 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003450
Chris Lattnerc30405e2005-08-26 17:15:30 +00003451 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003452 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3453 SDOperand FudgeInReg;
3454 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003455 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3456 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003457 else {
3458 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003459 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3460 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003461 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003462 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003463 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003464
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003465 // Check to see if the target has a custom way to lower this. If so, use it.
3466 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3467 default: assert(0 && "This action not implemented for this operation!");
3468 case TargetLowering::Legal:
3469 case TargetLowering::Expand:
3470 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003471 case TargetLowering::Custom: {
3472 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3473 Source), DAG);
3474 if (NV.Val)
3475 return LegalizeOp(NV);
3476 break; // The target decided this was legal after all
3477 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003478 }
3479
Chris Lattner153587e2005-05-12 07:00:44 +00003480 // Expand the source, then glue it back together for the call. We must expand
3481 // the source in case it is shared (this pass of legalize must traverse it).
3482 SDOperand SrcLo, SrcHi;
3483 ExpandOp(Source, SrcLo, SrcHi);
3484 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3485
Chris Lattner06bbeb62005-05-11 19:02:11 +00003486 const char *FnName = 0;
3487 if (DestTy == MVT::f32)
3488 FnName = "__floatdisf";
3489 else {
3490 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3491 FnName = "__floatdidf";
3492 }
Chris Lattner462505f2006-02-13 09:18:02 +00003493
3494 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3495 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003496 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003497}
Misha Brukman835702a2005-04-21 22:36:52 +00003498
Chris Lattner689bdcc2006-01-28 08:25:58 +00003499/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3500/// INT_TO_FP operation of the specified operand when the target requests that
3501/// we expand it. At this point, we know that the result and operand types are
3502/// legal for the target.
3503SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3504 SDOperand Op0,
3505 MVT::ValueType DestVT) {
3506 if (Op0.getValueType() == MVT::i32) {
3507 // simple 32-bit [signed|unsigned] integer to float/double expansion
3508
3509 // get the stack frame index of a 8 byte buffer
3510 MachineFunction &MF = DAG.getMachineFunction();
3511 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3512 // get address of 8 byte buffer
3513 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3514 // word offset constant for Hi/Lo address computation
3515 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3516 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00003517 SDOperand Hi = StackSlot;
3518 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3519 if (TLI.isLittleEndian())
3520 std::swap(Hi, Lo);
3521
Chris Lattner689bdcc2006-01-28 08:25:58 +00003522 // if signed map to unsigned space
3523 SDOperand Op0Mapped;
3524 if (isSigned) {
3525 // constant used to invert sign bit (signed to unsigned mapping)
3526 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3527 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3528 } else {
3529 Op0Mapped = Op0;
3530 }
3531 // store the lo of the constructed double - based on integer input
3532 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3533 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3534 // initial hi portion of constructed double
3535 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3536 // store the hi of the constructed double - biased exponent
3537 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3538 InitialHi, Hi, DAG.getSrcValue(NULL));
3539 // load the constructed double
3540 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3541 DAG.getSrcValue(NULL));
3542 // FP constant to bias correct the final result
3543 SDOperand Bias = DAG.getConstantFP(isSigned ?
3544 BitsToDouble(0x4330000080000000ULL)
3545 : BitsToDouble(0x4330000000000000ULL),
3546 MVT::f64);
3547 // subtract the bias
3548 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3549 // final result
3550 SDOperand Result;
3551 // handle final rounding
3552 if (DestVT == MVT::f64) {
3553 // do nothing
3554 Result = Sub;
3555 } else {
3556 // if f32 then cast to f32
3557 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3558 }
3559 return Result;
3560 }
3561 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3562 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3563
3564 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3565 DAG.getConstant(0, Op0.getValueType()),
3566 ISD::SETLT);
3567 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3568 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3569 SignSet, Four, Zero);
3570
3571 // If the sign bit of the integer is set, the large number will be treated
3572 // as a negative number. To counteract this, the dynamic code adds an
3573 // offset depending on the data type.
3574 uint64_t FF;
3575 switch (Op0.getValueType()) {
3576 default: assert(0 && "Unsupported integer type!");
3577 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3578 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3579 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3580 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3581 }
3582 if (TLI.isLittleEndian()) FF <<= 32;
3583 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3584
3585 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3586 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3587 SDOperand FudgeInReg;
3588 if (DestVT == MVT::f32)
3589 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3590 DAG.getSrcValue(NULL));
3591 else {
3592 assert(DestVT == MVT::f64 && "Unexpected conversion");
3593 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3594 DAG.getEntryNode(), CPIdx,
3595 DAG.getSrcValue(NULL), MVT::f32));
3596 }
3597
3598 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3599}
3600
3601/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3602/// *INT_TO_FP operation of the specified operand when the target requests that
3603/// we promote it. At this point, we know that the result and operand types are
3604/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3605/// operation that takes a larger input.
3606SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3607 MVT::ValueType DestVT,
3608 bool isSigned) {
3609 // First step, figure out the appropriate *INT_TO_FP operation to use.
3610 MVT::ValueType NewInTy = LegalOp.getValueType();
3611
3612 unsigned OpToUse = 0;
3613
3614 // Scan for the appropriate larger type to use.
3615 while (1) {
3616 NewInTy = (MVT::ValueType)(NewInTy+1);
3617 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3618
3619 // If the target supports SINT_TO_FP of this type, use it.
3620 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3621 default: break;
3622 case TargetLowering::Legal:
3623 if (!TLI.isTypeLegal(NewInTy))
3624 break; // Can't use this datatype.
3625 // FALL THROUGH.
3626 case TargetLowering::Custom:
3627 OpToUse = ISD::SINT_TO_FP;
3628 break;
3629 }
3630 if (OpToUse) break;
3631 if (isSigned) continue;
3632
3633 // If the target supports UINT_TO_FP of this type, use it.
3634 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3635 default: break;
3636 case TargetLowering::Legal:
3637 if (!TLI.isTypeLegal(NewInTy))
3638 break; // Can't use this datatype.
3639 // FALL THROUGH.
3640 case TargetLowering::Custom:
3641 OpToUse = ISD::UINT_TO_FP;
3642 break;
3643 }
3644 if (OpToUse) break;
3645
3646 // Otherwise, try a larger type.
3647 }
3648
3649 // Okay, we found the operation and type to use. Zero extend our input to the
3650 // desired type then run the operation on it.
3651 return DAG.getNode(OpToUse, DestVT,
3652 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3653 NewInTy, LegalOp));
3654}
3655
3656/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3657/// FP_TO_*INT operation of the specified operand when the target requests that
3658/// we promote it. At this point, we know that the result and operand types are
3659/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3660/// operation that returns a larger result.
3661SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3662 MVT::ValueType DestVT,
3663 bool isSigned) {
3664 // First step, figure out the appropriate FP_TO*INT operation to use.
3665 MVT::ValueType NewOutTy = DestVT;
3666
3667 unsigned OpToUse = 0;
3668
3669 // Scan for the appropriate larger type to use.
3670 while (1) {
3671 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3672 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3673
3674 // If the target supports FP_TO_SINT returning this type, use it.
3675 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3676 default: break;
3677 case TargetLowering::Legal:
3678 if (!TLI.isTypeLegal(NewOutTy))
3679 break; // Can't use this datatype.
3680 // FALL THROUGH.
3681 case TargetLowering::Custom:
3682 OpToUse = ISD::FP_TO_SINT;
3683 break;
3684 }
3685 if (OpToUse) break;
3686
3687 // If the target supports FP_TO_UINT of this type, use it.
3688 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3689 default: break;
3690 case TargetLowering::Legal:
3691 if (!TLI.isTypeLegal(NewOutTy))
3692 break; // Can't use this datatype.
3693 // FALL THROUGH.
3694 case TargetLowering::Custom:
3695 OpToUse = ISD::FP_TO_UINT;
3696 break;
3697 }
3698 if (OpToUse) break;
3699
3700 // Otherwise, try a larger type.
3701 }
3702
3703 // Okay, we found the operation and type to use. Truncate the result of the
3704 // extended FP_TO_*INT operation to the desired size.
3705 return DAG.getNode(ISD::TRUNCATE, DestVT,
3706 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3707}
3708
3709/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3710///
3711SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3712 MVT::ValueType VT = Op.getValueType();
3713 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3714 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3715 switch (VT) {
3716 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3717 case MVT::i16:
3718 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3719 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3720 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3721 case MVT::i32:
3722 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3723 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3724 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3725 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3726 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3727 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3728 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3729 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3730 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3731 case MVT::i64:
3732 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3733 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3734 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3735 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3736 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3737 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3738 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3739 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3740 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3741 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3742 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3743 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3744 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3745 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3746 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3747 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3748 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3749 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3750 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3751 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3752 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3753 }
3754}
3755
3756/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3757///
3758SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3759 switch (Opc) {
3760 default: assert(0 && "Cannot expand this yet!");
3761 case ISD::CTPOP: {
3762 static const uint64_t mask[6] = {
3763 0x5555555555555555ULL, 0x3333333333333333ULL,
3764 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3765 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3766 };
3767 MVT::ValueType VT = Op.getValueType();
3768 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3769 unsigned len = getSizeInBits(VT);
3770 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3771 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3772 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3773 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3774 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3775 DAG.getNode(ISD::AND, VT,
3776 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3777 }
3778 return Op;
3779 }
3780 case ISD::CTLZ: {
3781 // for now, we do this:
3782 // x = x | (x >> 1);
3783 // x = x | (x >> 2);
3784 // ...
3785 // x = x | (x >>16);
3786 // x = x | (x >>32); // for 64-bit input
3787 // return popcount(~x);
3788 //
3789 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3790 MVT::ValueType VT = Op.getValueType();
3791 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3792 unsigned len = getSizeInBits(VT);
3793 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3794 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3795 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3796 }
3797 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3798 return DAG.getNode(ISD::CTPOP, VT, Op);
3799 }
3800 case ISD::CTTZ: {
3801 // for now, we use: { return popcount(~x & (x - 1)); }
3802 // unless the target has ctlz but not ctpop, in which case we use:
3803 // { return 32 - nlz(~x & (x-1)); }
3804 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3805 MVT::ValueType VT = Op.getValueType();
3806 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3807 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3808 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3809 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3810 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3811 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3812 TLI.isOperationLegal(ISD::CTLZ, VT))
3813 return DAG.getNode(ISD::SUB, VT,
3814 DAG.getConstant(getSizeInBits(VT), VT),
3815 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3816 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3817 }
3818 }
3819}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003820
3821
Chris Lattnerdc750592005-01-07 07:47:09 +00003822/// ExpandOp - Expand the specified SDOperand into its two component pieces
3823/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3824/// LegalizeNodes map is filled in for any results that are not expanded, the
3825/// ExpandedNodes map is filled in for any results that are expanded, and the
3826/// Lo/Hi values are returned.
3827void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3828 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003829 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003830 SDNode *Node = Op.Val;
3831 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003832 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3833 "Cannot expand FP values!");
3834 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003835 "Cannot expand to FP value or to larger int value!");
3836
Chris Lattner1a570f12005-09-02 20:32:45 +00003837 // See if we already expanded it.
3838 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3839 = ExpandedNodes.find(Op);
3840 if (I != ExpandedNodes.end()) {
3841 Lo = I->second.first;
3842 Hi = I->second.second;
3843 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003844 }
3845
Chris Lattnerdc750592005-01-07 07:47:09 +00003846 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003847 case ISD::CopyFromReg:
3848 assert(0 && "CopyFromReg must be legal!");
3849 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003850 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3851 assert(0 && "Do not know how to expand this operator!");
3852 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003853 case ISD::UNDEF:
3854 Lo = DAG.getNode(ISD::UNDEF, NVT);
3855 Hi = DAG.getNode(ISD::UNDEF, NVT);
3856 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003857 case ISD::Constant: {
3858 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3859 Lo = DAG.getConstant(Cst, NVT);
3860 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3861 break;
3862 }
Chris Lattner32e08b72005-03-28 22:03:13 +00003863 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003864 // Return the operands.
3865 Lo = Node->getOperand(0);
3866 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003867 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003868
3869 case ISD::SIGN_EXTEND_INREG:
3870 ExpandOp(Node->getOperand(0), Lo, Hi);
3871 // Sign extend the lo-part.
3872 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3873 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3874 TLI.getShiftAmountTy()));
3875 // sext_inreg the low part if needed.
3876 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3877 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003878
Nate Begeman2fba8a32006-01-14 03:14:10 +00003879 case ISD::BSWAP: {
3880 ExpandOp(Node->getOperand(0), Lo, Hi);
3881 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3882 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3883 Lo = TempLo;
3884 break;
3885 }
3886
Chris Lattner55e9cde2005-05-11 04:51:16 +00003887 case ISD::CTPOP:
3888 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003889 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3890 DAG.getNode(ISD::CTPOP, NVT, Lo),
3891 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003892 Hi = DAG.getConstant(0, NVT);
3893 break;
3894
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003895 case ISD::CTLZ: {
3896 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003897 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003898 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3899 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003900 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3901 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003902 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3903 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3904
3905 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3906 Hi = DAG.getConstant(0, NVT);
3907 break;
3908 }
3909
3910 case ISD::CTTZ: {
3911 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003912 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003913 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3914 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003915 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3916 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003917 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3918 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3919
3920 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3921 Hi = DAG.getConstant(0, NVT);
3922 break;
3923 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003924
Nate Begemane74795c2006-01-25 18:21:52 +00003925 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003926 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3927 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003928 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3929 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3930
3931 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003932 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003933 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3934 if (!TLI.isLittleEndian())
3935 std::swap(Lo, Hi);
3936 break;
3937 }
3938
Chris Lattnerdc750592005-01-07 07:47:09 +00003939 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003940 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3941 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003942 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003943
3944 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003945 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003946 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3947 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003948 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003949 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003950
3951 // Build a factor node to remember that this load is independent of the
3952 // other one.
3953 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3954 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003955
Chris Lattnerdc750592005-01-07 07:47:09 +00003956 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003957 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003958 if (!TLI.isLittleEndian())
3959 std::swap(Lo, Hi);
3960 break;
3961 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003962 case ISD::AND:
3963 case ISD::OR:
3964 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3965 SDOperand LL, LH, RL, RH;
3966 ExpandOp(Node->getOperand(0), LL, LH);
3967 ExpandOp(Node->getOperand(1), RL, RH);
3968 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3969 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3970 break;
3971 }
3972 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003973 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003974 ExpandOp(Node->getOperand(1), LL, LH);
3975 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003976 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3977 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003978 break;
3979 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003980 case ISD::SELECT_CC: {
3981 SDOperand TL, TH, FL, FH;
3982 ExpandOp(Node->getOperand(2), TL, TH);
3983 ExpandOp(Node->getOperand(3), FL, FH);
3984 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3985 Node->getOperand(1), TL, FL, Node->getOperand(4));
3986 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3987 Node->getOperand(1), TH, FH, Node->getOperand(4));
3988 break;
3989 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003990 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003991 SDOperand Chain = Node->getOperand(0);
3992 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003993 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3994
3995 if (EVT == NVT)
3996 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3997 else
3998 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3999 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004000
4001 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004002 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004003
Nate Begemanc3a89c52005-10-13 17:15:37 +00004004 // The high part is obtained by SRA'ing all but one of the bits of the lo
4005 // part.
4006 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4007 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4008 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00004009 break;
4010 }
4011 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004012 SDOperand Chain = Node->getOperand(0);
4013 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004014 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4015
4016 if (EVT == NVT)
4017 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4018 else
4019 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4020 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004021
4022 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004023 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004024
Nate Begemanc3a89c52005-10-13 17:15:37 +00004025 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004026 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004027 break;
4028 }
4029 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004030 SDOperand Chain = Node->getOperand(0);
4031 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00004032 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4033
4034 if (EVT == NVT)
4035 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4036 else
4037 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4038 EVT);
4039
4040 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004041 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004042
4043 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00004044 Hi = DAG.getNode(ISD::UNDEF, NVT);
4045 break;
4046 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004047 case ISD::ANY_EXTEND:
4048 // The low part is any extension of the input (which degenerates to a copy).
4049 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4050 // The high part is undefined.
4051 Hi = DAG.getNode(ISD::UNDEF, NVT);
4052 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004053 case ISD::SIGN_EXTEND: {
4054 // The low part is just a sign extension of the input (which degenerates to
4055 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004056 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004057
Chris Lattnerdc750592005-01-07 07:47:09 +00004058 // The high part is obtained by SRA'ing all but one of the bits of the lo
4059 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004060 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004061 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4062 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004063 break;
4064 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004065 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004066 // The low part is just a zero extension of the input (which degenerates to
4067 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004068 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004069
Chris Lattnerdc750592005-01-07 07:47:09 +00004070 // The high part is just a zero.
4071 Hi = DAG.getConstant(0, NVT);
4072 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004073
4074 case ISD::BIT_CONVERT: {
4075 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4076 Node->getOperand(0));
4077 ExpandOp(Tmp, Lo, Hi);
4078 break;
4079 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004080
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004081 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004082 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4083 TargetLowering::Custom &&
4084 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004085 Lo = TLI.LowerOperation(Op, DAG);
4086 assert(Lo.Val && "Node must be custom expanded!");
4087 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004088 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004089 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004090 break;
4091
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004092 // These operators cannot be expanded directly, emit them as calls to
4093 // library functions.
4094 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004095 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004096 SDOperand Op;
4097 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4098 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004099 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4100 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004101 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004102
Chris Lattner941d84a2005-07-30 01:40:57 +00004103 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4104
Chris Lattnerfe68d752005-07-29 00:33:32 +00004105 // Now that the custom expander is done, expand the result, which is still
4106 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004107 if (Op.Val) {
4108 ExpandOp(Op, Lo, Hi);
4109 break;
4110 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004111 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004112
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004113 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004114 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004115 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004116 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004117 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004118
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004119 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004120 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004121 SDOperand Op;
4122 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4123 case Expand: assert(0 && "cannot expand FP!");
4124 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4125 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4126 }
4127
4128 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4129
4130 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004131 if (Op.Val) {
4132 ExpandOp(Op, Lo, Hi);
4133 break;
4134 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004135 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004136
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004137 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004138 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004139 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004140 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004141 break;
4142
Evan Cheng870e4f82006-01-09 18:31:59 +00004143 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004144 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004145 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004146 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004147 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004148 Op = TLI.LowerOperation(Op, DAG);
4149 if (Op.Val) {
4150 // Now that the custom expander is done, expand the result, which is
4151 // still VT.
4152 ExpandOp(Op, Lo, Hi);
4153 break;
4154 }
4155 }
4156
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004157 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004158 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004159 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004160
4161 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004162 TargetLowering::LegalizeAction Action =
4163 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4164 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4165 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004166 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004167 break;
4168 }
4169
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004170 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004171 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004172 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004173 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004174
Evan Cheng870e4f82006-01-09 18:31:59 +00004175 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004176 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004177 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004178 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004179 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004180 Op = TLI.LowerOperation(Op, DAG);
4181 if (Op.Val) {
4182 // Now that the custom expander is done, expand the result, which is
4183 // still VT.
4184 ExpandOp(Op, Lo, Hi);
4185 break;
4186 }
4187 }
4188
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004189 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004190 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004191 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004192
4193 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004194 TargetLowering::LegalizeAction Action =
4195 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4196 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4197 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004198 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004199 break;
4200 }
4201
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004202 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004203 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004204 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004205 }
4206
4207 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004208 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004209 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004210 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004211 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004212 Op = TLI.LowerOperation(Op, DAG);
4213 if (Op.Val) {
4214 // Now that the custom expander is done, expand the result, which is
4215 // still VT.
4216 ExpandOp(Op, Lo, Hi);
4217 break;
4218 }
4219 }
4220
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004221 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004222 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004223 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004224
4225 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004226 TargetLowering::LegalizeAction Action =
4227 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4228 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4229 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004230 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004231 break;
4232 }
4233
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004234 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004235 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004236 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004237 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004238
Misha Brukman835702a2005-04-21 22:36:52 +00004239 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004240 case ISD::SUB: {
4241 // If the target wants to custom expand this, let them.
4242 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4243 TargetLowering::Custom) {
4244 Op = TLI.LowerOperation(Op, DAG);
4245 if (Op.Val) {
4246 ExpandOp(Op, Lo, Hi);
4247 break;
4248 }
4249 }
4250
4251 // Expand the subcomponents.
4252 SDOperand LHSL, LHSH, RHSL, RHSH;
4253 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4254 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004255 std::vector<MVT::ValueType> VTs;
4256 std::vector<SDOperand> LoOps, HiOps;
4257 VTs.push_back(LHSL.getValueType());
4258 VTs.push_back(MVT::Flag);
4259 LoOps.push_back(LHSL);
4260 LoOps.push_back(RHSL);
4261 HiOps.push_back(LHSH);
4262 HiOps.push_back(RHSH);
4263 if (Node->getOpcode() == ISD::ADD) {
4264 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4265 HiOps.push_back(Lo.getValue(1));
4266 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4267 } else {
4268 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4269 HiOps.push_back(Lo.getValue(1));
4270 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4271 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004272 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004273 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004274 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004275 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004276 SDOperand LL, LH, RL, RH;
4277 ExpandOp(Node->getOperand(0), LL, LH);
4278 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004279 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4280 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4281 // extended the sign bit of the low half through the upper half, and if so
4282 // emit a MULHS instead of the alternate sequence that is valid for any
4283 // i64 x i64 multiply.
4284 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4285 // is RH an extension of the sign bit of RL?
4286 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4287 RH.getOperand(1).getOpcode() == ISD::Constant &&
4288 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4289 // is LH an extension of the sign bit of LL?
4290 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4291 LH.getOperand(1).getOpcode() == ISD::Constant &&
4292 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4293 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4294 } else {
4295 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4296 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4297 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4298 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4299 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4300 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004301 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4302 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004303 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004304 }
4305 break;
4306 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004307 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4308 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4309 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4310 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004311 }
4312
Chris Lattnerac12f682005-12-21 18:02:52 +00004313 // Make sure the resultant values have been legalized themselves, unless this
4314 // is a type that requires multi-step expansion.
4315 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4316 Lo = LegalizeOp(Lo);
4317 Hi = LegalizeOp(Hi);
4318 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004319
4320 // Remember in a map if the values will be reused later.
4321 bool isNew =
4322 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4323 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004324}
4325
Chris Lattner32206f52006-03-18 01:44:44 +00004326/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4327/// two smaller values of MVT::Vector type.
4328void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4329 SDOperand &Hi) {
4330 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4331 SDNode *Node = Op.Val;
4332 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4333 assert(NumElements > 1 && "Cannot split a single element vector!");
4334 unsigned NewNumElts = NumElements/2;
4335 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4336 SDOperand TypeNode = *(Node->op_end()-1);
4337
4338 // See if we already split it.
4339 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4340 = SplitNodes.find(Op);
4341 if (I != SplitNodes.end()) {
4342 Lo = I->second.first;
4343 Hi = I->second.second;
4344 return;
4345 }
4346
4347 switch (Node->getOpcode()) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004348 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004349 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004350 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4351 LoOps.push_back(NewNumEltsNode);
4352 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004353 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004354
4355 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4356 HiOps.push_back(NewNumEltsNode);
4357 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004358 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004359 break;
4360 }
4361 case ISD::VADD:
4362 case ISD::VSUB:
4363 case ISD::VMUL:
4364 case ISD::VSDIV:
4365 case ISD::VUDIV:
4366 case ISD::VAND:
4367 case ISD::VOR:
4368 case ISD::VXOR: {
4369 SDOperand LL, LH, RL, RH;
4370 SplitVectorOp(Node->getOperand(0), LL, LH);
4371 SplitVectorOp(Node->getOperand(1), RL, RH);
4372
4373 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4374 NewNumEltsNode, TypeNode);
4375 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4376 NewNumEltsNode, TypeNode);
4377 break;
4378 }
4379 case ISD::VLOAD: {
4380 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4381 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4382 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4383
4384 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4385 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4386 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4387 getIntPtrConstant(IncrementSize));
4388 // FIXME: This creates a bogus srcvalue!
4389 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4390
4391 // Build a factor node to remember that this load is independent of the
4392 // other one.
4393 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4394 Hi.getValue(1));
4395
4396 // Remember that we legalized the chain.
4397 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4398 if (!TLI.isLittleEndian())
4399 std::swap(Lo, Hi);
4400 break;
4401 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004402 case ISD::VBIT_CONVERT: {
4403 // We know the result is a vector. The input may be either a vector or a
4404 // scalar value.
4405 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4406 // Lower to a store/load. FIXME: this could be improved probably.
4407 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4408
4409 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4410 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4411 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4412 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4413 SplitVectorOp(St, Lo, Hi);
4414 } else {
4415 // If the input is a vector type, we have to either scalarize it, pack it
4416 // or convert it based on whether the input vector type is legal.
4417 SDNode *InVal = Node->getOperand(0).Val;
4418 unsigned NumElems =
4419 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4420 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4421
4422 // If the input is from a single element vector, scalarize the vector,
4423 // then treat like a scalar.
4424 if (NumElems == 1) {
4425 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4426 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4427 Op.getOperand(1), Op.getOperand(2));
4428 SplitVectorOp(Scalar, Lo, Hi);
4429 } else {
4430 // Split the input vector.
4431 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4432
4433 // Convert each of the pieces now.
4434 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4435 NewNumEltsNode, TypeNode);
4436 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4437 NewNumEltsNode, TypeNode);
4438 }
4439 break;
4440 }
4441 }
Chris Lattner32206f52006-03-18 01:44:44 +00004442 }
4443
4444 // Remember in a map if the values will be reused later.
4445 bool isNew =
4446 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4447 assert(isNew && "Value already expanded?!?");
4448}
4449
4450
4451/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4452/// equivalent operation that returns a scalar (e.g. F32) or packed value
4453/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4454/// type for the result.
4455SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4456 MVT::ValueType NewVT) {
Chris Lattner672a42d2006-03-21 19:20:37 +00004457 // FIXME: THIS IS A TEMPORARY HACK
4458 if (Op.getValueType() == NewVT) return Op;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00004459
Chris Lattner32206f52006-03-18 01:44:44 +00004460 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4461 SDNode *Node = Op.Val;
4462
4463 // See if we already packed it.
4464 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4465 if (I != PackedNodes.end()) return I->second;
4466
4467 SDOperand Result;
4468 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004469 default:
4470 Node->dump(); std::cerr << "\n";
4471 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004472 case ISD::VADD:
4473 case ISD::VSUB:
4474 case ISD::VMUL:
4475 case ISD::VSDIV:
4476 case ISD::VUDIV:
4477 case ISD::VAND:
4478 case ISD::VOR:
4479 case ISD::VXOR:
4480 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4481 NewVT,
4482 PackVectorOp(Node->getOperand(0), NewVT),
4483 PackVectorOp(Node->getOperand(1), NewVT));
4484 break;
4485 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004486 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4487 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004488
Chris Lattner93640542006-03-19 00:07:49 +00004489 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004490
4491 // Remember that we legalized the chain.
4492 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4493 break;
4494 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004495 case ISD::VBUILD_VECTOR:
Chris Lattner32206f52006-03-18 01:44:44 +00004496 if (!MVT::isVector(NewVT)) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004497 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004498 Result = Node->getOperand(0);
4499 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004500 // Returning a BUILD_VECTOR?
Chris Lattner32206f52006-03-18 01:44:44 +00004501 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004502 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattner32206f52006-03-18 01:44:44 +00004503 }
4504 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004505 case ISD::VINSERT_VECTOR_ELT:
4506 if (!MVT::isVector(NewVT)) {
4507 // Returning a scalar? Must be the inserted element.
4508 Result = Node->getOperand(1);
4509 } else {
4510 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4511 PackVectorOp(Node->getOperand(0), NewVT),
4512 Node->getOperand(1), Node->getOperand(2));
4513 }
4514 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00004515 case ISD::VBIT_CONVERT:
4516 if (Op.getOperand(0).getValueType() != MVT::Vector)
4517 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4518 else {
4519 // If the input is a vector type, we have to either scalarize it, pack it
4520 // or convert it based on whether the input vector type is legal.
4521 SDNode *InVal = Node->getOperand(0).Val;
4522 unsigned NumElems =
4523 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4524 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4525
4526 // Figure out if there is a Packed type corresponding to this Vector
4527 // type. If so, convert to the packed type.
4528 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4529 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4530 // Turn this into a bit convert of the packed input.
4531 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4532 PackVectorOp(Node->getOperand(0), TVT));
4533 break;
4534 } else if (NumElems == 1) {
4535 // Turn this into a bit convert of the scalar input.
4536 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4537 PackVectorOp(Node->getOperand(0), EVT));
4538 break;
4539 } else {
4540 // FIXME: UNIMP!
4541 assert(0 && "Cast from unsupported vector type not implemented yet!");
4542 }
4543 }
Chris Lattner32206f52006-03-18 01:44:44 +00004544 }
4545
4546 if (TLI.isTypeLegal(NewVT))
4547 Result = LegalizeOp(Result);
4548 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4549 assert(isNew && "Value already packed?");
4550 return Result;
4551}
4552
Chris Lattnerdc750592005-01-07 07:47:09 +00004553
4554// SelectionDAG::Legalize - This is the entry point for the file.
4555//
Chris Lattner4add7e32005-01-23 04:42:50 +00004556void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004557 /// run - This is the main entry point to this class.
4558 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004559 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004560}
4561