blob: 6c2b188b38f18f093a5cd4d24e7e8d8b776c7463 [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>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
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 Lattner435b4022005-11-29 06:21:05 +0000555
556 case ISD::LOCATION:
557 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
558 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
559
560 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
561 case TargetLowering::Promote:
562 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000563 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000564 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000565 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
566 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
567
568 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
569 const std::string &FName =
570 cast<StringSDNode>(Node->getOperand(3))->getValue();
571 const std::string &DirName =
572 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000573 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000574
Jim Laskey9e296be2005-12-21 20:51:37 +0000575 std::vector<SDOperand> Ops;
576 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000577 SDOperand LineOp = Node->getOperand(1);
578 SDOperand ColOp = Node->getOperand(2);
579
580 if (useDEBUG_LOC) {
581 Ops.push_back(LineOp); // line #
582 Ops.push_back(ColOp); // col #
583 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
584 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
585 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000586 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
587 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000588 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
589 Ops.push_back(DAG.getConstant(ID, MVT::i32));
590 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
591 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000592 } else {
593 Result = Tmp1; // chain
594 }
Chris Lattner435b4022005-11-29 06:21:05 +0000595 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000596 }
Chris Lattner435b4022005-11-29 06:21:05 +0000597 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000598 if (Tmp1 != Node->getOperand(0) ||
599 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000600 std::vector<SDOperand> Ops;
601 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000602 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
603 Ops.push_back(Node->getOperand(1)); // line # must be legal.
604 Ops.push_back(Node->getOperand(2)); // col # must be legal.
605 } else {
606 // Otherwise promote them.
607 Ops.push_back(PromoteOp(Node->getOperand(1)));
608 Ops.push_back(PromoteOp(Node->getOperand(2)));
609 }
Chris Lattner435b4022005-11-29 06:21:05 +0000610 Ops.push_back(Node->getOperand(3)); // filename must be legal.
611 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000612 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000613 }
614 break;
615 }
616 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000617
618 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000619 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000620 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000621 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000622 case TargetLowering::Legal:
623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
624 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
625 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
626 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000628 break;
629 }
630 break;
631
632 case ISD::DEBUG_LABEL:
633 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
634 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000635 default: assert(0 && "This action is not supported yet!");
636 case TargetLowering::Legal:
637 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
638 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000640 break;
641 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000642 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000643
Chris Lattnerdc750592005-01-07 07:47:09 +0000644 case ISD::Constant:
645 // We know we don't need to expand constants here, constants only have one
646 // value and we check that it is fine above.
647
648 // FIXME: Maybe we should handle things like targets that don't support full
649 // 32-bit immediates?
650 break;
651 case ISD::ConstantFP: {
652 // Spill FP immediates to the constant pool if the target cannot directly
653 // codegen them. Targets often have some immediate values that can be
654 // efficiently generated into an FP register without a load. We explicitly
655 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000656 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
657
658 // Check to see if this FP immediate is already legal.
659 bool isLegal = false;
660 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
661 E = TLI.legal_fpimm_end(); I != E; ++I)
662 if (CFP->isExactlyValue(*I)) {
663 isLegal = true;
664 break;
665 }
666
Chris Lattner758b0ac2006-01-29 06:26:56 +0000667 // If this is a legal constant, turn it into a TargetConstantFP node.
668 if (isLegal) {
669 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
670 break;
671 }
672
673 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
674 default: assert(0 && "This action is not supported yet!");
675 case TargetLowering::Custom:
676 Tmp3 = TLI.LowerOperation(Result, DAG);
677 if (Tmp3.Val) {
678 Result = Tmp3;
679 break;
680 }
681 // FALLTHROUGH
682 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000683 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000684 bool Extend = false;
685
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000686 // If a FP immediate is precise when represented as a float and if the
687 // target can do an extending load from float to double, we put it into
688 // the constant pool as a float, even if it's is statically typed as a
689 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000690 MVT::ValueType VT = CFP->getValueType(0);
691 bool isDouble = VT == MVT::f64;
692 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
693 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000694 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
695 // Only do this if the target has a native EXTLOAD instruction from
696 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000697 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000698 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
699 VT = MVT::f32;
700 Extend = true;
701 }
Misha Brukman835702a2005-04-21 22:36:52 +0000702
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000703 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000704 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000705 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
706 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000707 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000708 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
709 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000710 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000711 }
712 break;
713 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000714 case ISD::TokenFactor:
715 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000716 Tmp1 = LegalizeOp(Node->getOperand(0));
717 Tmp2 = LegalizeOp(Node->getOperand(1));
718 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
719 } else if (Node->getNumOperands() == 3) {
720 Tmp1 = LegalizeOp(Node->getOperand(0));
721 Tmp2 = LegalizeOp(Node->getOperand(1));
722 Tmp3 = LegalizeOp(Node->getOperand(2));
723 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000724 } else {
725 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000726 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000727 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
728 Ops.push_back(LegalizeOp(Node->getOperand(i)));
729 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000730 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000731 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000732
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000733 case ISD::BUILD_VECTOR:
734 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000735 default: assert(0 && "This action is not supported yet!");
736 case TargetLowering::Custom:
737 Tmp3 = TLI.LowerOperation(Result, DAG);
738 if (Tmp3.Val) {
739 Result = Tmp3;
740 break;
741 }
742 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000743 case TargetLowering::Expand:
744 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000745 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000746 }
Chris Lattner29b23012006-03-19 01:17:20 +0000747 break;
748 case ISD::INSERT_VECTOR_ELT:
749 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
750 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
751 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
752 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
753
754 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
755 Node->getValueType(0))) {
756 default: assert(0 && "This action is not supported yet!");
757 case TargetLowering::Legal:
758 break;
759 case TargetLowering::Custom:
760 Tmp3 = TLI.LowerOperation(Result, DAG);
761 if (Tmp3.Val) {
762 Result = Tmp3;
763 break;
764 }
765 // FALLTHROUGH
766 case TargetLowering::Expand: {
767 // If the target doesn't support this, we have to spill the input vector
768 // to a temporary stack slot, update the element, then reload it. This is
769 // badness. We could also load the value into a vector register (either
770 // with a "move to register" or "extload into register" instruction, then
771 // permute it into place, if the idx is a constant and if the idx is
772 // supported by the target.
773 assert(0 && "INSERT_VECTOR_ELT expand not supported yet!");
774 break;
775 }
776 }
777 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000778 case ISD::SCALAR_TO_VECTOR:
779 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
780 Result = DAG.UpdateNodeOperands(Result, Tmp1);
781 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
782 Node->getValueType(0))) {
783 default: assert(0 && "This action is not supported yet!");
784 case TargetLowering::Legal:
785 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000786 case TargetLowering::Custom:
787 Tmp3 = TLI.LowerOperation(Result, DAG);
788 if (Tmp3.Val) {
789 Result = Tmp3;
790 break;
791 }
792 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000793 case TargetLowering::Expand: {
794 // If the target doesn't support this, store the value to a temporary
795 // stack slot, then EXTLOAD the vector back out.
Chris Lattner79fb91c2006-03-19 06:47:21 +0000796 // TODO: If a target doesn't support this, create a stack slot for the
797 // whole vector, then store into it, then load the whole vector.
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000798 SDOperand StackPtr =
799 CreateStackTemporary(Node->getOperand(0).getValueType());
800 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
801 Node->getOperand(0), StackPtr,
802 DAG.getSrcValue(NULL));
803 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
804 DAG.getSrcValue(NULL),
805 Node->getOperand(0).getValueType());
806 break;
807 }
808 }
809 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000810 case ISD::VECTOR_SHUFFLE:
811 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
812 "vector shuffle should not be created if not legal!");
813 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
814 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
816
817 // Allow targets to custom lower the SHUFFLEs they support.
818 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
819 == TargetLowering::Custom) {
820 Tmp1 = TLI.LowerOperation(Result, DAG);
821 if (Tmp1.Val) Result = Tmp1;
822 }
823 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000824
825 case ISD::EXTRACT_VECTOR_ELT:
826 Tmp1 = LegalizeOp(Node->getOperand(0));
827 Tmp2 = LegalizeOp(Node->getOperand(1));
828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000829
830 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
831 Tmp1.getValueType())) {
832 default: assert(0 && "This action is not supported yet!");
833 case TargetLowering::Legal:
834 break;
835 case TargetLowering::Custom:
836 Tmp3 = TLI.LowerOperation(Result, DAG);
837 if (Tmp3.Val) {
838 Result = Tmp3;
839 break;
840 }
841 // FALLTHROUGH
842 case TargetLowering::Expand: {
843 // If the target doesn't support this, store the value to a temporary
844 // stack slot, then LOAD the scalar element back out.
845 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
846 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
847 Tmp1, StackPtr, DAG.getSrcValue(NULL));
848
849 // Add the offset to the index.
850 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
851 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
852 DAG.getConstant(EltSize, Tmp2.getValueType()));
853 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
854
855 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
856 DAG.getSrcValue(NULL));
857 break;
858 }
859 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000860 break;
861
862 case ISD::VEXTRACT_VECTOR_ELT:
863 // We know that operand #0 is the Vec vector. If the index is a constant
864 // or if the invec is a supported hardware type, we can use it. Otherwise,
865 // lower to a store then an indexed load.
866 Tmp1 = Node->getOperand(0);
867 Tmp2 = LegalizeOp(Node->getOperand(1));
868
869 SDNode *InVal = Tmp1.Val;
870 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
871 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
872
873 // Figure out if there is a Packed type corresponding to this Vector
874 // type. If so, convert to the packed type.
875 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
876 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
877 // Turn this into a packed extract_vector_elt operation.
878 Tmp1 = PackVectorOp(Tmp1, TVT);
879 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Node->getValueType(0),
880 Tmp1, Tmp2);
881 break;
882 } else if (NumElems == 1) {
883 // This must be an access of the only element.
884 Result = PackVectorOp(Tmp1, EVT);
885 break;
886 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Tmp2)) {
887 SDOperand Lo, Hi;
888 SplitVectorOp(Tmp1, Lo, Hi);
889 if (CIdx->getValue() < NumElems/2) {
890 Tmp1 = Lo;
891 } else {
892 Tmp1 = Hi;
893 Tmp2 = DAG.getConstant(CIdx->getValue() - NumElems/2,
894 Tmp2.getValueType());
895 }
896
897 // It's now an extract from the appropriate high or low part.
898 Result = LegalizeOp(DAG.UpdateNodeOperands(Result, Tmp1, Tmp2));
899 } else {
900 // FIXME: IMPLEMENT STORE/LOAD lowering.
901 assert(0 && "unimp!");
902 }
903 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000904
Chris Lattner462505f2006-02-13 09:18:02 +0000905 case ISD::CALLSEQ_START: {
906 SDNode *CallEnd = FindCallEndFromCallStart(Node);
907
908 // Recursively Legalize all of the inputs of the call end that do not lead
909 // to this call start. This ensures that any libcalls that need be inserted
910 // are inserted *before* the CALLSEQ_START.
911 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
912 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
913
914 // Now that we legalized all of the inputs (which may have inserted
915 // libcalls) create the new CALLSEQ_START node.
916 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
917
918 // Merge in the last call, to ensure that this call start after the last
919 // call ended.
920 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
921 Tmp1 = LegalizeOp(Tmp1);
922
923 // Do not try to legalize the target-specific arguments (#1+).
924 if (Tmp1 != Node->getOperand(0)) {
925 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
926 Ops[0] = Tmp1;
927 Result = DAG.UpdateNodeOperands(Result, Ops);
928 }
929
930 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000931 AddLegalizedOperand(Op.getValue(0), Result);
932 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
933 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
934
Chris Lattner462505f2006-02-13 09:18:02 +0000935 // Now that the callseq_start and all of the non-call nodes above this call
936 // sequence have been legalized, legalize the call itself. During this
937 // process, no libcalls can/will be inserted, guaranteeing that no calls
938 // can overlap.
939 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
940 SDOperand InCallSEQ = LastCALLSEQ_END;
941 // Note that we are selecting this call!
942 LastCALLSEQ_END = SDOperand(CallEnd, 0);
943 IsLegalizingCall = true;
944
945 // Legalize the call, starting from the CALLSEQ_END.
946 LegalizeOp(LastCALLSEQ_END);
947 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
948 return Result;
949 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000950 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +0000951 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
952 // will cause this node to be legalized as well as handling libcalls right.
953 if (LastCALLSEQ_END.Val != Node) {
954 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
955 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
956 assert(I != LegalizedNodes.end() &&
957 "Legalizing the call start should have legalized this node!");
958 return I->second;
959 }
960
961 // Otherwise, the call start has been legalized and everything is going
962 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +0000963 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000964 // Do not try to legalize the target-specific arguments (#1+), except for
965 // an optional flag input.
966 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
967 if (Tmp1 != Node->getOperand(0)) {
968 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
969 Ops[0] = Tmp1;
970 Result = DAG.UpdateNodeOperands(Result, Ops);
971 }
972 } else {
973 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
974 if (Tmp1 != Node->getOperand(0) ||
975 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
976 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
977 Ops[0] = Tmp1;
978 Ops.back() = Tmp2;
979 Result = DAG.UpdateNodeOperands(Result, Ops);
980 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +0000981 }
Chris Lattner8e2ee732006-02-14 00:55:02 +0000982 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +0000983 // This finishes up call legalization.
984 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +0000985
986 // If the CALLSEQ_END node has a flag, remember that we legalized it.
987 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
988 if (Node->getNumValues() == 2)
989 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
990 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000991 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +0000992 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
993 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
994 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000995 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +0000996
Chris Lattnerd02b0542006-01-28 10:58:55 +0000997 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +0000998 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000999 switch (TLI.getOperationAction(Node->getOpcode(),
1000 Node->getValueType(0))) {
1001 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001002 case TargetLowering::Expand: {
1003 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1004 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1005 " not tell us which reg is the stack pointer!");
1006 SDOperand Chain = Tmp1.getOperand(0);
1007 SDOperand Size = Tmp2.getOperand(1);
1008 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1009 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1010 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001011 Tmp1 = LegalizeOp(Tmp1);
1012 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001013 break;
1014 }
1015 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001016 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1017 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001018 Tmp1 = LegalizeOp(Tmp3);
1019 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001020 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001021 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001022 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001023 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001024 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001025 // Since this op produce two values, make sure to remember that we
1026 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001027 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1028 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001029 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001030 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001031 case ISD::INLINEASM:
1032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1033 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001034 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001035 Tmp2 = Tmp3 = SDOperand(0, 0);
1036 else
1037 Tmp3 = LegalizeOp(Tmp2);
1038
1039 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1040 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1041 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001042 if (Tmp3.Val) Ops.back() = Tmp3;
1043 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001044 }
1045
1046 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001047 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001048 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1049 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001050 case ISD::BR:
1051 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001052 // Ensure that libcalls are emitted before a branch.
1053 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1054 Tmp1 = LegalizeOp(Tmp1);
1055 LastCALLSEQ_END = DAG.getEntryNode();
1056
Chris Lattnerd02b0542006-01-28 10:58:55 +00001057 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001058 break;
1059
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001060 case ISD::BRCOND:
1061 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001062 // Ensure that libcalls are emitted before a return.
1063 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1064 Tmp1 = LegalizeOp(Tmp1);
1065 LastCALLSEQ_END = DAG.getEntryNode();
1066
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001067 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1068 case Expand: assert(0 && "It's impossible to expand bools");
1069 case Legal:
1070 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1071 break;
1072 case Promote:
1073 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1074 break;
1075 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001076
1077 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001079
1080 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1081 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001082 case TargetLowering::Legal: break;
1083 case TargetLowering::Custom:
1084 Tmp1 = TLI.LowerOperation(Result, DAG);
1085 if (Tmp1.Val) Result = Tmp1;
1086 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001087 case TargetLowering::Expand:
1088 // Expand brcond's setcc into its constituent parts and create a BR_CC
1089 // Node.
1090 if (Tmp2.getOpcode() == ISD::SETCC) {
1091 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1092 Tmp2.getOperand(0), Tmp2.getOperand(1),
1093 Node->getOperand(2));
1094 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001095 // Make sure the condition is either zero or one. It may have been
1096 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001097 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1098 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1099 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001100
Nate Begeman371e4952005-08-16 19:49:35 +00001101 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1102 DAG.getCondCode(ISD::SETNE), Tmp2,
1103 DAG.getConstant(0, Tmp2.getValueType()),
1104 Node->getOperand(2));
1105 }
1106 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001107 }
1108 break;
1109 case ISD::BR_CC:
1110 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001111 // Ensure that libcalls are emitted before a branch.
1112 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1113 Tmp1 = LegalizeOp(Tmp1);
1114 LastCALLSEQ_END = DAG.getEntryNode();
1115
Nate Begeman7e7f4392006-02-01 07:19:44 +00001116 Tmp2 = Node->getOperand(2); // LHS
1117 Tmp3 = Node->getOperand(3); // RHS
1118 Tmp4 = Node->getOperand(1); // CC
1119
1120 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1121
1122 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1123 // the LHS is a legal SETCC itself. In this case, we need to compare
1124 // the result against zero to select between true and false values.
1125 if (Tmp3.Val == 0) {
1126 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1127 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001128 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001129
1130 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1131 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001132
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001133 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1134 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001135 case TargetLowering::Legal: break;
1136 case TargetLowering::Custom:
1137 Tmp4 = TLI.LowerOperation(Result, DAG);
1138 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001139 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001140 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001141 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001142 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001143 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1144 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001145
Evan Cheng31d15fa2005-12-23 07:29:34 +00001146 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001147 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1148 Tmp2 = Result.getValue(0);
1149 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001150
Evan Cheng31d15fa2005-12-23 07:29:34 +00001151 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1152 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001153 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001154 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001155 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001156 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001157 Tmp2 = LegalizeOp(Tmp1);
1158 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001159 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001160 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001161 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001162 // Since loads produce two values, make sure to remember that we
1163 // legalized both of them.
1164 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1165 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1166 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001167 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001168 case ISD::EXTLOAD:
1169 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001170 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001171 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1172 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001173
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001174 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001175 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001176 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001177 case TargetLowering::Promote:
1178 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001179 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1180 DAG.getValueType(MVT::i8));
1181 Tmp1 = Result.getValue(0);
1182 Tmp2 = Result.getValue(1);
1183 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001184 case TargetLowering::Custom:
1185 isCustom = true;
1186 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001187 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1189 Node->getOperand(3));
1190 Tmp1 = Result.getValue(0);
1191 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001192
1193 if (isCustom) {
1194 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1195 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001196 Tmp1 = LegalizeOp(Tmp3);
1197 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001198 }
1199 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001200 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001201 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001202 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001203 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1204 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001205 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001206 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1207 Tmp2 = LegalizeOp(Load.getValue(1));
1208 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001209 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001210 assert(Node->getOpcode() != ISD::EXTLOAD &&
1211 "EXTLOAD should always be supported!");
1212 // Turn the unsupported load into an EXTLOAD followed by an explicit
1213 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001214 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1215 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001216 SDOperand ValRes;
1217 if (Node->getOpcode() == ISD::SEXTLOAD)
1218 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001219 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001220 else
1221 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001222 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1223 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1224 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001225 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001226 // Since loads produce two values, make sure to remember that we legalized
1227 // both of them.
1228 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1229 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1230 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001231 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001232 case ISD::EXTRACT_ELEMENT: {
1233 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1234 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001235 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001236 case Legal:
1237 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1238 // 1 -> Hi
1239 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1240 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1241 TLI.getShiftAmountTy()));
1242 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1243 } else {
1244 // 0 -> Lo
1245 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1246 Node->getOperand(0));
1247 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001248 break;
1249 case Expand:
1250 // Get both the low and high parts.
1251 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1252 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1253 Result = Tmp2; // 1 -> Hi
1254 else
1255 Result = Tmp1; // 0 -> Lo
1256 break;
1257 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001258 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001259 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001260
1261 case ISD::CopyToReg:
1262 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001263
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001264 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001265 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001266 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001267 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001268 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001269 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001270 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001271 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001272 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001273 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001274 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1275 Tmp3);
1276 } else {
1277 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001278 }
1279
1280 // Since this produces two values, make sure to remember that we legalized
1281 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001282 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001283 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001284 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001285 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001286 break;
1287
1288 case ISD::RET:
1289 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001290
1291 // Ensure that libcalls are emitted before a return.
1292 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1293 Tmp1 = LegalizeOp(Tmp1);
1294 LastCALLSEQ_END = DAG.getEntryNode();
1295
Chris Lattnerdc750592005-01-07 07:47:09 +00001296 switch (Node->getNumOperands()) {
1297 case 2: // ret val
1298 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1299 case Legal:
1300 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001302 break;
1303 case Expand: {
1304 SDOperand Lo, Hi;
1305 ExpandOp(Node->getOperand(1), Lo, Hi);
1306 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001307 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001308 }
1309 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001310 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001311 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1312 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001313 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001314 }
1315 break;
1316 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001317 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001318 break;
1319 default: { // ret <values>
1320 std::vector<SDOperand> NewValues;
1321 NewValues.push_back(Tmp1);
1322 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1323 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1324 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001325 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001326 break;
1327 case Expand: {
1328 SDOperand Lo, Hi;
1329 ExpandOp(Node->getOperand(i), Lo, Hi);
1330 NewValues.push_back(Lo);
1331 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001332 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001333 }
1334 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001335 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001336 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001337
1338 if (NewValues.size() == Node->getNumOperands())
1339 Result = DAG.UpdateNodeOperands(Result, NewValues);
1340 else
1341 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001342 break;
1343 }
1344 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001345
Chris Lattner4d1ea712006-01-29 21:02:23 +00001346 if (Result.getOpcode() == ISD::RET) {
1347 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1348 default: assert(0 && "This action is not supported yet!");
1349 case TargetLowering::Legal: break;
1350 case TargetLowering::Custom:
1351 Tmp1 = TLI.LowerOperation(Result, DAG);
1352 if (Tmp1.Val) Result = Tmp1;
1353 break;
1354 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001355 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001356 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001357 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001358 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1359 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1360
Chris Lattnere69daaf2005-01-08 06:25:56 +00001361 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001362 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001363 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001364 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001365 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001366 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001367 } else {
1368 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001369 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001370 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001371 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1372 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001373 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001374 }
1375
Chris Lattnerdc750592005-01-07 07:47:09 +00001376 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1377 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001378 Tmp3 = LegalizeOp(Node->getOperand(1));
1379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1380 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001381
Chris Lattnerd02b0542006-01-28 10:58:55 +00001382 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001383 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1384 default: assert(0 && "This action is not supported yet!");
1385 case TargetLowering::Legal: break;
1386 case TargetLowering::Custom:
1387 Tmp1 = TLI.LowerOperation(Result, DAG);
1388 if (Tmp1.Val) Result = Tmp1;
1389 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001390 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001391 break;
1392 }
1393 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001394 // Truncate the value and store the result.
1395 Tmp3 = PromoteOp(Node->getOperand(1));
1396 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001397 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001398 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001399 break;
1400
Chris Lattnerdc750592005-01-07 07:47:09 +00001401 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001402 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001403 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001404
1405 // If this is a vector type, then we have to calculate the increment as
1406 // the product of the element size in bytes, and the number of elements
1407 // in the high half of the vector.
1408 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1409 SDNode *InVal = Node->getOperand(1).Val;
1410 unsigned NumElems =
1411 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1412 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1413
1414 // Figure out if there is a Packed type corresponding to this Vector
1415 // type. If so, convert to the packed type.
1416 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1417 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1418 // Turn this into a normal store of the packed type.
1419 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1420 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1421 Node->getOperand(3));
1422 break;
1423 } else if (NumElems == 1) {
1424 // Turn this into a normal store of the scalar type.
1425 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1427 Node->getOperand(3));
1428 break;
1429 } else {
1430 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1431 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1432 }
1433 } else {
1434 ExpandOp(Node->getOperand(1), Lo, Hi);
1435 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1436 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001437
1438 if (!TLI.isLittleEndian())
1439 std::swap(Lo, Hi);
1440
Chris Lattner55e9cde2005-05-11 04:51:16 +00001441 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1442 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001443 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1444 getIntPtrConstant(IncrementSize));
1445 assert(isTypeLegal(Tmp2.getValueType()) &&
1446 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001447 // FIXME: This sets the srcvalue of both halves to be the same, which is
1448 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001449 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1450 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001451 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1452 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001453 }
1454 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001455 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001456 case ISD::PCMARKER:
1457 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001458 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001459 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001460 case ISD::STACKSAVE:
1461 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001462 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1463 Tmp1 = Result.getValue(0);
1464 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001465
Chris Lattnerb3266452006-01-13 02:50:02 +00001466 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1467 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001468 case TargetLowering::Legal: break;
1469 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001470 Tmp3 = TLI.LowerOperation(Result, DAG);
1471 if (Tmp3.Val) {
1472 Tmp1 = LegalizeOp(Tmp3);
1473 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001474 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001475 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001476 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001477 // Expand to CopyFromReg if the target set
1478 // StackPointerRegisterToSaveRestore.
1479 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001480 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001481 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001482 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001483 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001484 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1485 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001486 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001487 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001488 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001489
1490 // Since stacksave produce two values, make sure to remember that we
1491 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001492 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1493 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1494 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001495
Chris Lattnerb3266452006-01-13 02:50:02 +00001496 case ISD::STACKRESTORE:
1497 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1498 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001499 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001500
1501 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1502 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001503 case TargetLowering::Legal: break;
1504 case TargetLowering::Custom:
1505 Tmp1 = TLI.LowerOperation(Result, DAG);
1506 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001507 break;
1508 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001509 // Expand to CopyToReg if the target set
1510 // StackPointerRegisterToSaveRestore.
1511 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1512 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1513 } else {
1514 Result = Tmp1;
1515 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001516 break;
1517 }
1518 break;
1519
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001520 case ISD::READCYCLECOUNTER:
1521 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001522 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001523
1524 // Since rdcc produce two values, make sure to remember that we legalized
1525 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001526 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001527 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001528 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001529
Evan Cheng31d15fa2005-12-23 07:29:34 +00001530 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001531 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1532 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1533
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001534 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1535 "Cannot handle illegal TRUNCSTORE yet!");
1536 Tmp2 = LegalizeOp(Node->getOperand(1));
1537
1538 // The only promote case we handle is TRUNCSTORE:i1 X into
1539 // -> TRUNCSTORE:i8 (and X, 1)
1540 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1541 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1542 TargetLowering::Promote) {
1543 // Promote the bool to a mask then store.
1544 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1545 DAG.getConstant(1, Tmp2.getValueType()));
1546 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1547 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001548
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001549 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1550 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001551 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1552 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001553 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001554
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001555 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1556 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1557 default: assert(0 && "This action is not supported yet!");
1558 case TargetLowering::Legal: break;
1559 case TargetLowering::Custom:
1560 Tmp1 = TLI.LowerOperation(Result, DAG);
1561 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001562 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001563 }
1564 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001565 }
Chris Lattner39c67442005-01-14 22:08:15 +00001566 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001567 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1568 case Expand: assert(0 && "It's impossible to expand bools");
1569 case Legal:
1570 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1571 break;
1572 case Promote:
1573 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1574 break;
1575 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001576 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001577 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001578
Chris Lattnerd02b0542006-01-28 10:58:55 +00001579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001580
Nate Begeman987121a2005-08-23 04:29:48 +00001581 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001582 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001583 case TargetLowering::Legal: break;
1584 case TargetLowering::Custom: {
1585 Tmp1 = TLI.LowerOperation(Result, DAG);
1586 if (Tmp1.Val) Result = Tmp1;
1587 break;
1588 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001589 case TargetLowering::Expand:
1590 if (Tmp1.getOpcode() == ISD::SETCC) {
1591 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1592 Tmp2, Tmp3,
1593 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1594 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001595 // Make sure the condition is either zero or one. It may have been
1596 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001597 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1598 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1599 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001600 Result = DAG.getSelectCC(Tmp1,
1601 DAG.getConstant(0, Tmp1.getValueType()),
1602 Tmp2, Tmp3, ISD::SETNE);
1603 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001604 break;
1605 case TargetLowering::Promote: {
1606 MVT::ValueType NVT =
1607 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1608 unsigned ExtOp, TruncOp;
1609 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001610 ExtOp = ISD::ANY_EXTEND;
1611 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001612 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001613 ExtOp = ISD::FP_EXTEND;
1614 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001615 }
1616 // Promote each of the values to the new type.
1617 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1618 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1619 // Perform the larger operation, then round down.
1620 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1621 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1622 break;
1623 }
1624 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001625 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001626 case ISD::SELECT_CC: {
1627 Tmp1 = Node->getOperand(0); // LHS
1628 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001629 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1630 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001631 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001632
Nate Begeman7e7f4392006-02-01 07:19:44 +00001633 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1634
1635 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1636 // the LHS is a legal SETCC itself. In this case, we need to compare
1637 // the result against zero to select between true and false values.
1638 if (Tmp2.Val == 0) {
1639 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1640 CC = DAG.getCondCode(ISD::SETNE);
1641 }
1642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1643
1644 // Everything is legal, see if we should expand this op or something.
1645 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1646 default: assert(0 && "This action is not supported yet!");
1647 case TargetLowering::Legal: break;
1648 case TargetLowering::Custom:
1649 Tmp1 = TLI.LowerOperation(Result, DAG);
1650 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001651 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001652 }
1653 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001654 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001655 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001656 Tmp1 = Node->getOperand(0);
1657 Tmp2 = Node->getOperand(1);
1658 Tmp3 = Node->getOperand(2);
1659 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1660
1661 // If we had to Expand the SetCC operands into a SELECT node, then it may
1662 // not always be possible to return a true LHS & RHS. In this case, just
1663 // return the value we legalized, returned in the LHS
1664 if (Tmp2.Val == 0) {
1665 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001666 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001667 }
Nate Begeman987121a2005-08-23 04:29:48 +00001668
Chris Lattnerf263a232006-01-30 22:43:50 +00001669 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001670 default: assert(0 && "Cannot handle this action for SETCC yet!");
1671 case TargetLowering::Custom:
1672 isCustom = true;
1673 // FALLTHROUGH.
1674 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001675 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001676 if (isCustom) {
1677 Tmp3 = TLI.LowerOperation(Result, DAG);
1678 if (Tmp3.Val) Result = Tmp3;
1679 }
Nate Begeman987121a2005-08-23 04:29:48 +00001680 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001681 case TargetLowering::Promote: {
1682 // First step, figure out the appropriate operation to use.
1683 // Allow SETCC to not be supported for all legal data types
1684 // Mostly this targets FP
1685 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1686 MVT::ValueType OldVT = NewInTy;
1687
1688 // Scan for the appropriate larger type to use.
1689 while (1) {
1690 NewInTy = (MVT::ValueType)(NewInTy+1);
1691
1692 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1693 "Fell off of the edge of the integer world");
1694 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1695 "Fell off of the edge of the floating point world");
1696
1697 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001698 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001699 break;
1700 }
1701 if (MVT::isInteger(NewInTy))
1702 assert(0 && "Cannot promote Legal Integer SETCC yet");
1703 else {
1704 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1705 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1706 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001707 Tmp1 = LegalizeOp(Tmp1);
1708 Tmp2 = LegalizeOp(Tmp2);
1709 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001710 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001711 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001712 }
Nate Begeman987121a2005-08-23 04:29:48 +00001713 case TargetLowering::Expand:
1714 // Expand a setcc node into a select_cc of the same condition, lhs, and
1715 // rhs that selects between const 1 (true) and const 0 (false).
1716 MVT::ValueType VT = Node->getValueType(0);
1717 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1718 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1719 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001720 break;
1721 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001722 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001723 case ISD::MEMSET:
1724 case ISD::MEMCPY:
1725 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001726 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001727 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1728
1729 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1730 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1731 case Expand: assert(0 && "Cannot expand a byte!");
1732 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001733 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001734 break;
1735 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001736 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001737 break;
1738 }
1739 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001740 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001741 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001742
1743 SDOperand Tmp4;
1744 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001745 case Expand: {
1746 // Length is too big, just take the lo-part of the length.
1747 SDOperand HiPart;
1748 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1749 break;
1750 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001751 case Legal:
1752 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001753 break;
1754 case Promote:
1755 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001756 break;
1757 }
1758
1759 SDOperand Tmp5;
1760 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1761 case Expand: assert(0 && "Cannot expand this yet!");
1762 case Legal:
1763 Tmp5 = LegalizeOp(Node->getOperand(4));
1764 break;
1765 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001766 Tmp5 = PromoteOp(Node->getOperand(4));
1767 break;
1768 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001769
1770 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1771 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001772 case TargetLowering::Custom:
1773 isCustom = true;
1774 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001775 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001776 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001777 if (isCustom) {
1778 Tmp1 = TLI.LowerOperation(Result, DAG);
1779 if (Tmp1.Val) Result = Tmp1;
1780 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001781 break;
1782 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001783 // Otherwise, the target does not support this operation. Lower the
1784 // operation to an explicit libcall as appropriate.
1785 MVT::ValueType IntPtr = TLI.getPointerTy();
1786 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1787 std::vector<std::pair<SDOperand, const Type*> > Args;
1788
Reid Spencer6dced922005-01-12 14:53:45 +00001789 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001790 if (Node->getOpcode() == ISD::MEMSET) {
1791 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001792 // Extend the (previously legalized) ubyte argument to be an int value
1793 // for the call.
1794 if (Tmp3.getValueType() > MVT::i32)
1795 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1796 else
1797 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001798 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1799 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1800
1801 FnName = "memset";
1802 } else if (Node->getOpcode() == ISD::MEMCPY ||
1803 Node->getOpcode() == ISD::MEMMOVE) {
1804 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1805 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1806 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1807 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1808 } else {
1809 assert(0 && "Unknown op!");
1810 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001811
Chris Lattner85d70c62005-01-11 05:57:22 +00001812 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001813 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001814 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001815 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001816 break;
1817 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001818 }
1819 break;
1820 }
Chris Lattner5385db52005-05-09 20:23:03 +00001821
Chris Lattner4157c412005-04-02 04:00:59 +00001822 case ISD::SHL_PARTS:
1823 case ISD::SRA_PARTS:
1824 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001825 std::vector<SDOperand> Ops;
1826 bool Changed = false;
1827 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1828 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1829 Changed |= Ops.back() != Node->getOperand(i);
1830 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001831 if (Changed)
1832 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001833
Evan Cheng870e4f82006-01-09 18:31:59 +00001834 switch (TLI.getOperationAction(Node->getOpcode(),
1835 Node->getValueType(0))) {
1836 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001837 case TargetLowering::Legal: break;
1838 case TargetLowering::Custom:
1839 Tmp1 = TLI.LowerOperation(Result, DAG);
1840 if (Tmp1.Val) {
1841 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001842 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001843 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001844 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1845 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001846 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001847 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001848 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001849 return RetVal;
1850 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001851 break;
1852 }
1853
Chris Lattner13fe99c2005-04-02 05:00:07 +00001854 // Since these produce multiple values, make sure to remember that we
1855 // legalized all of them.
1856 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1857 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1858 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001859 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001860
1861 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001862 case ISD::ADD:
1863 case ISD::SUB:
1864 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001865 case ISD::MULHS:
1866 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001867 case ISD::UDIV:
1868 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001869 case ISD::AND:
1870 case ISD::OR:
1871 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001872 case ISD::SHL:
1873 case ISD::SRL:
1874 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001875 case ISD::FADD:
1876 case ISD::FSUB:
1877 case ISD::FMUL:
1878 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001879 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001880 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1881 case Expand: assert(0 && "Not possible");
1882 case Legal:
1883 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1884 break;
1885 case Promote:
1886 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1887 break;
1888 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001889
1890 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001891
Andrew Lenharth72594262005-12-24 23:42:32 +00001892 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001893 default: assert(0 && "Operation not supported");
1894 case TargetLowering::Legal: break;
1895 case TargetLowering::Custom:
1896 Tmp1 = TLI.LowerOperation(Result, DAG);
1897 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001898 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001899 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001900 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001901
1902 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1903 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1904 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1905 case Expand: assert(0 && "Not possible");
1906 case Legal:
1907 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1908 break;
1909 case Promote:
1910 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1911 break;
1912 }
1913
1914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1915
1916 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1917 default: assert(0 && "Operation not supported");
1918 case TargetLowering::Custom:
1919 Tmp1 = TLI.LowerOperation(Result, DAG);
1920 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00001921 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001922 case TargetLowering::Legal: break;
1923 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00001924 // If this target supports fabs/fneg natively, do this efficiently.
1925 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1926 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1927 // Get the sign bit of the RHS.
1928 MVT::ValueType IVT =
1929 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1930 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1931 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1932 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1933 // Get the absolute value of the result.
1934 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1935 // Select between the nabs and abs value based on the sign bit of
1936 // the input.
1937 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1938 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1939 AbsVal),
1940 AbsVal);
1941 Result = LegalizeOp(Result);
1942 break;
1943 }
1944
1945 // Otherwise, do bitwise ops!
1946
1947 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001948 const char *FnName;
1949 if (Node->getValueType(0) == MVT::f32) {
1950 FnName = "copysignf";
1951 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1952 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1953 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1954 } else {
1955 FnName = "copysign";
1956 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
1957 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1958 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
1959 }
1960 SDOperand Dummy;
1961 Result = ExpandLibCall(FnName, Node, Dummy);
1962 break;
1963 }
1964 break;
1965
Nate Begeman5965bd12006-02-17 05:43:56 +00001966 case ISD::ADDC:
1967 case ISD::SUBC:
1968 Tmp1 = LegalizeOp(Node->getOperand(0));
1969 Tmp2 = LegalizeOp(Node->getOperand(1));
1970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1971 // Since this produces two values, make sure to remember that we legalized
1972 // both of them.
1973 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1974 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1975 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00001976
Nate Begeman5965bd12006-02-17 05:43:56 +00001977 case ISD::ADDE:
1978 case ISD::SUBE:
1979 Tmp1 = LegalizeOp(Node->getOperand(0));
1980 Tmp2 = LegalizeOp(Node->getOperand(1));
1981 Tmp3 = LegalizeOp(Node->getOperand(2));
1982 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1983 // Since this produces two values, make sure to remember that we legalized
1984 // both of them.
1985 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1986 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1987 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00001988
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001989 case ISD::BUILD_PAIR: {
1990 MVT::ValueType PairTy = Node->getValueType(0);
1991 // TODO: handle the case where the Lo and Hi operands are not of legal type
1992 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1993 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1994 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001995 case TargetLowering::Promote:
1996 case TargetLowering::Custom:
1997 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001998 case TargetLowering::Legal:
1999 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2000 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2001 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002002 case TargetLowering::Expand:
2003 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2004 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2005 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2006 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2007 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002008 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002009 break;
2010 }
2011 break;
2012 }
2013
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002014 case ISD::UREM:
2015 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002016 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002017 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2018 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002019
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002020 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002021 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2022 case TargetLowering::Custom:
2023 isCustom = true;
2024 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002025 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002027 if (isCustom) {
2028 Tmp1 = TLI.LowerOperation(Result, DAG);
2029 if (Tmp1.Val) Result = Tmp1;
2030 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002031 break;
Chris Lattner81914422005-08-03 20:31:37 +00002032 case TargetLowering::Expand:
2033 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002034 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002035 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002036 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002037 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2038 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2039 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2040 } else {
2041 // Floating point mod -> fmod libcall.
2042 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2043 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002044 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002045 }
2046 break;
2047 }
2048 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002049 case ISD::VAARG: {
2050 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2051 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2052
Chris Lattner364b89a2006-01-28 07:42:08 +00002053 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002054 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2055 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002056 case TargetLowering::Custom:
2057 isCustom = true;
2058 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002059 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002060 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2061 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002062 Tmp1 = Result.getValue(1);
2063
2064 if (isCustom) {
2065 Tmp2 = TLI.LowerOperation(Result, DAG);
2066 if (Tmp2.Val) {
2067 Result = LegalizeOp(Tmp2);
2068 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2069 }
2070 }
Nate Begemane74795c2006-01-25 18:21:52 +00002071 break;
2072 case TargetLowering::Expand: {
2073 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2074 Node->getOperand(2));
2075 // Increment the pointer, VAList, to the next vaarg
2076 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2077 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2078 TLI.getPointerTy()));
2079 // Store the incremented VAList to the legalized pointer
2080 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2081 Node->getOperand(2));
2082 // Load the actual argument out of the pointer VAList
2083 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002084 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002085 Result = LegalizeOp(Result);
2086 break;
2087 }
2088 }
2089 // Since VAARG produces two values, make sure to remember that we
2090 // legalized both of them.
2091 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002092 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2093 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002094 }
2095
2096 case ISD::VACOPY:
2097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2098 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2099 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2100
2101 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2102 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002103 case TargetLowering::Custom:
2104 isCustom = true;
2105 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002106 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2108 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002109 if (isCustom) {
2110 Tmp1 = TLI.LowerOperation(Result, DAG);
2111 if (Tmp1.Val) Result = Tmp1;
2112 }
Nate Begemane74795c2006-01-25 18:21:52 +00002113 break;
2114 case TargetLowering::Expand:
2115 // This defaults to loading a pointer from the input and storing it to the
2116 // output, returning the chain.
2117 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2118 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2119 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002120 break;
2121 }
2122 break;
2123
2124 case ISD::VAEND:
2125 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2126 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2127
2128 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2129 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002130 case TargetLowering::Custom:
2131 isCustom = true;
2132 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002133 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002135 if (isCustom) {
2136 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2137 if (Tmp1.Val) Result = Tmp1;
2138 }
Nate Begemane74795c2006-01-25 18:21:52 +00002139 break;
2140 case TargetLowering::Expand:
2141 Result = Tmp1; // Default to a no-op, return the chain
2142 break;
2143 }
2144 break;
2145
2146 case ISD::VASTART:
2147 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2148 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2149
Chris Lattnerd02b0542006-01-28 10:58:55 +00002150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2151
Nate Begemane74795c2006-01-25 18:21:52 +00002152 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2153 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002154 case TargetLowering::Legal: break;
2155 case TargetLowering::Custom:
2156 Tmp1 = TLI.LowerOperation(Result, DAG);
2157 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002158 break;
2159 }
2160 break;
2161
Nate Begeman1b8121b2006-01-11 21:21:00 +00002162 case ISD::ROTL:
2163 case ISD::ROTR:
2164 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2165 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002166
2167 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2168 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002169 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002170 break;
2171
Nate Begeman2fba8a32006-01-14 03:14:10 +00002172 case ISD::BSWAP:
2173 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2174 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002175 case TargetLowering::Custom:
2176 assert(0 && "Cannot custom legalize this yet!");
2177 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002178 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002179 break;
2180 case TargetLowering::Promote: {
2181 MVT::ValueType OVT = Tmp1.getValueType();
2182 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2183 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002184
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002185 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2186 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2187 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2188 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2189 break;
2190 }
2191 case TargetLowering::Expand:
2192 Result = ExpandBSWAP(Tmp1);
2193 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002194 }
2195 break;
2196
Andrew Lenharth5e177822005-05-03 17:19:30 +00002197 case ISD::CTPOP:
2198 case ISD::CTTZ:
2199 case ISD::CTLZ:
2200 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2201 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002202 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002203 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002204 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002205 break;
2206 case TargetLowering::Promote: {
2207 MVT::ValueType OVT = Tmp1.getValueType();
2208 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002209
2210 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002211 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2212 // Perform the larger operation, then subtract if needed.
2213 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002214 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002215 case ISD::CTPOP:
2216 Result = Tmp1;
2217 break;
2218 case ISD::CTTZ:
2219 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002220 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2221 DAG.getConstant(getSizeInBits(NVT), NVT),
2222 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002223 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002224 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2225 break;
2226 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002227 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002228 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2229 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002230 getSizeInBits(OVT), NVT));
2231 break;
2232 }
2233 break;
2234 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002235 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002236 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002237 break;
2238 }
2239 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002240
Chris Lattner13fe99c2005-04-02 05:00:07 +00002241 // Unary operators
2242 case ISD::FABS:
2243 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002244 case ISD::FSQRT:
2245 case ISD::FSIN:
2246 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002247 Tmp1 = LegalizeOp(Node->getOperand(0));
2248 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002249 case TargetLowering::Promote:
2250 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002251 isCustom = true;
2252 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002253 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002254 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002255 if (isCustom) {
2256 Tmp1 = TLI.LowerOperation(Result, DAG);
2257 if (Tmp1.Val) Result = Tmp1;
2258 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002259 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002260 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002261 switch (Node->getOpcode()) {
2262 default: assert(0 && "Unreachable!");
2263 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002264 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2265 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002266 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002267 break;
Chris Lattner80026402005-04-30 04:43:14 +00002268 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002269 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2270 MVT::ValueType VT = Node->getValueType(0);
2271 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002272 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002273 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2274 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002275 break;
2276 }
2277 case ISD::FSQRT:
2278 case ISD::FSIN:
2279 case ISD::FCOS: {
2280 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002281 const char *FnName = 0;
2282 switch(Node->getOpcode()) {
2283 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2284 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2285 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2286 default: assert(0 && "Unreachable!");
2287 }
Nate Begeman77558da2005-08-04 21:43:28 +00002288 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002289 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002290 break;
2291 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002292 }
2293 break;
2294 }
2295 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002296
2297 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002298 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002299 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002300 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002301 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2302 Node->getOperand(0).getValueType())) {
2303 default: assert(0 && "Unknown operation action!");
2304 case TargetLowering::Expand:
2305 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2306 break;
2307 case TargetLowering::Legal:
2308 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002309 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002310 break;
2311 }
2312 }
2313 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002314 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002315 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002316 case ISD::UINT_TO_FP: {
2317 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002318 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2319 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002320 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002321 Node->getOperand(0).getValueType())) {
2322 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002323 case TargetLowering::Custom:
2324 isCustom = true;
2325 // FALLTHROUGH
2326 case TargetLowering::Legal:
2327 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002328 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002329 if (isCustom) {
2330 Tmp1 = TLI.LowerOperation(Result, DAG);
2331 if (Tmp1.Val) Result = Tmp1;
2332 }
2333 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002334 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002335 Result = ExpandLegalINT_TO_FP(isSigned,
2336 LegalizeOp(Node->getOperand(0)),
2337 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002338 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002339 case TargetLowering::Promote:
2340 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2341 Node->getValueType(0),
2342 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002343 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002344 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002345 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002346 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002347 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2348 Node->getValueType(0), Node->getOperand(0));
2349 break;
2350 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002351 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002352 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002353 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2354 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002355 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002356 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2357 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002358 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002359 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2360 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002361 break;
2362 }
2363 break;
2364 }
2365 case ISD::TRUNCATE:
2366 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2367 case Legal:
2368 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002369 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002370 break;
2371 case Expand:
2372 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2373
2374 // Since the result is legal, we should just be able to truncate the low
2375 // part of the source.
2376 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2377 break;
2378 case Promote:
2379 Result = PromoteOp(Node->getOperand(0));
2380 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2381 break;
2382 }
2383 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002384
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002385 case ISD::FP_TO_SINT:
2386 case ISD::FP_TO_UINT:
2387 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2388 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002389 Tmp1 = LegalizeOp(Node->getOperand(0));
2390
Chris Lattner44fe26f2005-07-29 00:11:56 +00002391 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2392 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002393 case TargetLowering::Custom:
2394 isCustom = true;
2395 // FALLTHROUGH
2396 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002397 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002398 if (isCustom) {
2399 Tmp1 = TLI.LowerOperation(Result, DAG);
2400 if (Tmp1.Val) Result = Tmp1;
2401 }
2402 break;
2403 case TargetLowering::Promote:
2404 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2405 Node->getOpcode() == ISD::FP_TO_SINT);
2406 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002407 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002408 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2409 SDOperand True, False;
2410 MVT::ValueType VT = Node->getOperand(0).getValueType();
2411 MVT::ValueType NVT = Node->getValueType(0);
2412 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2413 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2414 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2415 Node->getOperand(0), Tmp2, ISD::SETLT);
2416 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2417 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002418 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002419 Tmp2));
2420 False = DAG.getNode(ISD::XOR, NVT, False,
2421 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002422 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2423 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002424 } else {
2425 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2426 }
2427 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002428 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002429 break;
2430 case Expand:
2431 assert(0 && "Shouldn't need to expand other operators here!");
2432 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002433 Tmp1 = PromoteOp(Node->getOperand(0));
2434 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2435 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002436 break;
2437 }
2438 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002439
Chris Lattner7753f172005-09-02 00:18:10 +00002440 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002441 case ISD::ZERO_EXTEND:
2442 case ISD::SIGN_EXTEND:
2443 case ISD::FP_EXTEND:
2444 case ISD::FP_ROUND:
2445 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002446 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002447 case Legal:
2448 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002449 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002450 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002451 case Promote:
2452 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002453 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002454 Tmp1 = PromoteOp(Node->getOperand(0));
2455 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002456 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002457 case ISD::ZERO_EXTEND:
2458 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002459 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002460 Result = DAG.getZeroExtendInReg(Result,
2461 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002462 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002463 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002464 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002465 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002466 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002467 Result,
2468 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002469 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002470 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002471 Result = PromoteOp(Node->getOperand(0));
2472 if (Result.getValueType() != Op.getValueType())
2473 // Dynamically dead while we have only 2 FP types.
2474 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2475 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002476 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002477 Result = PromoteOp(Node->getOperand(0));
2478 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2479 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002480 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002481 }
2482 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002483 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002484 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002485 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002486 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002487
2488 // If this operation is not supported, convert it to a shl/shr or load/store
2489 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002490 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2491 default: assert(0 && "This action not supported for this op yet!");
2492 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002493 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002494 break;
2495 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002496 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002497 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002498 // NOTE: we could fall back on load/store here too for targets without
2499 // SAR. However, it is doubtful that any exist.
2500 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2501 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002502 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002503 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2504 Node->getOperand(0), ShiftCst);
2505 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2506 Result, ShiftCst);
2507 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2508 // The only way we can lower this is to turn it into a STORETRUNC,
2509 // EXTLOAD pair, targetting a temporary location (a stack slot).
2510
2511 // NOTE: there is a choice here between constantly creating new stack
2512 // slots and always reusing the same one. We currently always create
2513 // new ones, as reuse may inhibit scheduling.
2514 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2515 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2516 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2517 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002518 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002519 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2520 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2521 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002522 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002523 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002524 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2525 Result, StackSlot, DAG.getSrcValue(NULL),
2526 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002527 } else {
2528 assert(0 && "Unknown op");
2529 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002530 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002531 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002532 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002533 }
Chris Lattner99222f72005-01-15 07:15:18 +00002534 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002535
2536 // Make sure that the generated code is itself legal.
2537 if (Result != Op)
2538 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002539
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002540 // Note that LegalizeOp may be reentered even from single-use nodes, which
2541 // means that we always must cache transformed nodes.
2542 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002543 return Result;
2544}
2545
Chris Lattner4d978642005-01-15 22:16:26 +00002546/// PromoteOp - Given an operation that produces a value in an invalid type,
2547/// promote it to compute the value into a larger type. The produced value will
2548/// have the correct bits for the low portion of the register, but no guarantee
2549/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002550SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2551 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002552 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002553 assert(getTypeAction(VT) == Promote &&
2554 "Caller should expand or legalize operands that are not promotable!");
2555 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2556 "Cannot promote to smaller type!");
2557
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002558 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002559 SDOperand Result;
2560 SDNode *Node = Op.Val;
2561
Chris Lattner1a570f12005-09-02 20:32:45 +00002562 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2563 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002564
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002565 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002566 case ISD::CopyFromReg:
2567 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002568 default:
2569 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2570 assert(0 && "Do not know how to promote this operator!");
2571 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002572 case ISD::UNDEF:
2573 Result = DAG.getNode(ISD::UNDEF, NVT);
2574 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002575 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002576 if (VT != MVT::i1)
2577 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2578 else
2579 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002580 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2581 break;
2582 case ISD::ConstantFP:
2583 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2584 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2585 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002586
Chris Lattner2cb338d2005-01-18 02:59:52 +00002587 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002588 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002589 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2590 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002591 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002592
2593 case ISD::TRUNCATE:
2594 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2595 case Legal:
2596 Result = LegalizeOp(Node->getOperand(0));
2597 assert(Result.getValueType() >= NVT &&
2598 "This truncation doesn't make sense!");
2599 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2600 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2601 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002602 case Promote:
2603 // The truncation is not required, because we don't guarantee anything
2604 // about high bits anyway.
2605 Result = PromoteOp(Node->getOperand(0));
2606 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002607 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002608 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2609 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002610 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002611 }
2612 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002613 case ISD::SIGN_EXTEND:
2614 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002615 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2617 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2618 case Legal:
2619 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002620 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002621 break;
2622 case Promote:
2623 // Promote the reg if it's smaller.
2624 Result = PromoteOp(Node->getOperand(0));
2625 // The high bits are not guaranteed to be anything. Insert an extend.
2626 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002627 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002628 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002629 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002630 Result = DAG.getZeroExtendInReg(Result,
2631 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002632 break;
2633 }
2634 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002635 case ISD::BIT_CONVERT:
2636 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2637 Result = PromoteOp(Result);
2638 break;
2639
Chris Lattner4d978642005-01-15 22:16:26 +00002640 case ISD::FP_EXTEND:
2641 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2642 case ISD::FP_ROUND:
2643 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2644 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2645 case Promote: assert(0 && "Unreachable with 2 FP types!");
2646 case Legal:
2647 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002648 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002649 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002650 break;
2651 }
2652 break;
2653
2654 case ISD::SINT_TO_FP:
2655 case ISD::UINT_TO_FP:
2656 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2657 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002658 // No extra round required here.
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
2662 case Promote:
2663 Result = PromoteOp(Node->getOperand(0));
2664 if (Node->getOpcode() == ISD::SINT_TO_FP)
2665 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002666 Result,
2667 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002668 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002669 Result = DAG.getZeroExtendInReg(Result,
2670 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002671 // No extra round required here.
2672 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002673 break;
2674 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002675 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2676 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002677 // Round if we cannot tolerate excess precision.
2678 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002679 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2680 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002681 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002682 }
Chris Lattner4d978642005-01-15 22:16:26 +00002683 break;
2684
Chris Lattner268d4572005-12-09 17:32:47 +00002685 case ISD::SIGN_EXTEND_INREG:
2686 Result = PromoteOp(Node->getOperand(0));
2687 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2688 Node->getOperand(1));
2689 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002690 case ISD::FP_TO_SINT:
2691 case ISD::FP_TO_UINT:
2692 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2693 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002694 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002695 break;
2696 case Promote:
2697 // The input result is prerounded, so we don't have to do anything
2698 // special.
2699 Tmp1 = PromoteOp(Node->getOperand(0));
2700 break;
2701 case Expand:
2702 assert(0 && "not implemented");
2703 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002704 // If we're promoting a UINT to a larger size, check to see if the new node
2705 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2706 // we can use that instead. This allows us to generate better code for
2707 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2708 // legal, such as PowerPC.
2709 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002710 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002711 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2712 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002713 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2714 } else {
2715 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2716 }
Chris Lattner4d978642005-01-15 22:16:26 +00002717 break;
2718
Chris Lattner13fe99c2005-04-02 05:00:07 +00002719 case ISD::FABS:
2720 case ISD::FNEG:
2721 Tmp1 = PromoteOp(Node->getOperand(0));
2722 assert(Tmp1.getValueType() == NVT);
2723 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2724 // NOTE: we do not have to do any extra rounding here for
2725 // NoExcessFPPrecision, because we know the input will have the appropriate
2726 // precision, and these operations don't modify precision at all.
2727 break;
2728
Chris Lattner9d6fa982005-04-28 21:44:33 +00002729 case ISD::FSQRT:
2730 case ISD::FSIN:
2731 case ISD::FCOS:
2732 Tmp1 = PromoteOp(Node->getOperand(0));
2733 assert(Tmp1.getValueType() == NVT);
2734 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002735 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002736 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2737 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002738 break;
2739
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002740 case ISD::AND:
2741 case ISD::OR:
2742 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002743 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002744 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002745 case ISD::MUL:
2746 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002747 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002748 // that too is okay if they are integer operations.
2749 Tmp1 = PromoteOp(Node->getOperand(0));
2750 Tmp2 = PromoteOp(Node->getOperand(1));
2751 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2752 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002753 break;
2754 case ISD::FADD:
2755 case ISD::FSUB:
2756 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002757 Tmp1 = PromoteOp(Node->getOperand(0));
2758 Tmp2 = PromoteOp(Node->getOperand(1));
2759 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2760 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2761
2762 // Floating point operations will give excess precision that we may not be
2763 // able to tolerate. If we DO allow excess precision, just leave it,
2764 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002765 // FIXME: Why would we need to round FP ops more than integer ones?
2766 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002767 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002768 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2769 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002770 break;
2771
Chris Lattner4d978642005-01-15 22:16:26 +00002772 case ISD::SDIV:
2773 case ISD::SREM:
2774 // These operators require that their input be sign extended.
2775 Tmp1 = PromoteOp(Node->getOperand(0));
2776 Tmp2 = PromoteOp(Node->getOperand(1));
2777 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002778 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2779 DAG.getValueType(VT));
2780 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2781 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002782 }
2783 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2784
2785 // Perform FP_ROUND: this is probably overly pessimistic.
2786 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002787 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2788 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002789 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002790 case ISD::FDIV:
2791 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002792 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002793 // These operators require that their input be fp extended.
2794 Tmp1 = PromoteOp(Node->getOperand(0));
2795 Tmp2 = PromoteOp(Node->getOperand(1));
2796 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2797
2798 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002799 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002800 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2801 DAG.getValueType(VT));
2802 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002803
2804 case ISD::UDIV:
2805 case ISD::UREM:
2806 // These operators require that their input be zero extended.
2807 Tmp1 = PromoteOp(Node->getOperand(0));
2808 Tmp2 = PromoteOp(Node->getOperand(1));
2809 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002810 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2811 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002812 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2813 break;
2814
2815 case ISD::SHL:
2816 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002817 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002818 break;
2819 case ISD::SRA:
2820 // The input value must be properly sign extended.
2821 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002822 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2823 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002824 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002825 break;
2826 case ISD::SRL:
2827 // The input value must be properly zero extended.
2828 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002829 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002830 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002831 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002832
2833 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002834 Tmp1 = Node->getOperand(0); // Get the chain.
2835 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002836 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2837 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2838 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2839 } else {
2840 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2841 Node->getOperand(2));
2842 // Increment the pointer, VAList, to the next vaarg
2843 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2844 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2845 TLI.getPointerTy()));
2846 // Store the incremented VAList to the legalized pointer
2847 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2848 Node->getOperand(2));
2849 // Load the actual argument out of the pointer VAList
2850 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2851 DAG.getSrcValue(0), VT);
2852 }
2853 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002854 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002855 break;
2856
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002857 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002858 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2859 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002860 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002861 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002862 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002863 case ISD::SEXTLOAD:
2864 case ISD::ZEXTLOAD:
2865 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002866 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2867 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002868 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002869 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002870 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002871 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002872 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002873 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2874 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002875 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002876 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002877 case ISD::SELECT_CC:
2878 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2879 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2880 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002881 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002882 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002883 case ISD::BSWAP:
2884 Tmp1 = Node->getOperand(0);
2885 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2886 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2887 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2888 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2889 TLI.getShiftAmountTy()));
2890 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002891 case ISD::CTPOP:
2892 case ISD::CTTZ:
2893 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002894 // Zero extend the argument
2895 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002896 // Perform the larger operation, then subtract if needed.
2897 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002898 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002899 case ISD::CTPOP:
2900 Result = Tmp1;
2901 break;
2902 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002903 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002904 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002905 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002906 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002907 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002908 break;
2909 case ISD::CTLZ:
2910 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002911 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2912 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002913 getSizeInBits(VT), NVT));
2914 break;
2915 }
2916 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002917 }
2918
2919 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002920
2921 // Make sure the result is itself legal.
2922 Result = LegalizeOp(Result);
2923
2924 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002925 AddPromotedOperand(Op, Result);
2926 return Result;
2927}
Chris Lattnerdc750592005-01-07 07:47:09 +00002928
Nate Begeman7e7f4392006-02-01 07:19:44 +00002929/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
2930/// with condition CC on the current target. This usually involves legalizing
2931/// or promoting the arguments. In the case where LHS and RHS must be expanded,
2932/// there may be no choice but to create a new SetCC node to represent the
2933/// legalized value of setcc lhs, rhs. In this case, the value is returned in
2934/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
2935void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
2936 SDOperand &RHS,
2937 SDOperand &CC) {
2938 SDOperand Tmp1, Tmp2, Result;
2939
2940 switch (getTypeAction(LHS.getValueType())) {
2941 case Legal:
2942 Tmp1 = LegalizeOp(LHS); // LHS
2943 Tmp2 = LegalizeOp(RHS); // RHS
2944 break;
2945 case Promote:
2946 Tmp1 = PromoteOp(LHS); // LHS
2947 Tmp2 = PromoteOp(RHS); // RHS
2948
2949 // If this is an FP compare, the operands have already been extended.
2950 if (MVT::isInteger(LHS.getValueType())) {
2951 MVT::ValueType VT = LHS.getValueType();
2952 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
2953
2954 // Otherwise, we have to insert explicit sign or zero extends. Note
2955 // that we could insert sign extends for ALL conditions, but zero extend
2956 // is cheaper on many machines (an AND instead of two shifts), so prefer
2957 // it.
2958 switch (cast<CondCodeSDNode>(CC)->get()) {
2959 default: assert(0 && "Unknown integer comparison!");
2960 case ISD::SETEQ:
2961 case ISD::SETNE:
2962 case ISD::SETUGE:
2963 case ISD::SETUGT:
2964 case ISD::SETULE:
2965 case ISD::SETULT:
2966 // ALL of these operations will work if we either sign or zero extend
2967 // the operands (including the unsigned comparisons!). Zero extend is
2968 // usually a simpler/cheaper operation, so prefer it.
2969 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2970 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
2971 break;
2972 case ISD::SETGE:
2973 case ISD::SETGT:
2974 case ISD::SETLT:
2975 case ISD::SETLE:
2976 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2977 DAG.getValueType(VT));
2978 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2979 DAG.getValueType(VT));
2980 break;
2981 }
2982 }
2983 break;
2984 case Expand:
2985 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
2986 ExpandOp(LHS, LHSLo, LHSHi);
2987 ExpandOp(RHS, RHSLo, RHSHi);
2988 switch (cast<CondCodeSDNode>(CC)->get()) {
2989 case ISD::SETEQ:
2990 case ISD::SETNE:
2991 if (RHSLo == RHSHi)
2992 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
2993 if (RHSCST->isAllOnesValue()) {
2994 // Comparison to -1.
2995 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
2996 Tmp2 = RHSLo;
2997 break;
2998 }
2999
3000 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3001 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3002 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3003 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3004 break;
3005 default:
3006 // If this is a comparison of the sign bit, just look at the top part.
3007 // X > -1, x < 0
3008 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3009 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3010 CST->getValue() == 0) || // X < 0
3011 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3012 CST->isAllOnesValue())) { // X > -1
3013 Tmp1 = LHSHi;
3014 Tmp2 = RHSHi;
3015 break;
3016 }
3017
3018 // FIXME: This generated code sucks.
3019 ISD::CondCode LowCC;
3020 switch (cast<CondCodeSDNode>(CC)->get()) {
3021 default: assert(0 && "Unknown integer setcc!");
3022 case ISD::SETLT:
3023 case ISD::SETULT: LowCC = ISD::SETULT; break;
3024 case ISD::SETGT:
3025 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3026 case ISD::SETLE:
3027 case ISD::SETULE: LowCC = ISD::SETULE; break;
3028 case ISD::SETGE:
3029 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3030 }
3031
3032 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3033 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3034 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3035
3036 // NOTE: on targets without efficient SELECT of bools, we can always use
3037 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3038 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3039 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3040 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3041 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3042 Result, Tmp1, Tmp2));
3043 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003044 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003045 }
3046 }
3047 LHS = Tmp1;
3048 RHS = Tmp2;
3049}
3050
Chris Lattner36e663d2005-12-23 00:16:34 +00003051/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003052/// The resultant code need not be legal. Note that SrcOp is the input operand
3053/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003054SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3055 SDOperand SrcOp) {
3056 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003057 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003058
3059 // Emit a store to the stack slot.
3060 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003061 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003062 // Result is a load from the stack slot.
3063 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3064}
3065
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003066/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3067/// support the operation, but do support the resultant packed vector type.
3068SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3069
3070 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003071 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003072 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003073 SDOperand SplatValue = Node->getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003074 for (SDNode::op_iterator I = Node->op_begin()+1, E = Node->op_end();
3075 I != E; ++I) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003076 if (I->getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003077 isOnlyLowElement = false;
Chris Lattner21e68c82006-03-20 01:52:29 +00003078 if (SplatValue != *I)
3079 SplatValue = SDOperand(0,0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003080 }
3081
3082 if (isOnlyLowElement) {
3083 // If the low element is an undef too, then this whole things is an undef.
3084 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3085 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3086 // Otherwise, turn this into a scalar_to_vector node.
3087 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3088 Node->getOperand(0));
3089 }
3090
Chris Lattner21e68c82006-03-20 01:52:29 +00003091 if (SplatValue.Val) { // Splat of one value?
3092 // Build the shuffle constant vector: <0, 0, 0, 0>
3093 MVT::ValueType MaskVT =
3094 MVT::getIntVectorWithNumElements(Node->getNumOperands());
3095 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
3096 std::vector<SDOperand> ZeroVec(Node->getNumOperands(), Zero);
3097 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3098
3099 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3100 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3101 // Get the splatted value into the low element of a vector register.
3102 SDOperand LowValVec =
3103 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3104
3105 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3106 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3107 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3108 SplatMask);
3109 }
3110 }
3111
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003112 // If the elements are all constants, turn this into a load from the constant
3113 // pool.
3114 bool isConstant = true;
3115 for (SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
3116 I != E; ++I) {
3117 if (!isa<ConstantFPSDNode>(I) && !isa<ConstantSDNode>(I) &&
3118 I->getOpcode() != ISD::UNDEF) {
3119 isConstant = false;
3120 break;
3121 }
3122 }
3123
3124 // Create a ConstantPacked, and put it in the constant pool.
3125 if (isConstant) {
3126 MVT::ValueType VT = Node->getValueType(0);
3127 const Type *OpNTy =
3128 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3129 std::vector<Constant*> CV;
3130 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3131 if (ConstantFPSDNode *V =
3132 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3133 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3134 } else if (ConstantSDNode *V =
3135 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3136 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3137 } else {
3138 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3139 CV.push_back(UndefValue::get(OpNTy));
3140 }
3141 }
3142 Constant *CP = ConstantPacked::get(CV);
3143 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3144 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3145 DAG.getSrcValue(NULL));
3146 }
3147
3148 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3149 // aligned object on the stack, store each element into it, then load
3150 // the result as a vector.
3151 MVT::ValueType VT = Node->getValueType(0);
3152 // Create the stack frame object.
3153 SDOperand FIPtr = CreateStackTemporary(VT);
3154
3155 // Emit a store of each element to the stack slot.
3156 std::vector<SDOperand> Stores;
3157 bool isLittleEndian = TLI.isLittleEndian();
3158 unsigned TypeByteSize =
3159 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3160 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3161 // Store (in the right endianness) the elements to memory.
3162 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3163 // Ignore undef elements.
3164 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3165
3166 unsigned Offset;
3167 if (isLittleEndian)
3168 Offset = TypeByteSize*i;
3169 else
3170 Offset = TypeByteSize*(e-i-1);
3171
3172 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3173 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3174
3175 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3176 Node->getOperand(i), Idx,
3177 DAG.getSrcValue(NULL)));
3178 }
3179
3180 SDOperand StoreChain;
3181 if (!Stores.empty()) // Not all undef elements?
3182 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3183 else
3184 StoreChain = DAG.getEntryNode();
3185
3186 // Result is a load from the stack slot.
3187 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3188}
3189
3190/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3191/// specified value type.
3192SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3193 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3194 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3195 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3196 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3197}
3198
Chris Lattner4157c412005-04-02 04:00:59 +00003199void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3200 SDOperand Op, SDOperand Amt,
3201 SDOperand &Lo, SDOperand &Hi) {
3202 // Expand the subcomponents.
3203 SDOperand LHSL, LHSH;
3204 ExpandOp(Op, LHSL, LHSH);
3205
3206 std::vector<SDOperand> Ops;
3207 Ops.push_back(LHSL);
3208 Ops.push_back(LHSH);
3209 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003210 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003211 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003212 Hi = Lo.getValue(1);
3213}
3214
3215
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003216/// ExpandShift - Try to find a clever way to expand this shift operation out to
3217/// smaller elements. If we can't find a way that is more efficient than a
3218/// libcall on this target, return false. Otherwise, return true with the
3219/// low-parts expanded into Lo and Hi.
3220bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3221 SDOperand &Lo, SDOperand &Hi) {
3222 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3223 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003224
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003225 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003226 SDOperand ShAmt = LegalizeOp(Amt);
3227 MVT::ValueType ShTy = ShAmt.getValueType();
3228 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3229 unsigned NVTBits = MVT::getSizeInBits(NVT);
3230
3231 // Handle the case when Amt is an immediate. Other cases are currently broken
3232 // and are disabled.
3233 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3234 unsigned Cst = CN->getValue();
3235 // Expand the incoming operand to be shifted, so that we have its parts
3236 SDOperand InL, InH;
3237 ExpandOp(Op, InL, InH);
3238 switch(Opc) {
3239 case ISD::SHL:
3240 if (Cst > VTBits) {
3241 Lo = DAG.getConstant(0, NVT);
3242 Hi = DAG.getConstant(0, NVT);
3243 } else if (Cst > NVTBits) {
3244 Lo = DAG.getConstant(0, NVT);
3245 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003246 } else if (Cst == NVTBits) {
3247 Lo = DAG.getConstant(0, NVT);
3248 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003249 } else {
3250 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3251 Hi = DAG.getNode(ISD::OR, NVT,
3252 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3253 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3254 }
3255 return true;
3256 case ISD::SRL:
3257 if (Cst > VTBits) {
3258 Lo = DAG.getConstant(0, NVT);
3259 Hi = DAG.getConstant(0, NVT);
3260 } else if (Cst > NVTBits) {
3261 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3262 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003263 } else if (Cst == NVTBits) {
3264 Lo = InH;
3265 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003266 } else {
3267 Lo = DAG.getNode(ISD::OR, NVT,
3268 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3269 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3270 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3271 }
3272 return true;
3273 case ISD::SRA:
3274 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003275 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003276 DAG.getConstant(NVTBits-1, ShTy));
3277 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003278 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003279 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003280 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003281 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003282 } else if (Cst == NVTBits) {
3283 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003284 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003285 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003286 } else {
3287 Lo = DAG.getNode(ISD::OR, NVT,
3288 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3289 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3290 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3291 }
3292 return true;
3293 }
3294 }
Nate Begemanb0674922005-04-06 21:13:14 +00003295 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003296}
Chris Lattneraac464e2005-01-21 06:05:23 +00003297
Chris Lattner4add7e32005-01-23 04:42:50 +00003298
Chris Lattneraac464e2005-01-21 06:05:23 +00003299// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3300// does not fit into a register, return the lo part and set the hi part to the
3301// by-reg argument. If it does fit into a single register, return the result
3302// and leave the Hi part unset.
3303SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3304 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003305 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3306 // The input chain to this libcall is the entry node of the function.
3307 // Legalizing the call will automatically add the previous call to the
3308 // dependence.
3309 SDOperand InChain = DAG.getEntryNode();
3310
Chris Lattneraac464e2005-01-21 06:05:23 +00003311 TargetLowering::ArgListTy Args;
3312 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3313 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3314 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3315 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3316 }
3317 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003318
Chris Lattner06bbeb62005-05-11 19:02:11 +00003319 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003320 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003321 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003322 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3323 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003324
Chris Lattner462505f2006-02-13 09:18:02 +00003325 // Legalize the call sequence, starting with the chain. This will advance
3326 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3327 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3328 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003329 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003330 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003331 default: assert(0 && "Unknown thing");
3332 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003333 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003334 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003335 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003336 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003337 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003338 }
Chris Lattner63022662005-09-02 20:26:58 +00003339 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003340}
3341
Chris Lattner4add7e32005-01-23 04:42:50 +00003342
Chris Lattneraac464e2005-01-21 06:05:23 +00003343/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3344/// destination type is legal.
3345SDOperand SelectionDAGLegalize::
3346ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003347 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003348 assert(getTypeAction(Source.getValueType()) == Expand &&
3349 "This is not an expansion!");
3350 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3351
Chris Lattner06bbeb62005-05-11 19:02:11 +00003352 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003353 assert(Source.getValueType() == MVT::i64 &&
3354 "This only works for 64-bit -> FP");
3355 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3356 // incoming integer is set. To handle this, we dynamically test to see if
3357 // it is set, and, if so, add a fudge factor.
3358 SDOperand Lo, Hi;
3359 ExpandOp(Source, Lo, Hi);
3360
Chris Lattner2a4f7312005-05-13 04:45:13 +00003361 // If this is unsigned, and not supported, first perform the conversion to
3362 // signed, then adjust the result if the sign bit is set.
3363 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3364 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3365
Chris Lattnerd47675e2005-08-09 20:20:18 +00003366 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3367 DAG.getConstant(0, Hi.getValueType()),
3368 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003369 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3370 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3371 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003372 uint64_t FF = 0x5f800000ULL;
3373 if (TLI.isLittleEndian()) FF <<= 32;
3374 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003375
Chris Lattnerc30405e2005-08-26 17:15:30 +00003376 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003377 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3378 SDOperand FudgeInReg;
3379 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003380 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3381 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003382 else {
3383 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003384 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3385 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003386 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003387 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003388 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003389
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003390 // Check to see if the target has a custom way to lower this. If so, use it.
3391 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3392 default: assert(0 && "This action not implemented for this operation!");
3393 case TargetLowering::Legal:
3394 case TargetLowering::Expand:
3395 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003396 case TargetLowering::Custom: {
3397 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3398 Source), DAG);
3399 if (NV.Val)
3400 return LegalizeOp(NV);
3401 break; // The target decided this was legal after all
3402 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003403 }
3404
Chris Lattner153587e2005-05-12 07:00:44 +00003405 // Expand the source, then glue it back together for the call. We must expand
3406 // the source in case it is shared (this pass of legalize must traverse it).
3407 SDOperand SrcLo, SrcHi;
3408 ExpandOp(Source, SrcLo, SrcHi);
3409 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3410
Chris Lattner06bbeb62005-05-11 19:02:11 +00003411 const char *FnName = 0;
3412 if (DestTy == MVT::f32)
3413 FnName = "__floatdisf";
3414 else {
3415 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3416 FnName = "__floatdidf";
3417 }
Chris Lattner462505f2006-02-13 09:18:02 +00003418
3419 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3420 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003421 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003422}
Misha Brukman835702a2005-04-21 22:36:52 +00003423
Chris Lattner689bdcc2006-01-28 08:25:58 +00003424/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3425/// INT_TO_FP operation of the specified operand when the target requests that
3426/// we expand it. At this point, we know that the result and operand types are
3427/// legal for the target.
3428SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3429 SDOperand Op0,
3430 MVT::ValueType DestVT) {
3431 if (Op0.getValueType() == MVT::i32) {
3432 // simple 32-bit [signed|unsigned] integer to float/double expansion
3433
3434 // get the stack frame index of a 8 byte buffer
3435 MachineFunction &MF = DAG.getMachineFunction();
3436 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3437 // get address of 8 byte buffer
3438 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3439 // word offset constant for Hi/Lo address computation
3440 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3441 // set up Hi and Lo (into buffer) address based on endian
3442 SDOperand Hi, Lo;
3443 if (TLI.isLittleEndian()) {
3444 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3445 Lo = StackSlot;
3446 } else {
3447 Hi = StackSlot;
3448 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3449 }
3450 // if signed map to unsigned space
3451 SDOperand Op0Mapped;
3452 if (isSigned) {
3453 // constant used to invert sign bit (signed to unsigned mapping)
3454 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3455 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3456 } else {
3457 Op0Mapped = Op0;
3458 }
3459 // store the lo of the constructed double - based on integer input
3460 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3461 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3462 // initial hi portion of constructed double
3463 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3464 // store the hi of the constructed double - biased exponent
3465 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3466 InitialHi, Hi, DAG.getSrcValue(NULL));
3467 // load the constructed double
3468 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3469 DAG.getSrcValue(NULL));
3470 // FP constant to bias correct the final result
3471 SDOperand Bias = DAG.getConstantFP(isSigned ?
3472 BitsToDouble(0x4330000080000000ULL)
3473 : BitsToDouble(0x4330000000000000ULL),
3474 MVT::f64);
3475 // subtract the bias
3476 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3477 // final result
3478 SDOperand Result;
3479 // handle final rounding
3480 if (DestVT == MVT::f64) {
3481 // do nothing
3482 Result = Sub;
3483 } else {
3484 // if f32 then cast to f32
3485 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3486 }
3487 return Result;
3488 }
3489 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3490 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3491
3492 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3493 DAG.getConstant(0, Op0.getValueType()),
3494 ISD::SETLT);
3495 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3496 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3497 SignSet, Four, Zero);
3498
3499 // If the sign bit of the integer is set, the large number will be treated
3500 // as a negative number. To counteract this, the dynamic code adds an
3501 // offset depending on the data type.
3502 uint64_t FF;
3503 switch (Op0.getValueType()) {
3504 default: assert(0 && "Unsupported integer type!");
3505 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3506 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3507 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3508 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3509 }
3510 if (TLI.isLittleEndian()) FF <<= 32;
3511 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3512
3513 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3514 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3515 SDOperand FudgeInReg;
3516 if (DestVT == MVT::f32)
3517 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3518 DAG.getSrcValue(NULL));
3519 else {
3520 assert(DestVT == MVT::f64 && "Unexpected conversion");
3521 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3522 DAG.getEntryNode(), CPIdx,
3523 DAG.getSrcValue(NULL), MVT::f32));
3524 }
3525
3526 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3527}
3528
3529/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3530/// *INT_TO_FP operation of the specified operand when the target requests that
3531/// we promote it. At this point, we know that the result and operand types are
3532/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3533/// operation that takes a larger input.
3534SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3535 MVT::ValueType DestVT,
3536 bool isSigned) {
3537 // First step, figure out the appropriate *INT_TO_FP operation to use.
3538 MVT::ValueType NewInTy = LegalOp.getValueType();
3539
3540 unsigned OpToUse = 0;
3541
3542 // Scan for the appropriate larger type to use.
3543 while (1) {
3544 NewInTy = (MVT::ValueType)(NewInTy+1);
3545 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3546
3547 // If the target supports SINT_TO_FP of this type, use it.
3548 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3549 default: break;
3550 case TargetLowering::Legal:
3551 if (!TLI.isTypeLegal(NewInTy))
3552 break; // Can't use this datatype.
3553 // FALL THROUGH.
3554 case TargetLowering::Custom:
3555 OpToUse = ISD::SINT_TO_FP;
3556 break;
3557 }
3558 if (OpToUse) break;
3559 if (isSigned) continue;
3560
3561 // If the target supports UINT_TO_FP of this type, use it.
3562 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3563 default: break;
3564 case TargetLowering::Legal:
3565 if (!TLI.isTypeLegal(NewInTy))
3566 break; // Can't use this datatype.
3567 // FALL THROUGH.
3568 case TargetLowering::Custom:
3569 OpToUse = ISD::UINT_TO_FP;
3570 break;
3571 }
3572 if (OpToUse) break;
3573
3574 // Otherwise, try a larger type.
3575 }
3576
3577 // Okay, we found the operation and type to use. Zero extend our input to the
3578 // desired type then run the operation on it.
3579 return DAG.getNode(OpToUse, DestVT,
3580 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3581 NewInTy, LegalOp));
3582}
3583
3584/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3585/// FP_TO_*INT operation of the specified operand when the target requests that
3586/// we promote it. At this point, we know that the result and operand types are
3587/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3588/// operation that returns a larger result.
3589SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3590 MVT::ValueType DestVT,
3591 bool isSigned) {
3592 // First step, figure out the appropriate FP_TO*INT operation to use.
3593 MVT::ValueType NewOutTy = DestVT;
3594
3595 unsigned OpToUse = 0;
3596
3597 // Scan for the appropriate larger type to use.
3598 while (1) {
3599 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3600 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3601
3602 // If the target supports FP_TO_SINT returning this type, use it.
3603 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3604 default: break;
3605 case TargetLowering::Legal:
3606 if (!TLI.isTypeLegal(NewOutTy))
3607 break; // Can't use this datatype.
3608 // FALL THROUGH.
3609 case TargetLowering::Custom:
3610 OpToUse = ISD::FP_TO_SINT;
3611 break;
3612 }
3613 if (OpToUse) break;
3614
3615 // If the target supports FP_TO_UINT of this type, use it.
3616 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3617 default: break;
3618 case TargetLowering::Legal:
3619 if (!TLI.isTypeLegal(NewOutTy))
3620 break; // Can't use this datatype.
3621 // FALL THROUGH.
3622 case TargetLowering::Custom:
3623 OpToUse = ISD::FP_TO_UINT;
3624 break;
3625 }
3626 if (OpToUse) break;
3627
3628 // Otherwise, try a larger type.
3629 }
3630
3631 // Okay, we found the operation and type to use. Truncate the result of the
3632 // extended FP_TO_*INT operation to the desired size.
3633 return DAG.getNode(ISD::TRUNCATE, DestVT,
3634 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3635}
3636
3637/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3638///
3639SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3640 MVT::ValueType VT = Op.getValueType();
3641 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3642 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3643 switch (VT) {
3644 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3645 case MVT::i16:
3646 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3647 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3648 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3649 case MVT::i32:
3650 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3651 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3652 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3653 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3654 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3655 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3656 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3657 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3658 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3659 case MVT::i64:
3660 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3661 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3662 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3663 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3664 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3665 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3666 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3667 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3668 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3669 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3670 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3671 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3672 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3673 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3674 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3675 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3676 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3677 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3678 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3679 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3680 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3681 }
3682}
3683
3684/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3685///
3686SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3687 switch (Opc) {
3688 default: assert(0 && "Cannot expand this yet!");
3689 case ISD::CTPOP: {
3690 static const uint64_t mask[6] = {
3691 0x5555555555555555ULL, 0x3333333333333333ULL,
3692 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3693 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3694 };
3695 MVT::ValueType VT = Op.getValueType();
3696 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3697 unsigned len = getSizeInBits(VT);
3698 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3699 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3700 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3701 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3702 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3703 DAG.getNode(ISD::AND, VT,
3704 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3705 }
3706 return Op;
3707 }
3708 case ISD::CTLZ: {
3709 // for now, we do this:
3710 // x = x | (x >> 1);
3711 // x = x | (x >> 2);
3712 // ...
3713 // x = x | (x >>16);
3714 // x = x | (x >>32); // for 64-bit input
3715 // return popcount(~x);
3716 //
3717 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3718 MVT::ValueType VT = Op.getValueType();
3719 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3720 unsigned len = getSizeInBits(VT);
3721 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3722 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3723 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3724 }
3725 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3726 return DAG.getNode(ISD::CTPOP, VT, Op);
3727 }
3728 case ISD::CTTZ: {
3729 // for now, we use: { return popcount(~x & (x - 1)); }
3730 // unless the target has ctlz but not ctpop, in which case we use:
3731 // { return 32 - nlz(~x & (x-1)); }
3732 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3733 MVT::ValueType VT = Op.getValueType();
3734 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3735 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3736 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3737 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3738 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3739 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3740 TLI.isOperationLegal(ISD::CTLZ, VT))
3741 return DAG.getNode(ISD::SUB, VT,
3742 DAG.getConstant(getSizeInBits(VT), VT),
3743 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3744 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3745 }
3746 }
3747}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003748
3749
Chris Lattnerdc750592005-01-07 07:47:09 +00003750/// ExpandOp - Expand the specified SDOperand into its two component pieces
3751/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3752/// LegalizeNodes map is filled in for any results that are not expanded, the
3753/// ExpandedNodes map is filled in for any results that are expanded, and the
3754/// Lo/Hi values are returned.
3755void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3756 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003757 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003758 SDNode *Node = Op.Val;
3759 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003760 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3761 "Cannot expand FP values!");
3762 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003763 "Cannot expand to FP value or to larger int value!");
3764
Chris Lattner1a570f12005-09-02 20:32:45 +00003765 // See if we already expanded it.
3766 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3767 = ExpandedNodes.find(Op);
3768 if (I != ExpandedNodes.end()) {
3769 Lo = I->second.first;
3770 Hi = I->second.second;
3771 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003772 }
3773
Chris Lattnerdc750592005-01-07 07:47:09 +00003774 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003775 case ISD::CopyFromReg:
3776 assert(0 && "CopyFromReg must be legal!");
3777 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003778 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3779 assert(0 && "Do not know how to expand this operator!");
3780 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003781 case ISD::UNDEF:
3782 Lo = DAG.getNode(ISD::UNDEF, NVT);
3783 Hi = DAG.getNode(ISD::UNDEF, NVT);
3784 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003785 case ISD::Constant: {
3786 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3787 Lo = DAG.getConstant(Cst, NVT);
3788 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3789 break;
3790 }
Chris Lattner32e08b72005-03-28 22:03:13 +00003791 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003792 // Return the operands.
3793 Lo = Node->getOperand(0);
3794 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003795 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003796
3797 case ISD::SIGN_EXTEND_INREG:
3798 ExpandOp(Node->getOperand(0), Lo, Hi);
3799 // Sign extend the lo-part.
3800 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3801 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3802 TLI.getShiftAmountTy()));
3803 // sext_inreg the low part if needed.
3804 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3805 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003806
Nate Begeman2fba8a32006-01-14 03:14:10 +00003807 case ISD::BSWAP: {
3808 ExpandOp(Node->getOperand(0), Lo, Hi);
3809 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3810 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3811 Lo = TempLo;
3812 break;
3813 }
3814
Chris Lattner55e9cde2005-05-11 04:51:16 +00003815 case ISD::CTPOP:
3816 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003817 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3818 DAG.getNode(ISD::CTPOP, NVT, Lo),
3819 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003820 Hi = DAG.getConstant(0, NVT);
3821 break;
3822
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003823 case ISD::CTLZ: {
3824 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003825 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003826 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3827 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003828 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3829 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003830 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3831 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3832
3833 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3834 Hi = DAG.getConstant(0, NVT);
3835 break;
3836 }
3837
3838 case ISD::CTTZ: {
3839 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003840 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003841 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3842 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003843 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3844 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003845 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3846 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3847
3848 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3849 Hi = DAG.getConstant(0, NVT);
3850 break;
3851 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003852
Nate Begemane74795c2006-01-25 18:21:52 +00003853 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003854 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3855 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003856 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3857 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3858
3859 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003860 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003861 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3862 if (!TLI.isLittleEndian())
3863 std::swap(Lo, Hi);
3864 break;
3865 }
3866
Chris Lattnerdc750592005-01-07 07:47:09 +00003867 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003868 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3869 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003870 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003871
3872 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003873 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003874 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3875 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003876 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003877 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003878
3879 // Build a factor node to remember that this load is independent of the
3880 // other one.
3881 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3882 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003883
Chris Lattnerdc750592005-01-07 07:47:09 +00003884 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003885 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003886 if (!TLI.isLittleEndian())
3887 std::swap(Lo, Hi);
3888 break;
3889 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003890 case ISD::AND:
3891 case ISD::OR:
3892 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3893 SDOperand LL, LH, RL, RH;
3894 ExpandOp(Node->getOperand(0), LL, LH);
3895 ExpandOp(Node->getOperand(1), RL, RH);
3896 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3897 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3898 break;
3899 }
3900 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003901 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003902 ExpandOp(Node->getOperand(1), LL, LH);
3903 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003904 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3905 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003906 break;
3907 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003908 case ISD::SELECT_CC: {
3909 SDOperand TL, TH, FL, FH;
3910 ExpandOp(Node->getOperand(2), TL, TH);
3911 ExpandOp(Node->getOperand(3), FL, FH);
3912 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3913 Node->getOperand(1), TL, FL, Node->getOperand(4));
3914 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3915 Node->getOperand(1), TH, FH, Node->getOperand(4));
3916 break;
3917 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003918 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003919 SDOperand Chain = Node->getOperand(0);
3920 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003921 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3922
3923 if (EVT == NVT)
3924 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3925 else
3926 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3927 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003928
3929 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003930 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003931
Nate Begemanc3a89c52005-10-13 17:15:37 +00003932 // The high part is obtained by SRA'ing all but one of the bits of the lo
3933 // part.
3934 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3935 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3936 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003937 break;
3938 }
3939 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003940 SDOperand Chain = Node->getOperand(0);
3941 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003942 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3943
3944 if (EVT == NVT)
3945 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3946 else
3947 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3948 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003949
3950 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003951 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003952
Nate Begemanc3a89c52005-10-13 17:15:37 +00003953 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003954 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003955 break;
3956 }
3957 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003958 SDOperand Chain = Node->getOperand(0);
3959 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003960 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3961
3962 if (EVT == NVT)
3963 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3964 else
3965 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3966 EVT);
3967
3968 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003969 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003970
3971 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003972 Hi = DAG.getNode(ISD::UNDEF, NVT);
3973 break;
3974 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003975 case ISD::ANY_EXTEND:
3976 // The low part is any extension of the input (which degenerates to a copy).
3977 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3978 // The high part is undefined.
3979 Hi = DAG.getNode(ISD::UNDEF, NVT);
3980 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003981 case ISD::SIGN_EXTEND: {
3982 // The low part is just a sign extension of the input (which degenerates to
3983 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003984 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003985
Chris Lattnerdc750592005-01-07 07:47:09 +00003986 // The high part is obtained by SRA'ing all but one of the bits of the lo
3987 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003988 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003989 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3990 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003991 break;
3992 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003993 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003994 // The low part is just a zero extension of the input (which degenerates to
3995 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003996 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003997
Chris Lattnerdc750592005-01-07 07:47:09 +00003998 // The high part is just a zero.
3999 Hi = DAG.getConstant(0, NVT);
4000 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004001
4002 case ISD::BIT_CONVERT: {
4003 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4004 Node->getOperand(0));
4005 ExpandOp(Tmp, Lo, Hi);
4006 break;
4007 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004008
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004009 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004010 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4011 TargetLowering::Custom &&
4012 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004013 Lo = TLI.LowerOperation(Op, DAG);
4014 assert(Lo.Val && "Node must be custom expanded!");
4015 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004016 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004017 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004018 break;
4019
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004020 // These operators cannot be expanded directly, emit them as calls to
4021 // library functions.
4022 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004023 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004024 SDOperand Op;
4025 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4026 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004027 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4028 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004029 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004030
Chris Lattner941d84a2005-07-30 01:40:57 +00004031 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4032
Chris Lattnerfe68d752005-07-29 00:33:32 +00004033 // Now that the custom expander is done, expand the result, which is still
4034 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004035 if (Op.Val) {
4036 ExpandOp(Op, Lo, Hi);
4037 break;
4038 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004039 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004040
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004041 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004042 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004043 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004044 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004045 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004046
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004047 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004048 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004049 SDOperand Op;
4050 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4051 case Expand: assert(0 && "cannot expand FP!");
4052 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4053 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4054 }
4055
4056 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4057
4058 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004059 if (Op.Val) {
4060 ExpandOp(Op, Lo, Hi);
4061 break;
4062 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004063 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004064
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004065 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004066 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004067 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004068 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004069 break;
4070
Evan Cheng870e4f82006-01-09 18:31:59 +00004071 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004072 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004073 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004074 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004075 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004076 Op = TLI.LowerOperation(Op, DAG);
4077 if (Op.Val) {
4078 // Now that the custom expander is done, expand the result, which is
4079 // still VT.
4080 ExpandOp(Op, Lo, Hi);
4081 break;
4082 }
4083 }
4084
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004085 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004086 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004087 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004088
4089 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004090 TargetLowering::LegalizeAction Action =
4091 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4092 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4093 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004094 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004095 break;
4096 }
4097
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004098 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004099 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004100 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004101 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004102
Evan Cheng870e4f82006-01-09 18:31:59 +00004103 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004104 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004105 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004106 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004107 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004108 Op = TLI.LowerOperation(Op, DAG);
4109 if (Op.Val) {
4110 // Now that the custom expander is done, expand the result, which is
4111 // still VT.
4112 ExpandOp(Op, Lo, Hi);
4113 break;
4114 }
4115 }
4116
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004117 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004118 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004119 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004120
4121 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004122 TargetLowering::LegalizeAction Action =
4123 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4124 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4125 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004126 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004127 break;
4128 }
4129
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004130 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004131 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004132 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004133 }
4134
4135 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004136 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004137 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004138 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004139 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004140 Op = TLI.LowerOperation(Op, DAG);
4141 if (Op.Val) {
4142 // Now that the custom expander is done, expand the result, which is
4143 // still VT.
4144 ExpandOp(Op, Lo, Hi);
4145 break;
4146 }
4147 }
4148
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004149 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004150 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004151 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004152
4153 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004154 TargetLowering::LegalizeAction Action =
4155 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4156 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4157 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004158 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004159 break;
4160 }
4161
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004162 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004163 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004164 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004165 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004166
Misha Brukman835702a2005-04-21 22:36:52 +00004167 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004168 case ISD::SUB: {
4169 // If the target wants to custom expand this, let them.
4170 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4171 TargetLowering::Custom) {
4172 Op = TLI.LowerOperation(Op, DAG);
4173 if (Op.Val) {
4174 ExpandOp(Op, Lo, Hi);
4175 break;
4176 }
4177 }
4178
4179 // Expand the subcomponents.
4180 SDOperand LHSL, LHSH, RHSL, RHSH;
4181 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4182 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004183 std::vector<MVT::ValueType> VTs;
4184 std::vector<SDOperand> LoOps, HiOps;
4185 VTs.push_back(LHSL.getValueType());
4186 VTs.push_back(MVT::Flag);
4187 LoOps.push_back(LHSL);
4188 LoOps.push_back(RHSL);
4189 HiOps.push_back(LHSH);
4190 HiOps.push_back(RHSH);
4191 if (Node->getOpcode() == ISD::ADD) {
4192 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4193 HiOps.push_back(Lo.getValue(1));
4194 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4195 } else {
4196 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4197 HiOps.push_back(Lo.getValue(1));
4198 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4199 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004200 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004201 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004202 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004203 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004204 SDOperand LL, LH, RL, RH;
4205 ExpandOp(Node->getOperand(0), LL, LH);
4206 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004207 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4208 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4209 // extended the sign bit of the low half through the upper half, and if so
4210 // emit a MULHS instead of the alternate sequence that is valid for any
4211 // i64 x i64 multiply.
4212 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4213 // is RH an extension of the sign bit of RL?
4214 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4215 RH.getOperand(1).getOpcode() == ISD::Constant &&
4216 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4217 // is LH an extension of the sign bit of LL?
4218 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4219 LH.getOperand(1).getOpcode() == ISD::Constant &&
4220 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4221 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4222 } else {
4223 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4224 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4225 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4226 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4227 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4228 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004229 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4230 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004231 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004232 }
4233 break;
4234 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004235 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4236 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4237 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4238 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004239 }
4240
Chris Lattnerac12f682005-12-21 18:02:52 +00004241 // Make sure the resultant values have been legalized themselves, unless this
4242 // is a type that requires multi-step expansion.
4243 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4244 Lo = LegalizeOp(Lo);
4245 Hi = LegalizeOp(Hi);
4246 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004247
4248 // Remember in a map if the values will be reused later.
4249 bool isNew =
4250 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4251 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004252}
4253
Chris Lattner32206f52006-03-18 01:44:44 +00004254/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4255/// two smaller values of MVT::Vector type.
4256void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4257 SDOperand &Hi) {
4258 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4259 SDNode *Node = Op.Val;
4260 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4261 assert(NumElements > 1 && "Cannot split a single element vector!");
4262 unsigned NewNumElts = NumElements/2;
4263 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4264 SDOperand TypeNode = *(Node->op_end()-1);
4265
4266 // See if we already split it.
4267 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4268 = SplitNodes.find(Op);
4269 if (I != SplitNodes.end()) {
4270 Lo = I->second.first;
4271 Hi = I->second.second;
4272 return;
4273 }
4274
4275 switch (Node->getOpcode()) {
Chris Lattner93640542006-03-19 00:07:49 +00004276 default: assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004277 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004278 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4279 LoOps.push_back(NewNumEltsNode);
4280 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004281 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004282
4283 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4284 HiOps.push_back(NewNumEltsNode);
4285 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004286 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004287 break;
4288 }
4289 case ISD::VADD:
4290 case ISD::VSUB:
4291 case ISD::VMUL:
4292 case ISD::VSDIV:
4293 case ISD::VUDIV:
4294 case ISD::VAND:
4295 case ISD::VOR:
4296 case ISD::VXOR: {
4297 SDOperand LL, LH, RL, RH;
4298 SplitVectorOp(Node->getOperand(0), LL, LH);
4299 SplitVectorOp(Node->getOperand(1), RL, RH);
4300
4301 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4302 NewNumEltsNode, TypeNode);
4303 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4304 NewNumEltsNode, TypeNode);
4305 break;
4306 }
4307 case ISD::VLOAD: {
4308 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4309 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4310 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4311
4312 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4313 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4314 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4315 getIntPtrConstant(IncrementSize));
4316 // FIXME: This creates a bogus srcvalue!
4317 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4318
4319 // Build a factor node to remember that this load is independent of the
4320 // other one.
4321 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4322 Hi.getValue(1));
4323
4324 // Remember that we legalized the chain.
4325 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4326 if (!TLI.isLittleEndian())
4327 std::swap(Lo, Hi);
4328 break;
4329 }
4330 }
4331
4332 // Remember in a map if the values will be reused later.
4333 bool isNew =
4334 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4335 assert(isNew && "Value already expanded?!?");
4336}
4337
4338
4339/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4340/// equivalent operation that returns a scalar (e.g. F32) or packed value
4341/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4342/// type for the result.
4343SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4344 MVT::ValueType NewVT) {
Chris Lattner672a42d2006-03-21 19:20:37 +00004345 // FIXME: THIS IS A TEMPORARY HACK
4346 if (Op.getValueType() == NewVT) return Op;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00004347
Chris Lattner32206f52006-03-18 01:44:44 +00004348 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4349 SDNode *Node = Op.Val;
4350
4351 // See if we already packed it.
4352 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4353 if (I != PackedNodes.end()) return I->second;
4354
4355 SDOperand Result;
4356 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004357 default:
4358 Node->dump(); std::cerr << "\n";
4359 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004360 case ISD::VADD:
4361 case ISD::VSUB:
4362 case ISD::VMUL:
4363 case ISD::VSDIV:
4364 case ISD::VUDIV:
4365 case ISD::VAND:
4366 case ISD::VOR:
4367 case ISD::VXOR:
4368 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4369 NewVT,
4370 PackVectorOp(Node->getOperand(0), NewVT),
4371 PackVectorOp(Node->getOperand(1), NewVT));
4372 break;
4373 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004374 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4375 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004376
Chris Lattner93640542006-03-19 00:07:49 +00004377 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004378
4379 // Remember that we legalized the chain.
4380 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4381 break;
4382 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004383 case ISD::VBUILD_VECTOR:
Chris Lattner32206f52006-03-18 01:44:44 +00004384 if (!MVT::isVector(NewVT)) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004385 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004386 Result = Node->getOperand(0);
4387 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004388 // Returning a BUILD_VECTOR?
Chris Lattner32206f52006-03-18 01:44:44 +00004389 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004390 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattner32206f52006-03-18 01:44:44 +00004391 }
4392 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004393 case ISD::VINSERT_VECTOR_ELT:
4394 if (!MVT::isVector(NewVT)) {
4395 // Returning a scalar? Must be the inserted element.
4396 Result = Node->getOperand(1);
4397 } else {
4398 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4399 PackVectorOp(Node->getOperand(0), NewVT),
4400 Node->getOperand(1), Node->getOperand(2));
4401 }
4402 break;
Chris Lattner32206f52006-03-18 01:44:44 +00004403 }
4404
4405 if (TLI.isTypeLegal(NewVT))
4406 Result = LegalizeOp(Result);
4407 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4408 assert(isNew && "Value already packed?");
4409 return Result;
4410}
4411
Chris Lattnerdc750592005-01-07 07:47:09 +00004412
4413// SelectionDAG::Legalize - This is the entry point for the file.
4414//
Chris Lattner4add7e32005-01-23 04:42:50 +00004415void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004416 /// run - This is the main entry point to this class.
4417 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004418 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004419}
4420