blob: 0bb6dd0301ff7b76d840d0c59c7a9591ba9a64d9 [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 Lattner05b4e372005-01-13 17:59:25 +0000829
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000830 case ISD::BUILD_VECTOR:
831 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000832 default: assert(0 && "This action is not supported yet!");
833 case TargetLowering::Custom:
834 Tmp3 = TLI.LowerOperation(Result, DAG);
835 if (Tmp3.Val) {
836 Result = Tmp3;
837 break;
838 }
839 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000840 case TargetLowering::Expand:
841 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000842 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000843 }
Chris Lattner29b23012006-03-19 01:17:20 +0000844 break;
845 case ISD::INSERT_VECTOR_ELT:
846 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
847 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
848 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
849 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
850
851 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
852 Node->getValueType(0))) {
853 default: assert(0 && "This action is not supported yet!");
854 case TargetLowering::Legal:
855 break;
856 case TargetLowering::Custom:
857 Tmp3 = TLI.LowerOperation(Result, DAG);
858 if (Tmp3.Val) {
859 Result = Tmp3;
860 break;
861 }
862 // FALLTHROUGH
863 case TargetLowering::Expand: {
864 // If the target doesn't support this, we have to spill the input vector
865 // to a temporary stack slot, update the element, then reload it. This is
866 // badness. We could also load the value into a vector register (either
867 // with a "move to register" or "extload into register" instruction, then
868 // permute it into place, if the idx is a constant and if the idx is
869 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +0000870 MVT::ValueType VT = Tmp1.getValueType();
871 MVT::ValueType EltVT = Tmp2.getValueType();
872 MVT::ValueType IdxVT = Tmp3.getValueType();
873 MVT::ValueType PtrVT = TLI.getPointerTy();
874 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +0000875 // Store the vector.
876 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
877 Tmp1, StackPtr, DAG.getSrcValue(NULL));
878
879 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +0000880 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
881 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +0000882 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +0000883 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
884 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
885 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +0000886 // Store the scalar value.
887 Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch,
888 Tmp2, StackPtr2, DAG.getSrcValue(NULL));
889 // Load the updated vector.
Evan Cheng78e3d562006-04-08 01:46:37 +0000890 Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL));
Chris Lattner29b23012006-03-19 01:17:20 +0000891 break;
892 }
893 }
894 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000895 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +0000896 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
897 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
898 break;
899 }
900
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000901 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
902 Result = DAG.UpdateNodeOperands(Result, Tmp1);
903 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
904 Node->getValueType(0))) {
905 default: assert(0 && "This action is not supported yet!");
906 case TargetLowering::Legal:
907 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +0000908 case TargetLowering::Custom:
909 Tmp3 = TLI.LowerOperation(Result, DAG);
910 if (Tmp3.Val) {
911 Result = Tmp3;
912 break;
913 }
914 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +0000915 case TargetLowering::Expand:
916 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000917 break;
918 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000919 break;
Chris Lattner21e68c82006-03-20 01:52:29 +0000920 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +0000921 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
922 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
924
925 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +0000926 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
927 default: assert(0 && "Unknown operation action!");
928 case TargetLowering::Legal:
929 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
930 "vector shuffle should not be created if not legal!");
931 break;
932 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +0000933 Tmp3 = TLI.LowerOperation(Result, DAG);
934 if (Tmp3.Val) {
935 Result = Tmp3;
936 break;
937 }
938 // FALLTHROUGH
939 case TargetLowering::Expand: {
940 MVT::ValueType VT = Node->getValueType(0);
941 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
942 MVT::ValueType PtrVT = TLI.getPointerTy();
943 SDOperand Mask = Node->getOperand(2);
944 unsigned NumElems = Mask.getNumOperands();
945 std::vector<SDOperand> Ops;
946 for (unsigned i = 0; i != NumElems; ++i) {
947 SDOperand Arg = Mask.getOperand(i);
948 if (Arg.getOpcode() == ISD::UNDEF) {
949 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
950 } else {
951 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
952 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
953 if (Idx < NumElems)
954 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
955 DAG.getConstant(Idx, PtrVT)));
956 else
957 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
958 DAG.getConstant(Idx - NumElems, PtrVT)));
959 }
960 }
961 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops);
Chris Lattner6be79822006-04-04 17:23:26 +0000962 break;
Evan Cheng9fa89592006-04-05 06:07:11 +0000963 }
Chris Lattner6be79822006-04-04 17:23:26 +0000964 case TargetLowering::Promote: {
965 // Change base type to a different vector type.
966 MVT::ValueType OVT = Node->getValueType(0);
967 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
968
969 // Cast the two input vectors.
970 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
971 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
972
973 // Convert the shuffle mask to the right # elements.
974 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
975 assert(Tmp3.Val && "Shuffle not legal?");
976 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
977 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
978 break;
979 }
Chris Lattner21e68c82006-03-20 01:52:29 +0000980 }
981 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000982
983 case ISD::EXTRACT_VECTOR_ELT:
984 Tmp1 = LegalizeOp(Node->getOperand(0));
985 Tmp2 = LegalizeOp(Node->getOperand(1));
986 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +0000987
988 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
989 Tmp1.getValueType())) {
990 default: assert(0 && "This action is not supported yet!");
991 case TargetLowering::Legal:
992 break;
993 case TargetLowering::Custom:
994 Tmp3 = TLI.LowerOperation(Result, DAG);
995 if (Tmp3.Val) {
996 Result = Tmp3;
997 break;
998 }
999 // FALLTHROUGH
Chris Lattner42a5fca2006-04-02 05:06:04 +00001000 case TargetLowering::Expand:
1001 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner340a6b52006-03-21 21:02:03 +00001002 break;
1003 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001004 break;
1005
Chris Lattner6f423252006-03-31 17:55:51 +00001006 case ISD::VEXTRACT_VECTOR_ELT:
1007 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001008 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001009
Chris Lattner462505f2006-02-13 09:18:02 +00001010 case ISD::CALLSEQ_START: {
1011 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1012
1013 // Recursively Legalize all of the inputs of the call end that do not lead
1014 // to this call start. This ensures that any libcalls that need be inserted
1015 // are inserted *before* the CALLSEQ_START.
1016 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
1017 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node);
1018
1019 // Now that we legalized all of the inputs (which may have inserted
1020 // libcalls) create the new CALLSEQ_START node.
1021 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1022
1023 // Merge in the last call, to ensure that this call start after the last
1024 // call ended.
1025 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1026 Tmp1 = LegalizeOp(Tmp1);
1027
1028 // Do not try to legalize the target-specific arguments (#1+).
1029 if (Tmp1 != Node->getOperand(0)) {
1030 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1031 Ops[0] = Tmp1;
1032 Result = DAG.UpdateNodeOperands(Result, Ops);
1033 }
1034
1035 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001036 AddLegalizedOperand(Op.getValue(0), Result);
1037 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1038 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1039
Chris Lattner462505f2006-02-13 09:18:02 +00001040 // Now that the callseq_start and all of the non-call nodes above this call
1041 // sequence have been legalized, legalize the call itself. During this
1042 // process, no libcalls can/will be inserted, guaranteeing that no calls
1043 // can overlap.
1044 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1045 SDOperand InCallSEQ = LastCALLSEQ_END;
1046 // Note that we are selecting this call!
1047 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1048 IsLegalizingCall = true;
1049
1050 // Legalize the call, starting from the CALLSEQ_END.
1051 LegalizeOp(LastCALLSEQ_END);
1052 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1053 return Result;
1054 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001055 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001056 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1057 // will cause this node to be legalized as well as handling libcalls right.
1058 if (LastCALLSEQ_END.Val != Node) {
1059 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1060 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1061 assert(I != LegalizedNodes.end() &&
1062 "Legalizing the call start should have legalized this node!");
1063 return I->second;
1064 }
1065
1066 // Otherwise, the call start has been legalized and everything is going
1067 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001068 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001069 // Do not try to legalize the target-specific arguments (#1+), except for
1070 // an optional flag input.
1071 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1072 if (Tmp1 != Node->getOperand(0)) {
1073 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1074 Ops[0] = Tmp1;
1075 Result = DAG.UpdateNodeOperands(Result, Ops);
1076 }
1077 } else {
1078 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1079 if (Tmp1 != Node->getOperand(0) ||
1080 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
1081 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1082 Ops[0] = Tmp1;
1083 Ops.back() = Tmp2;
1084 Result = DAG.UpdateNodeOperands(Result, Ops);
1085 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001086 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001087 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001088 // This finishes up call legalization.
1089 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001090
1091 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1092 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1093 if (Node->getNumValues() == 2)
1094 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1095 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001096 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1098 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1099 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001100 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001101
Chris Lattnerd02b0542006-01-28 10:58:55 +00001102 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001103 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001104 switch (TLI.getOperationAction(Node->getOpcode(),
1105 Node->getValueType(0))) {
1106 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001107 case TargetLowering::Expand: {
1108 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1109 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1110 " not tell us which reg is the stack pointer!");
1111 SDOperand Chain = Tmp1.getOperand(0);
1112 SDOperand Size = Tmp2.getOperand(1);
1113 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1114 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1115 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001116 Tmp1 = LegalizeOp(Tmp1);
1117 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001118 break;
1119 }
1120 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001121 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1122 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001123 Tmp1 = LegalizeOp(Tmp3);
1124 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001125 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001126 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001127 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001128 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001129 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001130 // Since this op produce two values, make sure to remember that we
1131 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001132 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1133 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001134 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001135 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001136 case ISD::INLINEASM:
1137 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
1138 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001139 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +00001140 Tmp2 = Tmp3 = SDOperand(0, 0);
1141 else
1142 Tmp3 = LegalizeOp(Tmp2);
1143
1144 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
1145 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
1146 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +00001147 if (Tmp3.Val) Ops.back() = Tmp3;
1148 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +00001149 }
1150
1151 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001152 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001153 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1154 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +00001155 case ISD::BR:
1156 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001157 // Ensure that libcalls are emitted before a branch.
1158 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1159 Tmp1 = LegalizeOp(Tmp1);
1160 LastCALLSEQ_END = DAG.getEntryNode();
1161
Chris Lattnerd02b0542006-01-28 10:58:55 +00001162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001163 break;
1164
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001165 case ISD::BRCOND:
1166 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001167 // Ensure that libcalls are emitted before a return.
1168 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1169 Tmp1 = LegalizeOp(Tmp1);
1170 LastCALLSEQ_END = DAG.getEntryNode();
1171
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001172 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1173 case Expand: assert(0 && "It's impossible to expand bools");
1174 case Legal:
1175 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1176 break;
1177 case Promote:
1178 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
1179 break;
1180 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001181
1182 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001184
1185 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1186 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001187 case TargetLowering::Legal: break;
1188 case TargetLowering::Custom:
1189 Tmp1 = TLI.LowerOperation(Result, DAG);
1190 if (Tmp1.Val) Result = Tmp1;
1191 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001192 case TargetLowering::Expand:
1193 // Expand brcond's setcc into its constituent parts and create a BR_CC
1194 // Node.
1195 if (Tmp2.getOpcode() == ISD::SETCC) {
1196 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1197 Tmp2.getOperand(0), Tmp2.getOperand(1),
1198 Node->getOperand(2));
1199 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001200 // Make sure the condition is either zero or one. It may have been
1201 // promoted from something else.
Chris Lattnere9721b22006-01-31 05:04:52 +00001202 unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType());
1203 if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1))
1204 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner539c3fa2005-08-21 18:03:09 +00001205
Nate Begeman371e4952005-08-16 19:49:35 +00001206 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1207 DAG.getCondCode(ISD::SETNE), Tmp2,
1208 DAG.getConstant(0, Tmp2.getValueType()),
1209 Node->getOperand(2));
1210 }
1211 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001212 }
1213 break;
1214 case ISD::BR_CC:
1215 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001216 // Ensure that libcalls are emitted before a branch.
1217 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1218 Tmp1 = LegalizeOp(Tmp1);
1219 LastCALLSEQ_END = DAG.getEntryNode();
1220
Nate Begeman7e7f4392006-02-01 07:19:44 +00001221 Tmp2 = Node->getOperand(2); // LHS
1222 Tmp3 = Node->getOperand(3); // RHS
1223 Tmp4 = Node->getOperand(1); // CC
1224
1225 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
1226
1227 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1228 // the LHS is a legal SETCC itself. In this case, we need to compare
1229 // the result against zero to select between true and false values.
1230 if (Tmp3.Val == 0) {
1231 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1232 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001233 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001234
1235 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1236 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001237
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001238 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1239 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001240 case TargetLowering::Legal: break;
1241 case TargetLowering::Custom:
1242 Tmp4 = TLI.LowerOperation(Result, DAG);
1243 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001244 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001245 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001246 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001247 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001248 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1249 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001250
Evan Cheng31d15fa2005-12-23 07:29:34 +00001251 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001252 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1253 Tmp2 = Result.getValue(0);
1254 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001255
Evan Cheng31d15fa2005-12-23 07:29:34 +00001256 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1257 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001258 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001259 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001260 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001261 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001262 Tmp2 = LegalizeOp(Tmp1);
1263 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001264 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001265 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001266 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001267 // Since loads produce two values, make sure to remember that we
1268 // legalized both of them.
1269 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
1270 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
1271 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001272 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001273 case ISD::EXTLOAD:
1274 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001275 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001276 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1277 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001278
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001279 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001280 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001281 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +00001282 case TargetLowering::Promote:
1283 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001284 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1285 DAG.getValueType(MVT::i8));
1286 Tmp1 = Result.getValue(0);
1287 Tmp2 = Result.getValue(1);
1288 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001289 case TargetLowering::Custom:
1290 isCustom = true;
1291 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001292 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
1294 Node->getOperand(3));
1295 Tmp1 = Result.getValue(0);
1296 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001297
1298 if (isCustom) {
1299 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
1300 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001301 Tmp1 = LegalizeOp(Tmp3);
1302 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001303 }
1304 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001305 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001306 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +00001307 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001308 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1309 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +00001310 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001311 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1312 Tmp2 = LegalizeOp(Load.getValue(1));
1313 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001314 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001315 assert(Node->getOpcode() != ISD::EXTLOAD &&
1316 "EXTLOAD should always be supported!");
1317 // Turn the unsupported load into an EXTLOAD followed by an explicit
1318 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001319 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1320 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +00001321 SDOperand ValRes;
1322 if (Node->getOpcode() == ISD::SEXTLOAD)
1323 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001324 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +00001325 else
1326 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001327 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1328 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1329 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001330 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001331 // Since loads produce two values, make sure to remember that we legalized
1332 // both of them.
1333 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1334 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1335 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001336 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001337 case ISD::EXTRACT_ELEMENT: {
1338 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1339 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001340 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001341 case Legal:
1342 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1343 // 1 -> Hi
1344 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1345 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1346 TLI.getShiftAmountTy()));
1347 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1348 } else {
1349 // 0 -> Lo
1350 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1351 Node->getOperand(0));
1352 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001353 break;
1354 case Expand:
1355 // Get both the low and high parts.
1356 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1357 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1358 Result = Tmp2; // 1 -> Hi
1359 else
1360 Result = Tmp1; // 0 -> Lo
1361 break;
1362 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001363 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001364 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001365
1366 case ISD::CopyToReg:
1367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001368
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001369 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001370 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001371 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001372 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001373 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001375 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001376 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001377 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001378 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1380 Tmp3);
1381 } else {
1382 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001383 }
1384
1385 // Since this produces two values, make sure to remember that we legalized
1386 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001387 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001388 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001389 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001390 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001391 break;
1392
1393 case ISD::RET:
1394 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001395
1396 // Ensure that libcalls are emitted before a return.
1397 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1398 Tmp1 = LegalizeOp(Tmp1);
1399 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001400 Tmp2 = Node->getOperand(1);
1401
Chris Lattnerdc750592005-01-07 07:47:09 +00001402 switch (Node->getNumOperands()) {
1403 case 2: // ret val
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001404 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001405 case Legal:
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001406 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001407 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001408 case Expand:
1409 if (Tmp2.getValueType() != MVT::Vector) {
1410 SDOperand Lo, Hi;
1411 ExpandOp(Tmp2, Lo, Hi);
1412 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1413 } else {
1414 SDNode *InVal = Tmp2.Val;
1415 unsigned NumElems =
1416 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1417 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1418
1419 // Figure out if there is a Packed type corresponding to this Vector
1420 // type. If so, convert to the packed type.
1421 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1422 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1423 // Turn this into a return of the packed type.
1424 Tmp2 = PackVectorOp(Tmp2, TVT);
1425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1426 } else if (NumElems == 1) {
1427 // Turn this into a return of the scalar type.
1428 Tmp2 = PackVectorOp(Tmp2, EVT);
1429 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1430 // The scalarized value type may not be legal, e.g. it might require
1431 // promotion or expansion. Relegalize the return.
1432 Result = LegalizeOp(Result);
1433 } else {
1434 SDOperand Lo, Hi;
1435 SplitVectorOp(Tmp2, Lo, Hi);
1436 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
1437 Result = LegalizeOp(Result);
1438 }
1439 }
Misha Brukman835702a2005-04-21 22:36:52 +00001440 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001441 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001442 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001443 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1444 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001445 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001446 }
1447 break;
1448 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001449 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001450 break;
1451 default: { // ret <values>
1452 std::vector<SDOperand> NewValues;
1453 NewValues.push_back(Tmp1);
1454 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1455 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1456 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001457 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001458 break;
1459 case Expand: {
1460 SDOperand Lo, Hi;
1461 ExpandOp(Node->getOperand(i), Lo, Hi);
1462 NewValues.push_back(Lo);
1463 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001464 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001465 }
1466 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001467 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001468 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001469
1470 if (NewValues.size() == Node->getNumOperands())
1471 Result = DAG.UpdateNodeOperands(Result, NewValues);
1472 else
1473 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001474 break;
1475 }
1476 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001477
Chris Lattner4d1ea712006-01-29 21:02:23 +00001478 if (Result.getOpcode() == ISD::RET) {
1479 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1480 default: assert(0 && "This action is not supported yet!");
1481 case TargetLowering::Legal: break;
1482 case TargetLowering::Custom:
1483 Tmp1 = TLI.LowerOperation(Result, DAG);
1484 if (Tmp1.Val) Result = Tmp1;
1485 break;
1486 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001487 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001488 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001489 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001490 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1491 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1492
Chris Lattnere69daaf2005-01-08 06:25:56 +00001493 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001494 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattnercad70c32006-03-15 22:19:18 +00001495 // FIXME: move this to the DAG Combiner!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001496 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001497 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001498 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001499 } else {
1500 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001501 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001502 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001503 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1504 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001505 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001506 }
1507
Chris Lattnerdc750592005-01-07 07:47:09 +00001508 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1509 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001510 Tmp3 = LegalizeOp(Node->getOperand(1));
1511 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1512 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001513
Chris Lattnerd02b0542006-01-28 10:58:55 +00001514 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001515 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1516 default: assert(0 && "This action is not supported yet!");
1517 case TargetLowering::Legal: break;
1518 case TargetLowering::Custom:
1519 Tmp1 = TLI.LowerOperation(Result, DAG);
1520 if (Tmp1.Val) Result = Tmp1;
1521 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001522 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001523 break;
1524 }
1525 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001526 // Truncate the value and store the result.
1527 Tmp3 = PromoteOp(Node->getOperand(1));
1528 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001529 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001530 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001531 break;
1532
Chris Lattnerdc750592005-01-07 07:47:09 +00001533 case Expand:
Chris Lattner32206f52006-03-18 01:44:44 +00001534 unsigned IncrementSize = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001535 SDOperand Lo, Hi;
Chris Lattner32206f52006-03-18 01:44:44 +00001536
1537 // If this is a vector type, then we have to calculate the increment as
1538 // the product of the element size in bytes, and the number of elements
1539 // in the high half of the vector.
1540 if (Node->getOperand(1).getValueType() == MVT::Vector) {
1541 SDNode *InVal = Node->getOperand(1).Val;
1542 unsigned NumElems =
1543 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1544 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1545
1546 // Figure out if there is a Packed type corresponding to this Vector
1547 // type. If so, convert to the packed type.
1548 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1549 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1550 // Turn this into a normal store of the packed type.
1551 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1552 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1553 Node->getOperand(3));
1554 break;
1555 } else if (NumElems == 1) {
1556 // Turn this into a normal store of the scalar type.
1557 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1558 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1559 Node->getOperand(3));
Chris Lattner8e1fcab2006-03-31 17:37:22 +00001560 // The scalarized value type may not be legal, e.g. it might require
1561 // promotion or expansion. Relegalize the scalar store.
1562 Result = LegalizeOp(Result);
Chris Lattner32206f52006-03-18 01:44:44 +00001563 break;
1564 } else {
1565 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1566 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1567 }
1568 } else {
1569 ExpandOp(Node->getOperand(1), Lo, Hi);
1570 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001571
Chris Lattner8d90f522006-03-31 18:20:46 +00001572 if (!TLI.isLittleEndian())
1573 std::swap(Lo, Hi);
1574 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001575
Chris Lattner55e9cde2005-05-11 04:51:16 +00001576 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1577 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001578 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1579 getIntPtrConstant(IncrementSize));
1580 assert(isTypeLegal(Tmp2.getValueType()) &&
1581 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001582 // FIXME: This sets the srcvalue of both halves to be the same, which is
1583 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001584 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1585 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001586 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1587 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001588 }
1589 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001590 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001591 case ISD::PCMARKER:
1592 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001593 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001594 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001595 case ISD::STACKSAVE:
1596 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001597 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1598 Tmp1 = Result.getValue(0);
1599 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001600
Chris Lattnerb3266452006-01-13 02:50:02 +00001601 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1602 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001603 case TargetLowering::Legal: break;
1604 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001605 Tmp3 = TLI.LowerOperation(Result, DAG);
1606 if (Tmp3.Val) {
1607 Tmp1 = LegalizeOp(Tmp3);
1608 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001609 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001610 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001611 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001612 // Expand to CopyFromReg if the target set
1613 // StackPointerRegisterToSaveRestore.
1614 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001615 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001616 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001617 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001618 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001619 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1620 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001621 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001622 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001623 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001624
1625 // Since stacksave produce two values, make sure to remember that we
1626 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001627 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1628 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1629 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001630
Chris Lattnerb3266452006-01-13 02:50:02 +00001631 case ISD::STACKRESTORE:
1632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1633 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001634 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001635
1636 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1637 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001638 case TargetLowering::Legal: break;
1639 case TargetLowering::Custom:
1640 Tmp1 = TLI.LowerOperation(Result, DAG);
1641 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001642 break;
1643 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001644 // Expand to CopyToReg if the target set
1645 // StackPointerRegisterToSaveRestore.
1646 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1647 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1648 } else {
1649 Result = Tmp1;
1650 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001651 break;
1652 }
1653 break;
1654
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001655 case ISD::READCYCLECOUNTER:
1656 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001657 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001658
1659 // Since rdcc produce two values, make sure to remember that we legalized
1660 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001661 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001662 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001663 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001664
Evan Cheng31d15fa2005-12-23 07:29:34 +00001665 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001666 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1667 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1668
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001669 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1670 "Cannot handle illegal TRUNCSTORE yet!");
1671 Tmp2 = LegalizeOp(Node->getOperand(1));
1672
1673 // The only promote case we handle is TRUNCSTORE:i1 X into
1674 // -> TRUNCSTORE:i8 (and X, 1)
1675 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1676 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1677 TargetLowering::Promote) {
1678 // Promote the bool to a mask then store.
1679 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1680 DAG.getConstant(1, Tmp2.getValueType()));
1681 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1682 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001683
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001684 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1685 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001686 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1687 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001688 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001689
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001690 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1691 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1692 default: assert(0 && "This action is not supported yet!");
1693 case TargetLowering::Legal: break;
1694 case TargetLowering::Custom:
1695 Tmp1 = TLI.LowerOperation(Result, DAG);
1696 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001697 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001698 }
1699 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001700 }
Chris Lattner39c67442005-01-14 22:08:15 +00001701 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001702 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1703 case Expand: assert(0 && "It's impossible to expand bools");
1704 case Legal:
1705 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1706 break;
1707 case Promote:
1708 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1709 break;
1710 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001711 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001712 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001713
Chris Lattnerd02b0542006-01-28 10:58:55 +00001714 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001715
Nate Begeman987121a2005-08-23 04:29:48 +00001716 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001717 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001718 case TargetLowering::Legal: break;
1719 case TargetLowering::Custom: {
1720 Tmp1 = TLI.LowerOperation(Result, DAG);
1721 if (Tmp1.Val) Result = Tmp1;
1722 break;
1723 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001724 case TargetLowering::Expand:
1725 if (Tmp1.getOpcode() == ISD::SETCC) {
1726 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1727 Tmp2, Tmp3,
1728 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1729 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001730 // Make sure the condition is either zero or one. It may have been
1731 // promoted from something else.
Chris Lattnerd6f5ae42006-01-30 04:22:28 +00001732 unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType());
1733 if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1))
1734 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001735 Result = DAG.getSelectCC(Tmp1,
1736 DAG.getConstant(0, Tmp1.getValueType()),
1737 Tmp2, Tmp3, ISD::SETNE);
1738 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001739 break;
1740 case TargetLowering::Promote: {
1741 MVT::ValueType NVT =
1742 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1743 unsigned ExtOp, TruncOp;
1744 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001745 ExtOp = ISD::ANY_EXTEND;
1746 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001747 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001748 ExtOp = ISD::FP_EXTEND;
1749 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001750 }
1751 // Promote each of the values to the new type.
1752 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1753 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1754 // Perform the larger operation, then round down.
1755 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1756 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1757 break;
1758 }
1759 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001760 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001761 case ISD::SELECT_CC: {
1762 Tmp1 = Node->getOperand(0); // LHS
1763 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00001764 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1765 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00001766 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00001767
Nate Begeman7e7f4392006-02-01 07:19:44 +00001768 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
1769
1770 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1771 // the LHS is a legal SETCC itself. In this case, we need to compare
1772 // the result against zero to select between true and false values.
1773 if (Tmp2.Val == 0) {
1774 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
1775 CC = DAG.getCondCode(ISD::SETNE);
1776 }
1777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
1778
1779 // Everything is legal, see if we should expand this op or something.
1780 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
1781 default: assert(0 && "This action is not supported yet!");
1782 case TargetLowering::Legal: break;
1783 case TargetLowering::Custom:
1784 Tmp1 = TLI.LowerOperation(Result, DAG);
1785 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00001786 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001787 }
1788 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00001789 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001790 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00001791 Tmp1 = Node->getOperand(0);
1792 Tmp2 = Node->getOperand(1);
1793 Tmp3 = Node->getOperand(2);
1794 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
1795
1796 // If we had to Expand the SetCC operands into a SELECT node, then it may
1797 // not always be possible to return a true LHS & RHS. In this case, just
1798 // return the value we legalized, returned in the LHS
1799 if (Tmp2.Val == 0) {
1800 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00001801 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001802 }
Nate Begeman987121a2005-08-23 04:29:48 +00001803
Chris Lattnerf263a232006-01-30 22:43:50 +00001804 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001805 default: assert(0 && "Cannot handle this action for SETCC yet!");
1806 case TargetLowering::Custom:
1807 isCustom = true;
1808 // FALLTHROUGH.
1809 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001810 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001811 if (isCustom) {
1812 Tmp3 = TLI.LowerOperation(Result, DAG);
1813 if (Tmp3.Val) Result = Tmp3;
1814 }
Nate Begeman987121a2005-08-23 04:29:48 +00001815 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001816 case TargetLowering::Promote: {
1817 // First step, figure out the appropriate operation to use.
1818 // Allow SETCC to not be supported for all legal data types
1819 // Mostly this targets FP
1820 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1821 MVT::ValueType OldVT = NewInTy;
1822
1823 // Scan for the appropriate larger type to use.
1824 while (1) {
1825 NewInTy = (MVT::ValueType)(NewInTy+1);
1826
1827 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1828 "Fell off of the edge of the integer world");
1829 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1830 "Fell off of the edge of the floating point world");
1831
1832 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001833 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001834 break;
1835 }
1836 if (MVT::isInteger(NewInTy))
1837 assert(0 && "Cannot promote Legal Integer SETCC yet");
1838 else {
1839 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1840 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1841 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001842 Tmp1 = LegalizeOp(Tmp1);
1843 Tmp2 = LegalizeOp(Tmp2);
1844 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001845 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001846 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001847 }
Nate Begeman987121a2005-08-23 04:29:48 +00001848 case TargetLowering::Expand:
1849 // Expand a setcc node into a select_cc of the same condition, lhs, and
1850 // rhs that selects between const 1 (true) and const 0 (false).
1851 MVT::ValueType VT = Node->getValueType(0);
1852 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1853 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1854 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001855 break;
1856 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001857 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001858 case ISD::MEMSET:
1859 case ISD::MEMCPY:
1860 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001861 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001862 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1863
1864 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1865 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1866 case Expand: assert(0 && "Cannot expand a byte!");
1867 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001868 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001869 break;
1870 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001871 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001872 break;
1873 }
1874 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001875 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001876 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001877
1878 SDOperand Tmp4;
1879 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001880 case Expand: {
1881 // Length is too big, just take the lo-part of the length.
1882 SDOperand HiPart;
1883 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1884 break;
1885 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001886 case Legal:
1887 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001888 break;
1889 case Promote:
1890 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001891 break;
1892 }
1893
1894 SDOperand Tmp5;
1895 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1896 case Expand: assert(0 && "Cannot expand this yet!");
1897 case Legal:
1898 Tmp5 = LegalizeOp(Node->getOperand(4));
1899 break;
1900 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001901 Tmp5 = PromoteOp(Node->getOperand(4));
1902 break;
1903 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001904
1905 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1906 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001907 case TargetLowering::Custom:
1908 isCustom = true;
1909 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001910 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001911 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001912 if (isCustom) {
1913 Tmp1 = TLI.LowerOperation(Result, DAG);
1914 if (Tmp1.Val) Result = Tmp1;
1915 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001916 break;
1917 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001918 // Otherwise, the target does not support this operation. Lower the
1919 // operation to an explicit libcall as appropriate.
1920 MVT::ValueType IntPtr = TLI.getPointerTy();
1921 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1922 std::vector<std::pair<SDOperand, const Type*> > Args;
1923
Reid Spencer6dced922005-01-12 14:53:45 +00001924 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001925 if (Node->getOpcode() == ISD::MEMSET) {
1926 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
Chris Lattner486d1bc2006-02-20 06:38:35 +00001927 // Extend the (previously legalized) ubyte argument to be an int value
1928 // for the call.
1929 if (Tmp3.getValueType() > MVT::i32)
1930 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
1931 else
1932 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Chris Lattner85d70c62005-01-11 05:57:22 +00001933 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1934 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1935
1936 FnName = "memset";
1937 } else if (Node->getOpcode() == ISD::MEMCPY ||
1938 Node->getOpcode() == ISD::MEMMOVE) {
1939 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1940 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1941 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1942 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1943 } else {
1944 assert(0 && "Unknown op!");
1945 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001946
Chris Lattner85d70c62005-01-11 05:57:22 +00001947 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001948 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001949 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001950 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001951 break;
1952 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001953 }
1954 break;
1955 }
Chris Lattner5385db52005-05-09 20:23:03 +00001956
Chris Lattner4157c412005-04-02 04:00:59 +00001957 case ISD::SHL_PARTS:
1958 case ISD::SRA_PARTS:
1959 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001960 std::vector<SDOperand> Ops;
1961 bool Changed = false;
1962 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1963 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1964 Changed |= Ops.back() != Node->getOperand(i);
1965 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001966 if (Changed)
1967 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001968
Evan Cheng870e4f82006-01-09 18:31:59 +00001969 switch (TLI.getOperationAction(Node->getOpcode(),
1970 Node->getValueType(0))) {
1971 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001972 case TargetLowering::Legal: break;
1973 case TargetLowering::Custom:
1974 Tmp1 = TLI.LowerOperation(Result, DAG);
1975 if (Tmp1.Val) {
1976 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001977 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001978 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001979 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1980 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001981 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001982 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001983 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001984 return RetVal;
1985 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001986 break;
1987 }
1988
Chris Lattner13fe99c2005-04-02 05:00:07 +00001989 // Since these produce multiple values, make sure to remember that we
1990 // legalized all of them.
1991 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1992 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1993 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001994 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001995
1996 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001997 case ISD::ADD:
1998 case ISD::SUB:
1999 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002000 case ISD::MULHS:
2001 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002002 case ISD::UDIV:
2003 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002004 case ISD::AND:
2005 case ISD::OR:
2006 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002007 case ISD::SHL:
2008 case ISD::SRL:
2009 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002010 case ISD::FADD:
2011 case ISD::FSUB:
2012 case ISD::FMUL:
2013 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002014 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002015 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2016 case Expand: assert(0 && "Not possible");
2017 case Legal:
2018 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2019 break;
2020 case Promote:
2021 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2022 break;
2023 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002024
2025 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002026
Andrew Lenharth72594262005-12-24 23:42:32 +00002027 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002028 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002029 case TargetLowering::Legal: break;
2030 case TargetLowering::Custom:
2031 Tmp1 = TLI.LowerOperation(Result, DAG);
2032 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002033 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002034 case TargetLowering::Expand: {
2035 assert(MVT::isVector(Node->getValueType(0)) &&
2036 "Cannot expand this binary operator!");
2037 // Expand the operation into a bunch of nasty scalar code.
2038 std::vector<SDOperand> Ops;
2039 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2040 MVT::ValueType PtrVT = TLI.getPointerTy();
2041 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2042 i != e; ++i) {
2043 SDOperand Idx = DAG.getConstant(i, PtrVT);
2044 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2045 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2046 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2047 }
2048 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops);
2049 break;
2050 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002051 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002052 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002053
2054 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2055 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2056 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2057 case Expand: assert(0 && "Not possible");
2058 case Legal:
2059 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2060 break;
2061 case Promote:
2062 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2063 break;
2064 }
2065
2066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2067
2068 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2069 default: assert(0 && "Operation not supported");
2070 case TargetLowering::Custom:
2071 Tmp1 = TLI.LowerOperation(Result, DAG);
2072 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002073 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002074 case TargetLowering::Legal: break;
2075 case TargetLowering::Expand:
Chris Lattner994d8e62006-03-13 06:08:38 +00002076 // If this target supports fabs/fneg natively, do this efficiently.
2077 if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) &&
2078 TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) {
2079 // Get the sign bit of the RHS.
2080 MVT::ValueType IVT =
2081 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2082 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2083 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2084 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2085 // Get the absolute value of the result.
2086 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2087 // Select between the nabs and abs value based on the sign bit of
2088 // the input.
2089 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2090 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2091 AbsVal),
2092 AbsVal);
2093 Result = LegalizeOp(Result);
2094 break;
2095 }
2096
2097 // Otherwise, do bitwise ops!
2098
2099 // copysign -> copysignf/copysign libcall.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002100 const char *FnName;
2101 if (Node->getValueType(0) == MVT::f32) {
2102 FnName = "copysignf";
2103 if (Tmp2.getValueType() != MVT::f32) // Force operands to match type.
2104 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2105 DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2));
2106 } else {
2107 FnName = "copysign";
2108 if (Tmp2.getValueType() != MVT::f64) // Force operands to match type.
2109 Result = DAG.UpdateNodeOperands(Result, Tmp1,
2110 DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2));
2111 }
2112 SDOperand Dummy;
2113 Result = ExpandLibCall(FnName, Node, Dummy);
2114 break;
2115 }
2116 break;
2117
Nate Begeman5965bd12006-02-17 05:43:56 +00002118 case ISD::ADDC:
2119 case ISD::SUBC:
2120 Tmp1 = LegalizeOp(Node->getOperand(0));
2121 Tmp2 = LegalizeOp(Node->getOperand(1));
2122 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2123 // Since this produces two values, make sure to remember that we legalized
2124 // both of them.
2125 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2126 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2127 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002128
Nate Begeman5965bd12006-02-17 05:43:56 +00002129 case ISD::ADDE:
2130 case ISD::SUBE:
2131 Tmp1 = LegalizeOp(Node->getOperand(0));
2132 Tmp2 = LegalizeOp(Node->getOperand(1));
2133 Tmp3 = LegalizeOp(Node->getOperand(2));
2134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2135 // Since this produces two values, make sure to remember that we legalized
2136 // both of them.
2137 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2138 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2139 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002140
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002141 case ISD::BUILD_PAIR: {
2142 MVT::ValueType PairTy = Node->getValueType(0);
2143 // TODO: handle the case where the Lo and Hi operands are not of legal type
2144 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2145 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2146 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002147 case TargetLowering::Promote:
2148 case TargetLowering::Custom:
2149 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002150 case TargetLowering::Legal:
2151 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2152 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2153 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002154 case TargetLowering::Expand:
2155 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2156 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2157 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2158 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2159 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002160 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002161 break;
2162 }
2163 break;
2164 }
2165
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002166 case ISD::UREM:
2167 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002168 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002169 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2170 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002171
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002172 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002173 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2174 case TargetLowering::Custom:
2175 isCustom = true;
2176 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002177 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002179 if (isCustom) {
2180 Tmp1 = TLI.LowerOperation(Result, DAG);
2181 if (Tmp1.Val) Result = Tmp1;
2182 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002183 break;
Chris Lattner81914422005-08-03 20:31:37 +00002184 case TargetLowering::Expand:
2185 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002186 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00002187 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002188 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002189 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
2190 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2191 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2192 } else {
2193 // Floating point mod -> fmod libcall.
2194 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
2195 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002196 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002197 }
2198 break;
2199 }
2200 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002201 case ISD::VAARG: {
2202 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2203 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2204
Chris Lattner364b89a2006-01-28 07:42:08 +00002205 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002206 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2207 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002208 case TargetLowering::Custom:
2209 isCustom = true;
2210 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002211 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2213 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002214 Tmp1 = Result.getValue(1);
2215
2216 if (isCustom) {
2217 Tmp2 = TLI.LowerOperation(Result, DAG);
2218 if (Tmp2.Val) {
2219 Result = LegalizeOp(Tmp2);
2220 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2221 }
2222 }
Nate Begemane74795c2006-01-25 18:21:52 +00002223 break;
2224 case TargetLowering::Expand: {
2225 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2226 Node->getOperand(2));
2227 // Increment the pointer, VAList, to the next vaarg
2228 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2229 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2230 TLI.getPointerTy()));
2231 // Store the incremented VAList to the legalized pointer
2232 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2233 Node->getOperand(2));
2234 // Load the actual argument out of the pointer VAList
2235 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002236 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002237 Result = LegalizeOp(Result);
2238 break;
2239 }
2240 }
2241 // Since VAARG produces two values, make sure to remember that we
2242 // legalized both of them.
2243 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002244 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2245 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002246 }
2247
2248 case ISD::VACOPY:
2249 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2250 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2251 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2252
2253 switch (TLI.getOperationAction(ISD::VACOPY, 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, Tmp3,
2260 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002261 if (isCustom) {
2262 Tmp1 = TLI.LowerOperation(Result, DAG);
2263 if (Tmp1.Val) Result = Tmp1;
2264 }
Nate Begemane74795c2006-01-25 18:21:52 +00002265 break;
2266 case TargetLowering::Expand:
2267 // This defaults to loading a pointer from the input and storing it to the
2268 // output, returning the chain.
2269 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
2270 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
2271 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00002272 break;
2273 }
2274 break;
2275
2276 case ISD::VAEND:
2277 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2278 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2279
2280 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2281 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002282 case TargetLowering::Custom:
2283 isCustom = true;
2284 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002285 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002286 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002287 if (isCustom) {
2288 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2289 if (Tmp1.Val) Result = Tmp1;
2290 }
Nate Begemane74795c2006-01-25 18:21:52 +00002291 break;
2292 case TargetLowering::Expand:
2293 Result = Tmp1; // Default to a no-op, return the chain
2294 break;
2295 }
2296 break;
2297
2298 case ISD::VASTART:
2299 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2300 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2301
Chris Lattnerd02b0542006-01-28 10:58:55 +00002302 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2303
Nate Begemane74795c2006-01-25 18:21:52 +00002304 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2305 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002306 case TargetLowering::Legal: break;
2307 case TargetLowering::Custom:
2308 Tmp1 = TLI.LowerOperation(Result, DAG);
2309 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002310 break;
2311 }
2312 break;
2313
Nate Begeman1b8121b2006-01-11 21:21:00 +00002314 case ISD::ROTL:
2315 case ISD::ROTR:
2316 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2317 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002318
2319 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2320 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002322 break;
2323
Nate Begeman2fba8a32006-01-14 03:14:10 +00002324 case ISD::BSWAP:
2325 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2326 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002327 case TargetLowering::Custom:
2328 assert(0 && "Cannot custom legalize this yet!");
2329 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002330 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002331 break;
2332 case TargetLowering::Promote: {
2333 MVT::ValueType OVT = Tmp1.getValueType();
2334 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2335 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002336
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002337 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2338 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2339 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2340 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2341 break;
2342 }
2343 case TargetLowering::Expand:
2344 Result = ExpandBSWAP(Tmp1);
2345 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002346 }
2347 break;
2348
Andrew Lenharth5e177822005-05-03 17:19:30 +00002349 case ISD::CTPOP:
2350 case ISD::CTTZ:
2351 case ISD::CTLZ:
2352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2353 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002354 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002355 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002356 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002357 break;
2358 case TargetLowering::Promote: {
2359 MVT::ValueType OVT = Tmp1.getValueType();
2360 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002361
2362 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002363 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2364 // Perform the larger operation, then subtract if needed.
2365 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002366 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002367 case ISD::CTPOP:
2368 Result = Tmp1;
2369 break;
2370 case ISD::CTTZ:
2371 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002372 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2373 DAG.getConstant(getSizeInBits(NVT), NVT),
2374 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002375 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002376 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2377 break;
2378 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002379 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002380 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2381 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002382 getSizeInBits(OVT), NVT));
2383 break;
2384 }
2385 break;
2386 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002387 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002388 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002389 break;
2390 }
2391 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002392
Chris Lattner13fe99c2005-04-02 05:00:07 +00002393 // Unary operators
2394 case ISD::FABS:
2395 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002396 case ISD::FSQRT:
2397 case ISD::FSIN:
2398 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002399 Tmp1 = LegalizeOp(Node->getOperand(0));
2400 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002401 case TargetLowering::Promote:
2402 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002403 isCustom = true;
2404 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002405 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002406 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002407 if (isCustom) {
2408 Tmp1 = TLI.LowerOperation(Result, DAG);
2409 if (Tmp1.Val) Result = Tmp1;
2410 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002411 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002412 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002413 switch (Node->getOpcode()) {
2414 default: assert(0 && "Unreachable!");
2415 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002416 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2417 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002418 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002419 break;
Chris Lattner80026402005-04-30 04:43:14 +00002420 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002421 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2422 MVT::ValueType VT = Node->getValueType(0);
2423 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002424 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002425 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2426 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002427 break;
2428 }
2429 case ISD::FSQRT:
2430 case ISD::FSIN:
2431 case ISD::FCOS: {
2432 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002433 const char *FnName = 0;
2434 switch(Node->getOpcode()) {
2435 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2436 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2437 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2438 default: assert(0 && "Unreachable!");
2439 }
Nate Begeman77558da2005-08-04 21:43:28 +00002440 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002441 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002442 break;
2443 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002444 }
2445 break;
2446 }
2447 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002448
2449 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002450 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002451 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002452 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002453 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2454 Node->getOperand(0).getValueType())) {
2455 default: assert(0 && "Unknown operation action!");
2456 case TargetLowering::Expand:
2457 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2458 break;
2459 case TargetLowering::Legal:
2460 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002461 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002462 break;
2463 }
2464 }
2465 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002466 case ISD::VBIT_CONVERT: {
2467 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2468 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2469
2470 // The input has to be a vector type, we have to either scalarize it, pack
2471 // it, or convert it based on whether the input vector type is legal.
2472 SDNode *InVal = Node->getOperand(0).Val;
2473 unsigned NumElems =
2474 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2475 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2476
2477 // Figure out if there is a Packed type corresponding to this Vector
2478 // type. If so, convert to the packed type.
2479 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2480 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2481 // Turn this into a bit convert of the packed input.
2482 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2483 PackVectorOp(Node->getOperand(0), TVT));
2484 break;
2485 } else if (NumElems == 1) {
2486 // Turn this into a bit convert of the scalar input.
2487 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2488 PackVectorOp(Node->getOperand(0), EVT));
2489 break;
2490 } else {
2491 // FIXME: UNIMP! Store then reload
2492 assert(0 && "Cast from unsupported vector type not implemented yet!");
2493 }
2494 }
2495
Chris Lattner13fe99c2005-04-02 05:00:07 +00002496 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002497 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002498 case ISD::UINT_TO_FP: {
2499 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002500 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2501 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002502 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002503 Node->getOperand(0).getValueType())) {
2504 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002505 case TargetLowering::Custom:
2506 isCustom = true;
2507 // FALLTHROUGH
2508 case TargetLowering::Legal:
2509 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002510 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002511 if (isCustom) {
2512 Tmp1 = TLI.LowerOperation(Result, DAG);
2513 if (Tmp1.Val) Result = Tmp1;
2514 }
2515 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002516 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002517 Result = ExpandLegalINT_TO_FP(isSigned,
2518 LegalizeOp(Node->getOperand(0)),
2519 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002520 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002521 case TargetLowering::Promote:
2522 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2523 Node->getValueType(0),
2524 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002525 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002526 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002527 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002528 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002529 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2530 Node->getValueType(0), Node->getOperand(0));
2531 break;
2532 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002533 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002534 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002535 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2536 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002537 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002538 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2539 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002540 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002541 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2542 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002543 break;
2544 }
2545 break;
2546 }
2547 case ISD::TRUNCATE:
2548 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2549 case Legal:
2550 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002551 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002552 break;
2553 case Expand:
2554 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2555
2556 // Since the result is legal, we should just be able to truncate the low
2557 // part of the source.
2558 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2559 break;
2560 case Promote:
2561 Result = PromoteOp(Node->getOperand(0));
2562 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2563 break;
2564 }
2565 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002566
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002567 case ISD::FP_TO_SINT:
2568 case ISD::FP_TO_UINT:
2569 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2570 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002571 Tmp1 = LegalizeOp(Node->getOperand(0));
2572
Chris Lattner44fe26f2005-07-29 00:11:56 +00002573 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2574 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002575 case TargetLowering::Custom:
2576 isCustom = true;
2577 // FALLTHROUGH
2578 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002579 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002580 if (isCustom) {
2581 Tmp1 = TLI.LowerOperation(Result, DAG);
2582 if (Tmp1.Val) Result = Tmp1;
2583 }
2584 break;
2585 case TargetLowering::Promote:
2586 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2587 Node->getOpcode() == ISD::FP_TO_SINT);
2588 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002589 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002590 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2591 SDOperand True, False;
2592 MVT::ValueType VT = Node->getOperand(0).getValueType();
2593 MVT::ValueType NVT = Node->getValueType(0);
2594 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2595 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2596 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2597 Node->getOperand(0), Tmp2, ISD::SETLT);
2598 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2599 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002600 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002601 Tmp2));
2602 False = DAG.getNode(ISD::XOR, NVT, False,
2603 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002604 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2605 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002606 } else {
2607 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2608 }
2609 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002610 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002611 break;
2612 case Expand:
2613 assert(0 && "Shouldn't need to expand other operators here!");
2614 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002615 Tmp1 = PromoteOp(Node->getOperand(0));
2616 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2617 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002618 break;
2619 }
2620 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002621
Chris Lattner7753f172005-09-02 00:18:10 +00002622 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002623 case ISD::ZERO_EXTEND:
2624 case ISD::SIGN_EXTEND:
2625 case ISD::FP_EXTEND:
2626 case ISD::FP_ROUND:
2627 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002628 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002629 case Legal:
2630 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002631 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002632 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002633 case Promote:
2634 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002635 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002636 Tmp1 = PromoteOp(Node->getOperand(0));
2637 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002638 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002639 case ISD::ZERO_EXTEND:
2640 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002641 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002642 Result = DAG.getZeroExtendInReg(Result,
2643 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002644 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002645 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002646 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002647 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002648 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002649 Result,
2650 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002651 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002652 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002653 Result = PromoteOp(Node->getOperand(0));
2654 if (Result.getValueType() != Op.getValueType())
2655 // Dynamically dead while we have only 2 FP types.
2656 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2657 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002658 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002659 Result = PromoteOp(Node->getOperand(0));
2660 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2661 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002662 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002663 }
2664 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002665 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002666 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002667 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002668 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002669
2670 // If this operation is not supported, convert it to a shl/shr or load/store
2671 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002672 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2673 default: assert(0 && "This action not supported for this op yet!");
2674 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002675 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002676 break;
2677 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002678 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002679 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002680 // NOTE: we could fall back on load/store here too for targets without
2681 // SAR. However, it is doubtful that any exist.
2682 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2683 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002684 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002685 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2686 Node->getOperand(0), ShiftCst);
2687 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2688 Result, ShiftCst);
2689 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2690 // The only way we can lower this is to turn it into a STORETRUNC,
2691 // EXTLOAD pair, targetting a temporary location (a stack slot).
2692
2693 // NOTE: there is a choice here between constantly creating new stack
2694 // slots and always reusing the same one. We currently always create
2695 // new ones, as reuse may inhibit scheduling.
2696 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2697 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2698 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2699 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002700 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002701 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2702 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2703 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002704 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002705 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002706 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2707 Result, StackSlot, DAG.getSrcValue(NULL),
2708 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002709 } else {
2710 assert(0 && "Unknown op");
2711 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002712 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002713 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002714 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002715 }
Chris Lattner99222f72005-01-15 07:15:18 +00002716 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002717
Chris Lattner101ea662006-04-08 04:13:17 +00002718 assert(Result.getValueType() == Op.getValueType() &&
2719 "Bad legalization!");
2720
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002721 // Make sure that the generated code is itself legal.
2722 if (Result != Op)
2723 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002724
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002725 // Note that LegalizeOp may be reentered even from single-use nodes, which
2726 // means that we always must cache transformed nodes.
2727 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002728 return Result;
2729}
2730
Chris Lattner4d978642005-01-15 22:16:26 +00002731/// PromoteOp - Given an operation that produces a value in an invalid type,
2732/// promote it to compute the value into a larger type. The produced value will
2733/// have the correct bits for the low portion of the register, but no guarantee
2734/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002735SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2736 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002737 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002738 assert(getTypeAction(VT) == Promote &&
2739 "Caller should expand or legalize operands that are not promotable!");
2740 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2741 "Cannot promote to smaller type!");
2742
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002743 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002744 SDOperand Result;
2745 SDNode *Node = Op.Val;
2746
Chris Lattner1a570f12005-09-02 20:32:45 +00002747 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2748 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002749
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002750 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002751 case ISD::CopyFromReg:
2752 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002753 default:
2754 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2755 assert(0 && "Do not know how to promote this operator!");
2756 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002757 case ISD::UNDEF:
2758 Result = DAG.getNode(ISD::UNDEF, NVT);
2759 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002760 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002761 if (VT != MVT::i1)
2762 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2763 else
2764 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002765 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2766 break;
2767 case ISD::ConstantFP:
2768 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2769 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2770 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002771
Chris Lattner2cb338d2005-01-18 02:59:52 +00002772 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002773 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002774 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2775 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002776 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002777
2778 case ISD::TRUNCATE:
2779 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2780 case Legal:
2781 Result = LegalizeOp(Node->getOperand(0));
2782 assert(Result.getValueType() >= NVT &&
2783 "This truncation doesn't make sense!");
2784 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2785 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2786 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002787 case Promote:
2788 // The truncation is not required, because we don't guarantee anything
2789 // about high bits anyway.
2790 Result = PromoteOp(Node->getOperand(0));
2791 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002792 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002793 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2794 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002795 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002796 }
2797 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002798 case ISD::SIGN_EXTEND:
2799 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002800 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002801 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2802 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2803 case Legal:
2804 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002805 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002806 break;
2807 case Promote:
2808 // Promote the reg if it's smaller.
2809 Result = PromoteOp(Node->getOperand(0));
2810 // The high bits are not guaranteed to be anything. Insert an extend.
2811 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002812 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002813 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002814 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002815 Result = DAG.getZeroExtendInReg(Result,
2816 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002817 break;
2818 }
2819 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002820 case ISD::BIT_CONVERT:
2821 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2822 Result = PromoteOp(Result);
2823 break;
2824
Chris Lattner4d978642005-01-15 22:16:26 +00002825 case ISD::FP_EXTEND:
2826 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2827 case ISD::FP_ROUND:
2828 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2829 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2830 case Promote: assert(0 && "Unreachable with 2 FP types!");
2831 case Legal:
2832 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002833 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002834 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002835 break;
2836 }
2837 break;
2838
2839 case ISD::SINT_TO_FP:
2840 case ISD::UINT_TO_FP:
2841 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2842 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002843 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002844 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002845 break;
2846
2847 case Promote:
2848 Result = PromoteOp(Node->getOperand(0));
2849 if (Node->getOpcode() == ISD::SINT_TO_FP)
2850 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002851 Result,
2852 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002853 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002854 Result = DAG.getZeroExtendInReg(Result,
2855 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002856 // No extra round required here.
2857 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002858 break;
2859 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002860 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2861 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002862 // Round if we cannot tolerate excess precision.
2863 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002864 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2865 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002866 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002867 }
Chris Lattner4d978642005-01-15 22:16:26 +00002868 break;
2869
Chris Lattner268d4572005-12-09 17:32:47 +00002870 case ISD::SIGN_EXTEND_INREG:
2871 Result = PromoteOp(Node->getOperand(0));
2872 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2873 Node->getOperand(1));
2874 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002875 case ISD::FP_TO_SINT:
2876 case ISD::FP_TO_UINT:
2877 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2878 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002879 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002880 break;
2881 case Promote:
2882 // The input result is prerounded, so we don't have to do anything
2883 // special.
2884 Tmp1 = PromoteOp(Node->getOperand(0));
2885 break;
2886 case Expand:
2887 assert(0 && "not implemented");
2888 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002889 // If we're promoting a UINT to a larger size, check to see if the new node
2890 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2891 // we can use that instead. This allows us to generate better code for
2892 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2893 // legal, such as PowerPC.
2894 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002895 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002896 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2897 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002898 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2899 } else {
2900 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2901 }
Chris Lattner4d978642005-01-15 22:16:26 +00002902 break;
2903
Chris Lattner13fe99c2005-04-02 05:00:07 +00002904 case ISD::FABS:
2905 case ISD::FNEG:
2906 Tmp1 = PromoteOp(Node->getOperand(0));
2907 assert(Tmp1.getValueType() == NVT);
2908 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2909 // NOTE: we do not have to do any extra rounding here for
2910 // NoExcessFPPrecision, because we know the input will have the appropriate
2911 // precision, and these operations don't modify precision at all.
2912 break;
2913
Chris Lattner9d6fa982005-04-28 21:44:33 +00002914 case ISD::FSQRT:
2915 case ISD::FSIN:
2916 case ISD::FCOS:
2917 Tmp1 = PromoteOp(Node->getOperand(0));
2918 assert(Tmp1.getValueType() == NVT);
2919 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002920 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002921 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2922 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002923 break;
2924
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002925 case ISD::AND:
2926 case ISD::OR:
2927 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002928 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002929 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002930 case ISD::MUL:
2931 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002932 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002933 // that too is okay if they are integer operations.
2934 Tmp1 = PromoteOp(Node->getOperand(0));
2935 Tmp2 = PromoteOp(Node->getOperand(1));
2936 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2937 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002938 break;
2939 case ISD::FADD:
2940 case ISD::FSUB:
2941 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002942 Tmp1 = PromoteOp(Node->getOperand(0));
2943 Tmp2 = PromoteOp(Node->getOperand(1));
2944 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2945 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2946
2947 // Floating point operations will give excess precision that we may not be
2948 // able to tolerate. If we DO allow excess precision, just leave it,
2949 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002950 // FIXME: Why would we need to round FP ops more than integer ones?
2951 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002952 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002953 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2954 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002955 break;
2956
Chris Lattner4d978642005-01-15 22:16:26 +00002957 case ISD::SDIV:
2958 case ISD::SREM:
2959 // These operators require that their input be sign extended.
2960 Tmp1 = PromoteOp(Node->getOperand(0));
2961 Tmp2 = PromoteOp(Node->getOperand(1));
2962 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002963 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2964 DAG.getValueType(VT));
2965 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2966 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002967 }
2968 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2969
2970 // Perform FP_ROUND: this is probably overly pessimistic.
2971 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002972 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2973 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002974 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002975 case ISD::FDIV:
2976 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002977 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002978 // These operators require that their input be fp extended.
2979 Tmp1 = PromoteOp(Node->getOperand(0));
2980 Tmp2 = PromoteOp(Node->getOperand(1));
2981 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2982
2983 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002984 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00002985 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2986 DAG.getValueType(VT));
2987 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002988
2989 case ISD::UDIV:
2990 case ISD::UREM:
2991 // These operators require that their input be zero extended.
2992 Tmp1 = PromoteOp(Node->getOperand(0));
2993 Tmp2 = PromoteOp(Node->getOperand(1));
2994 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002995 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2996 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002997 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2998 break;
2999
3000 case ISD::SHL:
3001 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003002 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003003 break;
3004 case ISD::SRA:
3005 // The input value must be properly sign extended.
3006 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003007 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3008 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003009 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003010 break;
3011 case ISD::SRL:
3012 // The input value must be properly zero extended.
3013 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003014 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003015 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003016 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003017
3018 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003019 Tmp1 = Node->getOperand(0); // Get the chain.
3020 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003021 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3022 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3023 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3024 } else {
3025 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
3026 Node->getOperand(2));
3027 // Increment the pointer, VAList, to the next vaarg
3028 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3029 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3030 TLI.getPointerTy()));
3031 // Store the incremented VAList to the legalized pointer
3032 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
3033 Node->getOperand(2));
3034 // Load the actual argument out of the pointer VAList
3035 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
3036 DAG.getSrcValue(0), VT);
3037 }
3038 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003039 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003040 break;
3041
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003042 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003043 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
3044 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003045 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003046 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003047 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003048 case ISD::SEXTLOAD:
3049 case ISD::ZEXTLOAD:
3050 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003051 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
3052 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00003053 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003054 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003055 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00003056 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003057 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003058 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3059 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003060 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003061 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003062 case ISD::SELECT_CC:
3063 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3064 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3065 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003066 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003067 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003068 case ISD::BSWAP:
3069 Tmp1 = Node->getOperand(0);
3070 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3071 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3072 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3073 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3074 TLI.getShiftAmountTy()));
3075 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003076 case ISD::CTPOP:
3077 case ISD::CTTZ:
3078 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003079 // Zero extend the argument
3080 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003081 // Perform the larger operation, then subtract if needed.
3082 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003083 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003084 case ISD::CTPOP:
3085 Result = Tmp1;
3086 break;
3087 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003088 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003089 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003090 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003091 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003092 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003093 break;
3094 case ISD::CTLZ:
3095 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003096 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3097 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003098 getSizeInBits(VT), NVT));
3099 break;
3100 }
3101 break;
Chris Lattner6f423252006-03-31 17:55:51 +00003102 case ISD::VEXTRACT_VECTOR_ELT:
3103 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3104 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003105 case ISD::EXTRACT_VECTOR_ELT:
3106 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3107 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003108 }
3109
3110 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003111
3112 // Make sure the result is itself legal.
3113 Result = LegalizeOp(Result);
3114
3115 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003116 AddPromotedOperand(Op, Result);
3117 return Result;
3118}
Chris Lattnerdc750592005-01-07 07:47:09 +00003119
Chris Lattner6f423252006-03-31 17:55:51 +00003120/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3121/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3122/// on the vector type. The return type of this matches the element type of the
3123/// vector, which may not be legal for the target.
3124SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3125 // We know that operand #0 is the Vec vector. If the index is a constant
3126 // or if the invec is a supported hardware type, we can use it. Otherwise,
3127 // lower to a store then an indexed load.
3128 SDOperand Vec = Op.getOperand(0);
3129 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3130
3131 SDNode *InVal = Vec.Val;
3132 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3133 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3134
3135 // Figure out if there is a Packed type corresponding to this Vector
3136 // type. If so, convert to the packed type.
3137 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3138 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3139 // Turn this into a packed extract_vector_elt operation.
3140 Vec = PackVectorOp(Vec, TVT);
3141 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3142 } else if (NumElems == 1) {
3143 // This must be an access of the only element. Return it.
3144 return PackVectorOp(Vec, EVT);
3145 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3146 SDOperand Lo, Hi;
3147 SplitVectorOp(Vec, Lo, Hi);
3148 if (CIdx->getValue() < NumElems/2) {
3149 Vec = Lo;
3150 } else {
3151 Vec = Hi;
3152 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3153 }
3154
3155 // It's now an extract from the appropriate high or low part. Recurse.
3156 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3157 return LowerVEXTRACT_VECTOR_ELT(Op);
3158 } else {
3159 // Variable index case for extract element.
3160 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3161 assert(0 && "unimp!");
3162 return SDOperand();
3163 }
3164}
3165
Chris Lattner42a5fca2006-04-02 05:06:04 +00003166/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3167/// memory traffic.
3168SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3169 SDOperand Vector = Op.getOperand(0);
3170 SDOperand Idx = Op.getOperand(1);
3171
3172 // If the target doesn't support this, store the value to a temporary
3173 // stack slot, then LOAD the scalar element back out.
3174 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
3175 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3176 Vector, StackPtr, DAG.getSrcValue(NULL));
3177
3178 // Add the offset to the index.
3179 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3180 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3181 DAG.getConstant(EltSize, Idx.getValueType()));
3182 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3183
3184 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL));
3185}
3186
Chris Lattner6f423252006-03-31 17:55:51 +00003187
Nate Begeman7e7f4392006-02-01 07:19:44 +00003188/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3189/// with condition CC on the current target. This usually involves legalizing
3190/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3191/// there may be no choice but to create a new SetCC node to represent the
3192/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3193/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3194void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3195 SDOperand &RHS,
3196 SDOperand &CC) {
3197 SDOperand Tmp1, Tmp2, Result;
3198
3199 switch (getTypeAction(LHS.getValueType())) {
3200 case Legal:
3201 Tmp1 = LegalizeOp(LHS); // LHS
3202 Tmp2 = LegalizeOp(RHS); // RHS
3203 break;
3204 case Promote:
3205 Tmp1 = PromoteOp(LHS); // LHS
3206 Tmp2 = PromoteOp(RHS); // RHS
3207
3208 // If this is an FP compare, the operands have already been extended.
3209 if (MVT::isInteger(LHS.getValueType())) {
3210 MVT::ValueType VT = LHS.getValueType();
3211 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3212
3213 // Otherwise, we have to insert explicit sign or zero extends. Note
3214 // that we could insert sign extends for ALL conditions, but zero extend
3215 // is cheaper on many machines (an AND instead of two shifts), so prefer
3216 // it.
3217 switch (cast<CondCodeSDNode>(CC)->get()) {
3218 default: assert(0 && "Unknown integer comparison!");
3219 case ISD::SETEQ:
3220 case ISD::SETNE:
3221 case ISD::SETUGE:
3222 case ISD::SETUGT:
3223 case ISD::SETULE:
3224 case ISD::SETULT:
3225 // ALL of these operations will work if we either sign or zero extend
3226 // the operands (including the unsigned comparisons!). Zero extend is
3227 // usually a simpler/cheaper operation, so prefer it.
3228 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3229 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3230 break;
3231 case ISD::SETGE:
3232 case ISD::SETGT:
3233 case ISD::SETLT:
3234 case ISD::SETLE:
3235 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3236 DAG.getValueType(VT));
3237 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3238 DAG.getValueType(VT));
3239 break;
3240 }
3241 }
3242 break;
3243 case Expand:
3244 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3245 ExpandOp(LHS, LHSLo, LHSHi);
3246 ExpandOp(RHS, RHSLo, RHSHi);
3247 switch (cast<CondCodeSDNode>(CC)->get()) {
3248 case ISD::SETEQ:
3249 case ISD::SETNE:
3250 if (RHSLo == RHSHi)
3251 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3252 if (RHSCST->isAllOnesValue()) {
3253 // Comparison to -1.
3254 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3255 Tmp2 = RHSLo;
3256 break;
3257 }
3258
3259 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3260 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3261 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3262 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3263 break;
3264 default:
3265 // If this is a comparison of the sign bit, just look at the top part.
3266 // X > -1, x < 0
3267 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3268 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3269 CST->getValue() == 0) || // X < 0
3270 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3271 CST->isAllOnesValue())) { // X > -1
3272 Tmp1 = LHSHi;
3273 Tmp2 = RHSHi;
3274 break;
3275 }
3276
3277 // FIXME: This generated code sucks.
3278 ISD::CondCode LowCC;
3279 switch (cast<CondCodeSDNode>(CC)->get()) {
3280 default: assert(0 && "Unknown integer setcc!");
3281 case ISD::SETLT:
3282 case ISD::SETULT: LowCC = ISD::SETULT; break;
3283 case ISD::SETGT:
3284 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3285 case ISD::SETLE:
3286 case ISD::SETULE: LowCC = ISD::SETULE; break;
3287 case ISD::SETGE:
3288 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3289 }
3290
3291 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3292 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3293 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3294
3295 // NOTE: on targets without efficient SELECT of bools, we can always use
3296 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3297 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3298 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3299 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3300 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3301 Result, Tmp1, Tmp2));
3302 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003303 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003304 }
3305 }
3306 LHS = Tmp1;
3307 RHS = Tmp2;
3308}
3309
Chris Lattner36e663d2005-12-23 00:16:34 +00003310/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003311/// The resultant code need not be legal. Note that SrcOp is the input operand
3312/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003313SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3314 SDOperand SrcOp) {
3315 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003316 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003317
3318 // Emit a store to the stack slot.
3319 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00003320 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00003321 // Result is a load from the stack slot.
3322 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
3323}
3324
Chris Lattner6be79822006-04-04 17:23:26 +00003325SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3326 // Create a vector sized/aligned stack slot, store the value to element #0,
3327 // then load the whole vector back out.
3328 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
3329 SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3330 Node->getOperand(0), StackPtr,
3331 DAG.getSrcValue(NULL));
3332 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL));
3333}
3334
3335
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003336/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3337/// support the operation, but do support the resultant packed vector type.
3338SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3339
3340 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003341 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003342 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003343 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003344 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003345 std::map<SDOperand, std::vector<unsigned> > Values;
3346 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003347 bool isConstant = true;
3348 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3349 SplatValue.getOpcode() != ISD::UNDEF)
3350 isConstant = false;
3351
Evan Cheng1d2e9952006-03-24 01:17:21 +00003352 for (unsigned i = 1; i < NumElems; ++i) {
3353 SDOperand V = Node->getOperand(i);
3354 std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V);
3355 if (I != Values.end())
3356 I->second.push_back(i);
3357 else
3358 Values[V].push_back(i);
3359 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003360 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003361 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003362 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003363
3364 // If this isn't a constant element or an undef, we can't use a constant
3365 // pool load.
3366 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3367 V.getOpcode() != ISD::UNDEF)
3368 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003369 }
3370
3371 if (isOnlyLowElement) {
3372 // If the low element is an undef too, then this whole things is an undef.
3373 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3374 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3375 // Otherwise, turn this into a scalar_to_vector node.
3376 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3377 Node->getOperand(0));
3378 }
3379
Chris Lattner77e271c2006-03-24 07:29:17 +00003380 // If all elements are constants, create a load from the constant pool.
3381 if (isConstant) {
3382 MVT::ValueType VT = Node->getValueType(0);
3383 const Type *OpNTy =
3384 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3385 std::vector<Constant*> CV;
3386 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3387 if (ConstantFPSDNode *V =
3388 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3389 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3390 } else if (ConstantSDNode *V =
3391 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
3392 CV.push_back(ConstantUInt::get(OpNTy, V->getValue()));
3393 } else {
3394 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3395 CV.push_back(UndefValue::get(OpNTy));
3396 }
3397 }
3398 Constant *CP = ConstantPacked::get(CV);
3399 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
3400 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
3401 DAG.getSrcValue(NULL));
3402 }
3403
Chris Lattner21e68c82006-03-20 01:52:29 +00003404 if (SplatValue.Val) { // Splat of one value?
3405 // Build the shuffle constant vector: <0, 0, 0, 0>
3406 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003407 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003408 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003409 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattner21e68c82006-03-20 01:52:29 +00003410 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
3411
3412 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003413 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003414 // Get the splatted value into the low element of a vector register.
3415 SDOperand LowValVec =
3416 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3417
3418 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3419 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3420 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3421 SplatMask);
3422 }
3423 }
3424
Evan Cheng1d2e9952006-03-24 01:17:21 +00003425 // If there are only two unique elements, we may be able to turn this into a
3426 // vector shuffle.
3427 if (Values.size() == 2) {
3428 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3429 MVT::ValueType MaskVT =
3430 MVT::getIntVectorWithNumElements(NumElems);
3431 std::vector<SDOperand> MaskVec(NumElems);
3432 unsigned i = 0;
3433 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3434 E = Values.end(); I != E; ++I) {
3435 for (std::vector<unsigned>::iterator II = I->second.begin(),
3436 EE = I->second.end(); II != EE; ++II)
3437 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3438 i += NumElems;
3439 }
3440 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
3441
3442 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003443 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3444 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Evan Cheng1d2e9952006-03-24 01:17:21 +00003445 std::vector<SDOperand> Ops;
3446 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3447 E = Values.end(); I != E; ++I) {
3448 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3449 I->first);
3450 Ops.push_back(Op);
3451 }
3452 Ops.push_back(ShuffleMask);
3453
3454 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
3455 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops);
3456 }
3457 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003458
3459 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3460 // aligned object on the stack, store each element into it, then load
3461 // the result as a vector.
3462 MVT::ValueType VT = Node->getValueType(0);
3463 // Create the stack frame object.
3464 SDOperand FIPtr = CreateStackTemporary(VT);
3465
3466 // Emit a store of each element to the stack slot.
3467 std::vector<SDOperand> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003468 unsigned TypeByteSize =
3469 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
3470 unsigned VectorSize = MVT::getSizeInBits(VT)/8;
3471 // Store (in the right endianness) the elements to memory.
3472 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3473 // Ignore undef elements.
3474 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3475
Chris Lattner8fa445a2006-03-22 01:46:54 +00003476 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003477
3478 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3479 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3480
3481 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3482 Node->getOperand(i), Idx,
3483 DAG.getSrcValue(NULL)));
3484 }
3485
3486 SDOperand StoreChain;
3487 if (!Stores.empty()) // Not all undef elements?
3488 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
3489 else
3490 StoreChain = DAG.getEntryNode();
3491
3492 // Result is a load from the stack slot.
3493 return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0));
3494}
3495
3496/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3497/// specified value type.
3498SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3499 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3500 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3501 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3502 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3503}
3504
Chris Lattner4157c412005-04-02 04:00:59 +00003505void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3506 SDOperand Op, SDOperand Amt,
3507 SDOperand &Lo, SDOperand &Hi) {
3508 // Expand the subcomponents.
3509 SDOperand LHSL, LHSH;
3510 ExpandOp(Op, LHSL, LHSH);
3511
3512 std::vector<SDOperand> Ops;
3513 Ops.push_back(LHSL);
3514 Ops.push_back(LHSH);
3515 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00003516 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00003517 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00003518 Hi = Lo.getValue(1);
3519}
3520
3521
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003522/// ExpandShift - Try to find a clever way to expand this shift operation out to
3523/// smaller elements. If we can't find a way that is more efficient than a
3524/// libcall on this target, return false. Otherwise, return true with the
3525/// low-parts expanded into Lo and Hi.
3526bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3527 SDOperand &Lo, SDOperand &Hi) {
3528 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3529 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003530
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003531 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003532 SDOperand ShAmt = LegalizeOp(Amt);
3533 MVT::ValueType ShTy = ShAmt.getValueType();
3534 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3535 unsigned NVTBits = MVT::getSizeInBits(NVT);
3536
3537 // Handle the case when Amt is an immediate. Other cases are currently broken
3538 // and are disabled.
3539 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3540 unsigned Cst = CN->getValue();
3541 // Expand the incoming operand to be shifted, so that we have its parts
3542 SDOperand InL, InH;
3543 ExpandOp(Op, InL, InH);
3544 switch(Opc) {
3545 case ISD::SHL:
3546 if (Cst > VTBits) {
3547 Lo = DAG.getConstant(0, NVT);
3548 Hi = DAG.getConstant(0, NVT);
3549 } else if (Cst > NVTBits) {
3550 Lo = DAG.getConstant(0, NVT);
3551 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003552 } else if (Cst == NVTBits) {
3553 Lo = DAG.getConstant(0, NVT);
3554 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00003555 } else {
3556 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3557 Hi = DAG.getNode(ISD::OR, NVT,
3558 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3559 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3560 }
3561 return true;
3562 case ISD::SRL:
3563 if (Cst > VTBits) {
3564 Lo = DAG.getConstant(0, NVT);
3565 Hi = DAG.getConstant(0, NVT);
3566 } else if (Cst > NVTBits) {
3567 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
3568 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00003569 } else if (Cst == NVTBits) {
3570 Lo = InH;
3571 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00003572 } else {
3573 Lo = DAG.getNode(ISD::OR, NVT,
3574 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3575 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3576 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
3577 }
3578 return true;
3579 case ISD::SRA:
3580 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003581 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003582 DAG.getConstant(NVTBits-1, ShTy));
3583 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00003584 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003585 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00003586 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00003587 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00003588 } else if (Cst == NVTBits) {
3589 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00003590 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00003591 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00003592 } else {
3593 Lo = DAG.getNode(ISD::OR, NVT,
3594 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
3595 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
3596 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
3597 }
3598 return true;
3599 }
3600 }
Nate Begemanb0674922005-04-06 21:13:14 +00003601 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003602}
Chris Lattneraac464e2005-01-21 06:05:23 +00003603
Chris Lattner4add7e32005-01-23 04:42:50 +00003604
Chris Lattneraac464e2005-01-21 06:05:23 +00003605// ExpandLibCall - Expand a node into a call to a libcall. If the result value
3606// does not fit into a register, return the lo part and set the hi part to the
3607// by-reg argument. If it does fit into a single register, return the result
3608// and leave the Hi part unset.
3609SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
3610 SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00003611 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
3612 // The input chain to this libcall is the entry node of the function.
3613 // Legalizing the call will automatically add the previous call to the
3614 // dependence.
3615 SDOperand InChain = DAG.getEntryNode();
3616
Chris Lattneraac464e2005-01-21 06:05:23 +00003617 TargetLowering::ArgListTy Args;
3618 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3619 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
3620 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
3621 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
3622 }
3623 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00003624
Chris Lattner06bbeb62005-05-11 19:02:11 +00003625 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00003626 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00003627 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00003628 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
3629 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00003630
Chris Lattner462505f2006-02-13 09:18:02 +00003631 // Legalize the call sequence, starting with the chain. This will advance
3632 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
3633 // was added by LowerCallTo (guaranteeing proper serialization of calls).
3634 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00003635 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00003636 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00003637 default: assert(0 && "Unknown thing");
3638 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003639 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00003640 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003641 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00003642 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00003643 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00003644 }
Chris Lattner63022662005-09-02 20:26:58 +00003645 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00003646}
3647
Chris Lattner4add7e32005-01-23 04:42:50 +00003648
Chris Lattneraac464e2005-01-21 06:05:23 +00003649/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3650/// destination type is legal.
3651SDOperand SelectionDAGLegalize::
3652ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003653 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003654 assert(getTypeAction(Source.getValueType()) == Expand &&
3655 "This is not an expansion!");
3656 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3657
Chris Lattner06bbeb62005-05-11 19:02:11 +00003658 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003659 assert(Source.getValueType() == MVT::i64 &&
3660 "This only works for 64-bit -> FP");
3661 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3662 // incoming integer is set. To handle this, we dynamically test to see if
3663 // it is set, and, if so, add a fudge factor.
3664 SDOperand Lo, Hi;
3665 ExpandOp(Source, Lo, Hi);
3666
Chris Lattner2a4f7312005-05-13 04:45:13 +00003667 // If this is unsigned, and not supported, first perform the conversion to
3668 // signed, then adjust the result if the sign bit is set.
3669 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3670 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3671
Chris Lattnerd47675e2005-08-09 20:20:18 +00003672 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3673 DAG.getConstant(0, Hi.getValueType()),
3674 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003675 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3676 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3677 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003678 uint64_t FF = 0x5f800000ULL;
3679 if (TLI.isLittleEndian()) FF <<= 32;
3680 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003681
Chris Lattnerc30405e2005-08-26 17:15:30 +00003682 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003683 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3684 SDOperand FudgeInReg;
3685 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003686 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3687 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003688 else {
3689 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003690 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3691 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003692 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003693 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003694 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003695
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003696 // Check to see if the target has a custom way to lower this. If so, use it.
3697 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3698 default: assert(0 && "This action not implemented for this operation!");
3699 case TargetLowering::Legal:
3700 case TargetLowering::Expand:
3701 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003702 case TargetLowering::Custom: {
3703 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3704 Source), DAG);
3705 if (NV.Val)
3706 return LegalizeOp(NV);
3707 break; // The target decided this was legal after all
3708 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003709 }
3710
Chris Lattner153587e2005-05-12 07:00:44 +00003711 // Expand the source, then glue it back together for the call. We must expand
3712 // the source in case it is shared (this pass of legalize must traverse it).
3713 SDOperand SrcLo, SrcHi;
3714 ExpandOp(Source, SrcLo, SrcHi);
3715 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3716
Chris Lattner06bbeb62005-05-11 19:02:11 +00003717 const char *FnName = 0;
3718 if (DestTy == MVT::f32)
3719 FnName = "__floatdisf";
3720 else {
3721 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3722 FnName = "__floatdidf";
3723 }
Chris Lattner462505f2006-02-13 09:18:02 +00003724
3725 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
3726 SDOperand UnusedHiPart;
Chris Lattner9ec392b2006-02-17 04:32:33 +00003727 return ExpandLibCall(FnName, Source.Val, UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00003728}
Misha Brukman835702a2005-04-21 22:36:52 +00003729
Chris Lattner689bdcc2006-01-28 08:25:58 +00003730/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3731/// INT_TO_FP operation of the specified operand when the target requests that
3732/// we expand it. At this point, we know that the result and operand types are
3733/// legal for the target.
3734SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3735 SDOperand Op0,
3736 MVT::ValueType DestVT) {
3737 if (Op0.getValueType() == MVT::i32) {
3738 // simple 32-bit [signed|unsigned] integer to float/double expansion
3739
3740 // get the stack frame index of a 8 byte buffer
3741 MachineFunction &MF = DAG.getMachineFunction();
3742 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3743 // get address of 8 byte buffer
3744 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3745 // word offset constant for Hi/Lo address computation
3746 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3747 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00003748 SDOperand Hi = StackSlot;
3749 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
3750 if (TLI.isLittleEndian())
3751 std::swap(Hi, Lo);
3752
Chris Lattner689bdcc2006-01-28 08:25:58 +00003753 // if signed map to unsigned space
3754 SDOperand Op0Mapped;
3755 if (isSigned) {
3756 // constant used to invert sign bit (signed to unsigned mapping)
3757 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3758 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3759 } else {
3760 Op0Mapped = Op0;
3761 }
3762 // store the lo of the constructed double - based on integer input
3763 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3764 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3765 // initial hi portion of constructed double
3766 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3767 // store the hi of the constructed double - biased exponent
3768 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3769 InitialHi, Hi, DAG.getSrcValue(NULL));
3770 // load the constructed double
3771 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3772 DAG.getSrcValue(NULL));
3773 // FP constant to bias correct the final result
3774 SDOperand Bias = DAG.getConstantFP(isSigned ?
3775 BitsToDouble(0x4330000080000000ULL)
3776 : BitsToDouble(0x4330000000000000ULL),
3777 MVT::f64);
3778 // subtract the bias
3779 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3780 // final result
3781 SDOperand Result;
3782 // handle final rounding
3783 if (DestVT == MVT::f64) {
3784 // do nothing
3785 Result = Sub;
3786 } else {
3787 // if f32 then cast to f32
3788 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3789 }
3790 return Result;
3791 }
3792 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3793 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3794
3795 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3796 DAG.getConstant(0, Op0.getValueType()),
3797 ISD::SETLT);
3798 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3799 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3800 SignSet, Four, Zero);
3801
3802 // If the sign bit of the integer is set, the large number will be treated
3803 // as a negative number. To counteract this, the dynamic code adds an
3804 // offset depending on the data type.
3805 uint64_t FF;
3806 switch (Op0.getValueType()) {
3807 default: assert(0 && "Unsupported integer type!");
3808 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3809 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3810 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3811 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3812 }
3813 if (TLI.isLittleEndian()) FF <<= 32;
3814 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3815
3816 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3817 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3818 SDOperand FudgeInReg;
3819 if (DestVT == MVT::f32)
3820 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3821 DAG.getSrcValue(NULL));
3822 else {
3823 assert(DestVT == MVT::f64 && "Unexpected conversion");
3824 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3825 DAG.getEntryNode(), CPIdx,
3826 DAG.getSrcValue(NULL), MVT::f32));
3827 }
3828
3829 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3830}
3831
3832/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3833/// *INT_TO_FP operation of the specified operand when the target requests that
3834/// we promote it. At this point, we know that the result and operand types are
3835/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3836/// operation that takes a larger input.
3837SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3838 MVT::ValueType DestVT,
3839 bool isSigned) {
3840 // First step, figure out the appropriate *INT_TO_FP operation to use.
3841 MVT::ValueType NewInTy = LegalOp.getValueType();
3842
3843 unsigned OpToUse = 0;
3844
3845 // Scan for the appropriate larger type to use.
3846 while (1) {
3847 NewInTy = (MVT::ValueType)(NewInTy+1);
3848 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3849
3850 // If the target supports SINT_TO_FP of this type, use it.
3851 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3852 default: break;
3853 case TargetLowering::Legal:
3854 if (!TLI.isTypeLegal(NewInTy))
3855 break; // Can't use this datatype.
3856 // FALL THROUGH.
3857 case TargetLowering::Custom:
3858 OpToUse = ISD::SINT_TO_FP;
3859 break;
3860 }
3861 if (OpToUse) break;
3862 if (isSigned) continue;
3863
3864 // If the target supports UINT_TO_FP of this type, use it.
3865 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3866 default: break;
3867 case TargetLowering::Legal:
3868 if (!TLI.isTypeLegal(NewInTy))
3869 break; // Can't use this datatype.
3870 // FALL THROUGH.
3871 case TargetLowering::Custom:
3872 OpToUse = ISD::UINT_TO_FP;
3873 break;
3874 }
3875 if (OpToUse) break;
3876
3877 // Otherwise, try a larger type.
3878 }
3879
3880 // Okay, we found the operation and type to use. Zero extend our input to the
3881 // desired type then run the operation on it.
3882 return DAG.getNode(OpToUse, DestVT,
3883 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3884 NewInTy, LegalOp));
3885}
3886
3887/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3888/// FP_TO_*INT 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 FP_TO_UINT or FP_TO_SINT
3891/// operation that returns a larger result.
3892SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3893 MVT::ValueType DestVT,
3894 bool isSigned) {
3895 // First step, figure out the appropriate FP_TO*INT operation to use.
3896 MVT::ValueType NewOutTy = DestVT;
3897
3898 unsigned OpToUse = 0;
3899
3900 // Scan for the appropriate larger type to use.
3901 while (1) {
3902 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3903 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3904
3905 // If the target supports FP_TO_SINT returning this type, use it.
3906 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3907 default: break;
3908 case TargetLowering::Legal:
3909 if (!TLI.isTypeLegal(NewOutTy))
3910 break; // Can't use this datatype.
3911 // FALL THROUGH.
3912 case TargetLowering::Custom:
3913 OpToUse = ISD::FP_TO_SINT;
3914 break;
3915 }
3916 if (OpToUse) break;
3917
3918 // If the target supports FP_TO_UINT of this type, use it.
3919 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3920 default: break;
3921 case TargetLowering::Legal:
3922 if (!TLI.isTypeLegal(NewOutTy))
3923 break; // Can't use this datatype.
3924 // FALL THROUGH.
3925 case TargetLowering::Custom:
3926 OpToUse = ISD::FP_TO_UINT;
3927 break;
3928 }
3929 if (OpToUse) break;
3930
3931 // Otherwise, try a larger type.
3932 }
3933
3934 // Okay, we found the operation and type to use. Truncate the result of the
3935 // extended FP_TO_*INT operation to the desired size.
3936 return DAG.getNode(ISD::TRUNCATE, DestVT,
3937 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3938}
3939
3940/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3941///
3942SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3943 MVT::ValueType VT = Op.getValueType();
3944 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3945 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3946 switch (VT) {
3947 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3948 case MVT::i16:
3949 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3950 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3951 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3952 case MVT::i32:
3953 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3954 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3955 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3956 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3957 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3958 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3959 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3960 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3961 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3962 case MVT::i64:
3963 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3964 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3965 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3966 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3967 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3968 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3969 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3970 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3971 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3972 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3973 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3974 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3975 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3976 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3977 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3978 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3979 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3980 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3981 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3982 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3983 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3984 }
3985}
3986
3987/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3988///
3989SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3990 switch (Opc) {
3991 default: assert(0 && "Cannot expand this yet!");
3992 case ISD::CTPOP: {
3993 static const uint64_t mask[6] = {
3994 0x5555555555555555ULL, 0x3333333333333333ULL,
3995 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3996 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3997 };
3998 MVT::ValueType VT = Op.getValueType();
3999 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4000 unsigned len = getSizeInBits(VT);
4001 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4002 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4003 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4004 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4005 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4006 DAG.getNode(ISD::AND, VT,
4007 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4008 }
4009 return Op;
4010 }
4011 case ISD::CTLZ: {
4012 // for now, we do this:
4013 // x = x | (x >> 1);
4014 // x = x | (x >> 2);
4015 // ...
4016 // x = x | (x >>16);
4017 // x = x | (x >>32); // for 64-bit input
4018 // return popcount(~x);
4019 //
4020 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4021 MVT::ValueType VT = Op.getValueType();
4022 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4023 unsigned len = getSizeInBits(VT);
4024 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4025 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4026 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4027 }
4028 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4029 return DAG.getNode(ISD::CTPOP, VT, Op);
4030 }
4031 case ISD::CTTZ: {
4032 // for now, we use: { return popcount(~x & (x - 1)); }
4033 // unless the target has ctlz but not ctpop, in which case we use:
4034 // { return 32 - nlz(~x & (x-1)); }
4035 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4036 MVT::ValueType VT = Op.getValueType();
4037 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4038 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4039 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4040 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4041 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4042 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4043 TLI.isOperationLegal(ISD::CTLZ, VT))
4044 return DAG.getNode(ISD::SUB, VT,
4045 DAG.getConstant(getSizeInBits(VT), VT),
4046 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4047 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4048 }
4049 }
4050}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004051
4052
Chris Lattnerdc750592005-01-07 07:47:09 +00004053/// ExpandOp - Expand the specified SDOperand into its two component pieces
4054/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4055/// LegalizeNodes map is filled in for any results that are not expanded, the
4056/// ExpandedNodes map is filled in for any results that are expanded, and the
4057/// Lo/Hi values are returned.
4058void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4059 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004060 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004061 SDNode *Node = Op.Val;
4062 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00004063 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
4064 "Cannot expand FP values!");
4065 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004066 "Cannot expand to FP value or to larger int value!");
4067
Chris Lattner1a570f12005-09-02 20:32:45 +00004068 // See if we already expanded it.
4069 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4070 = ExpandedNodes.find(Op);
4071 if (I != ExpandedNodes.end()) {
4072 Lo = I->second.first;
4073 Hi = I->second.second;
4074 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004075 }
4076
Chris Lattnerdc750592005-01-07 07:47:09 +00004077 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004078 case ISD::CopyFromReg:
4079 assert(0 && "CopyFromReg must be legal!");
4080 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00004081 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
4082 assert(0 && "Do not know how to expand this operator!");
4083 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004084 case ISD::UNDEF:
4085 Lo = DAG.getNode(ISD::UNDEF, NVT);
4086 Hi = DAG.getNode(ISD::UNDEF, NVT);
4087 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004088 case ISD::Constant: {
4089 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4090 Lo = DAG.getConstant(Cst, NVT);
4091 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4092 break;
4093 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004094 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004095 // Return the operands.
4096 Lo = Node->getOperand(0);
4097 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004098 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004099
4100 case ISD::SIGN_EXTEND_INREG:
4101 ExpandOp(Node->getOperand(0), Lo, Hi);
4102 // Sign extend the lo-part.
4103 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4104 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4105 TLI.getShiftAmountTy()));
4106 // sext_inreg the low part if needed.
4107 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4108 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004109
Nate Begeman2fba8a32006-01-14 03:14:10 +00004110 case ISD::BSWAP: {
4111 ExpandOp(Node->getOperand(0), Lo, Hi);
4112 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4113 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4114 Lo = TempLo;
4115 break;
4116 }
4117
Chris Lattner55e9cde2005-05-11 04:51:16 +00004118 case ISD::CTPOP:
4119 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004120 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4121 DAG.getNode(ISD::CTPOP, NVT, Lo),
4122 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004123 Hi = DAG.getConstant(0, NVT);
4124 break;
4125
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004126 case ISD::CTLZ: {
4127 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004128 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004129 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4130 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004131 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4132 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004133 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4134 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4135
4136 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4137 Hi = DAG.getConstant(0, NVT);
4138 break;
4139 }
4140
4141 case ISD::CTTZ: {
4142 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004143 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004144 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4145 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004146 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4147 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004148 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4149 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4150
4151 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4152 Hi = DAG.getConstant(0, NVT);
4153 break;
4154 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004155
Nate Begemane74795c2006-01-25 18:21:52 +00004156 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004157 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4158 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004159 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4160 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4161
4162 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004163 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004164 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4165 if (!TLI.isLittleEndian())
4166 std::swap(Lo, Hi);
4167 break;
4168 }
4169
Chris Lattnerdc750592005-01-07 07:47:09 +00004170 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004171 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4172 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00004173 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00004174
4175 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00004176 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00004177 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4178 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004179 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00004180 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00004181
4182 // Build a factor node to remember that this load is independent of the
4183 // other one.
4184 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4185 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00004186
Chris Lattnerdc750592005-01-07 07:47:09 +00004187 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004188 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00004189 if (!TLI.isLittleEndian())
4190 std::swap(Lo, Hi);
4191 break;
4192 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004193 case ISD::AND:
4194 case ISD::OR:
4195 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4196 SDOperand LL, LH, RL, RH;
4197 ExpandOp(Node->getOperand(0), LL, LH);
4198 ExpandOp(Node->getOperand(1), RL, RH);
4199 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4200 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4201 break;
4202 }
4203 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004204 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004205 ExpandOp(Node->getOperand(1), LL, LH);
4206 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004207 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
4208 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004209 break;
4210 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004211 case ISD::SELECT_CC: {
4212 SDOperand TL, TH, FL, FH;
4213 ExpandOp(Node->getOperand(2), TL, TH);
4214 ExpandOp(Node->getOperand(3), FL, FH);
4215 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4216 Node->getOperand(1), TL, FL, Node->getOperand(4));
4217 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4218 Node->getOperand(1), TH, FH, Node->getOperand(4));
4219 break;
4220 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00004221 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004222 SDOperand Chain = Node->getOperand(0);
4223 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004224 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4225
4226 if (EVT == NVT)
4227 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4228 else
4229 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4230 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004231
4232 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004233 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004234
Nate Begemanc3a89c52005-10-13 17:15:37 +00004235 // The high part is obtained by SRA'ing all but one of the bits of the lo
4236 // part.
4237 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4238 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
4239 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00004240 break;
4241 }
4242 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004243 SDOperand Chain = Node->getOperand(0);
4244 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00004245 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4246
4247 if (EVT == NVT)
4248 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4249 else
4250 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4251 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004252
4253 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004254 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004255
Nate Begemanc3a89c52005-10-13 17:15:37 +00004256 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004257 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00004258 break;
4259 }
4260 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004261 SDOperand Chain = Node->getOperand(0);
4262 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00004263 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
4264
4265 if (EVT == NVT)
4266 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
4267 else
4268 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
4269 EVT);
4270
4271 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004272 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00004273
4274 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00004275 Hi = DAG.getNode(ISD::UNDEF, NVT);
4276 break;
4277 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004278 case ISD::ANY_EXTEND:
4279 // The low part is any extension of the input (which degenerates to a copy).
4280 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4281 // The high part is undefined.
4282 Hi = DAG.getNode(ISD::UNDEF, NVT);
4283 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004284 case ISD::SIGN_EXTEND: {
4285 // The low part is just a sign extension of the input (which degenerates to
4286 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004287 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004288
Chris Lattnerdc750592005-01-07 07:47:09 +00004289 // The high part is obtained by SRA'ing all but one of the bits of the lo
4290 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004291 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004292 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4293 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004294 break;
4295 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004296 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004297 // The low part is just a zero extension of the input (which degenerates to
4298 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004299 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004300
Chris Lattnerdc750592005-01-07 07:47:09 +00004301 // The high part is just a zero.
4302 Hi = DAG.getConstant(0, NVT);
4303 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004304
4305 case ISD::BIT_CONVERT: {
4306 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
4307 Node->getOperand(0));
4308 ExpandOp(Tmp, Lo, Hi);
4309 break;
4310 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004311
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004312 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004313 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4314 TargetLowering::Custom &&
4315 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004316 Lo = TLI.LowerOperation(Op, DAG);
4317 assert(Lo.Val && "Node must be custom expanded!");
4318 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004319 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004320 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004321 break;
4322
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004323 // These operators cannot be expanded directly, emit them as calls to
4324 // library functions.
4325 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004326 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004327 SDOperand Op;
4328 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4329 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004330 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4331 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004332 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004333
Chris Lattner941d84a2005-07-30 01:40:57 +00004334 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4335
Chris Lattnerfe68d752005-07-29 00:33:32 +00004336 // Now that the custom expander is done, expand the result, which is still
4337 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004338 if (Op.Val) {
4339 ExpandOp(Op, Lo, Hi);
4340 break;
4341 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004342 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004343
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004344 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004345 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004346 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004347 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004348 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00004349
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004350 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00004351 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004352 SDOperand Op;
4353 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4354 case Expand: assert(0 && "cannot expand FP!");
4355 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4356 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4357 }
4358
4359 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4360
4361 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004362 if (Op.Val) {
4363 ExpandOp(Op, Lo, Hi);
4364 break;
4365 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004366 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004367
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004368 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00004369 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004370 else
Chris Lattneraac464e2005-01-21 06:05:23 +00004371 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004372 break;
4373
Evan Cheng870e4f82006-01-09 18:31:59 +00004374 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004375 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004376 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004377 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004378 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004379 Op = TLI.LowerOperation(Op, DAG);
4380 if (Op.Val) {
4381 // Now that the custom expander is done, expand the result, which is
4382 // still VT.
4383 ExpandOp(Op, Lo, Hi);
4384 break;
4385 }
4386 }
4387
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004388 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004389 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004390 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004391
4392 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004393 TargetLowering::LegalizeAction Action =
4394 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4395 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4396 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004397 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004398 break;
4399 }
4400
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004401 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004402 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004403 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004404 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004405
Evan Cheng870e4f82006-01-09 18:31:59 +00004406 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004407 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004408 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004409 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004410 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004411 Op = TLI.LowerOperation(Op, DAG);
4412 if (Op.Val) {
4413 // Now that the custom expander is done, expand the result, which is
4414 // still VT.
4415 ExpandOp(Op, Lo, Hi);
4416 break;
4417 }
4418 }
4419
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004420 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004421 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004422 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004423
4424 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004425 TargetLowering::LegalizeAction Action =
4426 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4427 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4428 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004429 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004430 break;
4431 }
4432
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004433 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004434 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004435 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004436 }
4437
4438 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004439 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004440 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004441 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004442 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004443 Op = TLI.LowerOperation(Op, DAG);
4444 if (Op.Val) {
4445 // Now that the custom expander is done, expand the result, which is
4446 // still VT.
4447 ExpandOp(Op, Lo, Hi);
4448 break;
4449 }
4450 }
4451
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004452 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004453 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004454 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004455
4456 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004457 TargetLowering::LegalizeAction Action =
4458 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
4459 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4460 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004461 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004462 break;
4463 }
4464
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004465 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00004466 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004467 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004468 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004469
Misha Brukman835702a2005-04-21 22:36:52 +00004470 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004471 case ISD::SUB: {
4472 // If the target wants to custom expand this, let them.
4473 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
4474 TargetLowering::Custom) {
4475 Op = TLI.LowerOperation(Op, DAG);
4476 if (Op.Val) {
4477 ExpandOp(Op, Lo, Hi);
4478 break;
4479 }
4480 }
4481
4482 // Expand the subcomponents.
4483 SDOperand LHSL, LHSH, RHSL, RHSH;
4484 ExpandOp(Node->getOperand(0), LHSL, LHSH);
4485 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Nate Begeman5965bd12006-02-17 05:43:56 +00004486 std::vector<MVT::ValueType> VTs;
4487 std::vector<SDOperand> LoOps, HiOps;
4488 VTs.push_back(LHSL.getValueType());
4489 VTs.push_back(MVT::Flag);
4490 LoOps.push_back(LHSL);
4491 LoOps.push_back(RHSL);
4492 HiOps.push_back(LHSH);
4493 HiOps.push_back(RHSH);
4494 if (Node->getOpcode() == ISD::ADD) {
4495 Lo = DAG.getNode(ISD::ADDC, VTs, LoOps);
4496 HiOps.push_back(Lo.getValue(1));
4497 Hi = DAG.getNode(ISD::ADDE, VTs, HiOps);
4498 } else {
4499 Lo = DAG.getNode(ISD::SUBC, VTs, LoOps);
4500 HiOps.push_back(Lo.getValue(1));
4501 Hi = DAG.getNode(ISD::SUBE, VTs, HiOps);
4502 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00004503 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004504 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004505 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004506 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00004507 SDOperand LL, LH, RL, RH;
4508 ExpandOp(Node->getOperand(0), LL, LH);
4509 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00004510 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
4511 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
4512 // extended the sign bit of the low half through the upper half, and if so
4513 // emit a MULHS instead of the alternate sequence that is valid for any
4514 // i64 x i64 multiply.
4515 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
4516 // is RH an extension of the sign bit of RL?
4517 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
4518 RH.getOperand(1).getOpcode() == ISD::Constant &&
4519 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
4520 // is LH an extension of the sign bit of LL?
4521 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
4522 LH.getOperand(1).getOpcode() == ISD::Constant &&
4523 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
4524 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
4525 } else {
4526 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
4527 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
4528 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
4529 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
4530 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
4531 }
Nate Begemanadd0c632005-04-11 03:01:51 +00004532 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
4533 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00004534 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00004535 }
4536 break;
4537 }
Chris Lattneraac464e2005-01-21 06:05:23 +00004538 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
4539 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
4540 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
4541 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004542 }
4543
Chris Lattnerac12f682005-12-21 18:02:52 +00004544 // Make sure the resultant values have been legalized themselves, unless this
4545 // is a type that requires multi-step expansion.
4546 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4547 Lo = LegalizeOp(Lo);
4548 Hi = LegalizeOp(Hi);
4549 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004550
4551 // Remember in a map if the values will be reused later.
4552 bool isNew =
4553 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4554 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004555}
4556
Chris Lattner32206f52006-03-18 01:44:44 +00004557/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
4558/// two smaller values of MVT::Vector type.
4559void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
4560 SDOperand &Hi) {
4561 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
4562 SDNode *Node = Op.Val;
4563 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
4564 assert(NumElements > 1 && "Cannot split a single element vector!");
4565 unsigned NewNumElts = NumElements/2;
4566 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
4567 SDOperand TypeNode = *(Node->op_end()-1);
4568
4569 // See if we already split it.
4570 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4571 = SplitNodes.find(Op);
4572 if (I != SplitNodes.end()) {
4573 Lo = I->second.first;
4574 Hi = I->second.second;
4575 return;
4576 }
4577
4578 switch (Node->getOpcode()) {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004579 default: Node->dump(); assert(0 && "Unknown vector operation!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004580 case ISD::VBUILD_VECTOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00004581 std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts);
4582 LoOps.push_back(NewNumEltsNode);
4583 LoOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004584 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004585
4586 std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2);
4587 HiOps.push_back(NewNumEltsNode);
4588 HiOps.push_back(TypeNode);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004589 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps);
Chris Lattner32206f52006-03-18 01:44:44 +00004590 break;
4591 }
4592 case ISD::VADD:
4593 case ISD::VSUB:
4594 case ISD::VMUL:
4595 case ISD::VSDIV:
4596 case ISD::VUDIV:
4597 case ISD::VAND:
4598 case ISD::VOR:
4599 case ISD::VXOR: {
4600 SDOperand LL, LH, RL, RH;
4601 SplitVectorOp(Node->getOperand(0), LL, LH);
4602 SplitVectorOp(Node->getOperand(1), RL, RH);
4603
4604 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
4605 NewNumEltsNode, TypeNode);
4606 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
4607 NewNumEltsNode, TypeNode);
4608 break;
4609 }
4610 case ISD::VLOAD: {
4611 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4612 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
4613 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4614
4615 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4616 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
4617 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4618 getIntPtrConstant(IncrementSize));
4619 // FIXME: This creates a bogus srcvalue!
4620 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
4621
4622 // Build a factor node to remember that this load is independent of the
4623 // other one.
4624 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4625 Hi.getValue(1));
4626
4627 // Remember that we legalized the chain.
4628 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00004629 break;
4630 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00004631 case ISD::VBIT_CONVERT: {
4632 // We know the result is a vector. The input may be either a vector or a
4633 // scalar value.
4634 if (Op.getOperand(0).getValueType() != MVT::Vector) {
4635 // Lower to a store/load. FIXME: this could be improved probably.
4636 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
4637
4638 SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
4639 Op.getOperand(0), Ptr, DAG.getSrcValue(0));
4640 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
4641 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
4642 SplitVectorOp(St, Lo, Hi);
4643 } else {
4644 // If the input is a vector type, we have to either scalarize it, pack it
4645 // or convert it based on whether the input vector type is legal.
4646 SDNode *InVal = Node->getOperand(0).Val;
4647 unsigned NumElems =
4648 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4649 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4650
4651 // If the input is from a single element vector, scalarize the vector,
4652 // then treat like a scalar.
4653 if (NumElems == 1) {
4654 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
4655 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
4656 Op.getOperand(1), Op.getOperand(2));
4657 SplitVectorOp(Scalar, Lo, Hi);
4658 } else {
4659 // Split the input vector.
4660 SplitVectorOp(Op.getOperand(0), Lo, Hi);
4661
4662 // Convert each of the pieces now.
4663 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
4664 NewNumEltsNode, TypeNode);
4665 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
4666 NewNumEltsNode, TypeNode);
4667 }
4668 break;
4669 }
4670 }
Chris Lattner32206f52006-03-18 01:44:44 +00004671 }
4672
4673 // Remember in a map if the values will be reused later.
4674 bool isNew =
4675 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4676 assert(isNew && "Value already expanded?!?");
4677}
4678
4679
4680/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
4681/// equivalent operation that returns a scalar (e.g. F32) or packed value
4682/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
4683/// type for the result.
4684SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
4685 MVT::ValueType NewVT) {
4686 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
4687 SDNode *Node = Op.Val;
4688
4689 // See if we already packed it.
4690 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
4691 if (I != PackedNodes.end()) return I->second;
4692
4693 SDOperand Result;
4694 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00004695 default:
4696 Node->dump(); std::cerr << "\n";
4697 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00004698 case ISD::VADD:
4699 case ISD::VSUB:
4700 case ISD::VMUL:
4701 case ISD::VSDIV:
4702 case ISD::VUDIV:
4703 case ISD::VAND:
4704 case ISD::VOR:
4705 case ISD::VXOR:
4706 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
4707 NewVT,
4708 PackVectorOp(Node->getOperand(0), NewVT),
4709 PackVectorOp(Node->getOperand(1), NewVT));
4710 break;
4711 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00004712 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
4713 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00004714
Chris Lattner93640542006-03-19 00:07:49 +00004715 Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner32206f52006-03-18 01:44:44 +00004716
4717 // Remember that we legalized the chain.
4718 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4719 break;
4720 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004721 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00004722 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004723 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00004724 Result = Node->getOperand(0);
4725 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00004726 // Returning a BUILD_VECTOR?
Chris Lattnere1401e32006-04-08 05:34:25 +00004727
4728 // If all elements of the build_vector are undefs, return an undef.
4729 bool AllUndef = true;
4730 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
4731 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
4732 AllUndef = false;
4733 break;
4734 }
4735 if (AllUndef) {
4736 Result = DAG.getNode(ISD::UNDEF, NewVT);
4737 } else {
4738 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2);
4739 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops);
4740 }
Chris Lattner32206f52006-03-18 01:44:44 +00004741 }
4742 break;
Chris Lattner29b23012006-03-19 01:17:20 +00004743 case ISD::VINSERT_VECTOR_ELT:
4744 if (!MVT::isVector(NewVT)) {
4745 // Returning a scalar? Must be the inserted element.
4746 Result = Node->getOperand(1);
4747 } else {
4748 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
4749 PackVectorOp(Node->getOperand(0), NewVT),
4750 Node->getOperand(1), Node->getOperand(2));
4751 }
4752 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00004753 case ISD::VVECTOR_SHUFFLE:
4754 if (!MVT::isVector(NewVT)) {
4755 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
4756 SDOperand EltNum = Node->getOperand(2).getOperand(0);
4757 if (cast<ConstantSDNode>(EltNum)->getValue())
4758 Result = PackVectorOp(Node->getOperand(1), NewVT);
4759 else
4760 Result = PackVectorOp(Node->getOperand(0), NewVT);
4761 } else {
4762 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
4763 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
4764 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
4765 Node->getOperand(2).Val->op_end()-2);
4766 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
4767 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx);
4768
4769 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
4770 PackVectorOp(Node->getOperand(0), NewVT),
4771 PackVectorOp(Node->getOperand(1), NewVT), BV);
4772 }
4773 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00004774 case ISD::VBIT_CONVERT:
4775 if (Op.getOperand(0).getValueType() != MVT::Vector)
4776 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
4777 else {
4778 // If the input is a vector type, we have to either scalarize it, pack it
4779 // or convert it based on whether the input vector type is legal.
4780 SDNode *InVal = Node->getOperand(0).Val;
4781 unsigned NumElems =
4782 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
4783 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
4784
4785 // Figure out if there is a Packed type corresponding to this Vector
4786 // type. If so, convert to the packed type.
4787 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
4788 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
4789 // Turn this into a bit convert of the packed input.
4790 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4791 PackVectorOp(Node->getOperand(0), TVT));
4792 break;
4793 } else if (NumElems == 1) {
4794 // Turn this into a bit convert of the scalar input.
4795 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
4796 PackVectorOp(Node->getOperand(0), EVT));
4797 break;
4798 } else {
4799 // FIXME: UNIMP!
4800 assert(0 && "Cast from unsupported vector type not implemented yet!");
4801 }
4802 }
Evan Chengcb73b8d2006-04-10 18:54:36 +00004803 break;
Chris Lattner02274a52006-04-08 22:22:57 +00004804 case ISD::VSELECT:
4805 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
4806 PackVectorOp(Op.getOperand(1), NewVT),
4807 PackVectorOp(Op.getOperand(2), NewVT));
4808 break;
Chris Lattner32206f52006-03-18 01:44:44 +00004809 }
4810
4811 if (TLI.isTypeLegal(NewVT))
4812 Result = LegalizeOp(Result);
4813 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
4814 assert(isNew && "Value already packed?");
4815 return Result;
4816}
4817
Chris Lattnerdc750592005-01-07 07:47:09 +00004818
4819// SelectionDAG::Legalize - This is the entry point for the file.
4820//
Chris Lattner4add7e32005-01-23 04:42:50 +00004821void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00004822 if (ViewLegalizeDAGs) viewGraph();
4823
Chris Lattnerdc750592005-01-07 07:47:09 +00004824 /// run - This is the main entry point to this class.
4825 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004826 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004827}
4828