blob: 5cb1b7eccede981a0ca1065cbe2af088d45b6fd0 [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 Lattner6be79822006-04-04 17:23:26 +0000168 /// isShuffleLegal - Return true if a vector shuffle is legal with the
169 /// specified mask and type. Targets can specify exactly which masks they
170 /// support and the code generator is tasked with not creating illegal masks.
171 ///
172 /// Note that this will also return true for shuffles that are promoted to a
173 /// different type.
174 ///
175 /// If this is a legal shuffle, this method returns the (possibly promoted)
176 /// build_vector Mask. If it's not a legal shuffle, it returns null.
177 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
178
Chris Lattner462505f2006-02-13 09:18:02 +0000179 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest);
180
Nate Begeman7e7f4392006-02-01 07:19:44 +0000181 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
182
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000183 SDOperand CreateStackTemporary(MVT::ValueType VT);
184
Chris Lattneraac464e2005-01-21 06:05:23 +0000185 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
186 SDOperand &Hi);
187 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
188 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000189
Chris Lattner36e663d2005-12-23 00:16:34 +0000190 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000191 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000192 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000193 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
194 SDOperand LegalOp,
195 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000196 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
197 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000198 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
199 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000200
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000201 SDOperand ExpandBSWAP(SDOperand Op);
202 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000203 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
204 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000205 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
206 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000207
Chris Lattner6f423252006-03-31 17:55:51 +0000208 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000209 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000210
Chris Lattnerdc750592005-01-07 07:47:09 +0000211 SDOperand getIntPtrConstant(uint64_t Val) {
212 return DAG.getConstant(Val, TLI.getPointerTy());
213 }
214};
215}
216
Chris Lattner6be79822006-04-04 17:23:26 +0000217/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
218/// specified mask and type. Targets can specify exactly which masks they
219/// support and the code generator is tasked with not creating illegal masks.
220///
221/// Note that this will also return true for shuffles that are promoted to a
222/// different type.
223SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
224 SDOperand Mask) const {
225 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
226 default: return 0;
227 case TargetLowering::Legal:
228 case TargetLowering::Custom:
229 break;
230 case TargetLowering::Promote: {
231 // If this is promoted to a different type, convert the shuffle mask and
232 // ask if it is legal in the promoted type!
233 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
234
235 // If we changed # elements, change the shuffle mask.
236 unsigned NumEltsGrowth =
237 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
238 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
239 if (NumEltsGrowth > 1) {
240 // Renumber the elements.
241 std::vector<SDOperand> Ops;
242 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
243 SDOperand InOp = Mask.getOperand(i);
244 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
245 if (InOp.getOpcode() == ISD::UNDEF)
246 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
247 else {
248 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
249 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
250 }
251 }
252 }
253 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, Ops);
254 }
255 VT = NVT;
256 break;
257 }
258 }
259 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
260}
261
Chris Lattner32206f52006-03-18 01:44:44 +0000262/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
263/// specified vector opcode.
Chris Lattner301015a2005-11-19 05:51:46 +0000264static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000265 switch (VecOp) {
266 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3bf916d2006-03-03 07:01:07 +0000267 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
268 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
269 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
270 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
271 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
272 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
273 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
274 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begemanb2e089c2005-11-19 00:36:38 +0000275 }
276}
Chris Lattnerdc750592005-01-07 07:47:09 +0000277
Chris Lattner4add7e32005-01-23 04:42:50 +0000278SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
279 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
280 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000281 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000282 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000283}
284
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000285/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
286/// not been visited yet and if all of its operands have already been visited.
287static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
288 std::map<SDNode*, unsigned> &Visited) {
289 if (++Visited[N] != N->getNumOperands())
290 return; // Haven't visited all operands yet
291
292 Order.push_back(N);
293
294 if (N->hasOneUse()) { // Tail recurse in common case.
295 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
296 return;
297 }
298
299 // Now that we have N in, add anything that uses it if all of their operands
300 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000301 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
302 ComputeTopDownOrdering(*UI, Order, Visited);
303}
304
Chris Lattner44fe26f2005-07-29 00:11:56 +0000305
Chris Lattnerdc750592005-01-07 07:47:09 +0000306void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000307 LastCALLSEQ_END = DAG.getEntryNode();
308 IsLegalizingCall = false;
309
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000310 // The legalize process is inherently a bottom-up recursive process (users
311 // legalize their uses before themselves). Given infinite stack space, we
312 // could just start legalizing on the root and traverse the whole graph. In
313 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000314 // blocks. To avoid this problem, compute an ordering of the nodes where each
315 // node is only legalized after all of its operands are legalized.
316 std::map<SDNode*, unsigned> Visited;
317 std::vector<SDNode*> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000318
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000319 // Compute ordering from all of the leaves in the graphs, those (like the
320 // entry node) that have no operands.
321 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
322 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000323 if (I->getNumOperands() == 0) {
324 Visited[I] = 0 - 1U;
325 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000326 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000327 }
328
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000329 assert(Order.size() == Visited.size() &&
330 Order.size() ==
331 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000332 "Error: DAG is cyclic!");
333 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000334
Chris Lattner32206f52006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000346 SplitNodes.clear();
347 PackedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000350 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000351}
352
Chris Lattner462505f2006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
385 SDNode *User = *UI;
386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
408bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N,
409 SDNode *Dest) {
410 if (N == Dest) return true; // N certainly leads to Dest :)
411
412 // If the first result of this node has been already legalized, then it cannot
413 // reach N.
414 switch (getTypeAction(N->getValueType(0))) {
415 case Legal:
416 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
417 break;
418 case Promote:
419 if (PromotedNodes.count(SDOperand(N, 0))) return false;
420 break;
421 case Expand:
422 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
423 break;
424 }
425
426 // Okay, this node has not already been legalized. Check and legalize all
427 // operands. If none lead to Dest, then we can legalize this node.
428 bool OperandsLeadToDest = false;
429 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
430 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
431 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest);
432
433 if (OperandsLeadToDest) return true;
434
435 // Okay, this node looks safe, legalize it and return false.
436 switch (getTypeAction(N->getValueType(0))) {
437 case Legal:
438 LegalizeOp(SDOperand(N, 0));
439 break;
440 case Promote:
441 PromoteOp(SDOperand(N, 0));
442 break;
443 case Expand: {
444 SDOperand X, Y;
445 ExpandOp(SDOperand(N, 0), X, Y);
446 break;
447 }
448 }
449 return false;
450}
451
Chris Lattner32206f52006-03-18 01:44:44 +0000452/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
453/// appropriate for its type.
454void SelectionDAGLegalize::HandleOp(SDOperand Op) {
455 switch (getTypeAction(Op.getValueType())) {
456 default: assert(0 && "Bad type action!");
457 case Legal: LegalizeOp(Op); break;
458 case Promote: PromoteOp(Op); break;
459 case Expand:
460 if (Op.getValueType() != MVT::Vector) {
461 SDOperand X, Y;
462 ExpandOp(Op, X, Y);
463 } else {
464 SDNode *N = Op.Val;
465 unsigned NumOps = N->getNumOperands();
466 unsigned NumElements =
467 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
468 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
469 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
470 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
471 // In the common case, this is a legal vector type, convert it to the
472 // packed operation and type now.
473 PackVectorOp(Op, PackedVT);
474 } else if (NumElements == 1) {
475 // Otherwise, if this is a single element vector, convert it to a
476 // scalar operation.
477 PackVectorOp(Op, EVT);
478 } else {
479 // Otherwise, this is a multiple element vector that isn't supported.
480 // Split it in half and legalize both parts.
481 SDOperand X, Y;
Chris Lattner93640542006-03-19 00:07:49 +0000482 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000483 }
484 }
485 break;
486 }
487}
Chris Lattner462505f2006-02-13 09:18:02 +0000488
489
Chris Lattner32206f52006-03-18 01:44:44 +0000490/// LegalizeOp - We know that the specified value has a legal type.
491/// Recursively ensure that the operands have legal types, then return the
492/// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000493SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000494 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000495 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000496 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000497
Chris Lattnerdc750592005-01-07 07:47:09 +0000498 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000499 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000500 if (Node->getNumValues() > 1) {
501 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000502 if (getTypeAction(Node->getValueType(i)) != Legal) {
503 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000504 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000505 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000506 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000507 }
508 }
509
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000510 // Note that LegalizeOp may be reentered even from single-use nodes, which
511 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000512 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
513 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000514
Nate Begemane5b86d72005-08-10 20:51:12 +0000515 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000516 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000517 bool isCustom = false;
518
Chris Lattnerdc750592005-01-07 07:47:09 +0000519 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000520 case ISD::FrameIndex:
521 case ISD::EntryToken:
522 case ISD::Register:
523 case ISD::BasicBlock:
524 case ISD::TargetFrameIndex:
525 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000526 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000527 case ISD::TargetConstantPool:
528 case ISD::TargetGlobalAddress:
529 case ISD::TargetExternalSymbol:
530 case ISD::VALUETYPE:
531 case ISD::SRCVALUE:
532 case ISD::STRING:
533 case ISD::CONDCODE:
534 // Primitives must all be legal.
535 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
536 "This must be legal!");
537 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000538 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000539 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
540 // If this is a target node, legalize it by legalizing the operands then
541 // passing it through.
542 std::vector<SDOperand> Ops;
543 bool Changed = false;
544 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
545 Ops.push_back(LegalizeOp(Node->getOperand(i)));
546 Changed = Changed || Node->getOperand(i) != Ops.back();
547 }
548 if (Changed)
549 if (Node->getNumValues() == 1)
550 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
551 else {
552 std::vector<MVT::ValueType> VTs(Node->value_begin(),
553 Node->value_end());
554 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
555 }
556
557 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
558 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
559 return Result.getValue(Op.ResNo);
560 }
561 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000562 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
563 assert(0 && "Do not know how to legalize this operator!");
564 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000565 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000566 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000567 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000568 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
569 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000570 case TargetLowering::Custom:
571 Tmp1 = TLI.LowerOperation(Op, DAG);
572 if (Tmp1.Val) Result = Tmp1;
573 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000574 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000575 break;
576 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000577 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000578 case ISD::AssertSext:
579 case ISD::AssertZext:
580 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000581 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000582 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000583 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000584 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000585 Result = Node->getOperand(Op.ResNo);
586 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000587 case ISD::CopyFromReg:
588 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000589 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000590 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000591 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000592 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000593 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000594 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000595 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000596 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
597 } else {
598 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
599 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000600 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
601 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000602 // Since CopyFromReg produces two values, make sure to remember that we
603 // legalized both of them.
604 AddLegalizedOperand(Op.getValue(0), Result);
605 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
606 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000607 case ISD::UNDEF: {
608 MVT::ValueType VT = Op.getValueType();
609 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000610 default: assert(0 && "This action is not supported yet!");
611 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000612 if (MVT::isInteger(VT))
613 Result = DAG.getConstant(0, VT);
614 else if (MVT::isFloatingPoint(VT))
615 Result = DAG.getConstantFP(0, VT);
616 else
617 assert(0 && "Unknown value type!");
618 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000619 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000620 break;
621 }
622 break;
623 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000624
Chris Lattnere55d1712006-03-28 00:40:33 +0000625 case ISD::INTRINSIC_W_CHAIN:
626 case ISD::INTRINSIC_WO_CHAIN:
627 case ISD::INTRINSIC_VOID: {
Chris Lattnera4f68052006-03-24 02:26:29 +0000628 std::vector<SDOperand> Ops;
629 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
630 Ops.push_back(LegalizeOp(Node->getOperand(i)));
631 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner30ee7252006-03-26 09:12:51 +0000632
633 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000634 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000635 TargetLowering::Custom) {
636 Tmp3 = TLI.LowerOperation(Result, DAG);
637 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000638 }
639
640 if (Result.Val->getNumValues() == 1) break;
641
642 // Must have return value and chain result.
643 assert(Result.Val->getNumValues() == 2 &&
644 "Cannot return more than two values!");
645
646 // Since loads produce two values, make sure to remember that we
647 // legalized both of them.
648 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
649 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
650 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000651 }
Chris Lattner435b4022005-11-29 06:21:05 +0000652
653 case ISD::LOCATION:
654 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
656
657 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
658 case TargetLowering::Promote:
659 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000660 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000661 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000662 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
663 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
664
665 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
666 const std::string &FName =
667 cast<StringSDNode>(Node->getOperand(3))->getValue();
668 const std::string &DirName =
669 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyb9966022006-01-17 17:31:53 +0000670 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000671
Jim Laskey9e296be2005-12-21 20:51:37 +0000672 std::vector<SDOperand> Ops;
673 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000674 SDOperand LineOp = Node->getOperand(1);
675 SDOperand ColOp = Node->getOperand(2);
676
677 if (useDEBUG_LOC) {
678 Ops.push_back(LineOp); // line #
679 Ops.push_back(ColOp); // col #
680 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
681 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops);
682 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000683 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
684 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000685 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
686 Ops.push_back(DAG.getConstant(ID, MVT::i32));
687 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops);
688 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000689 } else {
690 Result = Tmp1; // chain
691 }
Chris Lattner435b4022005-11-29 06:21:05 +0000692 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000693 }
Chris Lattner435b4022005-11-29 06:21:05 +0000694 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000695 if (Tmp1 != Node->getOperand(0) ||
696 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000697 std::vector<SDOperand> Ops;
698 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000699 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
700 Ops.push_back(Node->getOperand(1)); // line # must be legal.
701 Ops.push_back(Node->getOperand(2)); // col # must be legal.
702 } else {
703 // Otherwise promote them.
704 Ops.push_back(PromoteOp(Node->getOperand(1)));
705 Ops.push_back(PromoteOp(Node->getOperand(2)));
706 }
Chris Lattner435b4022005-11-29 06:21:05 +0000707 Ops.push_back(Node->getOperand(3)); // filename must be legal.
708 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000709 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000710 }
711 break;
712 }
713 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000714
715 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000716 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000717 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000718 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000719 case TargetLowering::Legal:
720 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
721 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
722 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
723 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000724 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000725 break;
726 }
727 break;
728
729 case ISD::DEBUG_LABEL:
730 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
731 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000732 default: assert(0 && "This action is not supported yet!");
733 case TargetLowering::Legal:
734 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
735 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000736 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000737 break;
738 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000739 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000740
Chris Lattnerdc750592005-01-07 07:47:09 +0000741 case ISD::Constant:
742 // We know we don't need to expand constants here, constants only have one
743 // value and we check that it is fine above.
744
745 // FIXME: Maybe we should handle things like targets that don't support full
746 // 32-bit immediates?
747 break;
748 case ISD::ConstantFP: {
749 // Spill FP immediates to the constant pool if the target cannot directly
750 // codegen them. Targets often have some immediate values that can be
751 // efficiently generated into an FP register without a load. We explicitly
752 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000753 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
754
755 // Check to see if this FP immediate is already legal.
756 bool isLegal = false;
757 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
758 E = TLI.legal_fpimm_end(); I != E; ++I)
759 if (CFP->isExactlyValue(*I)) {
760 isLegal = true;
761 break;
762 }
763
Chris Lattner758b0ac2006-01-29 06:26:56 +0000764 // If this is a legal constant, turn it into a TargetConstantFP node.
765 if (isLegal) {
766 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
767 break;
768 }
769
770 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
771 default: assert(0 && "This action is not supported yet!");
772 case TargetLowering::Custom:
773 Tmp3 = TLI.LowerOperation(Result, DAG);
774 if (Tmp3.Val) {
775 Result = Tmp3;
776 break;
777 }
778 // FALLTHROUGH
779 case TargetLowering::Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000780 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000781 bool Extend = false;
782
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000783 // If a FP immediate is precise when represented as a float and if the
784 // target can do an extending load from float to double, we put it into
785 // the constant pool as a float, even if it's is statically typed as a
786 // double.
Chris Lattnerdc750592005-01-07 07:47:09 +0000787 MVT::ValueType VT = CFP->getValueType(0);
788 bool isDouble = VT == MVT::f64;
789 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
790 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000791 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
792 // Only do this if the target has a native EXTLOAD instruction from
793 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000794 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000795 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
796 VT = MVT::f32;
797 Extend = true;
798 }
Misha Brukman835702a2005-04-21 22:36:52 +0000799
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000800 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000801 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000802 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
803 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000804 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000805 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
806 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000807 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000808 }
809 break;
810 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000811 case ISD::TokenFactor:
812 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000813 Tmp1 = LegalizeOp(Node->getOperand(0));
814 Tmp2 = LegalizeOp(Node->getOperand(1));
815 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
816 } else if (Node->getNumOperands() == 3) {
817 Tmp1 = LegalizeOp(Node->getOperand(0));
818 Tmp2 = LegalizeOp(Node->getOperand(1));
819 Tmp3 = LegalizeOp(Node->getOperand(2));
820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000821 } else {
822 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000823 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000824 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
825 Ops.push_back(LegalizeOp(Node->getOperand(i)));
826 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000827 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000828 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000829
830 case ISD::FORMAL_ARGUMENTS:
831 // The only option for this is to custom lower it.
832 Result = TLI.LowerOperation(Result, DAG);
833 assert(Result.Val && "Target didn't custom lower ISD::FORMAL_ARGUMENTS!");
834 break;
835
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000836 case ISD::BUILD_VECTOR:
837 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000838 default: assert(0 && "This action is not supported yet!");
839 case TargetLowering::Custom:
840 Tmp3 = TLI.LowerOperation(Result, DAG);
841 if (Tmp3.Val) {
842 Result = Tmp3;
843 break;
844 }
845 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000846 case TargetLowering::Expand:
847 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000848 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000849 }
Chris Lattner29b23012006-03-19 01:17:20 +0000850 break;
851 case ISD::INSERT_VECTOR_ELT:
852 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
853 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
854 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
855 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
856
857 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
858 Node->getValueType(0))) {
859 default: assert(0 && "This action is not supported yet!");
860 case TargetLowering::Legal:
861 break;
862 case TargetLowering::Custom:
863 Tmp3 = TLI.LowerOperation(Result, DAG);
864 if (Tmp3.Val) {
865 Result = Tmp3;
866 break;
867 }
868 // FALLTHROUGH
869 case TargetLowering::Expand: {
870 // If the target doesn't support this, we have to spill the input vector
871 // to a temporary stack slot, update the element, then reload it. This is
872 // badness. We could also load the value into a vector register (either
873 // with a "move to register" or "extload into register" instruction, then
874 // permute it into place, if the idx is a constant and if the idx is
875 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +0000876 MVT::ValueType VT = Tmp1.getValueType();
877 MVT::ValueType EltVT = Tmp2.getValueType();
878 MVT::ValueType IdxVT = Tmp3.getValueType();
879 MVT::ValueType PtrVT = TLI.getPointerTy();
880 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +0000881 // Store the vector.
882 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
883 Tmp1, StackPtr, DAG.getSrcValue(NULL));
884
885 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +0000886 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
887 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +0000888 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +0000889 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
890 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
891 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +0000892 // Store the scalar value.
893 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
894 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
895 // Load the updated vector.
Evan Cheng78e3d562006-04-08 01:46:37 +0000896 Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL));
Chris Lattner29b23012006-03-19 01:17:20 +0000897 break;
898 }
899 }
900 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000901 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +0000902 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
903 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
904 break;
905 }
906
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000907 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
908 Result = DAG.UpdateNodeOperands(Result, Tmp1);
909 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
910 Node->getValueType(0))) {
911 default: assert(0 && "This action is not supported yet!");
912 case TargetLowering::Legal:
913 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000914 case TargetLowering::Custom:
915 Tmp3 = TLI.LowerOperation(Result, DAG);
916 if (Tmp3.Val) {
917 Result = Tmp3;
918 break;
919 }
920 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +0000921 case TargetLowering::Expand:
922 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000923 break;
924 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000925 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000926 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +0000927 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
928 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
929 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
930
931 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +0000932 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
933 default: assert(0 && "Unknown operation action!");
934 case TargetLowering::Legal:
935 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
936 "vector shuffle should not be created if not legal!");
937 break;
938 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +0000939 Tmp3 = TLI.LowerOperation(Result, DAG);
940 if (Tmp3.Val) {
941 Result = Tmp3;
942 break;
943 }
944 // FALLTHROUGH
945 case TargetLowering::Expand: {
946 MVT::ValueType VT = Node->getValueType(0);
947 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
948 MVT::ValueType PtrVT = TLI.getPointerTy();
949 SDOperand Mask = Node->getOperand(2);
950 unsigned NumElems = Mask.getNumOperands();
951 std::vector<SDOperand> Ops;
952 for (unsigned i = 0; i != NumElems; ++i) {
953 SDOperand Arg = Mask.getOperand(i);
954 if (Arg.getOpcode() == ISD::UNDEF) {
955 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
956 } else {
957 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
958 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
959 if (Idx < NumElems)
960 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
961 DAG.getConstant(Idx, PtrVT)));
962 else
963 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
964 DAG.getConstant(Idx - NumElems, PtrVT)));
965 }
966 }
967 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
Chris Lattner6be79822006-04-04 17:23:26 +0000968 break;
Evan Cheng9fa89592006-04-05 06:07:11 +0000969 }
Chris Lattner6be79822006-04-04 17:23:26 +0000970 case TargetLowering::Promote: {
971 // Change base type to a different vector type.
972 MVT::ValueType OVT = Node->getValueType(0);
973 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
974
975 // Cast the two input vectors.
976 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
977 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
978
979 // Convert the shuffle mask to the right # elements.
980 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
981 assert(Tmp3.Val && "Shuffle not legal?");
982 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
983 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
984 break;
985 }
Chris Lattner21e68c82006-03-20 01:52:29 +0000986 }
987 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000988
989 case ISD::EXTRACT_VECTOR_ELT:
990 Tmp1 = LegalizeOp(Node->getOperand(0));
991 Tmp2 = LegalizeOp(Node->getOperand(1));
992 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000993
994 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
995 Tmp1.getValueType())) {
996 default: assert(0 && "This action is not supported yet!");
997 case TargetLowering::Legal:
998 break;
999 case TargetLowering::Custom:
1000 Tmp3 = TLI.LowerOperation(Result, DAG);
1001 if (Tmp3.Val) {
1002 Result = Tmp3;
1003 break;
1004 }
1005 // FALLTHROUGH
Chris Lattner42a5fca2006-04-02 05:06:04 +00001006 case TargetLowering::Expand:
1007 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner340a6b52006-03-21 21:02:03 +00001008 break;
1009 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001010 break;
1011
Chris Lattner6f423252006-03-31 17:55:51 +00001012 case ISD::VEXTRACT_VECTOR_ELT:
1013 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001014 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001015
Chris Lattner462505f2006-02-13 09:18:02 +00001016 case ISD::CALLSEQ_START: {
1017 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1018
1019 // Recursively Legalize all of the inputs of the call end that do not lead
1020 // to this call start. This ensures that any libcalls that need be inserted
1021 // are inserted *before* the CALLSEQ_START.
1022 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1023 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
1024
1025 // Now that we legalized all of the inputs (which may have inserted
1026 // libcalls) create the new CALLSEQ_START node.
1027 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1028
1029 // Merge in the last call, to ensure that this call start after the last
1030 // call ended.
1031 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1032 Tmp1 = LegalizeOp(Tmp1);
1033
1034 // Do not try to legalize the target-specific arguments (#1+).
1035 if (Tmp1 != Node->getOperand(0)) {
1036 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1037 Ops[0] = Tmp1;
1038 Result = DAG.UpdateNodeOperands(Result, Ops);
1039 }
1040
1041 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001042 AddLegalizedOperand(Op.getValue(0), Result);
1043 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1044 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1045
Chris Lattner462505f2006-02-13 09:18:02 +00001046 // Now that the callseq_start and all of the non-call nodes above this call
1047 // sequence have been legalized, legalize the call itself. During this
1048 // process, no libcalls can/will be inserted, guaranteeing that no calls
1049 // can overlap.
1050 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1051 SDOperand InCallSEQ = LastCALLSEQ_END;
1052 // Note that we are selecting this call!
1053 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1054 IsLegalizingCall = true;
1055
1056 // Legalize the call, starting from the CALLSEQ_END.
1057 LegalizeOp(LastCALLSEQ_END);
1058 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1059 return Result;
1060 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001061 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001062 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1063 // will cause this node to be legalized as well as handling libcalls right.
1064 if (LastCALLSEQ_END.Val != Node) {
1065 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1066 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1067 assert(I != LegalizedNodes.end() &&
1068 "Legalizing the call start should have legalized this node!");
1069 return I->second;
1070 }
1071
1072 // Otherwise, the call start has been legalized and everything is going
1073 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001074 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001075 // Do not try to legalize the target-specific arguments (#1+), except for
1076 // an optional flag input.
1077 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1078 if (Tmp1 != Node->getOperand(0)) {
1079 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1080 Ops[0] = Tmp1;
1081 Result = DAG.UpdateNodeOperands(Result, Ops);
1082 }
1083 } else {
1084 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1085 if (Tmp1 != Node->getOperand(0) ||
1086 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1087 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1088 Ops[0] = Tmp1;
1089 Ops.back() = Tmp2;
1090 Result = DAG.UpdateNodeOperands(Result, Ops);
1091 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001092 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001093 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001094 // This finishes up call legalization.
1095 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001096
1097 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1098 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1099 if (Node->getNumValues() == 2)
1100 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1101 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001102 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001103 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1104 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1105 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001106 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001107
Chris Lattnerd02b0542006-01-28 10:58:55 +00001108 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001109 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001110 switch (TLI.getOperationAction(Node->getOpcode(),
1111 Node->getValueType(0))) {
1112 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001113 case TargetLowering::Expand: {
1114 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1115 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1116 " not tell us which reg is the stack pointer!");
1117 SDOperand Chain = Tmp1.getOperand(0);
1118 SDOperand Size = Tmp2.getOperand(1);
1119 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1120 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1121 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001122 Tmp1 = LegalizeOp(Tmp1);
1123 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001124 break;
1125 }
1126 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001127 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1128 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001129 Tmp1 = LegalizeOp(Tmp3);
1130 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001131 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001132 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001133 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001134 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001135 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001136 // Since this op produce two values, make sure to remember that we
1137 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001138 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1139 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001140 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001141 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001142 case ISD::INLINEASM:
1143 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1144 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001145 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001146 Tmp2 = Tmp3 = SDOperand(0, 0);
1147 else
1148 Tmp3 = LegalizeOp(Tmp2);
1149
1150 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1151 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1152 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001153 if (Tmp3.Val) Ops.back() = Tmp3;
1154 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001155 }
1156
1157 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001158 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001159 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1160 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001161 case ISD::BR:
1162 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001163 // Ensure that libcalls are emitted before a branch.
1164 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1165 Tmp1 = LegalizeOp(Tmp1);
1166 LastCALLSEQ_END = DAG.getEntryNode();
1167
Chris Lattnerd02b0542006-01-28 10:58:55 +00001168 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001169 break;
1170
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001171 case ISD::BRCOND:
1172 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001173 // Ensure that libcalls are emitted before a return.
1174 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1175 Tmp1 = LegalizeOp(Tmp1);
1176 LastCALLSEQ_END = DAG.getEntryNode();
1177
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001178 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1179 case Expand: assert(0 && "It's impossible to expand bools");
1180 case Legal:
1181 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1182 break;
1183 case Promote:
1184 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1185 break;
1186 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001187
1188 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001190
1191 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1192 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001193 case TargetLowering::Legal: break;
1194 case TargetLowering::Custom:
1195 Tmp1 = TLI.LowerOperation(Result, DAG);
1196 if (Tmp1.Val) Result = Tmp1;
1197 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001198 case TargetLowering::Expand:
1199 // Expand brcond's setcc into its constituent parts and create a BR_CC
1200 // Node.
1201 if (Tmp2.getOpcode() == ISD::SETCC) {
1202 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1203 Tmp2.getOperand(0), Tmp2.getOperand(1),
1204 Node->getOperand(2));
1205 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001206 // Make sure the condition is either zero or one. It may have been
1207 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001208 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1209 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1210 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001211
Nate Begeman371e4952005-08-16 19:49:35 +00001212 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1213 DAG.getCondCode(ISD::SETNE), Tmp2,
1214 DAG.getConstant(0, Tmp2.getValueType()),
1215 Node->getOperand(2));
1216 }
1217 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001218 }
1219 break;
1220 case ISD::BR_CC:
1221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001222 // Ensure that libcalls are emitted before a branch.
1223 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1224 Tmp1 = LegalizeOp(Tmp1);
1225 LastCALLSEQ_END = DAG.getEntryNode();
1226
Nate Begeman7e7f4392006-02-01 07:19:44 +00001227 Tmp2 = Node->getOperand(2); // LHS
1228 Tmp3 = Node->getOperand(3); // RHS
1229 Tmp4 = Node->getOperand(1); // CC
1230
1231 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1232
1233 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1234 // the LHS is a legal SETCC itself. In this case, we need to compare
1235 // the result against zero to select between true and false values.
1236 if (Tmp3.Val == 0) {
1237 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1238 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001239 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001240
1241 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1242 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001243
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001244 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1245 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001246 case TargetLowering::Legal: break;
1247 case TargetLowering::Custom:
1248 Tmp4 = TLI.LowerOperation(Result, DAG);
1249 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001250 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001251 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001252 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001253 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001254 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1255 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001256
Evan Cheng31d15fa2005-12-23 07:29:34 +00001257 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001258 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Chengbe8a8932006-04-12 16:33:18 +00001259 Tmp3 = Result.getValue(0);
1260 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001261
Evan Cheng31d15fa2005-12-23 07:29:34 +00001262 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1263 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001264 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001265 case TargetLowering::Custom:
Evan Chengbe8a8932006-04-12 16:33:18 +00001266 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001267 if (Tmp1.Val) {
Evan Chengbe8a8932006-04-12 16:33:18 +00001268 Tmp3 = LegalizeOp(Tmp1);
1269 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001270 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001271 break;
Evan Chengbe8a8932006-04-12 16:33:18 +00001272 case TargetLowering::Promote: {
1273 // Only promote a load of vector type to another.
1274 assert(MVT::isVector(VT) && "Cannot promote this load!");
1275 // Change base type to a different vector type.
1276 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1277
1278 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, Node->getOperand(2));
1279 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1280 Tmp4 = LegalizeOp(Tmp1.getValue(1));
1281 break;
1282 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001283 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001284 // Since loads produce two values, make sure to remember that we
1285 // legalized both of them.
Evan Chengbe8a8932006-04-12 16:33:18 +00001286 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1287 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1288 return Op.ResNo ? Tmp4 : Tmp3;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001289 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001290 case ISD::EXTLOAD:
1291 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001292 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1294 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001295
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001296 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001297 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001298 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001299 case TargetLowering::Promote:
1300 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1302 DAG.getValueType(MVT::i8));
1303 Tmp1 = Result.getValue(0);
1304 Tmp2 = Result.getValue(1);
1305 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001306 case TargetLowering::Custom:
1307 isCustom = true;
1308 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001309 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001310 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1311 Node->getOperand(3));
1312 Tmp1 = Result.getValue(0);
1313 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001314
1315 if (isCustom) {
1316 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1317 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001318 Tmp1 = LegalizeOp(Tmp3);
1319 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001320 }
1321 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001322 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001323 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001324 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001325 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1326 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001327 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001328 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1329 Tmp2 = LegalizeOp(Load.getValue(1));
1330 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001331 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001332 assert(Node->getOpcode() != ISD::EXTLOAD &&
1333 "EXTLOAD should always be supported!");
1334 // Turn the unsupported load into an EXTLOAD followed by an explicit
1335 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001336 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1337 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001338 SDOperand ValRes;
1339 if (Node->getOpcode() == ISD::SEXTLOAD)
1340 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001341 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001342 else
1343 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001344 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1345 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1346 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001347 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001348 // Since loads produce two values, make sure to remember that we legalized
1349 // both of them.
1350 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1351 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1352 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001353 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001354 case ISD::EXTRACT_ELEMENT: {
1355 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1356 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001357 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001358 case Legal:
1359 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1360 // 1 -> Hi
1361 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1362 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1363 TLI.getShiftAmountTy()));
1364 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1365 } else {
1366 // 0 -> Lo
1367 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1368 Node->getOperand(0));
1369 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001370 break;
1371 case Expand:
1372 // Get both the low and high parts.
1373 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1374 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1375 Result = Tmp2; // 1 -> Hi
1376 else
1377 Result = Tmp1; // 0 -> Lo
1378 break;
1379 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001380 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001381 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001382
1383 case ISD::CopyToReg:
1384 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001385
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001386 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001387 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001388 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001389 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001390 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001391 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001392 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001393 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001394 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001395 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001396 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1397 Tmp3);
1398 } else {
1399 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001400 }
1401
1402 // Since this produces two values, make sure to remember that we legalized
1403 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001404 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001405 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001406 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001407 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001408 break;
1409
1410 case ISD::RET:
1411 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001412
1413 // Ensure that libcalls are emitted before a return.
1414 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1415 Tmp1 = LegalizeOp(Tmp1);
1416 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001417
Chris Lattnerdc750592005-01-07 07:47:09 +00001418 switch (Node->getNumOperands()) {
1419 case 2: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001420 Tmp2 = Node->getOperand(1);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001421 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001422 case Legal:
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001423 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001424 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001425 case Expand:
1426 if (Tmp2.getValueType() != MVT::Vector) {
1427 SDOperand Lo, Hi;
1428 ExpandOp(Tmp2, Lo, Hi);
1429 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1430 } else {
1431 SDNode *InVal = Tmp2.Val;
1432 unsigned NumElems =
1433 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1434 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1435
1436 // Figure out if there is a Packed type corresponding to this Vector
1437 // type. If so, convert to the packed type.
1438 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1439 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1440 // Turn this into a return of the packed type.
1441 Tmp2 = PackVectorOp(Tmp2, TVT);
1442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1443 } else if (NumElems == 1) {
1444 // Turn this into a return of the scalar type.
1445 Tmp2 = PackVectorOp(Tmp2, EVT);
1446 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001447
1448 // FIXME: Returns of gcc generic vectors smaller than a legal type
1449 // should be returned in integer registers!
1450
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001451 // The scalarized value type may not be legal, e.g. it might require
1452 // promotion or expansion. Relegalize the return.
1453 Result = LegalizeOp(Result);
1454 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001455 // FIXME: Returns of gcc generic vectors larger than a legal vector
1456 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001457 SDOperand Lo, Hi;
1458 SplitVectorOp(Tmp2, Lo, Hi);
1459 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1460 Result = LegalizeOp(Result);
1461 }
1462 }
Misha Brukman835702a2005-04-21 22:36:52 +00001463 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001464 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001465 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001466 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1467 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001468 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001469 }
1470 break;
1471 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001472 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001473 break;
1474 default: { // ret <values>
1475 std::vector<SDOperand> NewValues;
1476 NewValues.push_back(Tmp1);
1477 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1478 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1479 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001480 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001481 break;
1482 case Expand: {
1483 SDOperand Lo, Hi;
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001484 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1485 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001486 ExpandOp(Node->getOperand(i), Lo, Hi);
1487 NewValues.push_back(Lo);
1488 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001489 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001490 }
1491 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001492 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001493 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001494
1495 if (NewValues.size() == Node->getNumOperands())
1496 Result = DAG.UpdateNodeOperands(Result, NewValues);
1497 else
1498 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001499 break;
1500 }
1501 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001502
Chris Lattner4d1ea712006-01-29 21:02:23 +00001503 if (Result.getOpcode() == ISD::RET) {
1504 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1505 default: assert(0 && "This action is not supported yet!");
1506 case TargetLowering::Legal: break;
1507 case TargetLowering::Custom:
1508 Tmp1 = TLI.LowerOperation(Result, DAG);
1509 if (Tmp1.Val) Result = Tmp1;
1510 break;
1511 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001512 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001513 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001514 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001515 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1516 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1517
Chris Lattnere69daaf2005-01-08 06:25:56 +00001518 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001519 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001520 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001521 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001522 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001523 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001524 } else {
1525 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001526 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001527 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001528 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1529 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001530 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001531 }
1532
Chris Lattnerdc750592005-01-07 07:47:09 +00001533 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1534 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001535 Tmp3 = LegalizeOp(Node->getOperand(1));
1536 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1537 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001538
Chris Lattnerd02b0542006-01-28 10:58:55 +00001539 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001540 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1541 default: assert(0 && "This action is not supported yet!");
1542 case TargetLowering::Legal: break;
1543 case TargetLowering::Custom:
1544 Tmp1 = TLI.LowerOperation(Result, DAG);
1545 if (Tmp1.Val) Result = Tmp1;
1546 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001547 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001548 break;
1549 }
1550 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001551 // Truncate the value and store the result.
1552 Tmp3 = PromoteOp(Node->getOperand(1));
1553 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001554 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001555 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001556 break;
1557
Chris Lattnerdc750592005-01-07 07:47:09 +00001558 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001559 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001560 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001561
1562 // If this is a vector type, then we have to calculate the increment as
1563 // the product of the element size in bytes, and the number of elements
1564 // in the high half of the vector.
1565 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1566 SDNode *InVal = Node->getOperand(1).Val;
1567 unsigned NumElems =
1568 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1569 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1570
1571 // Figure out if there is a Packed type corresponding to this Vector
1572 // type. If so, convert to the packed type.
1573 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1574 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1575 // Turn this into a normal store of the packed type.
1576 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1578 Node->getOperand(3));
1579 break;
1580 } else if (NumElems == 1) {
1581 // Turn this into a normal store of the scalar type.
1582 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1583 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1584 Node->getOperand(3));
Chris Lattner8e1fcab2006-03-31 17:37:22 +00001585 // The scalarized value type may not be legal, e.g. it might require
1586 // promotion or expansion. Relegalize the scalar store.
1587 Result = LegalizeOp(Result);
Chris Lattner32206f52006-03-18 01:44:44 +00001588 break;
1589 } else {
1590 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1591 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1592 }
1593 } else {
1594 ExpandOp(Node->getOperand(1), Lo, Hi);
1595 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001596
Chris Lattner8d90f522006-03-31 18:20:46 +00001597 if (!TLI.isLittleEndian())
1598 std::swap(Lo, Hi);
1599 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001600
Chris Lattner55e9cde2005-05-11 04:51:16 +00001601 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1602 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001603 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1604 getIntPtrConstant(IncrementSize));
1605 assert(isTypeLegal(Tmp2.getValueType()) &&
1606 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001607 // FIXME: This sets the srcvalue of both halves to be the same, which is
1608 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001609 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1610 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001611 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1612 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001613 }
1614 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001615 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001616 case ISD::PCMARKER:
1617 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001618 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001619 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001620 case ISD::STACKSAVE:
1621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001622 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1623 Tmp1 = Result.getValue(0);
1624 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001625
Chris Lattnerb3266452006-01-13 02:50:02 +00001626 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1627 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001628 case TargetLowering::Legal: break;
1629 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001630 Tmp3 = TLI.LowerOperation(Result, DAG);
1631 if (Tmp3.Val) {
1632 Tmp1 = LegalizeOp(Tmp3);
1633 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001634 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001635 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001636 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001637 // Expand to CopyFromReg if the target set
1638 // StackPointerRegisterToSaveRestore.
1639 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001640 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001641 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001642 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001643 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001644 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1645 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001646 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001647 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001648 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001649
1650 // Since stacksave produce two values, make sure to remember that we
1651 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001652 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1653 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1654 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001655
Chris Lattnerb3266452006-01-13 02:50:02 +00001656 case ISD::STACKRESTORE:
1657 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1658 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001659 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001660
1661 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1662 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001663 case TargetLowering::Legal: break;
1664 case TargetLowering::Custom:
1665 Tmp1 = TLI.LowerOperation(Result, DAG);
1666 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001667 break;
1668 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001669 // Expand to CopyToReg if the target set
1670 // StackPointerRegisterToSaveRestore.
1671 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1672 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1673 } else {
1674 Result = Tmp1;
1675 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001676 break;
1677 }
1678 break;
1679
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001680 case ISD::READCYCLECOUNTER:
1681 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001682 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001683
1684 // Since rdcc produce two values, make sure to remember that we legalized
1685 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001686 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001687 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001688 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001689
Evan Cheng31d15fa2005-12-23 07:29:34 +00001690 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1692 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1693
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001694 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1695 "Cannot handle illegal TRUNCSTORE yet!");
1696 Tmp2 = LegalizeOp(Node->getOperand(1));
1697
1698 // The only promote case we handle is TRUNCSTORE:i1 X into
1699 // -> TRUNCSTORE:i8 (and X, 1)
1700 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1701 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1702 TargetLowering::Promote) {
1703 // Promote the bool to a mask then store.
1704 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1705 DAG.getConstant(1, Tmp2.getValueType()));
1706 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1707 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001708
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001709 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1710 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001711 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1712 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001713 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001714
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001715 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1716 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1717 default: assert(0 && "This action is not supported yet!");
1718 case TargetLowering::Legal: break;
1719 case TargetLowering::Custom:
1720 Tmp1 = TLI.LowerOperation(Result, DAG);
1721 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001722 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001723 }
1724 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001725 }
Chris Lattner39c67442005-01-14 22:08:15 +00001726 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001727 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1728 case Expand: assert(0 && "It's impossible to expand bools");
1729 case Legal:
1730 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1731 break;
1732 case Promote:
1733 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1734 break;
1735 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001736 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001737 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001738
Chris Lattnerd02b0542006-01-28 10:58:55 +00001739 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001740
Nate Begeman987121a2005-08-23 04:29:48 +00001741 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001742 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001743 case TargetLowering::Legal: break;
1744 case TargetLowering::Custom: {
1745 Tmp1 = TLI.LowerOperation(Result, DAG);
1746 if (Tmp1.Val) Result = Tmp1;
1747 break;
1748 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001749 case TargetLowering::Expand:
1750 if (Tmp1.getOpcode() == ISD::SETCC) {
1751 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1752 Tmp2, Tmp3,
1753 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1754 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001755 // Make sure the condition is either zero or one. It may have been
1756 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001757 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1758 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1759 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001760 Result = DAG.getSelectCC(Tmp1,
1761 DAG.getConstant(0, Tmp1.getValueType()),
1762 Tmp2, Tmp3, ISD::SETNE);
1763 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001764 break;
1765 case TargetLowering::Promote: {
1766 MVT::ValueType NVT =
1767 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1768 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00001769 if (MVT::isVector(Tmp2.getValueType())) {
1770 ExtOp = ISD::BIT_CONVERT;
1771 TruncOp = ISD::BIT_CONVERT;
1772 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001773 ExtOp = ISD::ANY_EXTEND;
1774 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001775 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001776 ExtOp = ISD::FP_EXTEND;
1777 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001778 }
1779 // Promote each of the values to the new type.
1780 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1781 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1782 // Perform the larger operation, then round down.
1783 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1784 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1785 break;
1786 }
1787 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001788 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001789 case ISD::SELECT_CC: {
1790 Tmp1 = Node->getOperand(0); // LHS
1791 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001792 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1793 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001794 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001795
Nate Begeman7e7f4392006-02-01 07:19:44 +00001796 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1797
1798 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1799 // the LHS is a legal SETCC itself. In this case, we need to compare
1800 // the result against zero to select between true and false values.
1801 if (Tmp2.Val == 0) {
1802 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1803 CC = DAG.getCondCode(ISD::SETNE);
1804 }
1805 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1806
1807 // Everything is legal, see if we should expand this op or something.
1808 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1809 default: assert(0 && "This action is not supported yet!");
1810 case TargetLowering::Legal: break;
1811 case TargetLowering::Custom:
1812 Tmp1 = TLI.LowerOperation(Result, DAG);
1813 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001814 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001815 }
1816 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001817 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001818 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001819 Tmp1 = Node->getOperand(0);
1820 Tmp2 = Node->getOperand(1);
1821 Tmp3 = Node->getOperand(2);
1822 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1823
1824 // If we had to Expand the SetCC operands into a SELECT node, then it may
1825 // not always be possible to return a true LHS & RHS. In this case, just
1826 // return the value we legalized, returned in the LHS
1827 if (Tmp2.Val == 0) {
1828 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001829 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001830 }
Nate Begeman987121a2005-08-23 04:29:48 +00001831
Chris Lattnerf263a232006-01-30 22:43:50 +00001832 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001833 default: assert(0 && "Cannot handle this action for SETCC yet!");
1834 case TargetLowering::Custom:
1835 isCustom = true;
1836 // FALLTHROUGH.
1837 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001838 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001839 if (isCustom) {
1840 Tmp3 = TLI.LowerOperation(Result, DAG);
1841 if (Tmp3.Val) Result = Tmp3;
1842 }
Nate Begeman987121a2005-08-23 04:29:48 +00001843 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001844 case TargetLowering::Promote: {
1845 // First step, figure out the appropriate operation to use.
1846 // Allow SETCC to not be supported for all legal data types
1847 // Mostly this targets FP
1848 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1849 MVT::ValueType OldVT = NewInTy;
1850
1851 // Scan for the appropriate larger type to use.
1852 while (1) {
1853 NewInTy = (MVT::ValueType)(NewInTy+1);
1854
1855 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1856 "Fell off of the edge of the integer world");
1857 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1858 "Fell off of the edge of the floating point world");
1859
1860 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001861 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001862 break;
1863 }
1864 if (MVT::isInteger(NewInTy))
1865 assert(0 && "Cannot promote Legal Integer SETCC yet");
1866 else {
1867 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1868 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1869 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001870 Tmp1 = LegalizeOp(Tmp1);
1871 Tmp2 = LegalizeOp(Tmp2);
1872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001873 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001874 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001875 }
Nate Begeman987121a2005-08-23 04:29:48 +00001876 case TargetLowering::Expand:
1877 // Expand a setcc node into a select_cc of the same condition, lhs, and
1878 // rhs that selects between const 1 (true) and const 0 (false).
1879 MVT::ValueType VT = Node->getValueType(0);
1880 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1881 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1882 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001883 break;
1884 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001885 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001886 case ISD::MEMSET:
1887 case ISD::MEMCPY:
1888 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001889 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001890 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1891
1892 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1893 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1894 case Expand: assert(0 && "Cannot expand a byte!");
1895 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001896 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001897 break;
1898 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001899 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001900 break;
1901 }
1902 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001903 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001904 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001905
1906 SDOperand Tmp4;
1907 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001908 case Expand: {
1909 // Length is too big, just take the lo-part of the length.
1910 SDOperand HiPart;
1911 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1912 break;
1913 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001914 case Legal:
1915 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001916 break;
1917 case Promote:
1918 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001919 break;
1920 }
1921
1922 SDOperand Tmp5;
1923 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1924 case Expand: assert(0 && "Cannot expand this yet!");
1925 case Legal:
1926 Tmp5 = LegalizeOp(Node->getOperand(4));
1927 break;
1928 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001929 Tmp5 = PromoteOp(Node->getOperand(4));
1930 break;
1931 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001932
1933 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1934 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001935 case TargetLowering::Custom:
1936 isCustom = true;
1937 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001938 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001939 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001940 if (isCustom) {
1941 Tmp1 = TLI.LowerOperation(Result, DAG);
1942 if (Tmp1.Val) Result = Tmp1;
1943 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001944 break;
1945 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001946 // Otherwise, the target does not support this operation. Lower the
1947 // operation to an explicit libcall as appropriate.
1948 MVT::ValueType IntPtr = TLI.getPointerTy();
1949 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1950 std::vector<std::pair<SDOperand, const Type*> > Args;
1951
Reid Spencer6dced922005-01-12 14:53:45 +00001952 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001953 if (Node->getOpcode() == ISD::MEMSET) {
1954 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001955 // Extend the (previously legalized) ubyte argument to be an int value
1956 // for the call.
1957 if (Tmp3.getValueType() > MVT::i32)
1958 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1959 else
1960 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001961 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1962 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1963
1964 FnName = "memset";
1965 } else if (Node->getOpcode() == ISD::MEMCPY ||
1966 Node->getOpcode() == ISD::MEMMOVE) {
1967 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1968 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1969 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1970 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1971 } else {
1972 assert(0 && "Unknown op!");
1973 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001974
Chris Lattner85d70c62005-01-11 05:57:22 +00001975 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001976 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001977 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001978 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001979 break;
1980 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001981 }
1982 break;
1983 }
Chris Lattner5385db52005-05-09 20:23:03 +00001984
Chris Lattner4157c412005-04-02 04:00:59 +00001985 case ISD::SHL_PARTS:
1986 case ISD::SRA_PARTS:
1987 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001988 std::vector<SDOperand> Ops;
1989 bool Changed = false;
1990 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1991 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1992 Changed |= Ops.back() != Node->getOperand(i);
1993 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001994 if (Changed)
1995 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001996
Evan Cheng870e4f82006-01-09 18:31:59 +00001997 switch (TLI.getOperationAction(Node->getOpcode(),
1998 Node->getValueType(0))) {
1999 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002000 case TargetLowering::Legal: break;
2001 case TargetLowering::Custom:
2002 Tmp1 = TLI.LowerOperation(Result, DAG);
2003 if (Tmp1.Val) {
2004 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002005 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002006 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002007 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2008 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002009 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002010 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002011 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002012 return RetVal;
2013 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002014 break;
2015 }
2016
Chris Lattner13fe99c2005-04-02 05:00:07 +00002017 // Since these produce multiple values, make sure to remember that we
2018 // legalized all of them.
2019 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2020 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2021 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002022 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002023
2024 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002025 case ISD::ADD:
2026 case ISD::SUB:
2027 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002028 case ISD::MULHS:
2029 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002030 case ISD::UDIV:
2031 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002032 case ISD::AND:
2033 case ISD::OR:
2034 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002035 case ISD::SHL:
2036 case ISD::SRL:
2037 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002038 case ISD::FADD:
2039 case ISD::FSUB:
2040 case ISD::FMUL:
2041 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002042 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002043 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2044 case Expand: assert(0 && "Not possible");
2045 case Legal:
2046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2047 break;
2048 case Promote:
2049 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2050 break;
2051 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002052
2053 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002054
Andrew Lenharth72594262005-12-24 23:42:32 +00002055 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002056 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002057 case TargetLowering::Legal: break;
2058 case TargetLowering::Custom:
2059 Tmp1 = TLI.LowerOperation(Result, DAG);
2060 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002061 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002062 case TargetLowering::Expand: {
2063 assert(MVT::isVector(Node->getValueType(0)) &&
2064 "Cannot expand this binary operator!");
2065 // Expand the operation into a bunch of nasty scalar code.
2066 std::vector<SDOperand> Ops;
2067 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2068 MVT::ValueType PtrVT = TLI.getPointerTy();
2069 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2070 i != e; ++i) {
2071 SDOperand Idx = DAG.getConstant(i, PtrVT);
2072 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2073 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2074 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2075 }
2076 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
2077 break;
2078 }
Evan Cheng119266e2006-04-12 21:20:24 +00002079 case TargetLowering::Promote: {
2080 switch (Node->getOpcode()) {
2081 default: assert(0 && "Do not know how to promote this BinOp!");
2082 case ISD::AND:
2083 case ISD::OR:
2084 case ISD::XOR: {
2085 MVT::ValueType OVT = Node->getValueType(0);
2086 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2087 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2088 // Bit convert each of the values to the new type.
2089 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2090 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2091 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2092 // Bit convert the result back the original type.
2093 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2094 break;
2095 }
2096 }
2097 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002098 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002099 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002100
2101 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2102 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2103 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2104 case Expand: assert(0 && "Not possible");
2105 case Legal:
2106 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2107 break;
2108 case Promote:
2109 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2110 break;
2111 }
2112
2113 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2114
2115 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2116 default: assert(0 && "Operation not supported");
2117 case TargetLowering::Custom:
2118 Tmp1 = TLI.LowerOperation(Result, DAG);
2119 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002120 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002121 case TargetLowering::Legal: break;
2122 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00002123 // If this target supports fabs/fneg natively, do this efficiently.
2124 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2125 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2126 // Get the sign bit of the RHS.
2127 MVT::ValueType IVT =
2128 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2129 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2130 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2131 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2132 // Get the absolute value of the result.
2133 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2134 // Select between the nabs and abs value based on the sign bit of
2135 // the input.
2136 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2137 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2138 AbsVal),
2139 AbsVal);
2140 Result = LegalizeOp(Result);
2141 break;
2142 }
2143
2144 // Otherwise, do bitwise ops!
2145
2146 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002147 const char *FnName;
2148 if (Node->getValueType(0) == MVT::f32) {
2149 FnName = "copysignf";
2150 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2151 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2152 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2153 } else {
2154 FnName = "copysign";
2155 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2156 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2157 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2158 }
2159 SDOperand Dummy;
2160 Result = ExpandLibCall(FnName, Node, Dummy);
2161 break;
2162 }
2163 break;
2164
Nate Begeman5965bd12006-02-17 05:43:56 +00002165 case ISD::ADDC:
2166 case ISD::SUBC:
2167 Tmp1 = LegalizeOp(Node->getOperand(0));
2168 Tmp2 = LegalizeOp(Node->getOperand(1));
2169 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2170 // Since this produces two values, make sure to remember that we legalized
2171 // both of them.
2172 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2173 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2174 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002175
Nate Begeman5965bd12006-02-17 05:43:56 +00002176 case ISD::ADDE:
2177 case ISD::SUBE:
2178 Tmp1 = LegalizeOp(Node->getOperand(0));
2179 Tmp2 = LegalizeOp(Node->getOperand(1));
2180 Tmp3 = LegalizeOp(Node->getOperand(2));
2181 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2182 // Since this produces two values, make sure to remember that we legalized
2183 // both of them.
2184 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2185 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2186 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002187
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002188 case ISD::BUILD_PAIR: {
2189 MVT::ValueType PairTy = Node->getValueType(0);
2190 // TODO: handle the case where the Lo and Hi operands are not of legal type
2191 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2192 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2193 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002194 case TargetLowering::Promote:
2195 case TargetLowering::Custom:
2196 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002197 case TargetLowering::Legal:
2198 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2199 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2200 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002201 case TargetLowering::Expand:
2202 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2203 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2204 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2205 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2206 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002207 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002208 break;
2209 }
2210 break;
2211 }
2212
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002213 case ISD::UREM:
2214 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002215 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002216 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2217 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002218
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002219 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002220 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2221 case TargetLowering::Custom:
2222 isCustom = true;
2223 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002224 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002225 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002226 if (isCustom) {
2227 Tmp1 = TLI.LowerOperation(Result, DAG);
2228 if (Tmp1.Val) Result = Tmp1;
2229 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002230 break;
Chris Lattner81914422005-08-03 20:31:37 +00002231 case TargetLowering::Expand:
2232 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002233 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002234 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002235 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002236 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2237 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2238 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2239 } else {
2240 // Floating point mod -> fmod libcall.
2241 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2242 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002243 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002244 }
2245 break;
2246 }
2247 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002248 case ISD::VAARG: {
2249 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2250 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2251
Chris Lattner364b89a2006-01-28 07:42:08 +00002252 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002253 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2254 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002255 case TargetLowering::Custom:
2256 isCustom = true;
2257 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002258 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002259 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2260 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002261 Tmp1 = Result.getValue(1);
2262
2263 if (isCustom) {
2264 Tmp2 = TLI.LowerOperation(Result, DAG);
2265 if (Tmp2.Val) {
2266 Result = LegalizeOp(Tmp2);
2267 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2268 }
2269 }
Nate Begemane74795c2006-01-25 18:21:52 +00002270 break;
2271 case TargetLowering::Expand: {
2272 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2273 Node->getOperand(2));
2274 // Increment the pointer, VAList, to the next vaarg
2275 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2276 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2277 TLI.getPointerTy()));
2278 // Store the incremented VAList to the legalized pointer
2279 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2280 Node->getOperand(2));
2281 // Load the actual argument out of the pointer VAList
2282 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002283 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002284 Result = LegalizeOp(Result);
2285 break;
2286 }
2287 }
2288 // Since VAARG produces two values, make sure to remember that we
2289 // legalized both of them.
2290 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002291 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2292 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002293 }
2294
2295 case ISD::VACOPY:
2296 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2297 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2298 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2299
2300 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2301 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002302 case TargetLowering::Custom:
2303 isCustom = true;
2304 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002305 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002306 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2307 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002308 if (isCustom) {
2309 Tmp1 = TLI.LowerOperation(Result, DAG);
2310 if (Tmp1.Val) Result = Tmp1;
2311 }
Nate Begemane74795c2006-01-25 18:21:52 +00002312 break;
2313 case TargetLowering::Expand:
2314 // This defaults to loading a pointer from the input and storing it to the
2315 // output, returning the chain.
2316 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2317 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2318 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002319 break;
2320 }
2321 break;
2322
2323 case ISD::VAEND:
2324 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2325 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2326
2327 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2328 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002329 case TargetLowering::Custom:
2330 isCustom = true;
2331 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002332 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002333 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002334 if (isCustom) {
2335 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2336 if (Tmp1.Val) Result = Tmp1;
2337 }
Nate Begemane74795c2006-01-25 18:21:52 +00002338 break;
2339 case TargetLowering::Expand:
2340 Result = Tmp1; // Default to a no-op, return the chain
2341 break;
2342 }
2343 break;
2344
2345 case ISD::VASTART:
2346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2348
Chris Lattnerd02b0542006-01-28 10:58:55 +00002349 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2350
Nate Begemane74795c2006-01-25 18:21:52 +00002351 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2352 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002353 case TargetLowering::Legal: break;
2354 case TargetLowering::Custom:
2355 Tmp1 = TLI.LowerOperation(Result, DAG);
2356 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002357 break;
2358 }
2359 break;
2360
Nate Begeman1b8121b2006-01-11 21:21:00 +00002361 case ISD::ROTL:
2362 case ISD::ROTR:
2363 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2364 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002365
2366 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2367 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002368 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002369 break;
2370
Nate Begeman2fba8a32006-01-14 03:14:10 +00002371 case ISD::BSWAP:
2372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2373 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002374 case TargetLowering::Custom:
2375 assert(0 && "Cannot custom legalize this yet!");
2376 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002377 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002378 break;
2379 case TargetLowering::Promote: {
2380 MVT::ValueType OVT = Tmp1.getValueType();
2381 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2382 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002383
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002384 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2385 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2386 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2387 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2388 break;
2389 }
2390 case TargetLowering::Expand:
2391 Result = ExpandBSWAP(Tmp1);
2392 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002393 }
2394 break;
2395
Andrew Lenharth5e177822005-05-03 17:19:30 +00002396 case ISD::CTPOP:
2397 case ISD::CTTZ:
2398 case ISD::CTLZ:
2399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2400 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002401 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002402 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002403 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002404 break;
2405 case TargetLowering::Promote: {
2406 MVT::ValueType OVT = Tmp1.getValueType();
2407 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002408
2409 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002410 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2411 // Perform the larger operation, then subtract if needed.
2412 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002413 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002414 case ISD::CTPOP:
2415 Result = Tmp1;
2416 break;
2417 case ISD::CTTZ:
2418 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002419 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2420 DAG.getConstant(getSizeInBits(NVT), NVT),
2421 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002422 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002423 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2424 break;
2425 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002426 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002427 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2428 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002429 getSizeInBits(OVT), NVT));
2430 break;
2431 }
2432 break;
2433 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002434 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002435 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002436 break;
2437 }
2438 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002439
Chris Lattner13fe99c2005-04-02 05:00:07 +00002440 // Unary operators
2441 case ISD::FABS:
2442 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002443 case ISD::FSQRT:
2444 case ISD::FSIN:
2445 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002446 Tmp1 = LegalizeOp(Node->getOperand(0));
2447 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002448 case TargetLowering::Promote:
2449 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002450 isCustom = true;
2451 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002452 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002453 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002454 if (isCustom) {
2455 Tmp1 = TLI.LowerOperation(Result, DAG);
2456 if (Tmp1.Val) Result = Tmp1;
2457 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002458 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002459 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002460 switch (Node->getOpcode()) {
2461 default: assert(0 && "Unreachable!");
2462 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002463 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2464 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002465 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002466 break;
Chris Lattner80026402005-04-30 04:43:14 +00002467 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002468 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2469 MVT::ValueType VT = Node->getValueType(0);
2470 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002471 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002472 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2473 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002474 break;
2475 }
2476 case ISD::FSQRT:
2477 case ISD::FSIN:
2478 case ISD::FCOS: {
2479 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002480 const char *FnName = 0;
2481 switch(Node->getOpcode()) {
2482 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2483 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2484 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2485 default: assert(0 && "Unreachable!");
2486 }
Nate Begeman77558da2005-08-04 21:43:28 +00002487 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002488 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002489 break;
2490 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002491 }
2492 break;
2493 }
2494 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002495
2496 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002497 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002498 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002499 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002500 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2501 Node->getOperand(0).getValueType())) {
2502 default: assert(0 && "Unknown operation action!");
2503 case TargetLowering::Expand:
2504 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2505 break;
2506 case TargetLowering::Legal:
2507 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002508 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002509 break;
2510 }
2511 }
2512 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002513 case ISD::VBIT_CONVERT: {
2514 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2515 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2516
2517 // The input has to be a vector type, we have to either scalarize it, pack
2518 // it, or convert it based on whether the input vector type is legal.
2519 SDNode *InVal = Node->getOperand(0).Val;
2520 unsigned NumElems =
2521 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2522 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2523
2524 // Figure out if there is a Packed type corresponding to this Vector
2525 // type. If so, convert to the packed type.
2526 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2527 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2528 // Turn this into a bit convert of the packed input.
2529 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2530 PackVectorOp(Node->getOperand(0), TVT));
2531 break;
2532 } else if (NumElems == 1) {
2533 // Turn this into a bit convert of the scalar input.
2534 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2535 PackVectorOp(Node->getOperand(0), EVT));
2536 break;
2537 } else {
2538 // FIXME: UNIMP! Store then reload
2539 assert(0 && "Cast from unsupported vector type not implemented yet!");
2540 }
2541 }
2542
Chris Lattner13fe99c2005-04-02 05:00:07 +00002543 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002544 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002545 case ISD::UINT_TO_FP: {
2546 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002547 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2548 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002549 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002550 Node->getOperand(0).getValueType())) {
2551 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002552 case TargetLowering::Custom:
2553 isCustom = true;
2554 // FALLTHROUGH
2555 case TargetLowering::Legal:
2556 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002557 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002558 if (isCustom) {
2559 Tmp1 = TLI.LowerOperation(Result, DAG);
2560 if (Tmp1.Val) Result = Tmp1;
2561 }
2562 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002563 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002564 Result = ExpandLegalINT_TO_FP(isSigned,
2565 LegalizeOp(Node->getOperand(0)),
2566 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002567 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002568 case TargetLowering::Promote:
2569 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2570 Node->getValueType(0),
2571 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002572 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002573 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002574 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002575 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002576 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2577 Node->getValueType(0), Node->getOperand(0));
2578 break;
2579 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002580 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002581 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002582 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2583 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002584 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002585 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2586 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002587 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002588 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2589 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002590 break;
2591 }
2592 break;
2593 }
2594 case ISD::TRUNCATE:
2595 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2596 case Legal:
2597 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002598 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002599 break;
2600 case Expand:
2601 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2602
2603 // Since the result is legal, we should just be able to truncate the low
2604 // part of the source.
2605 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2606 break;
2607 case Promote:
2608 Result = PromoteOp(Node->getOperand(0));
2609 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2610 break;
2611 }
2612 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002613
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002614 case ISD::FP_TO_SINT:
2615 case ISD::FP_TO_UINT:
2616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2617 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002618 Tmp1 = LegalizeOp(Node->getOperand(0));
2619
Chris Lattner44fe26f2005-07-29 00:11:56 +00002620 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2621 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002622 case TargetLowering::Custom:
2623 isCustom = true;
2624 // FALLTHROUGH
2625 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002626 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002627 if (isCustom) {
2628 Tmp1 = TLI.LowerOperation(Result, DAG);
2629 if (Tmp1.Val) Result = Tmp1;
2630 }
2631 break;
2632 case TargetLowering::Promote:
2633 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2634 Node->getOpcode() == ISD::FP_TO_SINT);
2635 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002636 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002637 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2638 SDOperand True, False;
2639 MVT::ValueType VT = Node->getOperand(0).getValueType();
2640 MVT::ValueType NVT = Node->getValueType(0);
2641 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2642 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2643 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2644 Node->getOperand(0), Tmp2, ISD::SETLT);
2645 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2646 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002647 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002648 Tmp2));
2649 False = DAG.getNode(ISD::XOR, NVT, False,
2650 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002651 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2652 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002653 } else {
2654 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2655 }
2656 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002657 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002658 break;
2659 case Expand:
2660 assert(0 && "Shouldn't need to expand other operators here!");
2661 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002662 Tmp1 = PromoteOp(Node->getOperand(0));
2663 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2664 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002665 break;
2666 }
2667 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002668
Chris Lattner7753f172005-09-02 00:18:10 +00002669 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002670 case ISD::ZERO_EXTEND:
2671 case ISD::SIGN_EXTEND:
2672 case ISD::FP_EXTEND:
2673 case ISD::FP_ROUND:
2674 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002675 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002676 case Legal:
2677 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002678 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002679 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002680 case Promote:
2681 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002682 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002683 Tmp1 = PromoteOp(Node->getOperand(0));
2684 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002685 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002686 case ISD::ZERO_EXTEND:
2687 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002688 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002689 Result = DAG.getZeroExtendInReg(Result,
2690 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002691 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002692 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002693 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002694 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002695 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002696 Result,
2697 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002698 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002699 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002700 Result = PromoteOp(Node->getOperand(0));
2701 if (Result.getValueType() != Op.getValueType())
2702 // Dynamically dead while we have only 2 FP types.
2703 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2704 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002705 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002706 Result = PromoteOp(Node->getOperand(0));
2707 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2708 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002709 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002710 }
2711 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002712 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002713 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002714 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002715 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002716
2717 // If this operation is not supported, convert it to a shl/shr or load/store
2718 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002719 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2720 default: assert(0 && "This action not supported for this op yet!");
2721 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002722 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002723 break;
2724 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002725 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002726 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002727 // NOTE: we could fall back on load/store here too for targets without
2728 // SAR. However, it is doubtful that any exist.
2729 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2730 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002731 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002732 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2733 Node->getOperand(0), ShiftCst);
2734 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2735 Result, ShiftCst);
2736 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2737 // The only way we can lower this is to turn it into a STORETRUNC,
2738 // EXTLOAD pair, targetting a temporary location (a stack slot).
2739
2740 // NOTE: there is a choice here between constantly creating new stack
2741 // slots and always reusing the same one. We currently always create
2742 // new ones, as reuse may inhibit scheduling.
2743 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2744 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2745 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2746 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002747 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002748 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2749 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2750 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002751 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002752 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002753 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2754 Result, StackSlot, DAG.getSrcValue(NULL),
2755 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002756 } else {
2757 assert(0 && "Unknown op");
2758 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002759 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002760 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002761 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002762 }
Chris Lattner99222f72005-01-15 07:15:18 +00002763 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002764
Chris Lattner101ea662006-04-08 04:13:17 +00002765 assert(Result.getValueType() == Op.getValueType() &&
2766 "Bad legalization!");
2767
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002768 // Make sure that the generated code is itself legal.
2769 if (Result != Op)
2770 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002771
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002772 // Note that LegalizeOp may be reentered even from single-use nodes, which
2773 // means that we always must cache transformed nodes.
2774 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002775 return Result;
2776}
2777
Chris Lattner4d978642005-01-15 22:16:26 +00002778/// PromoteOp - Given an operation that produces a value in an invalid type,
2779/// promote it to compute the value into a larger type. The produced value will
2780/// have the correct bits for the low portion of the register, but no guarantee
2781/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002782SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2783 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002784 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002785 assert(getTypeAction(VT) == Promote &&
2786 "Caller should expand or legalize operands that are not promotable!");
2787 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2788 "Cannot promote to smaller type!");
2789
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002790 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002791 SDOperand Result;
2792 SDNode *Node = Op.Val;
2793
Chris Lattner1a570f12005-09-02 20:32:45 +00002794 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2795 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002796
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002797 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002798 case ISD::CopyFromReg:
2799 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002800 default:
2801 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2802 assert(0 && "Do not know how to promote this operator!");
2803 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002804 case ISD::UNDEF:
2805 Result = DAG.getNode(ISD::UNDEF, NVT);
2806 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002807 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002808 if (VT != MVT::i1)
2809 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2810 else
2811 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002812 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2813 break;
2814 case ISD::ConstantFP:
2815 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2816 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2817 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002818
Chris Lattner2cb338d2005-01-18 02:59:52 +00002819 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002820 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002821 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2822 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002823 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00002824
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002825 case ISD::TRUNCATE:
2826 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2827 case Legal:
2828 Result = LegalizeOp(Node->getOperand(0));
2829 assert(Result.getValueType() >= NVT &&
2830 "This truncation doesn't make sense!");
2831 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2832 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2833 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002834 case Promote:
2835 // The truncation is not required, because we don't guarantee anything
2836 // about high bits anyway.
2837 Result = PromoteOp(Node->getOperand(0));
2838 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002839 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002840 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2841 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002842 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002843 }
2844 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002845 case ISD::SIGN_EXTEND:
2846 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002847 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002848 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2849 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2850 case Legal:
2851 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002852 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002853 break;
2854 case Promote:
2855 // Promote the reg if it's smaller.
2856 Result = PromoteOp(Node->getOperand(0));
2857 // The high bits are not guaranteed to be anything. Insert an extend.
2858 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002859 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002860 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002861 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002862 Result = DAG.getZeroExtendInReg(Result,
2863 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002864 break;
2865 }
2866 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002867 case ISD::BIT_CONVERT:
2868 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2869 Result = PromoteOp(Result);
2870 break;
2871
Chris Lattner4d978642005-01-15 22:16:26 +00002872 case ISD::FP_EXTEND:
2873 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2874 case ISD::FP_ROUND:
2875 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2876 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2877 case Promote: assert(0 && "Unreachable with 2 FP types!");
2878 case Legal:
2879 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002880 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002881 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002882 break;
2883 }
2884 break;
2885
2886 case ISD::SINT_TO_FP:
2887 case ISD::UINT_TO_FP:
2888 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2889 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002890 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002891 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002892 break;
2893
2894 case Promote:
2895 Result = PromoteOp(Node->getOperand(0));
2896 if (Node->getOpcode() == ISD::SINT_TO_FP)
2897 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002898 Result,
2899 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002900 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002901 Result = DAG.getZeroExtendInReg(Result,
2902 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002903 // No extra round required here.
2904 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002905 break;
2906 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002907 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2908 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002909 // Round if we cannot tolerate excess precision.
2910 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002911 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2912 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002913 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002914 }
Chris Lattner4d978642005-01-15 22:16:26 +00002915 break;
2916
Chris Lattner268d4572005-12-09 17:32:47 +00002917 case ISD::SIGN_EXTEND_INREG:
2918 Result = PromoteOp(Node->getOperand(0));
2919 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2920 Node->getOperand(1));
2921 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002922 case ISD::FP_TO_SINT:
2923 case ISD::FP_TO_UINT:
2924 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2925 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002926 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002927 break;
2928 case Promote:
2929 // The input result is prerounded, so we don't have to do anything
2930 // special.
2931 Tmp1 = PromoteOp(Node->getOperand(0));
2932 break;
2933 case Expand:
2934 assert(0 && "not implemented");
2935 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002936 // If we're promoting a UINT to a larger size, check to see if the new node
2937 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2938 // we can use that instead. This allows us to generate better code for
2939 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2940 // legal, such as PowerPC.
2941 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002942 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002943 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2944 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002945 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2946 } else {
2947 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2948 }
Chris Lattner4d978642005-01-15 22:16:26 +00002949 break;
2950
Chris Lattner13fe99c2005-04-02 05:00:07 +00002951 case ISD::FABS:
2952 case ISD::FNEG:
2953 Tmp1 = PromoteOp(Node->getOperand(0));
2954 assert(Tmp1.getValueType() == NVT);
2955 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2956 // NOTE: we do not have to do any extra rounding here for
2957 // NoExcessFPPrecision, because we know the input will have the appropriate
2958 // precision, and these operations don't modify precision at all.
2959 break;
2960
Chris Lattner9d6fa982005-04-28 21:44:33 +00002961 case ISD::FSQRT:
2962 case ISD::FSIN:
2963 case ISD::FCOS:
2964 Tmp1 = PromoteOp(Node->getOperand(0));
2965 assert(Tmp1.getValueType() == NVT);
2966 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002967 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002968 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2969 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002970 break;
2971
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002972 case ISD::AND:
2973 case ISD::OR:
2974 case ISD::XOR:
Evan Cheng119266e2006-04-12 21:20:24 +00002975 // The input may have strange things in the top bits of the registers, but
2976 // these operations don't care. They may have weird bits going out, but
2977 // that too is okay if they are integer operations.
2978 Tmp1 = PromoteOp(Node->getOperand(0));
2979 Tmp2 = PromoteOp(Node->getOperand(1));
2980 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2981 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2982 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002983 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002984 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002985 case ISD::MUL:
2986 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002987 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002988 // that too is okay if they are integer operations.
2989 Tmp1 = PromoteOp(Node->getOperand(0));
2990 Tmp2 = PromoteOp(Node->getOperand(1));
2991 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2992 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002993 break;
2994 case ISD::FADD:
2995 case ISD::FSUB:
2996 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002997 Tmp1 = PromoteOp(Node->getOperand(0));
2998 Tmp2 = PromoteOp(Node->getOperand(1));
2999 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3000 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3001
3002 // Floating point operations will give excess precision that we may not be
3003 // able to tolerate. If we DO allow excess precision, just leave it,
3004 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003005 // FIXME: Why would we need to round FP ops more than integer ones?
3006 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003007 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003008 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3009 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003010 break;
3011
Chris Lattner4d978642005-01-15 22:16:26 +00003012 case ISD::SDIV:
3013 case ISD::SREM:
3014 // These operators require that their input be sign extended.
3015 Tmp1 = PromoteOp(Node->getOperand(0));
3016 Tmp2 = PromoteOp(Node->getOperand(1));
3017 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003018 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3019 DAG.getValueType(VT));
3020 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3021 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003022 }
3023 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3024
3025 // Perform FP_ROUND: this is probably overly pessimistic.
3026 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003027 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3028 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003029 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003030 case ISD::FDIV:
3031 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003032 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003033 // These operators require that their input be fp extended.
3034 Tmp1 = PromoteOp(Node->getOperand(0));
3035 Tmp2 = PromoteOp(Node->getOperand(1));
3036 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3037
3038 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003039 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003040 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3041 DAG.getValueType(VT));
3042 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003043
3044 case ISD::UDIV:
3045 case ISD::UREM:
3046 // These operators require that their input be zero extended.
3047 Tmp1 = PromoteOp(Node->getOperand(0));
3048 Tmp2 = PromoteOp(Node->getOperand(1));
3049 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003050 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3051 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003052 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3053 break;
3054
3055 case ISD::SHL:
3056 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003057 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003058 break;
3059 case ISD::SRA:
3060 // The input value must be properly sign extended.
3061 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003062 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3063 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003064 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003065 break;
3066 case ISD::SRL:
3067 // The input value must be properly zero extended.
3068 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003069 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003070 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003071 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003072
3073 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003074 Tmp1 = Node->getOperand(0); // Get the chain.
3075 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003076 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3077 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3078 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3079 } else {
3080 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3081 Node->getOperand(2));
3082 // Increment the pointer, VAList, to the next vaarg
3083 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3084 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3085 TLI.getPointerTy()));
3086 // Store the incremented VAList to the legalized pointer
3087 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3088 Node->getOperand(2));
3089 // Load the actual argument out of the pointer VAList
3090 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3091 DAG.getSrcValue(0), VT);
3092 }
3093 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003094 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003095 break;
3096
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003097 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003098 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3099 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003100 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003101 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003102 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003103 case ISD::SEXTLOAD:
3104 case ISD::ZEXTLOAD:
3105 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003106 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3107 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00003108 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003109 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003110 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003111 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003112 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003113 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3114 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003115 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003116 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003117 case ISD::SELECT_CC:
3118 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3119 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3120 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003121 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003122 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003123 case ISD::BSWAP:
3124 Tmp1 = Node->getOperand(0);
3125 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3126 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3127 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3128 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3129 TLI.getShiftAmountTy()));
3130 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003131 case ISD::CTPOP:
3132 case ISD::CTTZ:
3133 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003134 // Zero extend the argument
3135 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003136 // Perform the larger operation, then subtract if needed.
3137 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003138 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003139 case ISD::CTPOP:
3140 Result = Tmp1;
3141 break;
3142 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003143 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003144 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003145 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003146 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003147 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003148 break;
3149 case ISD::CTLZ:
3150 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003151 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3152 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003153 getSizeInBits(VT), NVT));
3154 break;
3155 }
3156 break;
Chris Lattner6f423252006-03-31 17:55:51 +00003157 case ISD::VEXTRACT_VECTOR_ELT:
3158 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3159 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003160 case ISD::EXTRACT_VECTOR_ELT:
3161 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3162 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003163 }
3164
3165 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003166
3167 // Make sure the result is itself legal.
3168 Result = LegalizeOp(Result);
3169
3170 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003171 AddPromotedOperand(Op, Result);
3172 return Result;
3173}
Chris Lattnerdc750592005-01-07 07:47:09 +00003174
Chris Lattner6f423252006-03-31 17:55:51 +00003175/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3176/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3177/// on the vector type. The return type of this matches the element type of the
3178/// vector, which may not be legal for the target.
3179SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3180 // We know that operand #0 is the Vec vector. If the index is a constant
3181 // or if the invec is a supported hardware type, we can use it. Otherwise,
3182 // lower to a store then an indexed load.
3183 SDOperand Vec = Op.getOperand(0);
3184 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3185
3186 SDNode *InVal = Vec.Val;
3187 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3188 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3189
3190 // Figure out if there is a Packed type corresponding to this Vector
3191 // type. If so, convert to the packed type.
3192 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3193 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3194 // Turn this into a packed extract_vector_elt operation.
3195 Vec = PackVectorOp(Vec, TVT);
3196 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3197 } else if (NumElems == 1) {
3198 // This must be an access of the only element. Return it.
3199 return PackVectorOp(Vec, EVT);
3200 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3201 SDOperand Lo, Hi;
3202 SplitVectorOp(Vec, Lo, Hi);
3203 if (CIdx->getValue() < NumElems/2) {
3204 Vec = Lo;
3205 } else {
3206 Vec = Hi;
3207 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3208 }
3209
3210 // It's now an extract from the appropriate high or low part. Recurse.
3211 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3212 return LowerVEXTRACT_VECTOR_ELT(Op);
3213 } else {
3214 // Variable index case for extract element.
3215 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3216 assert(0 && "unimp!");
3217 return SDOperand();
3218 }
3219}
3220
Chris Lattner42a5fca2006-04-02 05:06:04 +00003221/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3222/// memory traffic.
3223SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3224 SDOperand Vector = Op.getOperand(0);
3225 SDOperand Idx = Op.getOperand(1);
3226
3227 // If the target doesn't support this, store the value to a temporary
3228 // stack slot, then LOAD the scalar element back out.
3229 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
3230 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3231 Vector, StackPtr, DAG.getSrcValue(NULL));
3232
3233 // Add the offset to the index.
3234 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3235 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3236 DAG.getConstant(EltSize, Idx.getValueType()));
3237 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3238
3239 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL));
3240}
3241
Chris Lattner6f423252006-03-31 17:55:51 +00003242
Nate Begeman7e7f4392006-02-01 07:19:44 +00003243/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3244/// with condition CC on the current target. This usually involves legalizing
3245/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3246/// there may be no choice but to create a new SetCC node to represent the
3247/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3248/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3249void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3250 SDOperand &RHS,
3251 SDOperand &CC) {
3252 SDOperand Tmp1, Tmp2, Result;
3253
3254 switch (getTypeAction(LHS.getValueType())) {
3255 case Legal:
3256 Tmp1 = LegalizeOp(LHS); // LHS
3257 Tmp2 = LegalizeOp(RHS); // RHS
3258 break;
3259 case Promote:
3260 Tmp1 = PromoteOp(LHS); // LHS
3261 Tmp2 = PromoteOp(RHS); // RHS
3262
3263 // If this is an FP compare, the operands have already been extended.
3264 if (MVT::isInteger(LHS.getValueType())) {
3265 MVT::ValueType VT = LHS.getValueType();
3266 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3267
3268 // Otherwise, we have to insert explicit sign or zero extends. Note
3269 // that we could insert sign extends for ALL conditions, but zero extend
3270 // is cheaper on many machines (an AND instead of two shifts), so prefer
3271 // it.
3272 switch (cast<CondCodeSDNode>(CC)->get()) {
3273 default: assert(0 && "Unknown integer comparison!");
3274 case ISD::SETEQ:
3275 case ISD::SETNE:
3276 case ISD::SETUGE:
3277 case ISD::SETUGT:
3278 case ISD::SETULE:
3279 case ISD::SETULT:
3280 // ALL of these operations will work if we either sign or zero extend
3281 // the operands (including the unsigned comparisons!). Zero extend is
3282 // usually a simpler/cheaper operation, so prefer it.
3283 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3284 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3285 break;
3286 case ISD::SETGE:
3287 case ISD::SETGT:
3288 case ISD::SETLT:
3289 case ISD::SETLE:
3290 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3291 DAG.getValueType(VT));
3292 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3293 DAG.getValueType(VT));
3294 break;
3295 }
3296 }
3297 break;
3298 case Expand:
3299 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3300 ExpandOp(LHS, LHSLo, LHSHi);
3301 ExpandOp(RHS, RHSLo, RHSHi);
3302 switch (cast<CondCodeSDNode>(CC)->get()) {
3303 case ISD::SETEQ:
3304 case ISD::SETNE:
3305 if (RHSLo == RHSHi)
3306 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3307 if (RHSCST->isAllOnesValue()) {
3308 // Comparison to -1.
3309 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3310 Tmp2 = RHSLo;
3311 break;
3312 }
3313
3314 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3315 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3316 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3317 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3318 break;
3319 default:
3320 // If this is a comparison of the sign bit, just look at the top part.
3321 // X > -1, x < 0
3322 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3323 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3324 CST->getValue() == 0) || // X < 0
3325 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3326 CST->isAllOnesValue())) { // X > -1
3327 Tmp1 = LHSHi;
3328 Tmp2 = RHSHi;
3329 break;
3330 }
3331
3332 // FIXME: This generated code sucks.
3333 ISD::CondCode LowCC;
3334 switch (cast<CondCodeSDNode>(CC)->get()) {
3335 default: assert(0 && "Unknown integer setcc!");
3336 case ISD::SETLT:
3337 case ISD::SETULT: LowCC = ISD::SETULT; break;
3338 case ISD::SETGT:
3339 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3340 case ISD::SETLE:
3341 case ISD::SETULE: LowCC = ISD::SETULE; break;
3342 case ISD::SETGE:
3343 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3344 }
3345
3346 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3347 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3348 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3349
3350 // NOTE: on targets without efficient SELECT of bools, we can always use
3351 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3352 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3353 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3354 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3355 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3356 Result, Tmp1, Tmp2));
3357 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003358 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003359 }
3360 }
3361 LHS = Tmp1;
3362 RHS = Tmp2;
3363}
3364
Chris Lattner36e663d2005-12-23 00:16:34 +00003365/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003366/// The resultant code need not be legal. Note that SrcOp is the input operand
3367/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003368SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3369 SDOperand SrcOp) {
3370 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003371 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003372
3373 // Emit a store to the stack slot.
3374 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003375 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003376 // Result is a load from the stack slot.
3377 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3378}
3379
Chris Lattner6be79822006-04-04 17:23:26 +00003380SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3381 // Create a vector sized/aligned stack slot, store the value to element #0,
3382 // then load the whole vector back out.
3383 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
3384 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3385 Node->getOperand(0), StackPtr,
3386 DAG.getSrcValue(NULL));
3387 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL));
3388}
3389
3390
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003391/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3392/// support the operation, but do support the resultant packed vector type.
3393SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3394
3395 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003396 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003397 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003398 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003399 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003400 std::map<SDOperand, std::vector<unsigned> > Values;
3401 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003402 bool isConstant = true;
3403 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3404 SplatValue.getOpcode() != ISD::UNDEF)
3405 isConstant = false;
3406
Evan Cheng1d2e9952006-03-24 01:17:21 +00003407 for (unsigned i = 1; i < NumElems; ++i) {
3408 SDOperand V = Node->getOperand(i);
3409 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3410 if (I != Values.end())
3411 I->second.push_back(i);
3412 else
3413 Values[V].push_back(i);
3414 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003415 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003416 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003417 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003418
3419 // If this isn't a constant element or an undef, we can't use a constant
3420 // pool load.
3421 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3422 V.getOpcode() != ISD::UNDEF)
3423 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003424 }
3425
3426 if (isOnlyLowElement) {
3427 // If the low element is an undef too, then this whole things is an undef.
3428 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3429 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3430 // Otherwise, turn this into a scalar_to_vector node.
3431 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3432 Node->getOperand(0));
3433 }
3434
Chris Lattner77e271c2006-03-24 07:29:17 +00003435 // If all elements are constants, create a load from the constant pool.
3436 if (isConstant) {
3437 MVT::ValueType VT = Node->getValueType(0);
3438 const Type *OpNTy =
3439 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3440 std::vector<Constant*> CV;
3441 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3442 if (ConstantFPSDNode *V =
3443 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3444 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3445 } else if (ConstantSDNode *V =
3446 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3447 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3448 } else {
3449 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3450 CV.push_back(UndefValue::get(OpNTy));
3451 }
3452 }
3453 Constant *CP = ConstantPacked::get(CV);
3454 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3455 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3456 DAG.getSrcValue(NULL));
3457 }
3458
Chris Lattner21e68c82006-03-20 01:52:29 +00003459 if (SplatValue.Val) { // Splat of one value?
3460 // Build the shuffle constant vector: <0, 0, 0, 0>
3461 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003462 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003463 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003464 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner21e68c82006-03-20 01:52:29 +00003465 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3466
3467 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003468 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003469 // Get the splatted value into the low element of a vector register.
3470 SDOperand LowValVec =
3471 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3472
3473 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3474 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3475 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3476 SplatMask);
3477 }
3478 }
3479
Evan Cheng1d2e9952006-03-24 01:17:21 +00003480 // If there are only two unique elements, we may be able to turn this into a
3481 // vector shuffle.
3482 if (Values.size() == 2) {
3483 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3484 MVT::ValueType MaskVT =
3485 MVT::getIntVectorWithNumElements(NumElems);
3486 std::vector<SDOperand> MaskVec(NumElems);
3487 unsigned i = 0;
3488 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3489 E = Values.end(); I != E; ++I) {
3490 for (std::vector<unsigned>::iterator II = I->second.begin(),
3491 EE = I->second.end(); II != EE; ++II)
3492 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3493 i += NumElems;
3494 }
3495 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3496
3497 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003498 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3499 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Evan Cheng1d2e9952006-03-24 01:17:21 +00003500 std::vector<SDOperand> Ops;
3501 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3502 E = Values.end(); I != E; ++I) {
3503 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3504 I->first);
3505 Ops.push_back(Op);
3506 }
3507 Ops.push_back(ShuffleMask);
3508
3509 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3510 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3511 }
3512 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003513
3514 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3515 // aligned object on the stack, store each element into it, then load
3516 // the result as a vector.
3517 MVT::ValueType VT = Node->getValueType(0);
3518 // Create the stack frame object.
3519 SDOperand FIPtr = CreateStackTemporary(VT);
3520
3521 // Emit a store of each element to the stack slot.
3522 std::vector<SDOperand> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003523 unsigned TypeByteSize =
3524 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3525 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3526 // Store (in the right endianness) the elements to memory.
3527 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3528 // Ignore undef elements.
3529 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3530
Chris Lattner8fa445a2006-03-22 01:46:54 +00003531 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003532
3533 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3534 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3535
3536 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3537 Node->getOperand(i), Idx,
3538 DAG.getSrcValue(NULL)));
3539 }
3540
3541 SDOperand StoreChain;
3542 if (!Stores.empty()) // Not all undef elements?
3543 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3544 else
3545 StoreChain = DAG.getEntryNode();
3546
3547 // Result is a load from the stack slot.
3548 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3549}
3550
3551/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3552/// specified value type.
3553SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3554 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3555 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3556 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3557 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3558}
3559
Chris Lattner4157c412005-04-02 04:00:59 +00003560void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3561 SDOperand Op, SDOperand Amt,
3562 SDOperand &Lo, SDOperand &Hi) {
3563 // Expand the subcomponents.
3564 SDOperand LHSL, LHSH;
3565 ExpandOp(Op, LHSL, LHSH);
3566
3567 std::vector<SDOperand> Ops;
3568 Ops.push_back(LHSL);
3569 Ops.push_back(LHSH);
3570 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003571 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003572 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003573 Hi = Lo.getValue(1);
3574}
3575
3576
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003577/// ExpandShift - Try to find a clever way to expand this shift operation out to
3578/// smaller elements. If we can't find a way that is more efficient than a
3579/// libcall on this target, return false. Otherwise, return true with the
3580/// low-parts expanded into Lo and Hi.
3581bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3582 SDOperand &Lo, SDOperand &Hi) {
3583 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3584 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003585
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003586 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003587 SDOperand ShAmt = LegalizeOp(Amt);
3588 MVT::ValueType ShTy = ShAmt.getValueType();
3589 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3590 unsigned NVTBits = MVT::getSizeInBits(NVT);
3591
3592 // Handle the case when Amt is an immediate. Other cases are currently broken
3593 // and are disabled.
3594 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3595 unsigned Cst = CN->getValue();
3596 // Expand the incoming operand to be shifted, so that we have its parts
3597 SDOperand InL, InH;
3598 ExpandOp(Op, InL, InH);
3599 switch(Opc) {
3600 case ISD::SHL:
3601 if (Cst > VTBits) {
3602 Lo = DAG.getConstant(0, NVT);
3603 Hi = DAG.getConstant(0, NVT);
3604 } else if (Cst > NVTBits) {
3605 Lo = DAG.getConstant(0, NVT);
3606 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003607 } else if (Cst == NVTBits) {
3608 Lo = DAG.getConstant(0, NVT);
3609 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003610 } else {
3611 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3612 Hi = DAG.getNode(ISD::OR, NVT,
3613 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3614 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3615 }
3616 return true;
3617 case ISD::SRL:
3618 if (Cst > VTBits) {
3619 Lo = DAG.getConstant(0, NVT);
3620 Hi = DAG.getConstant(0, NVT);
3621 } else if (Cst > NVTBits) {
3622 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3623 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003624 } else if (Cst == NVTBits) {
3625 Lo = InH;
3626 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003627 } else {
3628 Lo = DAG.getNode(ISD::OR, NVT,
3629 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3630 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3631 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3632 }
3633 return true;
3634 case ISD::SRA:
3635 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003636 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003637 DAG.getConstant(NVTBits-1, ShTy));
3638 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003639 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003640 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003641 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003642 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003643 } else if (Cst == NVTBits) {
3644 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003645 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003646 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003647 } else {
3648 Lo = DAG.getNode(ISD::OR, NVT,
3649 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3650 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3651 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3652 }
3653 return true;
3654 }
3655 }
Nate Begemanb0674922005-04-06 21:13:14 +00003656 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003657}
Chris Lattneraac464e2005-01-21 06:05:23 +00003658
Chris Lattner4add7e32005-01-23 04:42:50 +00003659
Chris Lattneraac464e2005-01-21 06:05:23 +00003660// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3661// does not fit into a register, return the lo part and set the hi part to the
3662// by-reg argument. If it does fit into a single register, return the result
3663// and leave the Hi part unset.
3664SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3665 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003666 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3667 // The input chain to this libcall is the entry node of the function.
3668 // Legalizing the call will automatically add the previous call to the
3669 // dependence.
3670 SDOperand InChain = DAG.getEntryNode();
3671
Chris Lattneraac464e2005-01-21 06:05:23 +00003672 TargetLowering::ArgListTy Args;
3673 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3674 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3675 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3676 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3677 }
3678 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003679
Chris Lattner06bbeb62005-05-11 19:02:11 +00003680 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003681 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003682 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003683 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3684 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003685
Chris Lattner462505f2006-02-13 09:18:02 +00003686 // Legalize the call sequence, starting with the chain. This will advance
3687 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3688 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3689 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003690 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003691 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003692 default: assert(0 && "Unknown thing");
3693 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003694 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003695 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003696 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003697 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003698 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003699 }
Chris Lattner63022662005-09-02 20:26:58 +00003700 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003701}
3702
Chris Lattner4add7e32005-01-23 04:42:50 +00003703
Chris Lattneraac464e2005-01-21 06:05:23 +00003704/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3705/// destination type is legal.
3706SDOperand SelectionDAGLegalize::
3707ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003708 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003709 assert(getTypeAction(Source.getValueType()) == Expand &&
3710 "This is not an expansion!");
3711 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3712
Chris Lattner06bbeb62005-05-11 19:02:11 +00003713 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003714 assert(Source.getValueType() == MVT::i64 &&
3715 "This only works for 64-bit -> FP");
3716 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3717 // incoming integer is set. To handle this, we dynamically test to see if
3718 // it is set, and, if so, add a fudge factor.
3719 SDOperand Lo, Hi;
3720 ExpandOp(Source, Lo, Hi);
3721
Chris Lattner2a4f7312005-05-13 04:45:13 +00003722 // If this is unsigned, and not supported, first perform the conversion to
3723 // signed, then adjust the result if the sign bit is set.
3724 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3725 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3726
Chris Lattnerd47675e2005-08-09 20:20:18 +00003727 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3728 DAG.getConstant(0, Hi.getValueType()),
3729 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003730 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3731 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3732 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003733 uint64_t FF = 0x5f800000ULL;
3734 if (TLI.isLittleEndian()) FF <<= 32;
3735 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003736
Chris Lattnerc30405e2005-08-26 17:15:30 +00003737 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003738 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3739 SDOperand FudgeInReg;
3740 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003741 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3742 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003743 else {
3744 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003745 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3746 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003747 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003748 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003749 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003750
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003751 // Check to see if the target has a custom way to lower this. If so, use it.
3752 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3753 default: assert(0 && "This action not implemented for this operation!");
3754 case TargetLowering::Legal:
3755 case TargetLowering::Expand:
3756 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003757 case TargetLowering::Custom: {
3758 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3759 Source), DAG);
3760 if (NV.Val)
3761 return LegalizeOp(NV);
3762 break; // The target decided this was legal after all
3763 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003764 }
3765
Chris Lattner153587e2005-05-12 07:00:44 +00003766 // Expand the source, then glue it back together for the call. We must expand
3767 // the source in case it is shared (this pass of legalize must traverse it).
3768 SDOperand SrcLo, SrcHi;
3769 ExpandOp(Source, SrcLo, SrcHi);
3770 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3771
Chris Lattner06bbeb62005-05-11 19:02:11 +00003772 const char *FnName = 0;
3773 if (DestTy == MVT::f32)
3774 FnName = "__floatdisf";
3775 else {
3776 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3777 FnName = "__floatdidf";
3778 }
Chris Lattner462505f2006-02-13 09:18:02 +00003779
3780 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3781 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003782 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003783}
Misha Brukman835702a2005-04-21 22:36:52 +00003784
Chris Lattner689bdcc2006-01-28 08:25:58 +00003785/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3786/// INT_TO_FP operation of the specified operand when the target requests that
3787/// we expand it. At this point, we know that the result and operand types are
3788/// legal for the target.
3789SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3790 SDOperand Op0,
3791 MVT::ValueType DestVT) {
3792 if (Op0.getValueType() == MVT::i32) {
3793 // simple 32-bit [signed|unsigned] integer to float/double expansion
3794
3795 // get the stack frame index of a 8 byte buffer
3796 MachineFunction &MF = DAG.getMachineFunction();
3797 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3798 // get address of 8 byte buffer
3799 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3800 // word offset constant for Hi/Lo address computation
3801 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3802 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00003803 SDOperand Hi = StackSlot;
3804 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3805 if (TLI.isLittleEndian())
3806 std::swap(Hi, Lo);
3807
Chris Lattner689bdcc2006-01-28 08:25:58 +00003808 // if signed map to unsigned space
3809 SDOperand Op0Mapped;
3810 if (isSigned) {
3811 // constant used to invert sign bit (signed to unsigned mapping)
3812 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3813 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3814 } else {
3815 Op0Mapped = Op0;
3816 }
3817 // store the lo of the constructed double - based on integer input
3818 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3819 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3820 // initial hi portion of constructed double
3821 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3822 // store the hi of the constructed double - biased exponent
3823 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3824 InitialHi, Hi, DAG.getSrcValue(NULL));
3825 // load the constructed double
3826 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3827 DAG.getSrcValue(NULL));
3828 // FP constant to bias correct the final result
3829 SDOperand Bias = DAG.getConstantFP(isSigned ?
3830 BitsToDouble(0x4330000080000000ULL)
3831 : BitsToDouble(0x4330000000000000ULL),
3832 MVT::f64);
3833 // subtract the bias
3834 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3835 // final result
3836 SDOperand Result;
3837 // handle final rounding
3838 if (DestVT == MVT::f64) {
3839 // do nothing
3840 Result = Sub;
3841 } else {
3842 // if f32 then cast to f32
3843 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3844 }
3845 return Result;
3846 }
3847 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3848 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3849
3850 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3851 DAG.getConstant(0, Op0.getValueType()),
3852 ISD::SETLT);
3853 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3854 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3855 SignSet, Four, Zero);
3856
3857 // If the sign bit of the integer is set, the large number will be treated
3858 // as a negative number. To counteract this, the dynamic code adds an
3859 // offset depending on the data type.
3860 uint64_t FF;
3861 switch (Op0.getValueType()) {
3862 default: assert(0 && "Unsupported integer type!");
3863 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3864 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3865 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3866 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3867 }
3868 if (TLI.isLittleEndian()) FF <<= 32;
3869 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3870
3871 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3872 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3873 SDOperand FudgeInReg;
3874 if (DestVT == MVT::f32)
3875 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3876 DAG.getSrcValue(NULL));
3877 else {
3878 assert(DestVT == MVT::f64 && "Unexpected conversion");
3879 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3880 DAG.getEntryNode(), CPIdx,
3881 DAG.getSrcValue(NULL), MVT::f32));
3882 }
3883
3884 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3885}
3886
3887/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3888/// *INT_TO_FP operation of the specified operand when the target requests that
3889/// we promote it. At this point, we know that the result and operand types are
3890/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3891/// operation that takes a larger input.
3892SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3893 MVT::ValueType DestVT,
3894 bool isSigned) {
3895 // First step, figure out the appropriate *INT_TO_FP operation to use.
3896 MVT::ValueType NewInTy = LegalOp.getValueType();
3897
3898 unsigned OpToUse = 0;
3899
3900 // Scan for the appropriate larger type to use.
3901 while (1) {
3902 NewInTy = (MVT::ValueType)(NewInTy+1);
3903 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3904
3905 // If the target supports SINT_TO_FP of this type, use it.
3906 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3907 default: break;
3908 case TargetLowering::Legal:
3909 if (!TLI.isTypeLegal(NewInTy))
3910 break; // Can't use this datatype.
3911 // FALL THROUGH.
3912 case TargetLowering::Custom:
3913 OpToUse = ISD::SINT_TO_FP;
3914 break;
3915 }
3916 if (OpToUse) break;
3917 if (isSigned) continue;
3918
3919 // If the target supports UINT_TO_FP of this type, use it.
3920 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3921 default: break;
3922 case TargetLowering::Legal:
3923 if (!TLI.isTypeLegal(NewInTy))
3924 break; // Can't use this datatype.
3925 // FALL THROUGH.
3926 case TargetLowering::Custom:
3927 OpToUse = ISD::UINT_TO_FP;
3928 break;
3929 }
3930 if (OpToUse) break;
3931
3932 // Otherwise, try a larger type.
3933 }
3934
3935 // Okay, we found the operation and type to use. Zero extend our input to the
3936 // desired type then run the operation on it.
3937 return DAG.getNode(OpToUse, DestVT,
3938 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3939 NewInTy, LegalOp));
3940}
3941
3942/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3943/// FP_TO_*INT operation of the specified operand when the target requests that
3944/// we promote it. At this point, we know that the result and operand types are
3945/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3946/// operation that returns a larger result.
3947SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3948 MVT::ValueType DestVT,
3949 bool isSigned) {
3950 // First step, figure out the appropriate FP_TO*INT operation to use.
3951 MVT::ValueType NewOutTy = DestVT;
3952
3953 unsigned OpToUse = 0;
3954
3955 // Scan for the appropriate larger type to use.
3956 while (1) {
3957 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3958 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3959
3960 // If the target supports FP_TO_SINT returning this type, use it.
3961 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3962 default: break;
3963 case TargetLowering::Legal:
3964 if (!TLI.isTypeLegal(NewOutTy))
3965 break; // Can't use this datatype.
3966 // FALL THROUGH.
3967 case TargetLowering::Custom:
3968 OpToUse = ISD::FP_TO_SINT;
3969 break;
3970 }
3971 if (OpToUse) break;
3972
3973 // If the target supports FP_TO_UINT of this type, use it.
3974 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3975 default: break;
3976 case TargetLowering::Legal:
3977 if (!TLI.isTypeLegal(NewOutTy))
3978 break; // Can't use this datatype.
3979 // FALL THROUGH.
3980 case TargetLowering::Custom:
3981 OpToUse = ISD::FP_TO_UINT;
3982 break;
3983 }
3984 if (OpToUse) break;
3985
3986 // Otherwise, try a larger type.
3987 }
3988
3989 // Okay, we found the operation and type to use. Truncate the result of the
3990 // extended FP_TO_*INT operation to the desired size.
3991 return DAG.getNode(ISD::TRUNCATE, DestVT,
3992 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3993}
3994
3995/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3996///
3997SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3998 MVT::ValueType VT = Op.getValueType();
3999 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4000 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4001 switch (VT) {
4002 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4003 case MVT::i16:
4004 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4005 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4006 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4007 case MVT::i32:
4008 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4009 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4010 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4011 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4012 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4013 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4014 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4015 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4016 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4017 case MVT::i64:
4018 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4019 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4020 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4021 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4022 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4023 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4024 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4025 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4026 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4027 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4028 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4029 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4030 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4031 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4032 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4033 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4034 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4035 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4036 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4037 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4038 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4039 }
4040}
4041
4042/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4043///
4044SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4045 switch (Opc) {
4046 default: assert(0 && "Cannot expand this yet!");
4047 case ISD::CTPOP: {
4048 static const uint64_t mask[6] = {
4049 0x5555555555555555ULL, 0x3333333333333333ULL,
4050 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4051 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4052 };
4053 MVT::ValueType VT = Op.getValueType();
4054 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4055 unsigned len = getSizeInBits(VT);
4056 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4057 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4058 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4059 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4060 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4061 DAG.getNode(ISD::AND, VT,
4062 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4063 }
4064 return Op;
4065 }
4066 case ISD::CTLZ: {
4067 // for now, we do this:
4068 // x = x | (x >> 1);
4069 // x = x | (x >> 2);
4070 // ...
4071 // x = x | (x >>16);
4072 // x = x | (x >>32); // for 64-bit input
4073 // return popcount(~x);
4074 //
4075 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4076 MVT::ValueType VT = Op.getValueType();
4077 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4078 unsigned len = getSizeInBits(VT);
4079 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4080 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4081 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4082 }
4083 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4084 return DAG.getNode(ISD::CTPOP, VT, Op);
4085 }
4086 case ISD::CTTZ: {
4087 // for now, we use: { return popcount(~x & (x - 1)); }
4088 // unless the target has ctlz but not ctpop, in which case we use:
4089 // { return 32 - nlz(~x & (x-1)); }
4090 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4091 MVT::ValueType VT = Op.getValueType();
4092 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4093 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4094 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4095 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4096 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4097 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4098 TLI.isOperationLegal(ISD::CTLZ, VT))
4099 return DAG.getNode(ISD::SUB, VT,
4100 DAG.getConstant(getSizeInBits(VT), VT),
4101 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4102 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4103 }
4104 }
4105}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004106
Chris Lattnerdc750592005-01-07 07:47:09 +00004107/// ExpandOp - Expand the specified SDOperand into its two component pieces
4108/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4109/// LegalizeNodes map is filled in for any results that are not expanded, the
4110/// ExpandedNodes map is filled in for any results that are expanded, and the
4111/// Lo/Hi values are returned.
4112void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4113 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004114 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004115 SDNode *Node = Op.Val;
4116 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00004117 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
4118 "Cannot expand FP values!");
4119 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004120 "Cannot expand to FP value or to larger int value!");
4121
Chris Lattner1a570f12005-09-02 20:32:45 +00004122 // See if we already expanded it.
4123 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4124 = ExpandedNodes.find(Op);
4125 if (I != ExpandedNodes.end()) {
4126 Lo = I->second.first;
4127 Hi = I->second.second;
4128 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004129 }
4130
Chris Lattnerdc750592005-01-07 07:47:09 +00004131 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004132 case ISD::CopyFromReg:
4133 assert(0 && "CopyFromReg must be legal!");
4134 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00004135 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
4136 assert(0 && "Do not know how to expand this operator!");
4137 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004138 case ISD::UNDEF:
4139 Lo = DAG.getNode(ISD::UNDEF, NVT);
4140 Hi = DAG.getNode(ISD::UNDEF, NVT);
4141 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004142 case ISD::Constant: {
4143 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4144 Lo = DAG.getConstant(Cst, NVT);
4145 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4146 break;
4147 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004148 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004149 // Return the operands.
4150 Lo = Node->getOperand(0);
4151 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004152 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004153
4154 case ISD::SIGN_EXTEND_INREG:
4155 ExpandOp(Node->getOperand(0), Lo, Hi);
4156 // Sign extend the lo-part.
4157 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4158 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4159 TLI.getShiftAmountTy()));
4160 // sext_inreg the low part if needed.
4161 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4162 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004163
Nate Begeman2fba8a32006-01-14 03:14:10 +00004164 case ISD::BSWAP: {
4165 ExpandOp(Node->getOperand(0), Lo, Hi);
4166 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4167 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4168 Lo = TempLo;
4169 break;
4170 }
4171
Chris Lattner55e9cde2005-05-11 04:51:16 +00004172 case ISD::CTPOP:
4173 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004174 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4175 DAG.getNode(ISD::CTPOP, NVT, Lo),
4176 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004177 Hi = DAG.getConstant(0, NVT);
4178 break;
4179
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004180 case ISD::CTLZ: {
4181 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004182 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004183 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4184 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004185 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4186 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004187 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4188 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4189
4190 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4191 Hi = DAG.getConstant(0, NVT);
4192 break;
4193 }
4194
4195 case ISD::CTTZ: {
4196 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004197 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004198 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4199 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004200 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4201 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004202 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4203 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4204
4205 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4206 Hi = DAG.getConstant(0, NVT);
4207 break;
4208 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004209
Nate Begemane74795c2006-01-25 18:21:52 +00004210 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004211 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4212 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004213 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4214 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4215
4216 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004217 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004218 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4219 if (!TLI.isLittleEndian())
4220 std::swap(Lo, Hi);
4221 break;
4222 }
4223
Chris Lattnerdc750592005-01-07 07:47:09 +00004224 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004225 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4226 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00004227 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00004228
4229 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00004230 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00004231 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4232 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004233 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00004234 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00004235
4236 // Build a factor node to remember that this load is independent of the
4237 // other one.
4238 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4239 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00004240
Chris Lattnerdc750592005-01-07 07:47:09 +00004241 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004242 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00004243 if (!TLI.isLittleEndian())
4244 std::swap(Lo, Hi);
4245 break;
4246 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004247 case ISD::AND:
4248 case ISD::OR:
4249 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4250 SDOperand LL, LH, RL, RH;
4251 ExpandOp(Node->getOperand(0), LL, LH);
4252 ExpandOp(Node->getOperand(1), RL, RH);
4253 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4254 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4255 break;
4256 }
4257 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004258 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004259 ExpandOp(Node->getOperand(1), LL, LH);
4260 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004261 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4262 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004263 break;
4264 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004265 case ISD::SELECT_CC: {
4266 SDOperand TL, TH, FL, FH;
4267 ExpandOp(Node->getOperand(2), TL, TH);
4268 ExpandOp(Node->getOperand(3), FL, FH);
4269 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4270 Node->getOperand(1), TL, FL, Node->getOperand(4));
4271 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4272 Node->getOperand(1), TH, FH, Node->getOperand(4));
4273 break;
4274 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00004275 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004276 SDOperand Chain = Node->getOperand(0);
4277 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004278 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4279
4280 if (EVT == NVT)
4281 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4282 else
4283 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4284 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004285
4286 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004287 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004288
Nate Begemanc3a89c52005-10-13 17:15:37 +00004289 // The high part is obtained by SRA'ing all but one of the bits of the lo
4290 // part.
4291 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4292 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4293 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00004294 break;
4295 }
4296 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004297 SDOperand Chain = Node->getOperand(0);
4298 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004299 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4300
4301 if (EVT == NVT)
4302 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4303 else
4304 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4305 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004306
4307 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004308 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004309
Nate Begemanc3a89c52005-10-13 17:15:37 +00004310 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004311 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004312 break;
4313 }
4314 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004315 SDOperand Chain = Node->getOperand(0);
4316 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00004317 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4318
4319 if (EVT == NVT)
4320 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4321 else
4322 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4323 EVT);
4324
4325 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004326 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004327
4328 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00004329 Hi = DAG.getNode(ISD::UNDEF, NVT);
4330 break;
4331 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004332 case ISD::ANY_EXTEND:
4333 // The low part is any extension of the input (which degenerates to a copy).
4334 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4335 // The high part is undefined.
4336 Hi = DAG.getNode(ISD::UNDEF, NVT);
4337 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004338 case ISD::SIGN_EXTEND: {
4339 // The low part is just a sign extension of the input (which degenerates to
4340 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004341 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004342
Chris Lattnerdc750592005-01-07 07:47:09 +00004343 // The high part is obtained by SRA'ing all but one of the bits of the lo
4344 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004345 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004346 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4347 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004348 break;
4349 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004350 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004351 // The low part is just a zero extension of the input (which degenerates to
4352 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004353 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004354
Chris Lattnerdc750592005-01-07 07:47:09 +00004355 // The high part is just a zero.
4356 Hi = DAG.getConstant(0, NVT);
4357 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004358
4359 case ISD::BIT_CONVERT: {
4360 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4361 Node->getOperand(0));
4362 ExpandOp(Tmp, Lo, Hi);
4363 break;
4364 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004365
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004366 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004367 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4368 TargetLowering::Custom &&
4369 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004370 Lo = TLI.LowerOperation(Op, DAG);
4371 assert(Lo.Val && "Node must be custom expanded!");
4372 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004373 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004374 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004375 break;
4376
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004377 // These operators cannot be expanded directly, emit them as calls to
4378 // library functions.
4379 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004380 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004381 SDOperand Op;
4382 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4383 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004384 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4385 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004386 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004387
Chris Lattner941d84a2005-07-30 01:40:57 +00004388 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4389
Chris Lattnerfe68d752005-07-29 00:33:32 +00004390 // Now that the custom expander is done, expand the result, which is still
4391 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004392 if (Op.Val) {
4393 ExpandOp(Op, Lo, Hi);
4394 break;
4395 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004396 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004397
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004398 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004399 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004400 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004401 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004402 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004403
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004404 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004405 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004406 SDOperand Op;
4407 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4408 case Expand: assert(0 && "cannot expand FP!");
4409 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4410 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4411 }
4412
4413 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4414
4415 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004416 if (Op.Val) {
4417 ExpandOp(Op, Lo, Hi);
4418 break;
4419 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004420 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004421
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004422 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004423 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004424 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004425 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004426 break;
4427
Evan Cheng870e4f82006-01-09 18:31:59 +00004428 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004429 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004430 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004431 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004432 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004433 Op = TLI.LowerOperation(Op, DAG);
4434 if (Op.Val) {
4435 // Now that the custom expander is done, expand the result, which is
4436 // still VT.
4437 ExpandOp(Op, Lo, Hi);
4438 break;
4439 }
4440 }
4441
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004442 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004443 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004444 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004445
4446 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004447 TargetLowering::LegalizeAction Action =
4448 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4449 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4450 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004451 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004452 break;
4453 }
4454
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004455 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004456 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004457 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004458 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004459
Evan Cheng870e4f82006-01-09 18:31:59 +00004460 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004461 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004462 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004463 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004464 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004465 Op = TLI.LowerOperation(Op, DAG);
4466 if (Op.Val) {
4467 // Now that the custom expander is done, expand the result, which is
4468 // still VT.
4469 ExpandOp(Op, Lo, Hi);
4470 break;
4471 }
4472 }
4473
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004474 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004475 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004476 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004477
4478 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004479 TargetLowering::LegalizeAction Action =
4480 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4481 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4482 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004483 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004484 break;
4485 }
4486
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004487 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004488 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004489 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004490 }
4491
4492 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004493 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004494 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004495 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004496 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004497 Op = TLI.LowerOperation(Op, DAG);
4498 if (Op.Val) {
4499 // Now that the custom expander is done, expand the result, which is
4500 // still VT.
4501 ExpandOp(Op, Lo, Hi);
4502 break;
4503 }
4504 }
4505
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004506 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004507 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004508 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004509
4510 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004511 TargetLowering::LegalizeAction Action =
4512 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4513 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4514 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004515 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004516 break;
4517 }
4518
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004519 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004520 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004521 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004522 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004523
Misha Brukman835702a2005-04-21 22:36:52 +00004524 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004525 case ISD::SUB: {
4526 // If the target wants to custom expand this, let them.
4527 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4528 TargetLowering::Custom) {
4529 Op = TLI.LowerOperation(Op, DAG);
4530 if (Op.Val) {
4531 ExpandOp(Op, Lo, Hi);
4532 break;
4533 }
4534 }
4535
4536 // Expand the subcomponents.
4537 SDOperand LHSL, LHSH, RHSL, RHSH;
4538 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4539 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004540 std::vector<MVT::ValueType> VTs;
4541 std::vector<SDOperand> LoOps, HiOps;
4542 VTs.push_back(LHSL.getValueType());
4543 VTs.push_back(MVT::Flag);
4544 LoOps.push_back(LHSL);
4545 LoOps.push_back(RHSL);
4546 HiOps.push_back(LHSH);
4547 HiOps.push_back(RHSH);
4548 if (Node->getOpcode() == ISD::ADD) {
4549 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4550 HiOps.push_back(Lo.getValue(1));
4551 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4552 } else {
4553 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4554 HiOps.push_back(Lo.getValue(1));
4555 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4556 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004557 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004558 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004559 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004560 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004561 SDOperand LL, LH, RL, RH;
4562 ExpandOp(Node->getOperand(0), LL, LH);
4563 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004564 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4565 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4566 // extended the sign bit of the low half through the upper half, and if so
4567 // emit a MULHS instead of the alternate sequence that is valid for any
4568 // i64 x i64 multiply.
4569 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4570 // is RH an extension of the sign bit of RL?
4571 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4572 RH.getOperand(1).getOpcode() == ISD::Constant &&
4573 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4574 // is LH an extension of the sign bit of LL?
4575 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4576 LH.getOperand(1).getOpcode() == ISD::Constant &&
4577 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4578 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4579 } else {
4580 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4581 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4582 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4583 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4584 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4585 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004586 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4587 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004588 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004589 }
4590 break;
4591 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004592 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4593 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4594 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4595 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004596 }
4597
Chris Lattnerac12f682005-12-21 18:02:52 +00004598 // Make sure the resultant values have been legalized themselves, unless this
4599 // is a type that requires multi-step expansion.
4600 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4601 Lo = LegalizeOp(Lo);
4602 Hi = LegalizeOp(Hi);
4603 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004604
4605 // Remember in a map if the values will be reused later.
4606 bool isNew =
4607 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4608 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004609}
4610
Chris Lattner32206f52006-03-18 01:44:44 +00004611/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4612/// two smaller values of MVT::Vector type.
4613void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4614 SDOperand &Hi) {
4615 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4616 SDNode *Node = Op.Val;
4617 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4618 assert(NumElements > 1 && "Cannot split a single element vector!");
4619 unsigned NewNumElts = NumElements/2;
4620 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4621 SDOperand TypeNode = *(Node->op_end()-1);
4622
4623 // See if we already split it.
4624 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4625 = SplitNodes.find(Op);
4626 if (I != SplitNodes.end()) {
4627 Lo = I->second.first;
4628 Hi = I->second.second;
4629 return;
4630 }
4631
4632 switch (Node->getOpcode()) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004633 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004634 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004635 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4636 LoOps.push_back(NewNumEltsNode);
4637 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004638 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004639
4640 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4641 HiOps.push_back(NewNumEltsNode);
4642 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004643 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004644 break;
4645 }
4646 case ISD::VADD:
4647 case ISD::VSUB:
4648 case ISD::VMUL:
4649 case ISD::VSDIV:
4650 case ISD::VUDIV:
4651 case ISD::VAND:
4652 case ISD::VOR:
4653 case ISD::VXOR: {
4654 SDOperand LL, LH, RL, RH;
4655 SplitVectorOp(Node->getOperand(0), LL, LH);
4656 SplitVectorOp(Node->getOperand(1), RL, RH);
4657
4658 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4659 NewNumEltsNode, TypeNode);
4660 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4661 NewNumEltsNode, TypeNode);
4662 break;
4663 }
4664 case ISD::VLOAD: {
4665 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4666 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4667 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4668
4669 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4670 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4671 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4672 getIntPtrConstant(IncrementSize));
4673 // FIXME: This creates a bogus srcvalue!
4674 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4675
4676 // Build a factor node to remember that this load is independent of the
4677 // other one.
4678 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4679 Hi.getValue(1));
4680
4681 // Remember that we legalized the chain.
4682 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00004683 break;
4684 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004685 case ISD::VBIT_CONVERT: {
4686 // We know the result is a vector. The input may be either a vector or a
4687 // scalar value.
4688 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4689 // Lower to a store/load. FIXME: this could be improved probably.
4690 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4691
4692 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4693 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4694 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4695 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4696 SplitVectorOp(St, Lo, Hi);
4697 } else {
4698 // If the input is a vector type, we have to either scalarize it, pack it
4699 // or convert it based on whether the input vector type is legal.
4700 SDNode *InVal = Node->getOperand(0).Val;
4701 unsigned NumElems =
4702 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4703 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4704
4705 // If the input is from a single element vector, scalarize the vector,
4706 // then treat like a scalar.
4707 if (NumElems == 1) {
4708 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4709 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4710 Op.getOperand(1), Op.getOperand(2));
4711 SplitVectorOp(Scalar, Lo, Hi);
4712 } else {
4713 // Split the input vector.
4714 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4715
4716 // Convert each of the pieces now.
4717 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4718 NewNumEltsNode, TypeNode);
4719 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4720 NewNumEltsNode, TypeNode);
4721 }
4722 break;
4723 }
4724 }
Chris Lattner32206f52006-03-18 01:44:44 +00004725 }
4726
4727 // Remember in a map if the values will be reused later.
4728 bool isNew =
4729 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4730 assert(isNew && "Value already expanded?!?");
4731}
4732
4733
4734/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4735/// equivalent operation that returns a scalar (e.g. F32) or packed value
4736/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4737/// type for the result.
4738SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4739 MVT::ValueType NewVT) {
4740 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4741 SDNode *Node = Op.Val;
4742
4743 // See if we already packed it.
4744 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4745 if (I != PackedNodes.end()) return I->second;
4746
4747 SDOperand Result;
4748 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004749 default:
4750 Node->dump(); std::cerr << "\n";
4751 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004752 case ISD::VADD:
4753 case ISD::VSUB:
4754 case ISD::VMUL:
4755 case ISD::VSDIV:
4756 case ISD::VUDIV:
4757 case ISD::VAND:
4758 case ISD::VOR:
4759 case ISD::VXOR:
4760 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4761 NewVT,
4762 PackVectorOp(Node->getOperand(0), NewVT),
4763 PackVectorOp(Node->getOperand(1), NewVT));
4764 break;
4765 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004766 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4767 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004768
Chris Lattner93640542006-03-19 00:07:49 +00004769 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004770
4771 // Remember that we legalized the chain.
4772 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4773 break;
4774 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004775 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00004776 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004777 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004778 Result = Node->getOperand(0);
4779 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004780 // Returning a BUILD_VECTOR?
Chris Lattnere1401e32006-04-08 05:34:25 +00004781
4782 // If all elements of the build_vector are undefs, return an undef.
4783 bool AllUndef = true;
4784 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
4785 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
4786 AllUndef = false;
4787 break;
4788 }
4789 if (AllUndef) {
4790 Result = DAG.getNode(ISD::UNDEF, NewVT);
4791 } else {
4792 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
4793 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
4794 }
Chris Lattner32206f52006-03-18 01:44:44 +00004795 }
4796 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004797 case ISD::VINSERT_VECTOR_ELT:
4798 if (!MVT::isVector(NewVT)) {
4799 // Returning a scalar? Must be the inserted element.
4800 Result = Node->getOperand(1);
4801 } else {
4802 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4803 PackVectorOp(Node->getOperand(0), NewVT),
4804 Node->getOperand(1), Node->getOperand(2));
4805 }
4806 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00004807 case ISD::VVECTOR_SHUFFLE:
4808 if (!MVT::isVector(NewVT)) {
4809 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4810 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4811 if (cast<ConstantSDNode>(EltNum)->getValue())
4812 Result = PackVectorOp(Node->getOperand(1), NewVT);
4813 else
4814 Result = PackVectorOp(Node->getOperand(0), NewVT);
4815 } else {
4816 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4817 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4818 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4819 Node->getOperand(2).Val->op_end()-2);
4820 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4821 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4822
4823 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4824 PackVectorOp(Node->getOperand(0), NewVT),
4825 PackVectorOp(Node->getOperand(1), NewVT), BV);
4826 }
4827 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00004828 case ISD::VBIT_CONVERT:
4829 if (Op.getOperand(0).getValueType() != MVT::Vector)
4830 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4831 else {
4832 // If the input is a vector type, we have to either scalarize it, pack it
4833 // or convert it based on whether the input vector type is legal.
4834 SDNode *InVal = Node->getOperand(0).Val;
4835 unsigned NumElems =
4836 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4837 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4838
4839 // Figure out if there is a Packed type corresponding to this Vector
4840 // type. If so, convert to the packed type.
4841 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4842 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4843 // Turn this into a bit convert of the packed input.
4844 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4845 PackVectorOp(Node->getOperand(0), TVT));
4846 break;
4847 } else if (NumElems == 1) {
4848 // Turn this into a bit convert of the scalar input.
4849 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4850 PackVectorOp(Node->getOperand(0), EVT));
4851 break;
4852 } else {
4853 // FIXME: UNIMP!
4854 assert(0 && "Cast from unsupported vector type not implemented yet!");
4855 }
4856 }
Evan Chengcb73b8d2006-04-10 18:54:36 +00004857 break;
Chris Lattner02274a52006-04-08 22:22:57 +00004858 case ISD::VSELECT:
4859 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
4860 PackVectorOp(Op.getOperand(1), NewVT),
4861 PackVectorOp(Op.getOperand(2), NewVT));
4862 break;
Chris Lattner32206f52006-03-18 01:44:44 +00004863 }
4864
4865 if (TLI.isTypeLegal(NewVT))
4866 Result = LegalizeOp(Result);
4867 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4868 assert(isNew && "Value already packed?");
4869 return Result;
4870}
4871
Chris Lattnerdc750592005-01-07 07:47:09 +00004872
4873// SelectionDAG::Legalize - This is the entry point for the file.
4874//
Chris Lattner4add7e32005-01-23 04:42:50 +00004875void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00004876 if (ViewLegalizeDAGs) viewGraph();
4877
Chris Lattnerdc750592005-01-07 07:47:09 +00004878 /// run - This is the main entry point to this class.
4879 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004880 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004881}
4882