blob: 27afe117e6f62d83e61eb659552465c93803c48d [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"
Chris Lattnerdc750592005-01-07 07:47:09 +000017#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000018#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000019#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000020#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000021#include "llvm/Constants.h"
Chris Lattneref598052006-04-02 03:07:27 +000022#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/CommandLine.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000024#include <iostream>
Evan Cheng1d2e9952006-03-24 01:17:21 +000025#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000026using namespace llvm;
27
Chris Lattneref598052006-04-02 03:07:27 +000028#ifndef NDEBUG
29static cl::opt<bool>
30ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
31 cl::desc("Pop up a window to show dags before legalize"));
32#else
33static const bool ViewLegalizeDAGs = 0;
34#endif
35
Chris Lattnerdc750592005-01-07 07:47:09 +000036//===----------------------------------------------------------------------===//
37/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
38/// hacks on it until the target machine can handle it. This involves
39/// eliminating value sizes the machine cannot handle (promoting small sizes to
40/// large sizes or splitting up large values into small values) as well as
41/// eliminating operations the machine cannot handle.
42///
43/// This code also does a small amount of optimization and recognition of idioms
44/// as part of its processing. For example, if a target does not support a
45/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
46/// will attempt merge setcc and brc instructions into brcc's.
47///
48namespace {
49class SelectionDAGLegalize {
50 TargetLowering &TLI;
51 SelectionDAG &DAG;
52
Chris Lattner462505f2006-02-13 09:18:02 +000053 // Libcall insertion helpers.
54
55 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
56 /// legalized. We use this to ensure that calls are properly serialized
57 /// against each other, including inserted libcalls.
58 SDOperand LastCALLSEQ_END;
59
60 /// IsLegalizingCall - This member is used *only* for purposes of providing
61 /// helpful assertions that a libcall isn't created while another call is
62 /// being legalized (which could lead to non-serialized call sequences).
63 bool IsLegalizingCall;
64
Chris Lattnerdc750592005-01-07 07:47:09 +000065 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000066 Legal, // The target natively supports this operation.
67 Promote, // This operation should be executed in a larger type.
68 Expand, // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000069 };
Chris Lattner462505f2006-02-13 09:18:02 +000070
Chris Lattnerdc750592005-01-07 07:47:09 +000071 /// ValueTypeActions - This is a bitvector that contains two bits for each
72 /// value type, where the two bits correspond to the LegalizeAction enum.
73 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000074 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000075
Chris Lattnerdc750592005-01-07 07:47:09 +000076 /// LegalizedNodes - For nodes that are of legal width, and that have more
77 /// than one use, this map indicates what regularized operand to use. This
78 /// allows us to avoid legalizing the same thing more than once.
79 std::map<SDOperand, SDOperand> LegalizedNodes;
80
Chris Lattner1f2c9d82005-01-15 05:21:40 +000081 /// PromotedNodes - For nodes that are below legal width, and that have more
82 /// than one use, this map indicates what promoted value to use. This allows
83 /// us to avoid promoting the same thing more than once.
84 std::map<SDOperand, SDOperand> PromotedNodes;
85
Chris Lattner32206f52006-03-18 01:44:44 +000086 /// ExpandedNodes - For nodes that need to be expanded this map indicates
87 /// which which operands are the expanded version of the input. This allows
88 /// us to avoid expanding the same node more than once.
Chris Lattnerdc750592005-01-07 07:47:09 +000089 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
90
Chris Lattner32206f52006-03-18 01:44:44 +000091 /// SplitNodes - For vector nodes that need to be split, this map indicates
92 /// which which operands are the split version of the input. This allows us
93 /// to avoid splitting the same node more than once.
94 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
95
96 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
97 /// concrete packed types, this contains the mapping of ones we have already
98 /// processed to the result.
99 std::map<SDOperand, SDOperand> PackedNodes;
100
Chris Lattnerea4ca942005-01-07 22:28:47 +0000101 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000102 LegalizedNodes.insert(std::make_pair(From, To));
103 // If someone requests legalization of the new node, return itself.
104 if (From != To)
105 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000106 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000107 void AddPromotedOperand(SDOperand From, SDOperand To) {
108 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
109 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000110 // If someone requests legalization of the new node, return itself.
111 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000112 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000113
Chris Lattnerdc750592005-01-07 07:47:09 +0000114public:
115
Chris Lattner4add7e32005-01-23 04:42:50 +0000116 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000117
Chris Lattnerdc750592005-01-07 07:47:09 +0000118 /// getTypeAction - Return how we should legalize values of this type, either
119 /// it is already legal or we need to expand it into multiple registers of
120 /// smaller integer type, or we need to promote it to a larger type.
121 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000122 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123 }
124
125 /// isTypeLegal - Return true if this type is legal on this target.
126 ///
127 bool isTypeLegal(MVT::ValueType VT) const {
128 return getTypeAction(VT) == Legal;
129 }
130
Chris Lattnerdc750592005-01-07 07:47:09 +0000131 void LegalizeDAG();
132
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000133private:
Chris Lattner32206f52006-03-18 01:44:44 +0000134 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
135 /// appropriate for its type.
136 void HandleOp(SDOperand Op);
137
138 /// LegalizeOp - We know that the specified value has a legal type.
139 /// Recursively ensure that the operands have legal types, then return the
140 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000141 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000142
143 /// PromoteOp - Given an operation that produces a value in an invalid type,
144 /// promote it to compute the value into a larger type. The produced value
145 /// will have the correct bits for the low portion of the register, but no
146 /// guarantee is made about the top bits: it may be zero, sign-extended, or
147 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000148 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000149
Chris Lattner32206f52006-03-18 01:44:44 +0000150 /// ExpandOp - Expand the specified SDOperand into its two component pieces
151 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
152 /// the LegalizeNodes map is filled in for any results that are not expanded,
153 /// the ExpandedNodes map is filled in for any results that are expanded, and
154 /// the Lo/Hi values are returned. This applies to integer types and Vector
155 /// types.
156 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
157
158 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
159 /// two smaller values of MVT::Vector type.
160 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
161
162 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
163 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
164 /// this is called, we know that PackedVT is the right type for the result and
165 /// we know that this type is legal for the target.
166 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
167
Chris Lattner462505f2006-02-13 09:18:02 +0000168 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
169
Nate Begeman7e7f4392006-02-01 07:19:44 +0000170 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
171
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000172 SDOperand CreateStackTemporary(MVT::ValueType VT);
173
Chris Lattneraac464e2005-01-21 06:05:23 +0000174 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
175 SDOperand &Hi);
176 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
177 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000178
Chris Lattner36e663d2005-12-23 00:16:34 +0000179 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000180 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000181 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
182 SDOperand LegalOp,
183 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000184 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
185 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000186 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
187 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000188
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000189 SDOperand ExpandBSWAP(SDOperand Op);
190 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000191 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
192 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000193 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
194 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000195
Chris Lattner6f423252006-03-31 17:55:51 +0000196 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
197
Chris Lattnerdc750592005-01-07 07:47:09 +0000198 SDOperand getIntPtrConstant(uint64_t Val) {
199 return DAG.getConstant(Val, TLI.getPointerTy());
200 }
201};
202}
203
Chris Lattner32206f52006-03-18 01:44:44 +0000204/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
205/// specified vector opcode.
Chris Lattner301015a2005-11-19 05:51:46 +0000206static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000207 switch (VecOp) {
208 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3bf916d2006-03-03 07:01:07 +0000209 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
210 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
211 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
212 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
213 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
214 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
215 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
216 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begemanb2e089c2005-11-19 00:36:38 +0000217 }
218}
Chris Lattnerdc750592005-01-07 07:47:09 +0000219
Chris Lattner4add7e32005-01-23 04:42:50 +0000220SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
221 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
222 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000223 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000224 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000225}
226
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000227/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
228/// not been visited yet and if all of its operands have already been visited.
229static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
230 std::map<SDNode*, unsigned> &Visited) {
231 if (++Visited[N] != N->getNumOperands())
232 return; // Haven't visited all operands yet
233
234 Order.push_back(N);
235
236 if (N->hasOneUse()) { // Tail recurse in common case.
237 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
238 return;
239 }
240
241 // Now that we have N in, add anything that uses it if all of their operands
242 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000243 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
244 ComputeTopDownOrdering(*UI, Order, Visited);
245}
246
Chris Lattner44fe26f2005-07-29 00:11:56 +0000247
Chris Lattnerdc750592005-01-07 07:47:09 +0000248void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000249 LastCALLSEQ_END = DAG.getEntryNode();
250 IsLegalizingCall = false;
251
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000252 // The legalize process is inherently a bottom-up recursive process (users
253 // legalize their uses before themselves). Given infinite stack space, we
254 // could just start legalizing on the root and traverse the whole graph. In
255 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000256 // blocks. To avoid this problem, compute an ordering of the nodes where each
257 // node is only legalized after all of its operands are legalized.
258 std::map<SDNode*, unsigned> Visited;
259 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000260
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000261 // Compute ordering from all of the leaves in the graphs, those (like the
262 // entry node) that have no operands.
263 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
264 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000265 if (I->getNumOperands() == 0) {
266 Visited[I] = 0 - 1U;
267 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000268 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000269 }
270
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000271 assert(Order.size() == Visited.size() &&
272 Order.size() ==
273 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000274 "Error: DAG is cyclic!");
275 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000276
Chris Lattner32206f52006-03-18 01:44:44 +0000277 for (unsigned i = 0, e = Order.size(); i != e; ++i)
278 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000279
280 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000281 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000282 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
283 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000284
285 ExpandedNodes.clear();
286 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000287 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000288 SplitNodes.clear();
289 PackedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000290
291 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000292 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000293}
294
Chris Lattner462505f2006-02-13 09:18:02 +0000295
296/// FindCallEndFromCallStart - Given a chained node that is part of a call
297/// sequence, find the CALLSEQ_END node that terminates the call sequence.
298static SDNode *FindCallEndFromCallStart(SDNode *Node) {
299 if (Node->getOpcode() == ISD::CALLSEQ_END)
300 return Node;
301 if (Node->use_empty())
302 return 0; // No CallSeqEnd
303
304 // The chain is usually at the end.
305 SDOperand TheChain(Node, Node->getNumValues()-1);
306 if (TheChain.getValueType() != MVT::Other) {
307 // Sometimes it's at the beginning.
308 TheChain = SDOperand(Node, 0);
309 if (TheChain.getValueType() != MVT::Other) {
310 // Otherwise, hunt for it.
311 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
312 if (Node->getValueType(i) == MVT::Other) {
313 TheChain = SDOperand(Node, i);
314 break;
315 }
316
317 // Otherwise, we walked into a node without a chain.
318 if (TheChain.getValueType() != MVT::Other)
319 return 0;
320 }
321 }
322
323 for (SDNode::use_iterator UI = Node->use_begin(),
324 E = Node->use_end(); UI != E; ++UI) {
325
326 // Make sure to only follow users of our token chain.
327 SDNode *User = *UI;
328 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
329 if (User->getOperand(i) == TheChain)
330 if (SDNode *Result = FindCallEndFromCallStart(User))
331 return Result;
332 }
333 return 0;
334}
335
336/// FindCallStartFromCallEnd - Given a chained node that is part of a call
337/// sequence, find the CALLSEQ_START node that initiates the call sequence.
338static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
339 assert(Node && "Didn't find callseq_start for a call??");
340 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
341
342 assert(Node->getOperand(0).getValueType() == MVT::Other &&
343 "Node doesn't have a token chain argument!");
344 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
345}
346
347/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
348/// see if any uses can reach Dest. If no dest operands can get to dest,
349/// legalize them, legalize ourself, and return false, otherwise, return true.
350bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
351 SDNode *Dest) {
352 if (N == Dest) return true; // N certainly leads to Dest :)
353
354 // If the first result of this node has been already legalized, then it cannot
355 // reach N.
356 switch (getTypeAction(N->getValueType(0))) {
357 case Legal:
358 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
359 break;
360 case Promote:
361 if (PromotedNodes.count(SDOperand(N, 0))) return false;
362 break;
363 case Expand:
364 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
365 break;
366 }
367
368 // Okay, this node has not already been legalized. Check and legalize all
369 // operands. If none lead to Dest, then we can legalize this node.
370 bool OperandsLeadToDest = false;
371 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
372 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
373 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
374
375 if (OperandsLeadToDest) return true;
376
377 // Okay, this node looks safe, legalize it and return false.
378 switch (getTypeAction(N->getValueType(0))) {
379 case Legal:
380 LegalizeOp(SDOperand(N, 0));
381 break;
382 case Promote:
383 PromoteOp(SDOperand(N, 0));
384 break;
385 case Expand: {
386 SDOperand X, Y;
387 ExpandOp(SDOperand(N, 0), X, Y);
388 break;
389 }
390 }
391 return false;
392}
393
Chris Lattner32206f52006-03-18 01:44:44 +0000394/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
395/// appropriate for its type.
396void SelectionDAGLegalize::HandleOp(SDOperand Op) {
397 switch (getTypeAction(Op.getValueType())) {
398 default: assert(0 && "Bad type action!");
399 case Legal: LegalizeOp(Op); break;
400 case Promote: PromoteOp(Op); break;
401 case Expand:
402 if (Op.getValueType() != MVT::Vector) {
403 SDOperand X, Y;
404 ExpandOp(Op, X, Y);
405 } else {
406 SDNode *N = Op.Val;
407 unsigned NumOps = N->getNumOperands();
408 unsigned NumElements =
409 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
410 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
411 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
412 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
413 // In the common case, this is a legal vector type, convert it to the
414 // packed operation and type now.
415 PackVectorOp(Op, PackedVT);
416 } else if (NumElements == 1) {
417 // Otherwise, if this is a single element vector, convert it to a
418 // scalar operation.
419 PackVectorOp(Op, EVT);
420 } else {
421 // Otherwise, this is a multiple element vector that isn't supported.
422 // Split it in half and legalize both parts.
423 SDOperand X, Y;
Chris Lattner93640542006-03-19 00:07:49 +0000424 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000425 }
426 }
427 break;
428 }
429}
Chris Lattner462505f2006-02-13 09:18:02 +0000430
431
Chris Lattner32206f52006-03-18 01:44:44 +0000432/// LegalizeOp - We know that the specified value has a legal type.
433/// Recursively ensure that the operands have legal types, then return the
434/// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000435SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000436 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000437 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000438 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000439
Chris Lattnerdc750592005-01-07 07:47:09 +0000440 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000441 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000442 if (Node->getNumValues() > 1) {
443 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000444 if (getTypeAction(Node->getValueType(i)) != Legal) {
445 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000446 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000447 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000448 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000449 }
450 }
451
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000452 // Note that LegalizeOp may be reentered even from single-use nodes, which
453 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000454 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
455 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000456
Nate Begemane5b86d72005-08-10 20:51:12 +0000457 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000458 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000459 bool isCustom = false;
460
Chris Lattnerdc750592005-01-07 07:47:09 +0000461 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000462 case ISD::FrameIndex:
463 case ISD::EntryToken:
464 case ISD::Register:
465 case ISD::BasicBlock:
466 case ISD::TargetFrameIndex:
467 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000468 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000469 case ISD::TargetConstantPool:
470 case ISD::TargetGlobalAddress:
471 case ISD::TargetExternalSymbol:
472 case ISD::VALUETYPE:
473 case ISD::SRCVALUE:
474 case ISD::STRING:
475 case ISD::CONDCODE:
476 // Primitives must all be legal.
477 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
478 "This must be legal!");
479 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000480 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000481 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
482 // If this is a target node, legalize it by legalizing the operands then
483 // passing it through.
484 std::vector<SDOperand> Ops;
485 bool Changed = false;
486 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
487 Ops.push_back(LegalizeOp(Node->getOperand(i)));
488 Changed = Changed || Node->getOperand(i) != Ops.back();
489 }
490 if (Changed)
491 if (Node->getNumValues() == 1)
492 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
493 else {
494 std::vector<MVT::ValueType> VTs(Node->value_begin(),
495 Node->value_end());
496 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
497 }
498
499 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
500 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
501 return Result.getValue(Op.ResNo);
502 }
503 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000504 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
505 assert(0 && "Do not know how to legalize this operator!");
506 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000507 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000508 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000509 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000510 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
511 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000512 case TargetLowering::Custom:
513 Tmp1 = TLI.LowerOperation(Op, DAG);
514 if (Tmp1.Val) Result = Tmp1;
515 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000516 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000517 break;
518 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000519 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000520 case ISD::AssertSext:
521 case ISD::AssertZext:
522 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000523 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000524 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000525 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000526 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000527 Result = Node->getOperand(Op.ResNo);
528 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000529 case ISD::CopyFromReg:
530 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000531 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000532 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000533 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000534 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000535 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000536 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000537 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000538 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
539 } else {
540 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
541 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000542 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
543 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000544 // Since CopyFromReg produces two values, make sure to remember that we
545 // legalized both of them.
546 AddLegalizedOperand(Op.getValue(0), Result);
547 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
548 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000549 case ISD::UNDEF: {
550 MVT::ValueType VT = Op.getValueType();
551 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000552 default: assert(0 && "This action is not supported yet!");
553 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000554 if (MVT::isInteger(VT))
555 Result = DAG.getConstant(0, VT);
556 else if (MVT::isFloatingPoint(VT))
557 Result = DAG.getConstantFP(0, VT);
558 else
559 assert(0 && "Unknown value type!");
560 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000561 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000562 break;
563 }
564 break;
565 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000566
Chris Lattnere55d1712006-03-28 00:40:33 +0000567 case ISD::INTRINSIC_W_CHAIN:
568 case ISD::INTRINSIC_WO_CHAIN:
569 case ISD::INTRINSIC_VOID: {
Chris Lattnera4f68052006-03-24 02:26:29 +0000570 std::vector<SDOperand> Ops;
571 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
572 Ops.push_back(LegalizeOp(Node->getOperand(i)));
573 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner30ee7252006-03-26 09:12:51 +0000574
575 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000576 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000577 TargetLowering::Custom) {
578 Tmp3 = TLI.LowerOperation(Result, DAG);
579 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000580 }
581
582 if (Result.Val->getNumValues() == 1) break;
583
584 // Must have return value and chain result.
585 assert(Result.Val->getNumValues() == 2 &&
586 "Cannot return more than two values!");
587
588 // Since loads produce two values, make sure to remember that we
589 // legalized both of them.
590 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
591 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
592 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000593 }
Chris Lattner435b4022005-11-29 06:21:05 +0000594
595 case ISD::LOCATION:
596 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
598
599 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
600 case TargetLowering::Promote:
601 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000602 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000603 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000604 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
605 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
606
607 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
608 const std::string &FName =
609 cast<StringSDNode>(Node->getOperand(3))->getValue();
610 const std::string &DirName =
611 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000612 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000613
Jim Laskey9e296be2005-12-21 20:51:37 +0000614 std::vector<SDOperand> Ops;
615 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000616 SDOperand LineOp = Node->getOperand(1);
617 SDOperand ColOp = Node->getOperand(2);
618
619 if (useDEBUG_LOC) {
620 Ops.push_back(LineOp); // line #
621 Ops.push_back(ColOp); // col #
622 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
623 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
624 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000625 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
626 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000627 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
628 Ops.push_back(DAG.getConstant(ID, MVT::i32));
629 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
630 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000631 } else {
632 Result = Tmp1; // chain
633 }
Chris Lattner435b4022005-11-29 06:21:05 +0000634 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000635 }
Chris Lattner435b4022005-11-29 06:21:05 +0000636 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000637 if (Tmp1 != Node->getOperand(0) ||
638 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000639 std::vector<SDOperand> Ops;
640 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000641 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
642 Ops.push_back(Node->getOperand(1)); // line # must be legal.
643 Ops.push_back(Node->getOperand(2)); // col # must be legal.
644 } else {
645 // Otherwise promote them.
646 Ops.push_back(PromoteOp(Node->getOperand(1)));
647 Ops.push_back(PromoteOp(Node->getOperand(2)));
648 }
Chris Lattner435b4022005-11-29 06:21:05 +0000649 Ops.push_back(Node->getOperand(3)); // filename must be legal.
650 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000651 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000652 }
653 break;
654 }
655 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000656
657 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000658 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000659 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000660 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000661 case TargetLowering::Legal:
662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
663 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
664 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
665 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000666 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000667 break;
668 }
669 break;
670
671 case ISD::DEBUG_LABEL:
672 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
673 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000674 default: assert(0 && "This action is not supported yet!");
675 case TargetLowering::Legal:
676 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
677 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000678 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000679 break;
680 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000681 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000682
Chris Lattnerdc750592005-01-07 07:47:09 +0000683 case ISD::Constant:
684 // We know we don't need to expand constants here, constants only have one
685 // value and we check that it is fine above.
686
687 // FIXME: Maybe we should handle things like targets that don't support full
688 // 32-bit immediates?
689 break;
690 case ISD::ConstantFP: {
691 // Spill FP immediates to the constant pool if the target cannot directly
692 // codegen them. Targets often have some immediate values that can be
693 // efficiently generated into an FP register without a load. We explicitly
694 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000695 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
696
697 // Check to see if this FP immediate is already legal.
698 bool isLegal = false;
699 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
700 E = TLI.legal_fpimm_end(); I != E; ++I)
701 if (CFP->isExactlyValue(*I)) {
702 isLegal = true;
703 break;
704 }
705
Chris Lattner758b0ac2006-01-29 06:26:56 +0000706 // If this is a legal constant, turn it into a TargetConstantFP node.
707 if (isLegal) {
708 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
709 break;
710 }
711
712 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
713 default: assert(0 && "This action is not supported yet!");
714 case TargetLowering::Custom:
715 Tmp3 = TLI.LowerOperation(Result, DAG);
716 if (Tmp3.Val) {
717 Result = Tmp3;
718 break;
719 }
720 // FALLTHROUGH
721 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000722 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000723 bool Extend = false;
724
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000725 // If a FP immediate is precise when represented as a float and if the
726 // target can do an extending load from float to double, we put it into
727 // the constant pool as a float, even if it's is statically typed as a
728 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000729 MVT::ValueType VT = CFP->getValueType(0);
730 bool isDouble = VT == MVT::f64;
731 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
732 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000733 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
734 // Only do this if the target has a native EXTLOAD instruction from
735 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000736 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000737 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
738 VT = MVT::f32;
739 Extend = true;
740 }
Misha Brukman835702a2005-04-21 22:36:52 +0000741
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000742 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000743 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000744 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
745 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000746 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000747 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
748 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000749 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000750 }
751 break;
752 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000753 case ISD::TokenFactor:
754 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000755 Tmp1 = LegalizeOp(Node->getOperand(0));
756 Tmp2 = LegalizeOp(Node->getOperand(1));
757 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
758 } else if (Node->getNumOperands() == 3) {
759 Tmp1 = LegalizeOp(Node->getOperand(0));
760 Tmp2 = LegalizeOp(Node->getOperand(1));
761 Tmp3 = LegalizeOp(Node->getOperand(2));
762 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000763 } else {
764 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000765 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000766 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
767 Ops.push_back(LegalizeOp(Node->getOperand(i)));
768 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000769 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000770 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000771
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000772 case ISD::BUILD_VECTOR:
773 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000774 default: assert(0 && "This action is not supported yet!");
775 case TargetLowering::Custom:
776 Tmp3 = TLI.LowerOperation(Result, DAG);
777 if (Tmp3.Val) {
778 Result = Tmp3;
779 break;
780 }
781 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000782 case TargetLowering::Expand:
783 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000784 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000785 }
Chris Lattner29b23012006-03-19 01:17:20 +0000786 break;
787 case ISD::INSERT_VECTOR_ELT:
788 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
789 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
790 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
792
793 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
794 Node->getValueType(0))) {
795 default: assert(0 && "This action is not supported yet!");
796 case TargetLowering::Legal:
797 break;
798 case TargetLowering::Custom:
799 Tmp3 = TLI.LowerOperation(Result, DAG);
800 if (Tmp3.Val) {
801 Result = Tmp3;
802 break;
803 }
804 // FALLTHROUGH
805 case TargetLowering::Expand: {
806 // If the target doesn't support this, we have to spill the input vector
807 // to a temporary stack slot, update the element, then reload it. This is
808 // badness. We could also load the value into a vector register (either
809 // with a "move to register" or "extload into register" instruction, then
810 // permute it into place, if the idx is a constant and if the idx is
811 // supported by the target.
Evan Cheng168e45b2006-03-31 01:27:51 +0000812 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
813 // Store the vector.
814 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
815 Tmp1, StackPtr, DAG.getSrcValue(NULL));
816
817 // Truncate or zero extend offset to target pointer type.
818 MVT::ValueType IntPtr = TLI.getPointerTy();
819 if (Tmp3.getValueType() > IntPtr)
820 Tmp3 = DAG.getNode(ISD::TRUNCATE, IntPtr, Tmp3);
821 else
822 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Tmp3);
823
824 // Add the offset to the index.
825 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
826 Tmp3 = DAG.getNode(ISD::MUL, Tmp3.getValueType(), Tmp3,
827 DAG.getConstant(EltSize, Tmp3.getValueType()));
828 SDOperand StackPtr2 =
829 DAG.getNode(ISD::ADD, Tmp3.getValueType(), Tmp3, StackPtr);
830 // Store the scalar value.
831 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
832 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
833 // Load the updated vector.
834 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
835 DAG.getSrcValue(NULL));
Chris Lattner29b23012006-03-19 01:17:20 +0000836 break;
837 }
838 }
839 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000840 case ISD::SCALAR_TO_VECTOR:
841 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
842 Result = DAG.UpdateNodeOperands(Result, Tmp1);
843 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
844 Node->getValueType(0))) {
845 default: assert(0 && "This action is not supported yet!");
846 case TargetLowering::Legal:
847 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000848 case TargetLowering::Custom:
849 Tmp3 = TLI.LowerOperation(Result, DAG);
850 if (Tmp3.Val) {
851 Result = Tmp3;
852 break;
853 }
854 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000855 case TargetLowering::Expand: {
856 // If the target doesn't support this, store the value to a temporary
857 // stack slot, then EXTLOAD the vector back out.
Chris Lattner79fb91c2006-03-19 06:47:21 +0000858 // TODO: If a target doesn't support this, create a stack slot for the
859 // whole vector, then store into it, then load the whole vector.
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000860 SDOperand StackPtr =
861 CreateStackTemporary(Node->getOperand(0).getValueType());
862 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
863 Node->getOperand(0), StackPtr,
864 DAG.getSrcValue(NULL));
865 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), Ch, StackPtr,
866 DAG.getSrcValue(NULL),
867 Node->getOperand(0).getValueType());
868 break;
869 }
870 }
871 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000872 case ISD::VECTOR_SHUFFLE:
873 assert(TLI.isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
874 "vector shuffle should not be created if not legal!");
875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
876 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
878
879 // Allow targets to custom lower the SHUFFLEs they support.
880 if (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, Result.getValueType())
881 == TargetLowering::Custom) {
882 Tmp1 = TLI.LowerOperation(Result, DAG);
883 if (Tmp1.Val) Result = Tmp1;
884 }
885 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000886
887 case ISD::EXTRACT_VECTOR_ELT:
888 Tmp1 = LegalizeOp(Node->getOperand(0));
889 Tmp2 = LegalizeOp(Node->getOperand(1));
890 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000891
892 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
893 Tmp1.getValueType())) {
894 default: assert(0 && "This action is not supported yet!");
895 case TargetLowering::Legal:
896 break;
897 case TargetLowering::Custom:
898 Tmp3 = TLI.LowerOperation(Result, DAG);
899 if (Tmp3.Val) {
900 Result = Tmp3;
901 break;
902 }
903 // FALLTHROUGH
904 case TargetLowering::Expand: {
905 // If the target doesn't support this, store the value to a temporary
906 // stack slot, then LOAD the scalar element back out.
907 SDOperand StackPtr = CreateStackTemporary(Tmp1.getValueType());
908 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
909 Tmp1, StackPtr, DAG.getSrcValue(NULL));
910
911 // Add the offset to the index.
912 unsigned EltSize = MVT::getSizeInBits(Result.getValueType())/8;
913 Tmp2 = DAG.getNode(ISD::MUL, Tmp2.getValueType(), Tmp2,
914 DAG.getConstant(EltSize, Tmp2.getValueType()));
915 StackPtr = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, StackPtr);
916
917 Result = DAG.getLoad(Result.getValueType(), Ch, StackPtr,
918 DAG.getSrcValue(NULL));
919 break;
920 }
921 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000922 break;
923
Chris Lattner6f423252006-03-31 17:55:51 +0000924 case ISD::VEXTRACT_VECTOR_ELT:
925 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000926 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000927
Chris Lattner462505f2006-02-13 09:18:02 +0000928 case ISD::CALLSEQ_START: {
929 SDNode *CallEnd = FindCallEndFromCallStart(Node);
930
931 // Recursively Legalize all of the inputs of the call end that do not lead
932 // to this call start. This ensures that any libcalls that need be inserted
933 // are inserted *before* the CALLSEQ_START.
934 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
935 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
936
937 // Now that we legalized all of the inputs (which may have inserted
938 // libcalls) create the new CALLSEQ_START node.
939 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
940
941 // Merge in the last call, to ensure that this call start after the last
942 // call ended.
943 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
944 Tmp1 = LegalizeOp(Tmp1);
945
946 // Do not try to legalize the target-specific arguments (#1+).
947 if (Tmp1 != Node->getOperand(0)) {
948 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
949 Ops[0] = Tmp1;
950 Result = DAG.UpdateNodeOperands(Result, Ops);
951 }
952
953 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +0000954 AddLegalizedOperand(Op.getValue(0), Result);
955 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
956 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
957
Chris Lattner462505f2006-02-13 09:18:02 +0000958 // Now that the callseq_start and all of the non-call nodes above this call
959 // sequence have been legalized, legalize the call itself. During this
960 // process, no libcalls can/will be inserted, guaranteeing that no calls
961 // can overlap.
962 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
963 SDOperand InCallSEQ = LastCALLSEQ_END;
964 // Note that we are selecting this call!
965 LastCALLSEQ_END = SDOperand(CallEnd, 0);
966 IsLegalizingCall = true;
967
968 // Legalize the call, starting from the CALLSEQ_END.
969 LegalizeOp(LastCALLSEQ_END);
970 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
971 return Result;
972 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000973 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +0000974 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
975 // will cause this node to be legalized as well as handling libcalls right.
976 if (LastCALLSEQ_END.Val != Node) {
977 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
978 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
979 assert(I != LegalizedNodes.end() &&
980 "Legalizing the call start should have legalized this node!");
981 return I->second;
982 }
983
984 // Otherwise, the call start has been legalized and everything is going
985 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +0000986 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +0000987 // Do not try to legalize the target-specific arguments (#1+), except for
988 // an optional flag input.
989 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
990 if (Tmp1 != Node->getOperand(0)) {
991 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
992 Ops[0] = Tmp1;
993 Result = DAG.UpdateNodeOperands(Result, Ops);
994 }
995 } else {
996 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
997 if (Tmp1 != Node->getOperand(0) ||
998 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
999 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1000 Ops[0] = Tmp1;
1001 Ops.back() = Tmp2;
1002 Result = DAG.UpdateNodeOperands(Result, Ops);
1003 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001004 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001005 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001006 // This finishes up call legalization.
1007 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001008
1009 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1010 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1011 if (Node->getNumValues() == 2)
1012 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1013 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001014 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001015 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1016 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1017 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001018 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001019
Chris Lattnerd02b0542006-01-28 10:58:55 +00001020 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001021 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001022 switch (TLI.getOperationAction(Node->getOpcode(),
1023 Node->getValueType(0))) {
1024 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001025 case TargetLowering::Expand: {
1026 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1027 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1028 " not tell us which reg is the stack pointer!");
1029 SDOperand Chain = Tmp1.getOperand(0);
1030 SDOperand Size = Tmp2.getOperand(1);
1031 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1032 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1033 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001034 Tmp1 = LegalizeOp(Tmp1);
1035 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001036 break;
1037 }
1038 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001039 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1040 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001041 Tmp1 = LegalizeOp(Tmp3);
1042 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001043 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001044 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001045 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001046 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001047 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001048 // Since this op produce two values, make sure to remember that we
1049 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001050 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1051 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001052 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001053 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001054 case ISD::INLINEASM:
1055 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1056 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001057 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001058 Tmp2 = Tmp3 = SDOperand(0, 0);
1059 else
1060 Tmp3 = LegalizeOp(Tmp2);
1061
1062 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1063 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1064 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001065 if (Tmp3.Val) Ops.back() = Tmp3;
1066 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001067 }
1068
1069 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001070 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001071 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1072 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001073 case ISD::BR:
1074 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001075 // Ensure that libcalls are emitted before a branch.
1076 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1077 Tmp1 = LegalizeOp(Tmp1);
1078 LastCALLSEQ_END = DAG.getEntryNode();
1079
Chris Lattnerd02b0542006-01-28 10:58:55 +00001080 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001081 break;
1082
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001083 case ISD::BRCOND:
1084 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001085 // Ensure that libcalls are emitted before a return.
1086 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1087 Tmp1 = LegalizeOp(Tmp1);
1088 LastCALLSEQ_END = DAG.getEntryNode();
1089
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001090 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1091 case Expand: assert(0 && "It's impossible to expand bools");
1092 case Legal:
1093 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1094 break;
1095 case Promote:
1096 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1097 break;
1098 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001099
1100 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001102
1103 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1104 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001105 case TargetLowering::Legal: break;
1106 case TargetLowering::Custom:
1107 Tmp1 = TLI.LowerOperation(Result, DAG);
1108 if (Tmp1.Val) Result = Tmp1;
1109 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001110 case TargetLowering::Expand:
1111 // Expand brcond's setcc into its constituent parts and create a BR_CC
1112 // Node.
1113 if (Tmp2.getOpcode() == ISD::SETCC) {
1114 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1115 Tmp2.getOperand(0), Tmp2.getOperand(1),
1116 Node->getOperand(2));
1117 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001118 // Make sure the condition is either zero or one. It may have been
1119 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001120 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1121 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1122 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001123
Nate Begeman371e4952005-08-16 19:49:35 +00001124 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1125 DAG.getCondCode(ISD::SETNE), Tmp2,
1126 DAG.getConstant(0, Tmp2.getValueType()),
1127 Node->getOperand(2));
1128 }
1129 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001130 }
1131 break;
1132 case ISD::BR_CC:
1133 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001134 // Ensure that libcalls are emitted before a branch.
1135 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1136 Tmp1 = LegalizeOp(Tmp1);
1137 LastCALLSEQ_END = DAG.getEntryNode();
1138
Nate Begeman7e7f4392006-02-01 07:19:44 +00001139 Tmp2 = Node->getOperand(2); // LHS
1140 Tmp3 = Node->getOperand(3); // RHS
1141 Tmp4 = Node->getOperand(1); // CC
1142
1143 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1144
1145 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1146 // the LHS is a legal SETCC itself. In this case, we need to compare
1147 // the result against zero to select between true and false values.
1148 if (Tmp3.Val == 0) {
1149 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1150 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001151 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001152
1153 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1154 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001155
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001156 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1157 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001158 case TargetLowering::Legal: break;
1159 case TargetLowering::Custom:
1160 Tmp4 = TLI.LowerOperation(Result, DAG);
1161 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001162 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001163 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001164 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001165 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001166 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1167 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001168
Evan Cheng31d15fa2005-12-23 07:29:34 +00001169 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001170 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1171 Tmp2 = Result.getValue(0);
1172 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001173
Evan Cheng31d15fa2005-12-23 07:29:34 +00001174 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1175 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001176 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001177 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001178 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001179 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001180 Tmp2 = LegalizeOp(Tmp1);
1181 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001182 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001183 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001184 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001185 // Since loads produce two values, make sure to remember that we
1186 // legalized both of them.
1187 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1188 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1189 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001190 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001191 case ISD::EXTLOAD:
1192 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001193 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001194 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1195 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001196
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001197 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001198 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001199 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001200 case TargetLowering::Promote:
1201 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001202 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1203 DAG.getValueType(MVT::i8));
1204 Tmp1 = Result.getValue(0);
1205 Tmp2 = Result.getValue(1);
1206 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001207 case TargetLowering::Custom:
1208 isCustom = true;
1209 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001210 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001211 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1212 Node->getOperand(3));
1213 Tmp1 = Result.getValue(0);
1214 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001215
1216 if (isCustom) {
1217 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1218 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001219 Tmp1 = LegalizeOp(Tmp3);
1220 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001221 }
1222 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001223 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001224 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001225 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001226 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1227 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001228 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001229 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1230 Tmp2 = LegalizeOp(Load.getValue(1));
1231 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001232 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001233 assert(Node->getOpcode() != ISD::EXTLOAD &&
1234 "EXTLOAD should always be supported!");
1235 // Turn the unsupported load into an EXTLOAD followed by an explicit
1236 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001237 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1238 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001239 SDOperand ValRes;
1240 if (Node->getOpcode() == ISD::SEXTLOAD)
1241 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001242 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001243 else
1244 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001245 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1246 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1247 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001248 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001249 // Since loads produce two values, make sure to remember that we legalized
1250 // both of them.
1251 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1252 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1253 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001254 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001255 case ISD::EXTRACT_ELEMENT: {
1256 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1257 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001258 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001259 case Legal:
1260 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1261 // 1 -> Hi
1262 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1263 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1264 TLI.getShiftAmountTy()));
1265 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1266 } else {
1267 // 0 -> Lo
1268 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1269 Node->getOperand(0));
1270 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001271 break;
1272 case Expand:
1273 // Get both the low and high parts.
1274 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1275 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1276 Result = Tmp2; // 1 -> Hi
1277 else
1278 Result = Tmp1; // 0 -> Lo
1279 break;
1280 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001281 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001282 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001283
1284 case ISD::CopyToReg:
1285 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001286
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001287 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001288 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001289 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001290 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001291 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001292 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001293 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001294 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001295 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001296 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001297 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1298 Tmp3);
1299 } else {
1300 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001301 }
1302
1303 // Since this produces two values, make sure to remember that we legalized
1304 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001305 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001306 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001307 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001308 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001309 break;
1310
1311 case ISD::RET:
1312 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001313
1314 // Ensure that libcalls are emitted before a return.
1315 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1316 Tmp1 = LegalizeOp(Tmp1);
1317 LastCALLSEQ_END = DAG.getEntryNode();
1318
Chris Lattnerdc750592005-01-07 07:47:09 +00001319 switch (Node->getNumOperands()) {
1320 case 2: // ret val
1321 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1322 case Legal:
1323 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001324 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001325 break;
1326 case Expand: {
1327 SDOperand Lo, Hi;
1328 ExpandOp(Node->getOperand(1), Lo, Hi);
1329 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001330 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001331 }
1332 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001333 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001334 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1335 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001336 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001337 }
1338 break;
1339 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001340 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001341 break;
1342 default: { // ret <values>
1343 std::vector<SDOperand> NewValues;
1344 NewValues.push_back(Tmp1);
1345 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1346 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1347 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001348 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001349 break;
1350 case Expand: {
1351 SDOperand Lo, Hi;
1352 ExpandOp(Node->getOperand(i), Lo, Hi);
1353 NewValues.push_back(Lo);
1354 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001355 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001356 }
1357 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001358 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001359 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001360
1361 if (NewValues.size() == Node->getNumOperands())
1362 Result = DAG.UpdateNodeOperands(Result, NewValues);
1363 else
1364 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001365 break;
1366 }
1367 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001368
Chris Lattner4d1ea712006-01-29 21:02:23 +00001369 if (Result.getOpcode() == ISD::RET) {
1370 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1371 default: assert(0 && "This action is not supported yet!");
1372 case TargetLowering::Legal: break;
1373 case TargetLowering::Custom:
1374 Tmp1 = TLI.LowerOperation(Result, DAG);
1375 if (Tmp1.Val) Result = Tmp1;
1376 break;
1377 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001378 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001379 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001380 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001381 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1382 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1383
Chris Lattnere69daaf2005-01-08 06:25:56 +00001384 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001385 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001386 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001387 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001388 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001389 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001390 } else {
1391 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001392 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001393 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001394 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1395 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001396 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001397 }
1398
Chris Lattnerdc750592005-01-07 07:47:09 +00001399 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1400 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001401 Tmp3 = LegalizeOp(Node->getOperand(1));
1402 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1403 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001404
Chris Lattnerd02b0542006-01-28 10:58:55 +00001405 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001406 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1407 default: assert(0 && "This action is not supported yet!");
1408 case TargetLowering::Legal: break;
1409 case TargetLowering::Custom:
1410 Tmp1 = TLI.LowerOperation(Result, DAG);
1411 if (Tmp1.Val) Result = Tmp1;
1412 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001413 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001414 break;
1415 }
1416 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001417 // Truncate the value and store the result.
1418 Tmp3 = PromoteOp(Node->getOperand(1));
1419 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001420 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001421 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001422 break;
1423
Chris Lattnerdc750592005-01-07 07:47:09 +00001424 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001425 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001426 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001427
1428 // If this is a vector type, then we have to calculate the increment as
1429 // the product of the element size in bytes, and the number of elements
1430 // in the high half of the vector.
1431 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1432 SDNode *InVal = Node->getOperand(1).Val;
1433 unsigned NumElems =
1434 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1435 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1436
1437 // Figure out if there is a Packed type corresponding to this Vector
1438 // type. If so, convert to the packed type.
1439 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1440 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1441 // Turn this into a normal store of the packed type.
1442 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1443 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1444 Node->getOperand(3));
1445 break;
1446 } else if (NumElems == 1) {
1447 // Turn this into a normal store of the scalar type.
1448 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1449 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1450 Node->getOperand(3));
Chris Lattner8e1fcab2006-03-31 17:37:22 +00001451 // The scalarized value type may not be legal, e.g. it might require
1452 // promotion or expansion. Relegalize the scalar store.
1453 Result = LegalizeOp(Result);
Chris Lattner32206f52006-03-18 01:44:44 +00001454 break;
1455 } else {
1456 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1457 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1458 }
1459 } else {
1460 ExpandOp(Node->getOperand(1), Lo, Hi);
1461 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001462
Chris Lattner8d90f522006-03-31 18:20:46 +00001463 if (!TLI.isLittleEndian())
1464 std::swap(Lo, Hi);
1465 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001466
Chris Lattner55e9cde2005-05-11 04:51:16 +00001467 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1468 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001469 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1470 getIntPtrConstant(IncrementSize));
1471 assert(isTypeLegal(Tmp2.getValueType()) &&
1472 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001473 // FIXME: This sets the srcvalue of both halves to be the same, which is
1474 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001475 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1476 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001477 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1478 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001479 }
1480 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001481 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001482 case ISD::PCMARKER:
1483 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001484 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001485 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001486 case ISD::STACKSAVE:
1487 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001488 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1489 Tmp1 = Result.getValue(0);
1490 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001491
Chris Lattnerb3266452006-01-13 02:50:02 +00001492 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1493 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001494 case TargetLowering::Legal: break;
1495 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001496 Tmp3 = TLI.LowerOperation(Result, DAG);
1497 if (Tmp3.Val) {
1498 Tmp1 = LegalizeOp(Tmp3);
1499 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001500 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001501 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001502 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001503 // Expand to CopyFromReg if the target set
1504 // StackPointerRegisterToSaveRestore.
1505 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001506 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001507 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001508 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001509 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001510 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1511 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001512 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001513 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001514 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001515
1516 // Since stacksave produce two values, make sure to remember that we
1517 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001518 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1519 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1520 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001521
Chris Lattnerb3266452006-01-13 02:50:02 +00001522 case ISD::STACKRESTORE:
1523 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1524 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001525 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001526
1527 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1528 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001529 case TargetLowering::Legal: break;
1530 case TargetLowering::Custom:
1531 Tmp1 = TLI.LowerOperation(Result, DAG);
1532 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001533 break;
1534 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001535 // Expand to CopyToReg if the target set
1536 // StackPointerRegisterToSaveRestore.
1537 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1538 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1539 } else {
1540 Result = Tmp1;
1541 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001542 break;
1543 }
1544 break;
1545
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001546 case ISD::READCYCLECOUNTER:
1547 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001548 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001549
1550 // Since rdcc produce two values, make sure to remember that we legalized
1551 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001552 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001553 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001554 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001555
Evan Cheng31d15fa2005-12-23 07:29:34 +00001556 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1558 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1559
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001560 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1561 "Cannot handle illegal TRUNCSTORE yet!");
1562 Tmp2 = LegalizeOp(Node->getOperand(1));
1563
1564 // The only promote case we handle is TRUNCSTORE:i1 X into
1565 // -> TRUNCSTORE:i8 (and X, 1)
1566 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1567 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1568 TargetLowering::Promote) {
1569 // Promote the bool to a mask then store.
1570 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1571 DAG.getConstant(1, Tmp2.getValueType()));
1572 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1573 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001574
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001575 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1576 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1578 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001579 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001580
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001581 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1582 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1583 default: assert(0 && "This action is not supported yet!");
1584 case TargetLowering::Legal: break;
1585 case TargetLowering::Custom:
1586 Tmp1 = TLI.LowerOperation(Result, DAG);
1587 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001588 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001589 }
1590 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001591 }
Chris Lattner39c67442005-01-14 22:08:15 +00001592 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001593 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1594 case Expand: assert(0 && "It's impossible to expand bools");
1595 case Legal:
1596 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1597 break;
1598 case Promote:
1599 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1600 break;
1601 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001602 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001603 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001604
Chris Lattnerd02b0542006-01-28 10:58:55 +00001605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001606
Nate Begeman987121a2005-08-23 04:29:48 +00001607 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001608 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001609 case TargetLowering::Legal: break;
1610 case TargetLowering::Custom: {
1611 Tmp1 = TLI.LowerOperation(Result, DAG);
1612 if (Tmp1.Val) Result = Tmp1;
1613 break;
1614 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001615 case TargetLowering::Expand:
1616 if (Tmp1.getOpcode() == ISD::SETCC) {
1617 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1618 Tmp2, Tmp3,
1619 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1620 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001621 // Make sure the condition is either zero or one. It may have been
1622 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001623 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1624 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1625 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001626 Result = DAG.getSelectCC(Tmp1,
1627 DAG.getConstant(0, Tmp1.getValueType()),
1628 Tmp2, Tmp3, ISD::SETNE);
1629 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001630 break;
1631 case TargetLowering::Promote: {
1632 MVT::ValueType NVT =
1633 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1634 unsigned ExtOp, TruncOp;
1635 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001636 ExtOp = ISD::ANY_EXTEND;
1637 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001638 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001639 ExtOp = ISD::FP_EXTEND;
1640 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001641 }
1642 // Promote each of the values to the new type.
1643 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1644 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1645 // Perform the larger operation, then round down.
1646 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1647 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1648 break;
1649 }
1650 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001651 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001652 case ISD::SELECT_CC: {
1653 Tmp1 = Node->getOperand(0); // LHS
1654 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001655 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1656 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001657 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001658
Nate Begeman7e7f4392006-02-01 07:19:44 +00001659 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1660
1661 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1662 // the LHS is a legal SETCC itself. In this case, we need to compare
1663 // the result against zero to select between true and false values.
1664 if (Tmp2.Val == 0) {
1665 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1666 CC = DAG.getCondCode(ISD::SETNE);
1667 }
1668 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1669
1670 // Everything is legal, see if we should expand this op or something.
1671 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1672 default: assert(0 && "This action is not supported yet!");
1673 case TargetLowering::Legal: break;
1674 case TargetLowering::Custom:
1675 Tmp1 = TLI.LowerOperation(Result, DAG);
1676 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001677 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001678 }
1679 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001680 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001681 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001682 Tmp1 = Node->getOperand(0);
1683 Tmp2 = Node->getOperand(1);
1684 Tmp3 = Node->getOperand(2);
1685 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1686
1687 // If we had to Expand the SetCC operands into a SELECT node, then it may
1688 // not always be possible to return a true LHS & RHS. In this case, just
1689 // return the value we legalized, returned in the LHS
1690 if (Tmp2.Val == 0) {
1691 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001692 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001693 }
Nate Begeman987121a2005-08-23 04:29:48 +00001694
Chris Lattnerf263a232006-01-30 22:43:50 +00001695 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001696 default: assert(0 && "Cannot handle this action for SETCC yet!");
1697 case TargetLowering::Custom:
1698 isCustom = true;
1699 // FALLTHROUGH.
1700 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001701 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001702 if (isCustom) {
1703 Tmp3 = TLI.LowerOperation(Result, DAG);
1704 if (Tmp3.Val) Result = Tmp3;
1705 }
Nate Begeman987121a2005-08-23 04:29:48 +00001706 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001707 case TargetLowering::Promote: {
1708 // First step, figure out the appropriate operation to use.
1709 // Allow SETCC to not be supported for all legal data types
1710 // Mostly this targets FP
1711 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1712 MVT::ValueType OldVT = NewInTy;
1713
1714 // Scan for the appropriate larger type to use.
1715 while (1) {
1716 NewInTy = (MVT::ValueType)(NewInTy+1);
1717
1718 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1719 "Fell off of the edge of the integer world");
1720 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1721 "Fell off of the edge of the floating point world");
1722
1723 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001724 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001725 break;
1726 }
1727 if (MVT::isInteger(NewInTy))
1728 assert(0 && "Cannot promote Legal Integer SETCC yet");
1729 else {
1730 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1731 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1732 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001733 Tmp1 = LegalizeOp(Tmp1);
1734 Tmp2 = LegalizeOp(Tmp2);
1735 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001736 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001737 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001738 }
Nate Begeman987121a2005-08-23 04:29:48 +00001739 case TargetLowering::Expand:
1740 // Expand a setcc node into a select_cc of the same condition, lhs, and
1741 // rhs that selects between const 1 (true) and const 0 (false).
1742 MVT::ValueType VT = Node->getValueType(0);
1743 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1744 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1745 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001746 break;
1747 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001748 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001749 case ISD::MEMSET:
1750 case ISD::MEMCPY:
1751 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001752 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001753 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1754
1755 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1756 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1757 case Expand: assert(0 && "Cannot expand a byte!");
1758 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001759 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001760 break;
1761 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001762 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001763 break;
1764 }
1765 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001766 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001767 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001768
1769 SDOperand Tmp4;
1770 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001771 case Expand: {
1772 // Length is too big, just take the lo-part of the length.
1773 SDOperand HiPart;
1774 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1775 break;
1776 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001777 case Legal:
1778 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001779 break;
1780 case Promote:
1781 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001782 break;
1783 }
1784
1785 SDOperand Tmp5;
1786 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1787 case Expand: assert(0 && "Cannot expand this yet!");
1788 case Legal:
1789 Tmp5 = LegalizeOp(Node->getOperand(4));
1790 break;
1791 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001792 Tmp5 = PromoteOp(Node->getOperand(4));
1793 break;
1794 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001795
1796 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1797 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001798 case TargetLowering::Custom:
1799 isCustom = true;
1800 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001801 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001802 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001803 if (isCustom) {
1804 Tmp1 = TLI.LowerOperation(Result, DAG);
1805 if (Tmp1.Val) Result = Tmp1;
1806 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001807 break;
1808 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001809 // Otherwise, the target does not support this operation. Lower the
1810 // operation to an explicit libcall as appropriate.
1811 MVT::ValueType IntPtr = TLI.getPointerTy();
1812 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1813 std::vector<std::pair<SDOperand, const Type*> > Args;
1814
Reid Spencer6dced922005-01-12 14:53:45 +00001815 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001816 if (Node->getOpcode() == ISD::MEMSET) {
1817 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001818 // Extend the (previously legalized) ubyte argument to be an int value
1819 // for the call.
1820 if (Tmp3.getValueType() > MVT::i32)
1821 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1822 else
1823 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001824 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1825 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1826
1827 FnName = "memset";
1828 } else if (Node->getOpcode() == ISD::MEMCPY ||
1829 Node->getOpcode() == ISD::MEMMOVE) {
1830 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1831 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1832 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1833 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1834 } else {
1835 assert(0 && "Unknown op!");
1836 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001837
Chris Lattner85d70c62005-01-11 05:57:22 +00001838 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001839 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001840 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001841 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001842 break;
1843 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001844 }
1845 break;
1846 }
Chris Lattner5385db52005-05-09 20:23:03 +00001847
Chris Lattner4157c412005-04-02 04:00:59 +00001848 case ISD::SHL_PARTS:
1849 case ISD::SRA_PARTS:
1850 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001851 std::vector<SDOperand> Ops;
1852 bool Changed = false;
1853 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1854 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1855 Changed |= Ops.back() != Node->getOperand(i);
1856 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001857 if (Changed)
1858 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001859
Evan Cheng870e4f82006-01-09 18:31:59 +00001860 switch (TLI.getOperationAction(Node->getOpcode(),
1861 Node->getValueType(0))) {
1862 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001863 case TargetLowering::Legal: break;
1864 case TargetLowering::Custom:
1865 Tmp1 = TLI.LowerOperation(Result, DAG);
1866 if (Tmp1.Val) {
1867 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001868 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001869 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001870 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1871 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001872 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001873 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001874 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001875 return RetVal;
1876 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001877 break;
1878 }
1879
Chris Lattner13fe99c2005-04-02 05:00:07 +00001880 // Since these produce multiple values, make sure to remember that we
1881 // legalized all of them.
1882 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1883 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1884 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001885 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001886
1887 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001888 case ISD::ADD:
1889 case ISD::SUB:
1890 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001891 case ISD::MULHS:
1892 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001893 case ISD::UDIV:
1894 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001895 case ISD::AND:
1896 case ISD::OR:
1897 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001898 case ISD::SHL:
1899 case ISD::SRL:
1900 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001901 case ISD::FADD:
1902 case ISD::FSUB:
1903 case ISD::FMUL:
1904 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001905 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001906 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1907 case Expand: assert(0 && "Not possible");
1908 case Legal:
1909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1910 break;
1911 case Promote:
1912 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1913 break;
1914 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001915
1916 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001917
Andrew Lenharth72594262005-12-24 23:42:32 +00001918 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00001919 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001920 case TargetLowering::Legal: break;
1921 case TargetLowering::Custom:
1922 Tmp1 = TLI.LowerOperation(Result, DAG);
1923 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001924 break;
Chris Lattner87f08092006-04-02 03:57:31 +00001925 case TargetLowering::Expand: {
1926 assert(MVT::isVector(Node->getValueType(0)) &&
1927 "Cannot expand this binary operator!");
1928 // Expand the operation into a bunch of nasty scalar code.
1929 std::vector<SDOperand> Ops;
1930 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
1931 MVT::ValueType PtrVT = TLI.getPointerTy();
1932 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
1933 i != e; ++i) {
1934 SDOperand Idx = DAG.getConstant(i, PtrVT);
1935 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
1936 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
1937 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
1938 }
1939 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
1940 break;
1941 }
Andrew Lenharth72594262005-12-24 23:42:32 +00001942 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001943 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001944
1945 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
1946 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1947 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1948 case Expand: assert(0 && "Not possible");
1949 case Legal:
1950 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1951 break;
1952 case Promote:
1953 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1954 break;
1955 }
1956
1957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1958
1959 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1960 default: assert(0 && "Operation not supported");
1961 case TargetLowering::Custom:
1962 Tmp1 = TLI.LowerOperation(Result, DAG);
1963 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00001964 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001965 case TargetLowering::Legal: break;
1966 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00001967 // If this target supports fabs/fneg natively, do this efficiently.
1968 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
1969 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
1970 // Get the sign bit of the RHS.
1971 MVT::ValueType IVT =
1972 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
1973 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
1974 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
1975 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
1976 // Get the absolute value of the result.
1977 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
1978 // Select between the nabs and abs value based on the sign bit of
1979 // the input.
1980 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
1981 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
1982 AbsVal),
1983 AbsVal);
1984 Result = LegalizeOp(Result);
1985 break;
1986 }
1987
1988 // Otherwise, do bitwise ops!
1989
1990 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001991 const char *FnName;
1992 if (Node->getValueType(0) == MVT::f32) {
1993 FnName = "copysignf";
1994 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
1995 Result = DAG.UpdateNodeOperands(Result, Tmp1,
1996 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
1997 } else {
1998 FnName = "copysign";
1999 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2000 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2001 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2002 }
2003 SDOperand Dummy;
2004 Result = ExpandLibCall(FnName, Node, Dummy);
2005 break;
2006 }
2007 break;
2008
Nate Begeman5965bd12006-02-17 05:43:56 +00002009 case ISD::ADDC:
2010 case ISD::SUBC:
2011 Tmp1 = LegalizeOp(Node->getOperand(0));
2012 Tmp2 = LegalizeOp(Node->getOperand(1));
2013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2014 // Since this produces two values, make sure to remember that we legalized
2015 // both of them.
2016 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2017 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2018 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002019
Nate Begeman5965bd12006-02-17 05:43:56 +00002020 case ISD::ADDE:
2021 case ISD::SUBE:
2022 Tmp1 = LegalizeOp(Node->getOperand(0));
2023 Tmp2 = LegalizeOp(Node->getOperand(1));
2024 Tmp3 = LegalizeOp(Node->getOperand(2));
2025 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2026 // Since this produces two values, make sure to remember that we legalized
2027 // both of them.
2028 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2029 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2030 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002031
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002032 case ISD::BUILD_PAIR: {
2033 MVT::ValueType PairTy = Node->getValueType(0);
2034 // TODO: handle the case where the Lo and Hi operands are not of legal type
2035 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2036 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2037 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002038 case TargetLowering::Promote:
2039 case TargetLowering::Custom:
2040 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002041 case TargetLowering::Legal:
2042 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2043 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2044 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002045 case TargetLowering::Expand:
2046 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2047 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2048 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2049 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2050 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002051 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002052 break;
2053 }
2054 break;
2055 }
2056
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002057 case ISD::UREM:
2058 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002059 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002060 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2061 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002062
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002063 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002064 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2065 case TargetLowering::Custom:
2066 isCustom = true;
2067 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002068 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002070 if (isCustom) {
2071 Tmp1 = TLI.LowerOperation(Result, DAG);
2072 if (Tmp1.Val) Result = Tmp1;
2073 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002074 break;
Chris Lattner81914422005-08-03 20:31:37 +00002075 case TargetLowering::Expand:
2076 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002077 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002078 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002079 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002080 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2081 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2082 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2083 } else {
2084 // Floating point mod -> fmod libcall.
2085 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2086 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002087 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002088 }
2089 break;
2090 }
2091 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002092 case ISD::VAARG: {
2093 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2094 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2095
Chris Lattner364b89a2006-01-28 07:42:08 +00002096 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002097 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2098 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002099 case TargetLowering::Custom:
2100 isCustom = true;
2101 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002102 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002103 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2104 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002105 Tmp1 = Result.getValue(1);
2106
2107 if (isCustom) {
2108 Tmp2 = TLI.LowerOperation(Result, DAG);
2109 if (Tmp2.Val) {
2110 Result = LegalizeOp(Tmp2);
2111 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2112 }
2113 }
Nate Begemane74795c2006-01-25 18:21:52 +00002114 break;
2115 case TargetLowering::Expand: {
2116 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2117 Node->getOperand(2));
2118 // Increment the pointer, VAList, to the next vaarg
2119 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2120 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2121 TLI.getPointerTy()));
2122 // Store the incremented VAList to the legalized pointer
2123 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2124 Node->getOperand(2));
2125 // Load the actual argument out of the pointer VAList
2126 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002127 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002128 Result = LegalizeOp(Result);
2129 break;
2130 }
2131 }
2132 // Since VAARG produces two values, make sure to remember that we
2133 // legalized both of them.
2134 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002135 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2136 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002137 }
2138
2139 case ISD::VACOPY:
2140 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2141 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2142 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2143
2144 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2145 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002146 case TargetLowering::Custom:
2147 isCustom = true;
2148 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002149 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2151 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002152 if (isCustom) {
2153 Tmp1 = TLI.LowerOperation(Result, DAG);
2154 if (Tmp1.Val) Result = Tmp1;
2155 }
Nate Begemane74795c2006-01-25 18:21:52 +00002156 break;
2157 case TargetLowering::Expand:
2158 // This defaults to loading a pointer from the input and storing it to the
2159 // output, returning the chain.
2160 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2161 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2162 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002163 break;
2164 }
2165 break;
2166
2167 case ISD::VAEND:
2168 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2169 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2170
2171 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2172 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002173 case TargetLowering::Custom:
2174 isCustom = true;
2175 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002176 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002178 if (isCustom) {
2179 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2180 if (Tmp1.Val) Result = Tmp1;
2181 }
Nate Begemane74795c2006-01-25 18:21:52 +00002182 break;
2183 case TargetLowering::Expand:
2184 Result = Tmp1; // Default to a no-op, return the chain
2185 break;
2186 }
2187 break;
2188
2189 case ISD::VASTART:
2190 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2191 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2192
Chris Lattnerd02b0542006-01-28 10:58:55 +00002193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2194
Nate Begemane74795c2006-01-25 18:21:52 +00002195 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2196 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002197 case TargetLowering::Legal: break;
2198 case TargetLowering::Custom:
2199 Tmp1 = TLI.LowerOperation(Result, DAG);
2200 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002201 break;
2202 }
2203 break;
2204
Nate Begeman1b8121b2006-01-11 21:21:00 +00002205 case ISD::ROTL:
2206 case ISD::ROTR:
2207 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2208 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002209
2210 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2211 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002213 break;
2214
Nate Begeman2fba8a32006-01-14 03:14:10 +00002215 case ISD::BSWAP:
2216 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2217 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002218 case TargetLowering::Custom:
2219 assert(0 && "Cannot custom legalize this yet!");
2220 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002221 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002222 break;
2223 case TargetLowering::Promote: {
2224 MVT::ValueType OVT = Tmp1.getValueType();
2225 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2226 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002227
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002228 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2229 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2230 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2231 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2232 break;
2233 }
2234 case TargetLowering::Expand:
2235 Result = ExpandBSWAP(Tmp1);
2236 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002237 }
2238 break;
2239
Andrew Lenharth5e177822005-05-03 17:19:30 +00002240 case ISD::CTPOP:
2241 case ISD::CTTZ:
2242 case ISD::CTLZ:
2243 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2244 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002245 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002246 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002247 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002248 break;
2249 case TargetLowering::Promote: {
2250 MVT::ValueType OVT = Tmp1.getValueType();
2251 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002252
2253 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002254 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2255 // Perform the larger operation, then subtract if needed.
2256 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002257 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002258 case ISD::CTPOP:
2259 Result = Tmp1;
2260 break;
2261 case ISD::CTTZ:
2262 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002263 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2264 DAG.getConstant(getSizeInBits(NVT), NVT),
2265 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002266 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002267 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2268 break;
2269 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002270 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002271 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2272 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002273 getSizeInBits(OVT), NVT));
2274 break;
2275 }
2276 break;
2277 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002278 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002279 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002280 break;
2281 }
2282 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002283
Chris Lattner13fe99c2005-04-02 05:00:07 +00002284 // Unary operators
2285 case ISD::FABS:
2286 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002287 case ISD::FSQRT:
2288 case ISD::FSIN:
2289 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002290 Tmp1 = LegalizeOp(Node->getOperand(0));
2291 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002292 case TargetLowering::Promote:
2293 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002294 isCustom = true;
2295 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002296 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002297 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002298 if (isCustom) {
2299 Tmp1 = TLI.LowerOperation(Result, DAG);
2300 if (Tmp1.Val) Result = Tmp1;
2301 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002302 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002303 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002304 switch (Node->getOpcode()) {
2305 default: assert(0 && "Unreachable!");
2306 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002307 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2308 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002309 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002310 break;
Chris Lattner80026402005-04-30 04:43:14 +00002311 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002312 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2313 MVT::ValueType VT = Node->getValueType(0);
2314 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002315 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002316 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2317 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002318 break;
2319 }
2320 case ISD::FSQRT:
2321 case ISD::FSIN:
2322 case ISD::FCOS: {
2323 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002324 const char *FnName = 0;
2325 switch(Node->getOpcode()) {
2326 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2327 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2328 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2329 default: assert(0 && "Unreachable!");
2330 }
Nate Begeman77558da2005-08-04 21:43:28 +00002331 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002332 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002333 break;
2334 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002335 }
2336 break;
2337 }
2338 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002339
2340 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002341 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002342 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002343 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002344 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2345 Node->getOperand(0).getValueType())) {
2346 default: assert(0 && "Unknown operation action!");
2347 case TargetLowering::Expand:
2348 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2349 break;
2350 case TargetLowering::Legal:
2351 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002352 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002353 break;
2354 }
2355 }
2356 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002357 case ISD::VBIT_CONVERT: {
2358 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2359 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2360
2361 // The input has to be a vector type, we have to either scalarize it, pack
2362 // it, or convert it based on whether the input vector type is legal.
2363 SDNode *InVal = Node->getOperand(0).Val;
2364 unsigned NumElems =
2365 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2366 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2367
2368 // Figure out if there is a Packed type corresponding to this Vector
2369 // type. If so, convert to the packed type.
2370 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2371 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2372 // Turn this into a bit convert of the packed input.
2373 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2374 PackVectorOp(Node->getOperand(0), TVT));
2375 break;
2376 } else if (NumElems == 1) {
2377 // Turn this into a bit convert of the scalar input.
2378 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2379 PackVectorOp(Node->getOperand(0), EVT));
2380 break;
2381 } else {
2382 // FIXME: UNIMP! Store then reload
2383 assert(0 && "Cast from unsupported vector type not implemented yet!");
2384 }
2385 }
2386
Chris Lattner13fe99c2005-04-02 05:00:07 +00002387 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002388 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002389 case ISD::UINT_TO_FP: {
2390 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002391 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2392 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002393 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002394 Node->getOperand(0).getValueType())) {
2395 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002396 case TargetLowering::Custom:
2397 isCustom = true;
2398 // FALLTHROUGH
2399 case TargetLowering::Legal:
2400 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002401 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002402 if (isCustom) {
2403 Tmp1 = TLI.LowerOperation(Result, DAG);
2404 if (Tmp1.Val) Result = Tmp1;
2405 }
2406 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002407 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002408 Result = ExpandLegalINT_TO_FP(isSigned,
2409 LegalizeOp(Node->getOperand(0)),
2410 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002411 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002412 case TargetLowering::Promote:
2413 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2414 Node->getValueType(0),
2415 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002416 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002417 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002418 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002419 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002420 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2421 Node->getValueType(0), Node->getOperand(0));
2422 break;
2423 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002424 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002425 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002426 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2427 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002428 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002429 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2430 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002431 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002432 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2433 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002434 break;
2435 }
2436 break;
2437 }
2438 case ISD::TRUNCATE:
2439 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2440 case Legal:
2441 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002442 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002443 break;
2444 case Expand:
2445 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2446
2447 // Since the result is legal, we should just be able to truncate the low
2448 // part of the source.
2449 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2450 break;
2451 case Promote:
2452 Result = PromoteOp(Node->getOperand(0));
2453 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2454 break;
2455 }
2456 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002457
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002458 case ISD::FP_TO_SINT:
2459 case ISD::FP_TO_UINT:
2460 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2461 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002462 Tmp1 = LegalizeOp(Node->getOperand(0));
2463
Chris Lattner44fe26f2005-07-29 00:11:56 +00002464 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2465 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002466 case TargetLowering::Custom:
2467 isCustom = true;
2468 // FALLTHROUGH
2469 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002470 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002471 if (isCustom) {
2472 Tmp1 = TLI.LowerOperation(Result, DAG);
2473 if (Tmp1.Val) Result = Tmp1;
2474 }
2475 break;
2476 case TargetLowering::Promote:
2477 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2478 Node->getOpcode() == ISD::FP_TO_SINT);
2479 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002480 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002481 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2482 SDOperand True, False;
2483 MVT::ValueType VT = Node->getOperand(0).getValueType();
2484 MVT::ValueType NVT = Node->getValueType(0);
2485 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2486 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2487 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2488 Node->getOperand(0), Tmp2, ISD::SETLT);
2489 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2490 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002491 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002492 Tmp2));
2493 False = DAG.getNode(ISD::XOR, NVT, False,
2494 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002495 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2496 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002497 } else {
2498 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2499 }
2500 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002501 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002502 break;
2503 case Expand:
2504 assert(0 && "Shouldn't need to expand other operators here!");
2505 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002506 Tmp1 = PromoteOp(Node->getOperand(0));
2507 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2508 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002509 break;
2510 }
2511 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002512
Chris Lattner7753f172005-09-02 00:18:10 +00002513 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002514 case ISD::ZERO_EXTEND:
2515 case ISD::SIGN_EXTEND:
2516 case ISD::FP_EXTEND:
2517 case ISD::FP_ROUND:
2518 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002519 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002520 case Legal:
2521 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002522 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002523 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002524 case Promote:
2525 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002526 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002527 Tmp1 = PromoteOp(Node->getOperand(0));
2528 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002529 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002530 case ISD::ZERO_EXTEND:
2531 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002532 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002533 Result = DAG.getZeroExtendInReg(Result,
2534 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002535 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002536 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002537 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002538 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002539 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002540 Result,
2541 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002542 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002543 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002544 Result = PromoteOp(Node->getOperand(0));
2545 if (Result.getValueType() != Op.getValueType())
2546 // Dynamically dead while we have only 2 FP types.
2547 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2548 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002549 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002550 Result = PromoteOp(Node->getOperand(0));
2551 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2552 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002553 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002554 }
2555 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002556 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002557 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002558 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002559 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002560
2561 // If this operation is not supported, convert it to a shl/shr or load/store
2562 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002563 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2564 default: assert(0 && "This action not supported for this op yet!");
2565 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002566 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002567 break;
2568 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002569 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002570 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002571 // NOTE: we could fall back on load/store here too for targets without
2572 // SAR. However, it is doubtful that any exist.
2573 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2574 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002575 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002576 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2577 Node->getOperand(0), ShiftCst);
2578 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2579 Result, ShiftCst);
2580 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2581 // The only way we can lower this is to turn it into a STORETRUNC,
2582 // EXTLOAD pair, targetting a temporary location (a stack slot).
2583
2584 // NOTE: there is a choice here between constantly creating new stack
2585 // slots and always reusing the same one. We currently always create
2586 // new ones, as reuse may inhibit scheduling.
2587 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2588 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2589 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2590 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002591 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002592 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2593 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2594 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002595 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002596 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002597 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2598 Result, StackSlot, DAG.getSrcValue(NULL),
2599 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002600 } else {
2601 assert(0 && "Unknown op");
2602 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002603 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002604 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002605 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002606 }
Chris Lattner99222f72005-01-15 07:15:18 +00002607 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002608
2609 // Make sure that the generated code is itself legal.
2610 if (Result != Op)
2611 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002612
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002613 // Note that LegalizeOp may be reentered even from single-use nodes, which
2614 // means that we always must cache transformed nodes.
2615 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002616 return Result;
2617}
2618
Chris Lattner4d978642005-01-15 22:16:26 +00002619/// PromoteOp - Given an operation that produces a value in an invalid type,
2620/// promote it to compute the value into a larger type. The produced value will
2621/// have the correct bits for the low portion of the register, but no guarantee
2622/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002623SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2624 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002625 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002626 assert(getTypeAction(VT) == Promote &&
2627 "Caller should expand or legalize operands that are not promotable!");
2628 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2629 "Cannot promote to smaller type!");
2630
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002631 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002632 SDOperand Result;
2633 SDNode *Node = Op.Val;
2634
Chris Lattner1a570f12005-09-02 20:32:45 +00002635 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2636 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002637
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002638 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002639 case ISD::CopyFromReg:
2640 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002641 default:
2642 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2643 assert(0 && "Do not know how to promote this operator!");
2644 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002645 case ISD::UNDEF:
2646 Result = DAG.getNode(ISD::UNDEF, NVT);
2647 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002648 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002649 if (VT != MVT::i1)
2650 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2651 else
2652 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002653 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2654 break;
2655 case ISD::ConstantFP:
2656 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2657 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2658 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002659
Chris Lattner2cb338d2005-01-18 02:59:52 +00002660 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002661 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002662 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2663 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002664 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002665
2666 case ISD::TRUNCATE:
2667 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2668 case Legal:
2669 Result = LegalizeOp(Node->getOperand(0));
2670 assert(Result.getValueType() >= NVT &&
2671 "This truncation doesn't make sense!");
2672 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2673 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2674 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002675 case Promote:
2676 // The truncation is not required, because we don't guarantee anything
2677 // about high bits anyway.
2678 Result = PromoteOp(Node->getOperand(0));
2679 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002680 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002681 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2682 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002683 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002684 }
2685 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002686 case ISD::SIGN_EXTEND:
2687 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002688 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002689 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2690 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2691 case Legal:
2692 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002693 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002694 break;
2695 case Promote:
2696 // Promote the reg if it's smaller.
2697 Result = PromoteOp(Node->getOperand(0));
2698 // The high bits are not guaranteed to be anything. Insert an extend.
2699 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002700 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002701 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002702 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002703 Result = DAG.getZeroExtendInReg(Result,
2704 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002705 break;
2706 }
2707 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002708 case ISD::BIT_CONVERT:
2709 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2710 Result = PromoteOp(Result);
2711 break;
2712
Chris Lattner4d978642005-01-15 22:16:26 +00002713 case ISD::FP_EXTEND:
2714 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2715 case ISD::FP_ROUND:
2716 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2717 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2718 case Promote: assert(0 && "Unreachable with 2 FP types!");
2719 case Legal:
2720 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002721 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002722 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002723 break;
2724 }
2725 break;
2726
2727 case ISD::SINT_TO_FP:
2728 case ISD::UINT_TO_FP:
2729 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2730 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002731 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002732 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002733 break;
2734
2735 case Promote:
2736 Result = PromoteOp(Node->getOperand(0));
2737 if (Node->getOpcode() == ISD::SINT_TO_FP)
2738 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002739 Result,
2740 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002741 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002742 Result = DAG.getZeroExtendInReg(Result,
2743 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002744 // No extra round required here.
2745 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002746 break;
2747 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002748 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2749 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002750 // Round if we cannot tolerate excess precision.
2751 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002752 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2753 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002754 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002755 }
Chris Lattner4d978642005-01-15 22:16:26 +00002756 break;
2757
Chris Lattner268d4572005-12-09 17:32:47 +00002758 case ISD::SIGN_EXTEND_INREG:
2759 Result = PromoteOp(Node->getOperand(0));
2760 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2761 Node->getOperand(1));
2762 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002763 case ISD::FP_TO_SINT:
2764 case ISD::FP_TO_UINT:
2765 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2766 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002767 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002768 break;
2769 case Promote:
2770 // The input result is prerounded, so we don't have to do anything
2771 // special.
2772 Tmp1 = PromoteOp(Node->getOperand(0));
2773 break;
2774 case Expand:
2775 assert(0 && "not implemented");
2776 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002777 // If we're promoting a UINT to a larger size, check to see if the new node
2778 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2779 // we can use that instead. This allows us to generate better code for
2780 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2781 // legal, such as PowerPC.
2782 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002783 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002784 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2785 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002786 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2787 } else {
2788 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2789 }
Chris Lattner4d978642005-01-15 22:16:26 +00002790 break;
2791
Chris Lattner13fe99c2005-04-02 05:00:07 +00002792 case ISD::FABS:
2793 case ISD::FNEG:
2794 Tmp1 = PromoteOp(Node->getOperand(0));
2795 assert(Tmp1.getValueType() == NVT);
2796 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2797 // NOTE: we do not have to do any extra rounding here for
2798 // NoExcessFPPrecision, because we know the input will have the appropriate
2799 // precision, and these operations don't modify precision at all.
2800 break;
2801
Chris Lattner9d6fa982005-04-28 21:44:33 +00002802 case ISD::FSQRT:
2803 case ISD::FSIN:
2804 case ISD::FCOS:
2805 Tmp1 = PromoteOp(Node->getOperand(0));
2806 assert(Tmp1.getValueType() == NVT);
2807 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002808 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002809 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2810 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002811 break;
2812
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002813 case ISD::AND:
2814 case ISD::OR:
2815 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002816 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002817 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002818 case ISD::MUL:
2819 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002820 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002821 // that too is okay if they are integer operations.
2822 Tmp1 = PromoteOp(Node->getOperand(0));
2823 Tmp2 = PromoteOp(Node->getOperand(1));
2824 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2825 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002826 break;
2827 case ISD::FADD:
2828 case ISD::FSUB:
2829 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002830 Tmp1 = PromoteOp(Node->getOperand(0));
2831 Tmp2 = PromoteOp(Node->getOperand(1));
2832 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2833 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2834
2835 // Floating point operations will give excess precision that we may not be
2836 // able to tolerate. If we DO allow excess precision, just leave it,
2837 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002838 // FIXME: Why would we need to round FP ops more than integer ones?
2839 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002840 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002841 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2842 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002843 break;
2844
Chris Lattner4d978642005-01-15 22:16:26 +00002845 case ISD::SDIV:
2846 case ISD::SREM:
2847 // These operators require that their input be sign extended.
2848 Tmp1 = PromoteOp(Node->getOperand(0));
2849 Tmp2 = PromoteOp(Node->getOperand(1));
2850 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002851 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2852 DAG.getValueType(VT));
2853 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2854 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002855 }
2856 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2857
2858 // Perform FP_ROUND: this is probably overly pessimistic.
2859 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002860 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2861 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002862 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002863 case ISD::FDIV:
2864 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002865 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002866 // These operators require that their input be fp extended.
2867 Tmp1 = PromoteOp(Node->getOperand(0));
2868 Tmp2 = PromoteOp(Node->getOperand(1));
2869 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2870
2871 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002872 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002873 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2874 DAG.getValueType(VT));
2875 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002876
2877 case ISD::UDIV:
2878 case ISD::UREM:
2879 // These operators require that their input be zero extended.
2880 Tmp1 = PromoteOp(Node->getOperand(0));
2881 Tmp2 = PromoteOp(Node->getOperand(1));
2882 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002883 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2884 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002885 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2886 break;
2887
2888 case ISD::SHL:
2889 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002890 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002891 break;
2892 case ISD::SRA:
2893 // The input value must be properly sign extended.
2894 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002895 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2896 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002897 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002898 break;
2899 case ISD::SRL:
2900 // The input value must be properly zero extended.
2901 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002902 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002903 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002904 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002905
2906 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002907 Tmp1 = Node->getOperand(0); // Get the chain.
2908 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002909 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2910 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2911 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2912 } else {
2913 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2914 Node->getOperand(2));
2915 // Increment the pointer, VAList, to the next vaarg
2916 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2917 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2918 TLI.getPointerTy()));
2919 // Store the incremented VAList to the legalized pointer
2920 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2921 Node->getOperand(2));
2922 // Load the actual argument out of the pointer VAList
2923 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2924 DAG.getSrcValue(0), VT);
2925 }
2926 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002927 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002928 break;
2929
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002930 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002931 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2932 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002933 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002934 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002935 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002936 case ISD::SEXTLOAD:
2937 case ISD::ZEXTLOAD:
2938 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002939 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2940 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002941 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002942 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002943 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002944 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002945 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002946 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2947 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002948 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002949 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002950 case ISD::SELECT_CC:
2951 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2952 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2953 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002954 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002955 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002956 case ISD::BSWAP:
2957 Tmp1 = Node->getOperand(0);
2958 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2959 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2960 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2961 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2962 TLI.getShiftAmountTy()));
2963 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002964 case ISD::CTPOP:
2965 case ISD::CTTZ:
2966 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002967 // Zero extend the argument
2968 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002969 // Perform the larger operation, then subtract if needed.
2970 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002971 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002972 case ISD::CTPOP:
2973 Result = Tmp1;
2974 break;
2975 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002976 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002977 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002978 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002979 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002980 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002981 break;
2982 case ISD::CTLZ:
2983 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002984 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2985 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002986 getSizeInBits(VT), NVT));
2987 break;
2988 }
2989 break;
Chris Lattner6f423252006-03-31 17:55:51 +00002990 case ISD::VEXTRACT_VECTOR_ELT:
2991 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
2992 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002993 }
2994
2995 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002996
2997 // Make sure the result is itself legal.
2998 Result = LegalizeOp(Result);
2999
3000 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003001 AddPromotedOperand(Op, Result);
3002 return Result;
3003}
Chris Lattnerdc750592005-01-07 07:47:09 +00003004
Chris Lattner6f423252006-03-31 17:55:51 +00003005/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3006/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3007/// on the vector type. The return type of this matches the element type of the
3008/// vector, which may not be legal for the target.
3009SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3010 // We know that operand #0 is the Vec vector. If the index is a constant
3011 // or if the invec is a supported hardware type, we can use it. Otherwise,
3012 // lower to a store then an indexed load.
3013 SDOperand Vec = Op.getOperand(0);
3014 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3015
3016 SDNode *InVal = Vec.Val;
3017 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3018 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3019
3020 // Figure out if there is a Packed type corresponding to this Vector
3021 // type. If so, convert to the packed type.
3022 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3023 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3024 // Turn this into a packed extract_vector_elt operation.
3025 Vec = PackVectorOp(Vec, TVT);
3026 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3027 } else if (NumElems == 1) {
3028 // This must be an access of the only element. Return it.
3029 return PackVectorOp(Vec, EVT);
3030 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3031 SDOperand Lo, Hi;
3032 SplitVectorOp(Vec, Lo, Hi);
3033 if (CIdx->getValue() < NumElems/2) {
3034 Vec = Lo;
3035 } else {
3036 Vec = Hi;
3037 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3038 }
3039
3040 // It's now an extract from the appropriate high or low part. Recurse.
3041 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3042 return LowerVEXTRACT_VECTOR_ELT(Op);
3043 } else {
3044 // Variable index case for extract element.
3045 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3046 assert(0 && "unimp!");
3047 return SDOperand();
3048 }
3049}
3050
3051
Nate Begeman7e7f4392006-02-01 07:19:44 +00003052/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3053/// with condition CC on the current target. This usually involves legalizing
3054/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3055/// there may be no choice but to create a new SetCC node to represent the
3056/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3057/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3058void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3059 SDOperand &RHS,
3060 SDOperand &CC) {
3061 SDOperand Tmp1, Tmp2, Result;
3062
3063 switch (getTypeAction(LHS.getValueType())) {
3064 case Legal:
3065 Tmp1 = LegalizeOp(LHS); // LHS
3066 Tmp2 = LegalizeOp(RHS); // RHS
3067 break;
3068 case Promote:
3069 Tmp1 = PromoteOp(LHS); // LHS
3070 Tmp2 = PromoteOp(RHS); // RHS
3071
3072 // If this is an FP compare, the operands have already been extended.
3073 if (MVT::isInteger(LHS.getValueType())) {
3074 MVT::ValueType VT = LHS.getValueType();
3075 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3076
3077 // Otherwise, we have to insert explicit sign or zero extends. Note
3078 // that we could insert sign extends for ALL conditions, but zero extend
3079 // is cheaper on many machines (an AND instead of two shifts), so prefer
3080 // it.
3081 switch (cast<CondCodeSDNode>(CC)->get()) {
3082 default: assert(0 && "Unknown integer comparison!");
3083 case ISD::SETEQ:
3084 case ISD::SETNE:
3085 case ISD::SETUGE:
3086 case ISD::SETUGT:
3087 case ISD::SETULE:
3088 case ISD::SETULT:
3089 // ALL of these operations will work if we either sign or zero extend
3090 // the operands (including the unsigned comparisons!). Zero extend is
3091 // usually a simpler/cheaper operation, so prefer it.
3092 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3093 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3094 break;
3095 case ISD::SETGE:
3096 case ISD::SETGT:
3097 case ISD::SETLT:
3098 case ISD::SETLE:
3099 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3100 DAG.getValueType(VT));
3101 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3102 DAG.getValueType(VT));
3103 break;
3104 }
3105 }
3106 break;
3107 case Expand:
3108 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3109 ExpandOp(LHS, LHSLo, LHSHi);
3110 ExpandOp(RHS, RHSLo, RHSHi);
3111 switch (cast<CondCodeSDNode>(CC)->get()) {
3112 case ISD::SETEQ:
3113 case ISD::SETNE:
3114 if (RHSLo == RHSHi)
3115 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3116 if (RHSCST->isAllOnesValue()) {
3117 // Comparison to -1.
3118 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3119 Tmp2 = RHSLo;
3120 break;
3121 }
3122
3123 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3124 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3125 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3126 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3127 break;
3128 default:
3129 // If this is a comparison of the sign bit, just look at the top part.
3130 // X > -1, x < 0
3131 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3132 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3133 CST->getValue() == 0) || // X < 0
3134 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3135 CST->isAllOnesValue())) { // X > -1
3136 Tmp1 = LHSHi;
3137 Tmp2 = RHSHi;
3138 break;
3139 }
3140
3141 // FIXME: This generated code sucks.
3142 ISD::CondCode LowCC;
3143 switch (cast<CondCodeSDNode>(CC)->get()) {
3144 default: assert(0 && "Unknown integer setcc!");
3145 case ISD::SETLT:
3146 case ISD::SETULT: LowCC = ISD::SETULT; break;
3147 case ISD::SETGT:
3148 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3149 case ISD::SETLE:
3150 case ISD::SETULE: LowCC = ISD::SETULE; break;
3151 case ISD::SETGE:
3152 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3153 }
3154
3155 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3156 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3157 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3158
3159 // NOTE: on targets without efficient SELECT of bools, we can always use
3160 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3161 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3162 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3163 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3164 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3165 Result, Tmp1, Tmp2));
3166 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003167 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003168 }
3169 }
3170 LHS = Tmp1;
3171 RHS = Tmp2;
3172}
3173
Chris Lattner36e663d2005-12-23 00:16:34 +00003174/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003175/// The resultant code need not be legal. Note that SrcOp is the input operand
3176/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003177SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3178 SDOperand SrcOp) {
3179 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003180 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003181
3182 // Emit a store to the stack slot.
3183 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003184 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003185 // Result is a load from the stack slot.
3186 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3187}
3188
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003189/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3190/// support the operation, but do support the resultant packed vector type.
3191SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3192
3193 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003194 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003195 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003196 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003197 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003198 std::map<SDOperand, std::vector<unsigned> > Values;
3199 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003200 bool isConstant = true;
3201 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3202 SplatValue.getOpcode() != ISD::UNDEF)
3203 isConstant = false;
3204
Evan Cheng1d2e9952006-03-24 01:17:21 +00003205 for (unsigned i = 1; i < NumElems; ++i) {
3206 SDOperand V = Node->getOperand(i);
3207 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3208 if (I != Values.end())
3209 I->second.push_back(i);
3210 else
3211 Values[V].push_back(i);
3212 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003213 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003214 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003215 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003216
3217 // If this isn't a constant element or an undef, we can't use a constant
3218 // pool load.
3219 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3220 V.getOpcode() != ISD::UNDEF)
3221 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003222 }
3223
3224 if (isOnlyLowElement) {
3225 // If the low element is an undef too, then this whole things is an undef.
3226 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3227 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3228 // Otherwise, turn this into a scalar_to_vector node.
3229 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3230 Node->getOperand(0));
3231 }
3232
Chris Lattner77e271c2006-03-24 07:29:17 +00003233 // If all elements are constants, create a load from the constant pool.
3234 if (isConstant) {
3235 MVT::ValueType VT = Node->getValueType(0);
3236 const Type *OpNTy =
3237 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3238 std::vector<Constant*> CV;
3239 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3240 if (ConstantFPSDNode *V =
3241 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3242 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3243 } else if (ConstantSDNode *V =
3244 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3245 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3246 } else {
3247 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3248 CV.push_back(UndefValue::get(OpNTy));
3249 }
3250 }
3251 Constant *CP = ConstantPacked::get(CV);
3252 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3253 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3254 DAG.getSrcValue(NULL));
3255 }
3256
Chris Lattner21e68c82006-03-20 01:52:29 +00003257 if (SplatValue.Val) { // Splat of one value?
3258 // Build the shuffle constant vector: <0, 0, 0, 0>
3259 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003260 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003261 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003262 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner21e68c82006-03-20 01:52:29 +00003263 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3264
3265 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
3266 if (TLI.isShuffleLegal(Node->getValueType(0), SplatMask)) {
3267 // Get the splatted value into the low element of a vector register.
3268 SDOperand LowValVec =
3269 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3270
3271 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3272 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3273 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3274 SplatMask);
3275 }
3276 }
3277
Evan Cheng1d2e9952006-03-24 01:17:21 +00003278 // If there are only two unique elements, we may be able to turn this into a
3279 // vector shuffle.
3280 if (Values.size() == 2) {
3281 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3282 MVT::ValueType MaskVT =
3283 MVT::getIntVectorWithNumElements(NumElems);
3284 std::vector<SDOperand> MaskVec(NumElems);
3285 unsigned i = 0;
3286 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3287 E = Values.end(); I != E; ++I) {
3288 for (std::vector<unsigned>::iterator II = I->second.begin(),
3289 EE = I->second.end(); II != EE; ++II)
3290 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3291 i += NumElems;
3292 }
3293 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3294
3295 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Evan Cheng68d9bf22006-03-24 18:45:20 +00003296 if (TLI.isShuffleLegal(Node->getValueType(0), ShuffleMask) &&
3297 TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0))) {
Evan Cheng1d2e9952006-03-24 01:17:21 +00003298 std::vector<SDOperand> Ops;
3299 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3300 E = Values.end(); I != E; ++I) {
3301 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3302 I->first);
3303 Ops.push_back(Op);
3304 }
3305 Ops.push_back(ShuffleMask);
3306
3307 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3308 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3309 }
3310 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003311
3312 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3313 // aligned object on the stack, store each element into it, then load
3314 // the result as a vector.
3315 MVT::ValueType VT = Node->getValueType(0);
3316 // Create the stack frame object.
3317 SDOperand FIPtr = CreateStackTemporary(VT);
3318
3319 // Emit a store of each element to the stack slot.
3320 std::vector<SDOperand> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003321 unsigned TypeByteSize =
3322 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3323 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3324 // Store (in the right endianness) the elements to memory.
3325 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3326 // Ignore undef elements.
3327 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3328
Chris Lattner8fa445a2006-03-22 01:46:54 +00003329 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003330
3331 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3332 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3333
3334 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3335 Node->getOperand(i), Idx,
3336 DAG.getSrcValue(NULL)));
3337 }
3338
3339 SDOperand StoreChain;
3340 if (!Stores.empty()) // Not all undef elements?
3341 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3342 else
3343 StoreChain = DAG.getEntryNode();
3344
3345 // Result is a load from the stack slot.
3346 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3347}
3348
3349/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3350/// specified value type.
3351SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3352 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3353 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3354 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3355 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3356}
3357
Chris Lattner4157c412005-04-02 04:00:59 +00003358void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3359 SDOperand Op, SDOperand Amt,
3360 SDOperand &Lo, SDOperand &Hi) {
3361 // Expand the subcomponents.
3362 SDOperand LHSL, LHSH;
3363 ExpandOp(Op, LHSL, LHSH);
3364
3365 std::vector<SDOperand> Ops;
3366 Ops.push_back(LHSL);
3367 Ops.push_back(LHSH);
3368 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003369 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003370 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003371 Hi = Lo.getValue(1);
3372}
3373
3374
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003375/// ExpandShift - Try to find a clever way to expand this shift operation out to
3376/// smaller elements. If we can't find a way that is more efficient than a
3377/// libcall on this target, return false. Otherwise, return true with the
3378/// low-parts expanded into Lo and Hi.
3379bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3380 SDOperand &Lo, SDOperand &Hi) {
3381 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3382 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003383
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003384 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003385 SDOperand ShAmt = LegalizeOp(Amt);
3386 MVT::ValueType ShTy = ShAmt.getValueType();
3387 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3388 unsigned NVTBits = MVT::getSizeInBits(NVT);
3389
3390 // Handle the case when Amt is an immediate. Other cases are currently broken
3391 // and are disabled.
3392 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3393 unsigned Cst = CN->getValue();
3394 // Expand the incoming operand to be shifted, so that we have its parts
3395 SDOperand InL, InH;
3396 ExpandOp(Op, InL, InH);
3397 switch(Opc) {
3398 case ISD::SHL:
3399 if (Cst > VTBits) {
3400 Lo = DAG.getConstant(0, NVT);
3401 Hi = DAG.getConstant(0, NVT);
3402 } else if (Cst > NVTBits) {
3403 Lo = DAG.getConstant(0, NVT);
3404 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003405 } else if (Cst == NVTBits) {
3406 Lo = DAG.getConstant(0, NVT);
3407 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003408 } else {
3409 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3410 Hi = DAG.getNode(ISD::OR, NVT,
3411 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3412 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3413 }
3414 return true;
3415 case ISD::SRL:
3416 if (Cst > VTBits) {
3417 Lo = DAG.getConstant(0, NVT);
3418 Hi = DAG.getConstant(0, NVT);
3419 } else if (Cst > NVTBits) {
3420 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3421 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003422 } else if (Cst == NVTBits) {
3423 Lo = InH;
3424 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003425 } else {
3426 Lo = DAG.getNode(ISD::OR, NVT,
3427 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3428 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3429 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3430 }
3431 return true;
3432 case ISD::SRA:
3433 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003434 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003435 DAG.getConstant(NVTBits-1, ShTy));
3436 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003437 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003438 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003439 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003440 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003441 } else if (Cst == NVTBits) {
3442 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003443 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003444 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003445 } else {
3446 Lo = DAG.getNode(ISD::OR, NVT,
3447 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3448 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3449 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3450 }
3451 return true;
3452 }
3453 }
Nate Begemanb0674922005-04-06 21:13:14 +00003454 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003455}
Chris Lattneraac464e2005-01-21 06:05:23 +00003456
Chris Lattner4add7e32005-01-23 04:42:50 +00003457
Chris Lattneraac464e2005-01-21 06:05:23 +00003458// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3459// does not fit into a register, return the lo part and set the hi part to the
3460// by-reg argument. If it does fit into a single register, return the result
3461// and leave the Hi part unset.
3462SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3463 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003464 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3465 // The input chain to this libcall is the entry node of the function.
3466 // Legalizing the call will automatically add the previous call to the
3467 // dependence.
3468 SDOperand InChain = DAG.getEntryNode();
3469
Chris Lattneraac464e2005-01-21 06:05:23 +00003470 TargetLowering::ArgListTy Args;
3471 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3472 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3473 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3474 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3475 }
3476 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003477
Chris Lattner06bbeb62005-05-11 19:02:11 +00003478 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003479 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003480 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003481 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3482 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003483
Chris Lattner462505f2006-02-13 09:18:02 +00003484 // Legalize the call sequence, starting with the chain. This will advance
3485 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3486 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3487 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003488 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003489 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003490 default: assert(0 && "Unknown thing");
3491 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003492 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003493 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003494 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003495 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003496 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003497 }
Chris Lattner63022662005-09-02 20:26:58 +00003498 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003499}
3500
Chris Lattner4add7e32005-01-23 04:42:50 +00003501
Chris Lattneraac464e2005-01-21 06:05:23 +00003502/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3503/// destination type is legal.
3504SDOperand SelectionDAGLegalize::
3505ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003506 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003507 assert(getTypeAction(Source.getValueType()) == Expand &&
3508 "This is not an expansion!");
3509 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3510
Chris Lattner06bbeb62005-05-11 19:02:11 +00003511 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003512 assert(Source.getValueType() == MVT::i64 &&
3513 "This only works for 64-bit -> FP");
3514 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3515 // incoming integer is set. To handle this, we dynamically test to see if
3516 // it is set, and, if so, add a fudge factor.
3517 SDOperand Lo, Hi;
3518 ExpandOp(Source, Lo, Hi);
3519
Chris Lattner2a4f7312005-05-13 04:45:13 +00003520 // If this is unsigned, and not supported, first perform the conversion to
3521 // signed, then adjust the result if the sign bit is set.
3522 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3523 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3524
Chris Lattnerd47675e2005-08-09 20:20:18 +00003525 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3526 DAG.getConstant(0, Hi.getValueType()),
3527 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003528 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3529 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3530 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003531 uint64_t FF = 0x5f800000ULL;
3532 if (TLI.isLittleEndian()) FF <<= 32;
3533 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003534
Chris Lattnerc30405e2005-08-26 17:15:30 +00003535 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003536 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3537 SDOperand FudgeInReg;
3538 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003539 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3540 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003541 else {
3542 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003543 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3544 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003545 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003546 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003547 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003548
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003549 // Check to see if the target has a custom way to lower this. If so, use it.
3550 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3551 default: assert(0 && "This action not implemented for this operation!");
3552 case TargetLowering::Legal:
3553 case TargetLowering::Expand:
3554 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003555 case TargetLowering::Custom: {
3556 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3557 Source), DAG);
3558 if (NV.Val)
3559 return LegalizeOp(NV);
3560 break; // The target decided this was legal after all
3561 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003562 }
3563
Chris Lattner153587e2005-05-12 07:00:44 +00003564 // Expand the source, then glue it back together for the call. We must expand
3565 // the source in case it is shared (this pass of legalize must traverse it).
3566 SDOperand SrcLo, SrcHi;
3567 ExpandOp(Source, SrcLo, SrcHi);
3568 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3569
Chris Lattner06bbeb62005-05-11 19:02:11 +00003570 const char *FnName = 0;
3571 if (DestTy == MVT::f32)
3572 FnName = "__floatdisf";
3573 else {
3574 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3575 FnName = "__floatdidf";
3576 }
Chris Lattner462505f2006-02-13 09:18:02 +00003577
3578 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3579 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003580 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003581}
Misha Brukman835702a2005-04-21 22:36:52 +00003582
Chris Lattner689bdcc2006-01-28 08:25:58 +00003583/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3584/// INT_TO_FP operation of the specified operand when the target requests that
3585/// we expand it. At this point, we know that the result and operand types are
3586/// legal for the target.
3587SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3588 SDOperand Op0,
3589 MVT::ValueType DestVT) {
3590 if (Op0.getValueType() == MVT::i32) {
3591 // simple 32-bit [signed|unsigned] integer to float/double expansion
3592
3593 // get the stack frame index of a 8 byte buffer
3594 MachineFunction &MF = DAG.getMachineFunction();
3595 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3596 // get address of 8 byte buffer
3597 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3598 // word offset constant for Hi/Lo address computation
3599 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3600 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00003601 SDOperand Hi = StackSlot;
3602 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3603 if (TLI.isLittleEndian())
3604 std::swap(Hi, Lo);
3605
Chris Lattner689bdcc2006-01-28 08:25:58 +00003606 // if signed map to unsigned space
3607 SDOperand Op0Mapped;
3608 if (isSigned) {
3609 // constant used to invert sign bit (signed to unsigned mapping)
3610 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3611 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3612 } else {
3613 Op0Mapped = Op0;
3614 }
3615 // store the lo of the constructed double - based on integer input
3616 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3617 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3618 // initial hi portion of constructed double
3619 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3620 // store the hi of the constructed double - biased exponent
3621 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3622 InitialHi, Hi, DAG.getSrcValue(NULL));
3623 // load the constructed double
3624 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3625 DAG.getSrcValue(NULL));
3626 // FP constant to bias correct the final result
3627 SDOperand Bias = DAG.getConstantFP(isSigned ?
3628 BitsToDouble(0x4330000080000000ULL)
3629 : BitsToDouble(0x4330000000000000ULL),
3630 MVT::f64);
3631 // subtract the bias
3632 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3633 // final result
3634 SDOperand Result;
3635 // handle final rounding
3636 if (DestVT == MVT::f64) {
3637 // do nothing
3638 Result = Sub;
3639 } else {
3640 // if f32 then cast to f32
3641 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3642 }
3643 return Result;
3644 }
3645 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3646 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3647
3648 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3649 DAG.getConstant(0, Op0.getValueType()),
3650 ISD::SETLT);
3651 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3652 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3653 SignSet, Four, Zero);
3654
3655 // If the sign bit of the integer is set, the large number will be treated
3656 // as a negative number. To counteract this, the dynamic code adds an
3657 // offset depending on the data type.
3658 uint64_t FF;
3659 switch (Op0.getValueType()) {
3660 default: assert(0 && "Unsupported integer type!");
3661 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3662 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3663 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3664 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3665 }
3666 if (TLI.isLittleEndian()) FF <<= 32;
3667 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3668
3669 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3670 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3671 SDOperand FudgeInReg;
3672 if (DestVT == MVT::f32)
3673 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3674 DAG.getSrcValue(NULL));
3675 else {
3676 assert(DestVT == MVT::f64 && "Unexpected conversion");
3677 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3678 DAG.getEntryNode(), CPIdx,
3679 DAG.getSrcValue(NULL), MVT::f32));
3680 }
3681
3682 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3683}
3684
3685/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3686/// *INT_TO_FP operation of the specified operand when the target requests that
3687/// we promote it. At this point, we know that the result and operand types are
3688/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3689/// operation that takes a larger input.
3690SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3691 MVT::ValueType DestVT,
3692 bool isSigned) {
3693 // First step, figure out the appropriate *INT_TO_FP operation to use.
3694 MVT::ValueType NewInTy = LegalOp.getValueType();
3695
3696 unsigned OpToUse = 0;
3697
3698 // Scan for the appropriate larger type to use.
3699 while (1) {
3700 NewInTy = (MVT::ValueType)(NewInTy+1);
3701 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3702
3703 // If the target supports SINT_TO_FP of this type, use it.
3704 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3705 default: break;
3706 case TargetLowering::Legal:
3707 if (!TLI.isTypeLegal(NewInTy))
3708 break; // Can't use this datatype.
3709 // FALL THROUGH.
3710 case TargetLowering::Custom:
3711 OpToUse = ISD::SINT_TO_FP;
3712 break;
3713 }
3714 if (OpToUse) break;
3715 if (isSigned) continue;
3716
3717 // If the target supports UINT_TO_FP of this type, use it.
3718 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3719 default: break;
3720 case TargetLowering::Legal:
3721 if (!TLI.isTypeLegal(NewInTy))
3722 break; // Can't use this datatype.
3723 // FALL THROUGH.
3724 case TargetLowering::Custom:
3725 OpToUse = ISD::UINT_TO_FP;
3726 break;
3727 }
3728 if (OpToUse) break;
3729
3730 // Otherwise, try a larger type.
3731 }
3732
3733 // Okay, we found the operation and type to use. Zero extend our input to the
3734 // desired type then run the operation on it.
3735 return DAG.getNode(OpToUse, DestVT,
3736 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3737 NewInTy, LegalOp));
3738}
3739
3740/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3741/// FP_TO_*INT operation of the specified operand when the target requests that
3742/// we promote it. At this point, we know that the result and operand types are
3743/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3744/// operation that returns a larger result.
3745SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3746 MVT::ValueType DestVT,
3747 bool isSigned) {
3748 // First step, figure out the appropriate FP_TO*INT operation to use.
3749 MVT::ValueType NewOutTy = DestVT;
3750
3751 unsigned OpToUse = 0;
3752
3753 // Scan for the appropriate larger type to use.
3754 while (1) {
3755 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3756 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3757
3758 // If the target supports FP_TO_SINT returning this type, use it.
3759 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3760 default: break;
3761 case TargetLowering::Legal:
3762 if (!TLI.isTypeLegal(NewOutTy))
3763 break; // Can't use this datatype.
3764 // FALL THROUGH.
3765 case TargetLowering::Custom:
3766 OpToUse = ISD::FP_TO_SINT;
3767 break;
3768 }
3769 if (OpToUse) break;
3770
3771 // If the target supports FP_TO_UINT of this type, use it.
3772 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3773 default: break;
3774 case TargetLowering::Legal:
3775 if (!TLI.isTypeLegal(NewOutTy))
3776 break; // Can't use this datatype.
3777 // FALL THROUGH.
3778 case TargetLowering::Custom:
3779 OpToUse = ISD::FP_TO_UINT;
3780 break;
3781 }
3782 if (OpToUse) break;
3783
3784 // Otherwise, try a larger type.
3785 }
3786
3787 // Okay, we found the operation and type to use. Truncate the result of the
3788 // extended FP_TO_*INT operation to the desired size.
3789 return DAG.getNode(ISD::TRUNCATE, DestVT,
3790 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3791}
3792
3793/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3794///
3795SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3796 MVT::ValueType VT = Op.getValueType();
3797 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3798 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3799 switch (VT) {
3800 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3801 case MVT::i16:
3802 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3803 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3804 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3805 case MVT::i32:
3806 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3807 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3808 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3809 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3810 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3811 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3812 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3813 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3814 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3815 case MVT::i64:
3816 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3817 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3818 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3819 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3820 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3821 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3822 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3823 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3824 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3825 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3826 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3827 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3828 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3829 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3830 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3831 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3832 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3833 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3834 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3835 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3836 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3837 }
3838}
3839
3840/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3841///
3842SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3843 switch (Opc) {
3844 default: assert(0 && "Cannot expand this yet!");
3845 case ISD::CTPOP: {
3846 static const uint64_t mask[6] = {
3847 0x5555555555555555ULL, 0x3333333333333333ULL,
3848 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3849 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3850 };
3851 MVT::ValueType VT = Op.getValueType();
3852 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3853 unsigned len = getSizeInBits(VT);
3854 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3855 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3856 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3857 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3858 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3859 DAG.getNode(ISD::AND, VT,
3860 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3861 }
3862 return Op;
3863 }
3864 case ISD::CTLZ: {
3865 // for now, we do this:
3866 // x = x | (x >> 1);
3867 // x = x | (x >> 2);
3868 // ...
3869 // x = x | (x >>16);
3870 // x = x | (x >>32); // for 64-bit input
3871 // return popcount(~x);
3872 //
3873 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3874 MVT::ValueType VT = Op.getValueType();
3875 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3876 unsigned len = getSizeInBits(VT);
3877 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3878 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3879 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3880 }
3881 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3882 return DAG.getNode(ISD::CTPOP, VT, Op);
3883 }
3884 case ISD::CTTZ: {
3885 // for now, we use: { return popcount(~x & (x - 1)); }
3886 // unless the target has ctlz but not ctpop, in which case we use:
3887 // { return 32 - nlz(~x & (x-1)); }
3888 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3889 MVT::ValueType VT = Op.getValueType();
3890 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3891 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3892 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3893 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3894 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3895 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3896 TLI.isOperationLegal(ISD::CTLZ, VT))
3897 return DAG.getNode(ISD::SUB, VT,
3898 DAG.getConstant(getSizeInBits(VT), VT),
3899 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3900 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3901 }
3902 }
3903}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003904
3905
Chris Lattnerdc750592005-01-07 07:47:09 +00003906/// ExpandOp - Expand the specified SDOperand into its two component pieces
3907/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3908/// LegalizeNodes map is filled in for any results that are not expanded, the
3909/// ExpandedNodes map is filled in for any results that are expanded, and the
3910/// Lo/Hi values are returned.
3911void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3912 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003913 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003914 SDNode *Node = Op.Val;
3915 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003916 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3917 "Cannot expand FP values!");
3918 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003919 "Cannot expand to FP value or to larger int value!");
3920
Chris Lattner1a570f12005-09-02 20:32:45 +00003921 // See if we already expanded it.
3922 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3923 = ExpandedNodes.find(Op);
3924 if (I != ExpandedNodes.end()) {
3925 Lo = I->second.first;
3926 Hi = I->second.second;
3927 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003928 }
3929
Chris Lattnerdc750592005-01-07 07:47:09 +00003930 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003931 case ISD::CopyFromReg:
3932 assert(0 && "CopyFromReg must be legal!");
3933 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003934 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3935 assert(0 && "Do not know how to expand this operator!");
3936 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003937 case ISD::UNDEF:
3938 Lo = DAG.getNode(ISD::UNDEF, NVT);
3939 Hi = DAG.getNode(ISD::UNDEF, NVT);
3940 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003941 case ISD::Constant: {
3942 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3943 Lo = DAG.getConstant(Cst, NVT);
3944 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3945 break;
3946 }
Chris Lattner32e08b72005-03-28 22:03:13 +00003947 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003948 // Return the operands.
3949 Lo = Node->getOperand(0);
3950 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003951 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003952
3953 case ISD::SIGN_EXTEND_INREG:
3954 ExpandOp(Node->getOperand(0), Lo, Hi);
3955 // Sign extend the lo-part.
3956 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3957 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3958 TLI.getShiftAmountTy()));
3959 // sext_inreg the low part if needed.
3960 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3961 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003962
Nate Begeman2fba8a32006-01-14 03:14:10 +00003963 case ISD::BSWAP: {
3964 ExpandOp(Node->getOperand(0), Lo, Hi);
3965 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3966 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3967 Lo = TempLo;
3968 break;
3969 }
3970
Chris Lattner55e9cde2005-05-11 04:51:16 +00003971 case ISD::CTPOP:
3972 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003973 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3974 DAG.getNode(ISD::CTPOP, NVT, Lo),
3975 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003976 Hi = DAG.getConstant(0, NVT);
3977 break;
3978
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003979 case ISD::CTLZ: {
3980 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003981 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003982 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3983 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003984 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3985 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003986 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3987 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3988
3989 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3990 Hi = DAG.getConstant(0, NVT);
3991 break;
3992 }
3993
3994 case ISD::CTTZ: {
3995 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003996 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003997 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3998 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003999 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4000 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004001 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4002 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4003
4004 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4005 Hi = DAG.getConstant(0, NVT);
4006 break;
4007 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004008
Nate Begemane74795c2006-01-25 18:21:52 +00004009 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004010 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4011 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004012 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4013 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4014
4015 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004016 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004017 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4018 if (!TLI.isLittleEndian())
4019 std::swap(Lo, Hi);
4020 break;
4021 }
4022
Chris Lattnerdc750592005-01-07 07:47:09 +00004023 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004024 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4025 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00004026 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00004027
4028 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00004029 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00004030 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4031 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004032 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00004033 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00004034
4035 // Build a factor node to remember that this load is independent of the
4036 // other one.
4037 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4038 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00004039
Chris Lattnerdc750592005-01-07 07:47:09 +00004040 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004041 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00004042 if (!TLI.isLittleEndian())
4043 std::swap(Lo, Hi);
4044 break;
4045 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004046 case ISD::AND:
4047 case ISD::OR:
4048 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4049 SDOperand LL, LH, RL, RH;
4050 ExpandOp(Node->getOperand(0), LL, LH);
4051 ExpandOp(Node->getOperand(1), RL, RH);
4052 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4053 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4054 break;
4055 }
4056 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004057 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004058 ExpandOp(Node->getOperand(1), LL, LH);
4059 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004060 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4061 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004062 break;
4063 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004064 case ISD::SELECT_CC: {
4065 SDOperand TL, TH, FL, FH;
4066 ExpandOp(Node->getOperand(2), TL, TH);
4067 ExpandOp(Node->getOperand(3), FL, FH);
4068 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4069 Node->getOperand(1), TL, FL, Node->getOperand(4));
4070 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4071 Node->getOperand(1), TH, FH, Node->getOperand(4));
4072 break;
4073 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00004074 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004075 SDOperand Chain = Node->getOperand(0);
4076 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004077 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4078
4079 if (EVT == NVT)
4080 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4081 else
4082 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4083 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004084
4085 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004086 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004087
Nate Begemanc3a89c52005-10-13 17:15:37 +00004088 // The high part is obtained by SRA'ing all but one of the bits of the lo
4089 // part.
4090 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4091 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4092 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00004093 break;
4094 }
4095 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004096 SDOperand Chain = Node->getOperand(0);
4097 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004098 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4099
4100 if (EVT == NVT)
4101 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4102 else
4103 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4104 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004105
4106 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004107 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004108
Nate Begemanc3a89c52005-10-13 17:15:37 +00004109 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004110 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004111 break;
4112 }
4113 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004114 SDOperand Chain = Node->getOperand(0);
4115 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00004116 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4117
4118 if (EVT == NVT)
4119 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4120 else
4121 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4122 EVT);
4123
4124 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004125 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004126
4127 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00004128 Hi = DAG.getNode(ISD::UNDEF, NVT);
4129 break;
4130 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004131 case ISD::ANY_EXTEND:
4132 // The low part is any extension of the input (which degenerates to a copy).
4133 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4134 // The high part is undefined.
4135 Hi = DAG.getNode(ISD::UNDEF, NVT);
4136 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004137 case ISD::SIGN_EXTEND: {
4138 // The low part is just a sign extension of the input (which degenerates to
4139 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004140 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004141
Chris Lattnerdc750592005-01-07 07:47:09 +00004142 // The high part is obtained by SRA'ing all but one of the bits of the lo
4143 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004144 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004145 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4146 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004147 break;
4148 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004149 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004150 // The low part is just a zero extension of the input (which degenerates to
4151 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004152 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004153
Chris Lattnerdc750592005-01-07 07:47:09 +00004154 // The high part is just a zero.
4155 Hi = DAG.getConstant(0, NVT);
4156 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004157
4158 case ISD::BIT_CONVERT: {
4159 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4160 Node->getOperand(0));
4161 ExpandOp(Tmp, Lo, Hi);
4162 break;
4163 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004164
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004165 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004166 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4167 TargetLowering::Custom &&
4168 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004169 Lo = TLI.LowerOperation(Op, DAG);
4170 assert(Lo.Val && "Node must be custom expanded!");
4171 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004172 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004173 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004174 break;
4175
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004176 // These operators cannot be expanded directly, emit them as calls to
4177 // library functions.
4178 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004179 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004180 SDOperand Op;
4181 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4182 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004183 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4184 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004185 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004186
Chris Lattner941d84a2005-07-30 01:40:57 +00004187 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4188
Chris Lattnerfe68d752005-07-29 00:33:32 +00004189 // Now that the custom expander is done, expand the result, which is still
4190 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004191 if (Op.Val) {
4192 ExpandOp(Op, Lo, Hi);
4193 break;
4194 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004195 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004196
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004197 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004198 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004199 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004200 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004201 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004202
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004203 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004204 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004205 SDOperand Op;
4206 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4207 case Expand: assert(0 && "cannot expand FP!");
4208 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4209 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4210 }
4211
4212 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4213
4214 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004215 if (Op.Val) {
4216 ExpandOp(Op, Lo, Hi);
4217 break;
4218 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004219 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004220
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004221 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004222 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004223 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004224 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004225 break;
4226
Evan Cheng870e4f82006-01-09 18:31:59 +00004227 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004228 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004229 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004230 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004231 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004232 Op = TLI.LowerOperation(Op, DAG);
4233 if (Op.Val) {
4234 // Now that the custom expander is done, expand the result, which is
4235 // still VT.
4236 ExpandOp(Op, Lo, Hi);
4237 break;
4238 }
4239 }
4240
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004241 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004242 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004243 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004244
4245 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004246 TargetLowering::LegalizeAction Action =
4247 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4248 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4249 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004250 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004251 break;
4252 }
4253
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004254 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004255 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004256 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004257 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004258
Evan Cheng870e4f82006-01-09 18:31:59 +00004259 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004260 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004261 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004262 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004263 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004264 Op = TLI.LowerOperation(Op, DAG);
4265 if (Op.Val) {
4266 // Now that the custom expander is done, expand the result, which is
4267 // still VT.
4268 ExpandOp(Op, Lo, Hi);
4269 break;
4270 }
4271 }
4272
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004273 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004274 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004275 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004276
4277 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004278 TargetLowering::LegalizeAction Action =
4279 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4280 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4281 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004282 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004283 break;
4284 }
4285
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004286 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004287 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004288 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004289 }
4290
4291 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004292 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004293 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004294 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004295 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004296 Op = TLI.LowerOperation(Op, DAG);
4297 if (Op.Val) {
4298 // Now that the custom expander is done, expand the result, which is
4299 // still VT.
4300 ExpandOp(Op, Lo, Hi);
4301 break;
4302 }
4303 }
4304
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004305 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004306 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004307 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004308
4309 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004310 TargetLowering::LegalizeAction Action =
4311 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4312 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4313 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004314 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004315 break;
4316 }
4317
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004318 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004319 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004320 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004321 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004322
Misha Brukman835702a2005-04-21 22:36:52 +00004323 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004324 case ISD::SUB: {
4325 // If the target wants to custom expand this, let them.
4326 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4327 TargetLowering::Custom) {
4328 Op = TLI.LowerOperation(Op, DAG);
4329 if (Op.Val) {
4330 ExpandOp(Op, Lo, Hi);
4331 break;
4332 }
4333 }
4334
4335 // Expand the subcomponents.
4336 SDOperand LHSL, LHSH, RHSL, RHSH;
4337 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4338 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004339 std::vector<MVT::ValueType> VTs;
4340 std::vector<SDOperand> LoOps, HiOps;
4341 VTs.push_back(LHSL.getValueType());
4342 VTs.push_back(MVT::Flag);
4343 LoOps.push_back(LHSL);
4344 LoOps.push_back(RHSL);
4345 HiOps.push_back(LHSH);
4346 HiOps.push_back(RHSH);
4347 if (Node->getOpcode() == ISD::ADD) {
4348 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4349 HiOps.push_back(Lo.getValue(1));
4350 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4351 } else {
4352 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4353 HiOps.push_back(Lo.getValue(1));
4354 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4355 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004356 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004357 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004358 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004359 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004360 SDOperand LL, LH, RL, RH;
4361 ExpandOp(Node->getOperand(0), LL, LH);
4362 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004363 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4364 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4365 // extended the sign bit of the low half through the upper half, and if so
4366 // emit a MULHS instead of the alternate sequence that is valid for any
4367 // i64 x i64 multiply.
4368 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4369 // is RH an extension of the sign bit of RL?
4370 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4371 RH.getOperand(1).getOpcode() == ISD::Constant &&
4372 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4373 // is LH an extension of the sign bit of LL?
4374 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4375 LH.getOperand(1).getOpcode() == ISD::Constant &&
4376 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4377 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4378 } else {
4379 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4380 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4381 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4382 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4383 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4384 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004385 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4386 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004387 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004388 }
4389 break;
4390 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004391 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4392 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4393 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4394 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004395 }
4396
Chris Lattnerac12f682005-12-21 18:02:52 +00004397 // Make sure the resultant values have been legalized themselves, unless this
4398 // is a type that requires multi-step expansion.
4399 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4400 Lo = LegalizeOp(Lo);
4401 Hi = LegalizeOp(Hi);
4402 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004403
4404 // Remember in a map if the values will be reused later.
4405 bool isNew =
4406 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4407 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004408}
4409
Chris Lattner32206f52006-03-18 01:44:44 +00004410/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4411/// two smaller values of MVT::Vector type.
4412void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4413 SDOperand &Hi) {
4414 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4415 SDNode *Node = Op.Val;
4416 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4417 assert(NumElements > 1 && "Cannot split a single element vector!");
4418 unsigned NewNumElts = NumElements/2;
4419 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4420 SDOperand TypeNode = *(Node->op_end()-1);
4421
4422 // See if we already split it.
4423 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4424 = SplitNodes.find(Op);
4425 if (I != SplitNodes.end()) {
4426 Lo = I->second.first;
4427 Hi = I->second.second;
4428 return;
4429 }
4430
4431 switch (Node->getOpcode()) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004432 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004433 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004434 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4435 LoOps.push_back(NewNumEltsNode);
4436 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004437 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004438
4439 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4440 HiOps.push_back(NewNumEltsNode);
4441 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004442 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004443 break;
4444 }
4445 case ISD::VADD:
4446 case ISD::VSUB:
4447 case ISD::VMUL:
4448 case ISD::VSDIV:
4449 case ISD::VUDIV:
4450 case ISD::VAND:
4451 case ISD::VOR:
4452 case ISD::VXOR: {
4453 SDOperand LL, LH, RL, RH;
4454 SplitVectorOp(Node->getOperand(0), LL, LH);
4455 SplitVectorOp(Node->getOperand(1), RL, RH);
4456
4457 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4458 NewNumEltsNode, TypeNode);
4459 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4460 NewNumEltsNode, TypeNode);
4461 break;
4462 }
4463 case ISD::VLOAD: {
4464 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4465 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4466 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4467
4468 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4469 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4470 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4471 getIntPtrConstant(IncrementSize));
4472 // FIXME: This creates a bogus srcvalue!
4473 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4474
4475 // Build a factor node to remember that this load is independent of the
4476 // other one.
4477 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4478 Hi.getValue(1));
4479
4480 // Remember that we legalized the chain.
4481 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00004482 break;
4483 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004484 case ISD::VBIT_CONVERT: {
4485 // We know the result is a vector. The input may be either a vector or a
4486 // scalar value.
4487 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4488 // Lower to a store/load. FIXME: this could be improved probably.
4489 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4490
4491 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4492 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4493 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4494 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4495 SplitVectorOp(St, Lo, Hi);
4496 } else {
4497 // If the input is a vector type, we have to either scalarize it, pack it
4498 // or convert it based on whether the input vector type is legal.
4499 SDNode *InVal = Node->getOperand(0).Val;
4500 unsigned NumElems =
4501 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4502 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4503
4504 // If the input is from a single element vector, scalarize the vector,
4505 // then treat like a scalar.
4506 if (NumElems == 1) {
4507 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4508 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4509 Op.getOperand(1), Op.getOperand(2));
4510 SplitVectorOp(Scalar, Lo, Hi);
4511 } else {
4512 // Split the input vector.
4513 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4514
4515 // Convert each of the pieces now.
4516 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4517 NewNumEltsNode, TypeNode);
4518 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4519 NewNumEltsNode, TypeNode);
4520 }
4521 break;
4522 }
4523 }
Chris Lattner32206f52006-03-18 01:44:44 +00004524 }
4525
4526 // Remember in a map if the values will be reused later.
4527 bool isNew =
4528 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4529 assert(isNew && "Value already expanded?!?");
4530}
4531
4532
4533/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4534/// equivalent operation that returns a scalar (e.g. F32) or packed value
4535/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4536/// type for the result.
4537SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4538 MVT::ValueType NewVT) {
4539 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4540 SDNode *Node = Op.Val;
4541
4542 // See if we already packed it.
4543 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4544 if (I != PackedNodes.end()) return I->second;
4545
4546 SDOperand Result;
4547 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004548 default:
4549 Node->dump(); std::cerr << "\n";
4550 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004551 case ISD::VADD:
4552 case ISD::VSUB:
4553 case ISD::VMUL:
4554 case ISD::VSDIV:
4555 case ISD::VUDIV:
4556 case ISD::VAND:
4557 case ISD::VOR:
4558 case ISD::VXOR:
4559 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4560 NewVT,
4561 PackVectorOp(Node->getOperand(0), NewVT),
4562 PackVectorOp(Node->getOperand(1), NewVT));
4563 break;
4564 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004565 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4566 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004567
Chris Lattner93640542006-03-19 00:07:49 +00004568 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004569
4570 // Remember that we legalized the chain.
4571 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4572 break;
4573 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004574 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00004575 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004576 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004577 Result = Node->getOperand(0);
4578 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004579 // Returning a BUILD_VECTOR?
Chris Lattner32206f52006-03-18 01:44:44 +00004580 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004581 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
Chris Lattner32206f52006-03-18 01:44:44 +00004582 }
4583 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004584 case ISD::VINSERT_VECTOR_ELT:
4585 if (!MVT::isVector(NewVT)) {
4586 // Returning a scalar? Must be the inserted element.
4587 Result = Node->getOperand(1);
4588 } else {
4589 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4590 PackVectorOp(Node->getOperand(0), NewVT),
4591 Node->getOperand(1), Node->getOperand(2));
4592 }
4593 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00004594 case ISD::VVECTOR_SHUFFLE:
4595 if (!MVT::isVector(NewVT)) {
4596 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4597 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4598 if (cast<ConstantSDNode>(EltNum)->getValue())
4599 Result = PackVectorOp(Node->getOperand(1), NewVT);
4600 else
4601 Result = PackVectorOp(Node->getOperand(0), NewVT);
4602 } else {
4603 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4604 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4605 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4606 Node->getOperand(2).Val->op_end()-2);
4607 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4608 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4609
4610 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4611 PackVectorOp(Node->getOperand(0), NewVT),
4612 PackVectorOp(Node->getOperand(1), NewVT), BV);
4613 }
4614 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00004615 case ISD::VBIT_CONVERT:
4616 if (Op.getOperand(0).getValueType() != MVT::Vector)
4617 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4618 else {
4619 // If the input is a vector type, we have to either scalarize it, pack it
4620 // or convert it based on whether the input vector type is legal.
4621 SDNode *InVal = Node->getOperand(0).Val;
4622 unsigned NumElems =
4623 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4624 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4625
4626 // Figure out if there is a Packed type corresponding to this Vector
4627 // type. If so, convert to the packed type.
4628 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4629 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4630 // Turn this into a bit convert of the packed input.
4631 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4632 PackVectorOp(Node->getOperand(0), TVT));
4633 break;
4634 } else if (NumElems == 1) {
4635 // Turn this into a bit convert of the scalar input.
4636 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4637 PackVectorOp(Node->getOperand(0), EVT));
4638 break;
4639 } else {
4640 // FIXME: UNIMP!
4641 assert(0 && "Cast from unsupported vector type not implemented yet!");
4642 }
4643 }
Chris Lattner32206f52006-03-18 01:44:44 +00004644 }
4645
4646 if (TLI.isTypeLegal(NewVT))
4647 Result = LegalizeOp(Result);
4648 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4649 assert(isNew && "Value already packed?");
4650 return Result;
4651}
4652
Chris Lattnerdc750592005-01-07 07:47:09 +00004653
4654// SelectionDAG::Legalize - This is the entry point for the file.
4655//
Chris Lattner4add7e32005-01-23 04:42:50 +00004656void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00004657 if (ViewLegalizeDAGs) viewGraph();
4658
Chris Lattnerdc750592005-01-07 07:47:09 +00004659 /// run - This is the main entry point to this class.
4660 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004661 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004662}
4663