blob: d83dcc4d50b4febd1f3d7f58532bdd36bd9c650a [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-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 Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000025#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000028#include "llvm/ADT/SmallVector.h"
Evan Cheng033e6812006-03-24 01:17:21 +000029#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000030using namespace llvm;
31
Chris Lattner5e46a192006-04-02 03:07:27 +000032#ifndef NDEBUG
33static cl::opt<bool>
34ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
35 cl::desc("Pop up a window to show dags before legalize"));
36#else
37static const bool ViewLegalizeDAGs = 0;
38#endif
39
Chris Lattner3e928bb2005-01-07 07:47:09 +000040//===----------------------------------------------------------------------===//
41/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
42/// hacks on it until the target machine can handle it. This involves
43/// eliminating value sizes the machine cannot handle (promoting small sizes to
44/// large sizes or splitting up large values into small values) as well as
45/// eliminating operations the machine cannot handle.
46///
47/// This code also does a small amount of optimization and recognition of idioms
48/// as part of its processing. For example, if a target does not support a
49/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
50/// will attempt merge setcc and brc instructions into brcc's.
51///
52namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000053class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000054 TargetLowering &TLI;
55 SelectionDAG &DAG;
56
Chris Lattner6831a812006-02-13 09:18:02 +000057 // Libcall insertion helpers.
58
59 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
60 /// legalized. We use this to ensure that calls are properly serialized
61 /// against each other, including inserted libcalls.
62 SDOperand LastCALLSEQ_END;
63
64 /// IsLegalizingCall - This member is used *only* for purposes of providing
65 /// helpful assertions that a libcall isn't created while another call is
66 /// being legalized (which could lead to non-serialized call sequences).
67 bool IsLegalizingCall;
68
Chris Lattner3e928bb2005-01-07 07:47:09 +000069 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000070 Legal, // The target natively supports this operation.
71 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000072 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 };
Chris Lattner6831a812006-02-13 09:18:02 +000074
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 /// ValueTypeActions - This is a bitvector that contains two bits for each
76 /// value type, where the two bits correspond to the LegalizeAction enum.
77 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000078 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000079
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 /// LegalizedNodes - For nodes that are of legal width, and that have more
81 /// than one use, this map indicates what regularized operand to use. This
82 /// allows us to avoid legalizing the same thing more than once.
83 std::map<SDOperand, SDOperand> LegalizedNodes;
84
Chris Lattner03c85462005-01-15 05:21:40 +000085 /// PromotedNodes - For nodes that are below legal width, and that have more
86 /// than one use, this map indicates what promoted value to use. This allows
87 /// us to avoid promoting the same thing more than once.
88 std::map<SDOperand, SDOperand> PromotedNodes;
89
Chris Lattnerc7029802006-03-18 01:44:44 +000090 /// ExpandedNodes - For nodes that need to be expanded this map indicates
91 /// which which operands are the expanded version of the input. This allows
92 /// us to avoid expanding the same node more than once.
Chris Lattner3e928bb2005-01-07 07:47:09 +000093 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
94
Chris Lattnerc7029802006-03-18 01:44:44 +000095 /// SplitNodes - For vector nodes that need to be split, this map indicates
96 /// which which operands are the split version of the input. This allows us
97 /// to avoid splitting the same node more than once.
98 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
99
100 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
101 /// concrete packed types, this contains the mapping of ones we have already
102 /// processed to the result.
103 std::map<SDOperand, SDOperand> PackedNodes;
104
Chris Lattner8afc48e2005-01-07 22:28:47 +0000105 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000106 LegalizedNodes.insert(std::make_pair(From, To));
107 // If someone requests legalization of the new node, return itself.
108 if (From != To)
109 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000110 }
Chris Lattner03c85462005-01-15 05:21:40 +0000111 void AddPromotedOperand(SDOperand From, SDOperand To) {
112 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
113 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000114 // If someone requests legalization of the new node, return itself.
115 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000116 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000117
Chris Lattner3e928bb2005-01-07 07:47:09 +0000118public:
119
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000120 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122 /// getTypeAction - Return how we should legalize values of this type, either
123 /// it is already legal or we need to expand it into multiple registers of
124 /// smaller integer type, or we need to promote it to a larger type.
125 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000126 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127 }
128
129 /// isTypeLegal - Return true if this type is legal on this target.
130 ///
131 bool isTypeLegal(MVT::ValueType VT) const {
132 return getTypeAction(VT) == Legal;
133 }
134
Chris Lattner3e928bb2005-01-07 07:47:09 +0000135 void LegalizeDAG();
136
Chris Lattner456a93a2006-01-28 07:39:30 +0000137private:
Chris Lattnerc7029802006-03-18 01:44:44 +0000138 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
139 /// appropriate for its type.
140 void HandleOp(SDOperand Op);
141
142 /// LegalizeOp - We know that the specified value has a legal type.
143 /// Recursively ensure that the operands have legal types, then return the
144 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000146
147 /// PromoteOp - Given an operation that produces a value in an invalid type,
148 /// promote it to compute the value into a larger type. The produced value
149 /// will have the correct bits for the low portion of the register, but no
150 /// guarantee is made about the top bits: it may be zero, sign-extended, or
151 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000152 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000153
Chris Lattnerc7029802006-03-18 01:44:44 +0000154 /// ExpandOp - Expand the specified SDOperand into its two component pieces
155 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
156 /// the LegalizeNodes map is filled in for any results that are not expanded,
157 /// the ExpandedNodes map is filled in for any results that are expanded, and
158 /// the Lo/Hi values are returned. This applies to integer types and Vector
159 /// types.
160 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
161
162 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
163 /// two smaller values of MVT::Vector type.
164 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
165
166 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
167 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
168 /// this is called, we know that PackedVT is the right type for the result and
169 /// we know that this type is legal for the target.
170 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
171
Chris Lattner4352cc92006-04-04 17:23:26 +0000172 /// isShuffleLegal - Return true if a vector shuffle is legal with the
173 /// specified mask and type. Targets can specify exactly which masks they
174 /// support and the code generator is tasked with not creating illegal masks.
175 ///
176 /// Note that this will also return true for shuffles that are promoted to a
177 /// different type.
178 ///
179 /// If this is a legal shuffle, this method returns the (possibly promoted)
180 /// build_vector Mask. If it's not a legal shuffle, it returns null.
181 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
182
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000183 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
184 std::set<SDNode*> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000185
Nate Begeman750ac1b2006-02-01 07:19:44 +0000186 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
187
Chris Lattnerce872152006-03-19 06:31:19 +0000188 SDOperand CreateStackTemporary(MVT::ValueType VT);
189
Reid Spencer47857812006-12-31 05:55:36 +0000190 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000191 SDOperand &Hi);
192 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
193 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000194
Chris Lattner35481892005-12-23 00:16:34 +0000195 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000196 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000197 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000198 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
199 SDOperand LegalOp,
200 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000201 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
202 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000203 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
204 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000205
Chris Lattner456a93a2006-01-28 07:39:30 +0000206 SDOperand ExpandBSWAP(SDOperand Op);
207 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000208 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
209 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000210 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000212
Chris Lattner15972212006-03-31 17:55:51 +0000213 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000214 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000215
Chris Lattner3e928bb2005-01-07 07:47:09 +0000216 SDOperand getIntPtrConstant(uint64_t Val) {
217 return DAG.getConstant(Val, TLI.getPointerTy());
218 }
219};
220}
221
Chris Lattner4352cc92006-04-04 17:23:26 +0000222/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
223/// specified mask and type. Targets can specify exactly which masks they
224/// support and the code generator is tasked with not creating illegal masks.
225///
226/// Note that this will also return true for shuffles that are promoted to a
227/// different type.
228SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
229 SDOperand Mask) const {
230 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
231 default: return 0;
232 case TargetLowering::Legal:
233 case TargetLowering::Custom:
234 break;
235 case TargetLowering::Promote: {
236 // If this is promoted to a different type, convert the shuffle mask and
237 // ask if it is legal in the promoted type!
238 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
239
240 // If we changed # elements, change the shuffle mask.
241 unsigned NumEltsGrowth =
242 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
243 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
244 if (NumEltsGrowth > 1) {
245 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000246 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000247 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
248 SDOperand InOp = Mask.getOperand(i);
249 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
250 if (InOp.getOpcode() == ISD::UNDEF)
251 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
252 else {
253 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
254 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
255 }
256 }
257 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000258 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000259 }
260 VT = NVT;
261 break;
262 }
263 }
264 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
265}
266
Chris Lattnerc7029802006-03-18 01:44:44 +0000267/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
268/// specified vector opcode.
Chris Lattnerb89175f2005-11-19 05:51:46 +0000269static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000270 switch (VecOp) {
271 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3e1ce5a2006-03-03 07:01:07 +0000272 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
273 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
274 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
275 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
276 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
277 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
278 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
279 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000280 }
281}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000282
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000283SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
284 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
285 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000286 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000287 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000288}
289
Chris Lattner32fca002005-10-06 01:20:27 +0000290/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
291/// not been visited yet and if all of its operands have already been visited.
292static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
293 std::map<SDNode*, unsigned> &Visited) {
294 if (++Visited[N] != N->getNumOperands())
295 return; // Haven't visited all operands yet
296
297 Order.push_back(N);
298
299 if (N->hasOneUse()) { // Tail recurse in common case.
300 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
301 return;
302 }
303
304 // Now that we have N in, add anything that uses it if all of their operands
305 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000306 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
307 ComputeTopDownOrdering(*UI, Order, Visited);
308}
309
Chris Lattner1618beb2005-07-29 00:11:56 +0000310
Chris Lattner3e928bb2005-01-07 07:47:09 +0000311void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000312 LastCALLSEQ_END = DAG.getEntryNode();
313 IsLegalizingCall = false;
314
Chris Lattnerab510a72005-10-02 17:49:46 +0000315 // The legalize process is inherently a bottom-up recursive process (users
316 // legalize their uses before themselves). Given infinite stack space, we
317 // could just start legalizing on the root and traverse the whole graph. In
318 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000319 // blocks. To avoid this problem, compute an ordering of the nodes where each
320 // node is only legalized after all of its operands are legalized.
321 std::map<SDNode*, unsigned> Visited;
322 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000323
Chris Lattner32fca002005-10-06 01:20:27 +0000324 // Compute ordering from all of the leaves in the graphs, those (like the
325 // entry node) that have no operands.
326 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
327 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000328 if (I->getNumOperands() == 0) {
329 Visited[I] = 0 - 1U;
330 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000331 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000332 }
333
Chris Lattnerde202b32005-11-09 23:47:37 +0000334 assert(Order.size() == Visited.size() &&
335 Order.size() ==
336 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000337 "Error: DAG is cyclic!");
338 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000339
Chris Lattnerc7029802006-03-18 01:44:44 +0000340 for (unsigned i = 0, e = Order.size(); i != e; ++i)
341 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000342
343 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000344 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000345 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
346 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347
348 ExpandedNodes.clear();
349 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000350 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000351 SplitNodes.clear();
352 PackedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000353
354 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000355 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000356}
357
Chris Lattner6831a812006-02-13 09:18:02 +0000358
359/// FindCallEndFromCallStart - Given a chained node that is part of a call
360/// sequence, find the CALLSEQ_END node that terminates the call sequence.
361static SDNode *FindCallEndFromCallStart(SDNode *Node) {
362 if (Node->getOpcode() == ISD::CALLSEQ_END)
363 return Node;
364 if (Node->use_empty())
365 return 0; // No CallSeqEnd
366
367 // The chain is usually at the end.
368 SDOperand TheChain(Node, Node->getNumValues()-1);
369 if (TheChain.getValueType() != MVT::Other) {
370 // Sometimes it's at the beginning.
371 TheChain = SDOperand(Node, 0);
372 if (TheChain.getValueType() != MVT::Other) {
373 // Otherwise, hunt for it.
374 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
375 if (Node->getValueType(i) == MVT::Other) {
376 TheChain = SDOperand(Node, i);
377 break;
378 }
379
380 // Otherwise, we walked into a node without a chain.
381 if (TheChain.getValueType() != MVT::Other)
382 return 0;
383 }
384 }
385
386 for (SDNode::use_iterator UI = Node->use_begin(),
387 E = Node->use_end(); UI != E; ++UI) {
388
389 // Make sure to only follow users of our token chain.
390 SDNode *User = *UI;
391 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
392 if (User->getOperand(i) == TheChain)
393 if (SDNode *Result = FindCallEndFromCallStart(User))
394 return Result;
395 }
396 return 0;
397}
398
399/// FindCallStartFromCallEnd - Given a chained node that is part of a call
400/// sequence, find the CALLSEQ_START node that initiates the call sequence.
401static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
402 assert(Node && "Didn't find callseq_start for a call??");
403 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
404
405 assert(Node->getOperand(0).getValueType() == MVT::Other &&
406 "Node doesn't have a token chain argument!");
407 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
408}
409
410/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
411/// see if any uses can reach Dest. If no dest operands can get to dest,
412/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000413///
414/// Keep track of the nodes we fine that actually do lead to Dest in
415/// NodesLeadingTo. This avoids retraversing them exponential number of times.
416///
417bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
418 std::set<SDNode*> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000419 if (N == Dest) return true; // N certainly leads to Dest :)
420
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000421 // If we've already processed this node and it does lead to Dest, there is no
422 // need to reprocess it.
423 if (NodesLeadingTo.count(N)) return true;
424
Chris Lattner6831a812006-02-13 09:18:02 +0000425 // If the first result of this node has been already legalized, then it cannot
426 // reach N.
427 switch (getTypeAction(N->getValueType(0))) {
428 case Legal:
429 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 case Promote:
432 if (PromotedNodes.count(SDOperand(N, 0))) return false;
433 break;
434 case Expand:
435 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
436 break;
437 }
438
439 // Okay, this node has not already been legalized. Check and legalize all
440 // operands. If none lead to Dest, then we can legalize this node.
441 bool OperandsLeadToDest = false;
442 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
443 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000444 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000445
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000446 if (OperandsLeadToDest) {
447 NodesLeadingTo.insert(N);
448 return true;
449 }
Chris Lattner6831a812006-02-13 09:18:02 +0000450
451 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000452 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000453 return false;
454}
455
Chris Lattnerc7029802006-03-18 01:44:44 +0000456/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
457/// appropriate for its type.
458void SelectionDAGLegalize::HandleOp(SDOperand Op) {
459 switch (getTypeAction(Op.getValueType())) {
460 default: assert(0 && "Bad type action!");
461 case Legal: LegalizeOp(Op); break;
462 case Promote: PromoteOp(Op); break;
463 case Expand:
464 if (Op.getValueType() != MVT::Vector) {
465 SDOperand X, Y;
466 ExpandOp(Op, X, Y);
467 } else {
468 SDNode *N = Op.Val;
469 unsigned NumOps = N->getNumOperands();
470 unsigned NumElements =
471 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
472 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
473 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
474 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
475 // In the common case, this is a legal vector type, convert it to the
476 // packed operation and type now.
477 PackVectorOp(Op, PackedVT);
478 } else if (NumElements == 1) {
479 // Otherwise, if this is a single element vector, convert it to a
480 // scalar operation.
481 PackVectorOp(Op, EVT);
482 } else {
483 // Otherwise, this is a multiple element vector that isn't supported.
484 // Split it in half and legalize both parts.
485 SDOperand X, Y;
Chris Lattner4794a6b2006-03-19 00:07:49 +0000486 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000487 }
488 }
489 break;
490 }
491}
Chris Lattner6831a812006-02-13 09:18:02 +0000492
Evan Cheng9f877882006-12-13 20:57:08 +0000493/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
494/// a load from the constant pool.
495static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000496 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000497 bool Extend = false;
498
499 // If a FP immediate is precise when represented as a float and if the
500 // target can do an extending load from float to double, we put it into
501 // the constant pool as a float, even if it's is statically typed as a
502 // double.
503 MVT::ValueType VT = CFP->getValueType(0);
504 bool isDouble = VT == MVT::f64;
505 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
506 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000507 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000508 double Val = LLVMC->getValue();
509 return isDouble
510 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
511 : DAG.getConstant(FloatToBits(Val), MVT::i32);
512 }
513
Evan Cheng00495212006-12-12 21:32:44 +0000514 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
515 // Only do this if the target has a native EXTLOAD instruction from f32.
516 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
517 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
518 VT = MVT::f32;
519 Extend = true;
520 }
521
522 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
523 if (Extend) {
524 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
525 CPIdx, NULL, 0, MVT::f32);
526 } else {
527 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
528 }
529}
530
Chris Lattner6831a812006-02-13 09:18:02 +0000531
Evan Cheng912095b2007-01-04 21:56:39 +0000532/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
533/// operations.
534static
535SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
536 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000537 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000538 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
539 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000540
Evan Cheng912095b2007-01-04 21:56:39 +0000541 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000542 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000543 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
544 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000545 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000546 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000547 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000548 // Shift right or sign-extend it if the two operands have different types.
549 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
550 if (SizeDiff > 0) {
551 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
552 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
553 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
554 } else if (SizeDiff < 0)
555 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000556
557 // Clear the sign bit of first operand.
558 SDOperand Mask2 = (VT == MVT::f64)
559 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
560 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
561 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000562 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000563 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
564
565 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000566 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
567 return Result;
568}
569
570
Chris Lattnerc7029802006-03-18 01:44:44 +0000571/// LegalizeOp - We know that the specified value has a legal type.
572/// Recursively ensure that the operands have legal types, then return the
573/// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000574SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000575 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000576 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000577 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000578
Chris Lattner3e928bb2005-01-07 07:47:09 +0000579 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000580 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000581 if (Node->getNumValues() > 1) {
582 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000583 if (getTypeAction(Node->getValueType(i)) != Legal) {
584 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000585 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000586 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000587 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000588 }
589 }
590
Chris Lattner45982da2005-05-12 16:53:42 +0000591 // Note that LegalizeOp may be reentered even from single-use nodes, which
592 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000593 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
594 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000595
Nate Begeman9373a812005-08-10 20:51:12 +0000596 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000597 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000598 bool isCustom = false;
599
Chris Lattner3e928bb2005-01-07 07:47:09 +0000600 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000601 case ISD::FrameIndex:
602 case ISD::EntryToken:
603 case ISD::Register:
604 case ISD::BasicBlock:
605 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000606 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000607 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000608 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000609 case ISD::TargetConstantPool:
610 case ISD::TargetGlobalAddress:
611 case ISD::TargetExternalSymbol:
612 case ISD::VALUETYPE:
613 case ISD::SRCVALUE:
614 case ISD::STRING:
615 case ISD::CONDCODE:
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +0000616 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner948c1b12006-01-28 08:31:04 +0000617 // Primitives must all be legal.
618 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
619 "This must be legal!");
620 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000621 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000622 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
623 // If this is a target node, legalize it by legalizing the operands then
624 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000625 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000626 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000627 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000628
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000629 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000630
631 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
632 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
633 return Result.getValue(Op.ResNo);
634 }
635 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000636#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +0000637 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000638#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000639 assert(0 && "Do not know how to legalize this operator!");
640 abort();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000642 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000643 case ISD::ConstantPool:
644 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000645 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
646 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000647 case TargetLowering::Custom:
648 Tmp1 = TLI.LowerOperation(Op, DAG);
649 if (Tmp1.Val) Result = Tmp1;
650 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000651 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000652 break;
653 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000654 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000655 case ISD::AssertSext:
656 case ISD::AssertZext:
657 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000658 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000659 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000660 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000661 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000662 Result = Node->getOperand(Op.ResNo);
663 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000664 case ISD::CopyFromReg:
665 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000666 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000667 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000668 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000669 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000670 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000671 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000672 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000673 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
674 } else {
675 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
676 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000677 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
678 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000679 // Since CopyFromReg produces two values, make sure to remember that we
680 // legalized both of them.
681 AddLegalizedOperand(Op.getValue(0), Result);
682 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
683 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000684 case ISD::UNDEF: {
685 MVT::ValueType VT = Op.getValueType();
686 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000687 default: assert(0 && "This action is not supported yet!");
688 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000689 if (MVT::isInteger(VT))
690 Result = DAG.getConstant(0, VT);
691 else if (MVT::isFloatingPoint(VT))
692 Result = DAG.getConstantFP(0, VT);
693 else
694 assert(0 && "Unknown value type!");
695 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000696 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000697 break;
698 }
699 break;
700 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000701
Chris Lattner48b61a72006-03-28 00:40:33 +0000702 case ISD::INTRINSIC_W_CHAIN:
703 case ISD::INTRINSIC_WO_CHAIN:
704 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000705 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000706 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
707 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000708 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000709
710 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000711 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000712 TargetLowering::Custom) {
713 Tmp3 = TLI.LowerOperation(Result, DAG);
714 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000715 }
716
717 if (Result.Val->getNumValues() == 1) break;
718
719 // Must have return value and chain result.
720 assert(Result.Val->getNumValues() == 2 &&
721 "Cannot return more than two values!");
722
723 // Since loads produce two values, make sure to remember that we
724 // legalized both of them.
725 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
726 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
727 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000728 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000729
730 case ISD::LOCATION:
731 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
732 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
733
734 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
735 case TargetLowering::Promote:
736 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000737 case TargetLowering::Expand: {
Jim Laskeyb2efb852006-01-04 22:28:25 +0000738 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000739 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
740 bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other);
741
742 if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) {
743 const std::string &FName =
744 cast<StringSDNode>(Node->getOperand(3))->getValue();
745 const std::string &DirName =
746 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey063e7652006-01-17 17:31:53 +0000747 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000748
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000749 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000750 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000751 SDOperand LineOp = Node->getOperand(1);
752 SDOperand ColOp = Node->getOperand(2);
753
754 if (useDEBUG_LOC) {
755 Ops.push_back(LineOp); // line #
756 Ops.push_back(ColOp); // col #
757 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000758 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000759 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000760 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
761 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000762 unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile);
763 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000764 Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000765 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000766 } else {
767 Result = Tmp1; // chain
768 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000769 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000770 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000771 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000772 if (Tmp1 != Node->getOperand(0) ||
773 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000774 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000775 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000776 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
777 Ops.push_back(Node->getOperand(1)); // line # must be legal.
778 Ops.push_back(Node->getOperand(2)); // col # must be legal.
779 } else {
780 // Otherwise promote them.
781 Ops.push_back(PromoteOp(Node->getOperand(1)));
782 Ops.push_back(PromoteOp(Node->getOperand(2)));
783 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000784 Ops.push_back(Node->getOperand(3)); // filename must be legal.
785 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000786 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000787 }
788 break;
789 }
790 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000791
792 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000793 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000794 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000795 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000796 case TargetLowering::Legal:
797 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
798 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
799 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
800 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000801 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000802 break;
803 }
804 break;
805
806 case ISD::DEBUG_LABEL:
807 assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!");
808 switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000809 default: assert(0 && "This action is not supported yet!");
810 case TargetLowering::Legal:
811 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
812 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000813 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000814 break;
815 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000816 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000817
Chris Lattner3e928bb2005-01-07 07:47:09 +0000818 case ISD::Constant:
819 // We know we don't need to expand constants here, constants only have one
820 // value and we check that it is fine above.
821
822 // FIXME: Maybe we should handle things like targets that don't support full
823 // 32-bit immediates?
824 break;
825 case ISD::ConstantFP: {
826 // Spill FP immediates to the constant pool if the target cannot directly
827 // codegen them. Targets often have some immediate values that can be
828 // efficiently generated into an FP register without a load. We explicitly
829 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000830 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
831
832 // Check to see if this FP immediate is already legal.
833 bool isLegal = false;
834 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
835 E = TLI.legal_fpimm_end(); I != E; ++I)
836 if (CFP->isExactlyValue(*I)) {
837 isLegal = true;
838 break;
839 }
840
Chris Lattner3181a772006-01-29 06:26:56 +0000841 // If this is a legal constant, turn it into a TargetConstantFP node.
842 if (isLegal) {
843 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
844 break;
845 }
846
847 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
848 default: assert(0 && "This action is not supported yet!");
849 case TargetLowering::Custom:
850 Tmp3 = TLI.LowerOperation(Result, DAG);
851 if (Tmp3.Val) {
852 Result = Tmp3;
853 break;
854 }
855 // FALLTHROUGH
856 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000857 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000858 }
859 break;
860 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000861 case ISD::TokenFactor:
862 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000863 Tmp1 = LegalizeOp(Node->getOperand(0));
864 Tmp2 = LegalizeOp(Node->getOperand(1));
865 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
866 } else if (Node->getNumOperands() == 3) {
867 Tmp1 = LegalizeOp(Node->getOperand(0));
868 Tmp2 = LegalizeOp(Node->getOperand(1));
869 Tmp3 = LegalizeOp(Node->getOperand(2));
870 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000871 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000872 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000873 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000874 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
875 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000876 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000877 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000878 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000879
880 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000881 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000882 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000883 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
884 assert(Tmp3.Val && "Target didn't custom lower this node!");
885 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
886 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000887
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000888 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000889 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000890 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
891 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000892 if (Op.ResNo == i)
893 Tmp2 = Tmp1;
894 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
895 }
896 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000897
Chris Lattnerb2827b02006-03-19 00:52:58 +0000898 case ISD::BUILD_VECTOR:
899 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000900 default: assert(0 && "This action is not supported yet!");
901 case TargetLowering::Custom:
902 Tmp3 = TLI.LowerOperation(Result, DAG);
903 if (Tmp3.Val) {
904 Result = Tmp3;
905 break;
906 }
907 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000908 case TargetLowering::Expand:
909 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000910 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000911 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000912 break;
913 case ISD::INSERT_VECTOR_ELT:
914 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
915 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
916 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
917 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
918
919 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
920 Node->getValueType(0))) {
921 default: assert(0 && "This action is not supported yet!");
922 case TargetLowering::Legal:
923 break;
924 case TargetLowering::Custom:
925 Tmp3 = TLI.LowerOperation(Result, DAG);
926 if (Tmp3.Val) {
927 Result = Tmp3;
928 break;
929 }
930 // FALLTHROUGH
931 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000932 // If the insert index is a constant, codegen this as a scalar_to_vector,
933 // then a shuffle that inserts it into the right position in the vector.
934 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
935 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
936 Tmp1.getValueType(), Tmp2);
937
938 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
939 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
940 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
941
942 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
943 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
944 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000945 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +0000946 for (unsigned i = 0; i != NumElts; ++i) {
947 if (i != InsertPos->getValue())
948 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
949 else
950 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
951 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000952 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
953 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +0000954
955 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
956 Tmp1, ScVec, ShufMask);
957 Result = LegalizeOp(Result);
958 break;
959 }
960
Chris Lattner2332b9f2006-03-19 01:17:20 +0000961 // If the target doesn't support this, we have to spill the input vector
962 // to a temporary stack slot, update the element, then reload it. This is
963 // badness. We could also load the value into a vector register (either
964 // with a "move to register" or "extload into register" instruction, then
965 // permute it into place, if the idx is a constant and if the idx is
966 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000967 MVT::ValueType VT = Tmp1.getValueType();
968 MVT::ValueType EltVT = Tmp2.getValueType();
969 MVT::ValueType IdxVT = Tmp3.getValueType();
970 MVT::ValueType PtrVT = TLI.getPointerTy();
971 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000972 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000973 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000974
975 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000976 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
977 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000978 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000979 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
980 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
981 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000982 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000983 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000984 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +0000985 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +0000986 break;
987 }
988 }
989 break;
Chris Lattnerce872152006-03-19 06:31:19 +0000990 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +0000991 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
992 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
993 break;
994 }
995
Chris Lattnerce872152006-03-19 06:31:19 +0000996 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
997 Result = DAG.UpdateNodeOperands(Result, Tmp1);
998 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
999 Node->getValueType(0))) {
1000 default: assert(0 && "This action is not supported yet!");
1001 case TargetLowering::Legal:
1002 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001003 case TargetLowering::Custom:
1004 Tmp3 = TLI.LowerOperation(Result, DAG);
1005 if (Tmp3.Val) {
1006 Result = Tmp3;
1007 break;
1008 }
1009 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001010 case TargetLowering::Expand:
1011 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001012 break;
1013 }
Chris Lattnerce872152006-03-19 06:31:19 +00001014 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001015 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001016 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1017 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1018 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1019
1020 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001021 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1022 default: assert(0 && "Unknown operation action!");
1023 case TargetLowering::Legal:
1024 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1025 "vector shuffle should not be created if not legal!");
1026 break;
1027 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001028 Tmp3 = TLI.LowerOperation(Result, DAG);
1029 if (Tmp3.Val) {
1030 Result = Tmp3;
1031 break;
1032 }
1033 // FALLTHROUGH
1034 case TargetLowering::Expand: {
1035 MVT::ValueType VT = Node->getValueType(0);
1036 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1037 MVT::ValueType PtrVT = TLI.getPointerTy();
1038 SDOperand Mask = Node->getOperand(2);
1039 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001040 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001041 for (unsigned i = 0; i != NumElems; ++i) {
1042 SDOperand Arg = Mask.getOperand(i);
1043 if (Arg.getOpcode() == ISD::UNDEF) {
1044 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1045 } else {
1046 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1047 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1048 if (Idx < NumElems)
1049 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1050 DAG.getConstant(Idx, PtrVT)));
1051 else
1052 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1053 DAG.getConstant(Idx - NumElems, PtrVT)));
1054 }
1055 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001056 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001057 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001058 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001059 case TargetLowering::Promote: {
1060 // Change base type to a different vector type.
1061 MVT::ValueType OVT = Node->getValueType(0);
1062 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1063
1064 // Cast the two input vectors.
1065 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1066 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1067
1068 // Convert the shuffle mask to the right # elements.
1069 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1070 assert(Tmp3.Val && "Shuffle not legal?");
1071 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1072 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1073 break;
1074 }
Chris Lattner87100e02006-03-20 01:52:29 +00001075 }
1076 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001077
1078 case ISD::EXTRACT_VECTOR_ELT:
1079 Tmp1 = LegalizeOp(Node->getOperand(0));
1080 Tmp2 = LegalizeOp(Node->getOperand(1));
1081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001082
1083 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1084 Tmp1.getValueType())) {
1085 default: assert(0 && "This action is not supported yet!");
1086 case TargetLowering::Legal:
1087 break;
1088 case TargetLowering::Custom:
1089 Tmp3 = TLI.LowerOperation(Result, DAG);
1090 if (Tmp3.Val) {
1091 Result = Tmp3;
1092 break;
1093 }
1094 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001095 case TargetLowering::Expand:
1096 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001097 break;
1098 }
Chris Lattner384504c2006-03-21 20:44:12 +00001099 break;
1100
Chris Lattner15972212006-03-31 17:55:51 +00001101 case ISD::VEXTRACT_VECTOR_ELT:
1102 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001103 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001104
Chris Lattner6831a812006-02-13 09:18:02 +00001105 case ISD::CALLSEQ_START: {
1106 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1107
1108 // Recursively Legalize all of the inputs of the call end that do not lead
1109 // to this call start. This ensures that any libcalls that need be inserted
1110 // are inserted *before* the CALLSEQ_START.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001111 {std::set<SDNode*> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001112 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001113 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1114 NodesLeadingTo);
1115 }
Chris Lattner6831a812006-02-13 09:18:02 +00001116
1117 // Now that we legalized all of the inputs (which may have inserted
1118 // libcalls) create the new CALLSEQ_START node.
1119 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1120
1121 // Merge in the last call, to ensure that this call start after the last
1122 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001123 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001124 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1125 Tmp1 = LegalizeOp(Tmp1);
1126 }
Chris Lattner6831a812006-02-13 09:18:02 +00001127
1128 // Do not try to legalize the target-specific arguments (#1+).
1129 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001130 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001131 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001132 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001133 }
1134
1135 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001136 AddLegalizedOperand(Op.getValue(0), Result);
1137 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1138 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1139
Chris Lattner6831a812006-02-13 09:18:02 +00001140 // Now that the callseq_start and all of the non-call nodes above this call
1141 // sequence have been legalized, legalize the call itself. During this
1142 // process, no libcalls can/will be inserted, guaranteeing that no calls
1143 // can overlap.
1144 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1145 SDOperand InCallSEQ = LastCALLSEQ_END;
1146 // Note that we are selecting this call!
1147 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1148 IsLegalizingCall = true;
1149
1150 // Legalize the call, starting from the CALLSEQ_END.
1151 LegalizeOp(LastCALLSEQ_END);
1152 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1153 return Result;
1154 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001155 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001156 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1157 // will cause this node to be legalized as well as handling libcalls right.
1158 if (LastCALLSEQ_END.Val != Node) {
1159 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1160 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1161 assert(I != LegalizedNodes.end() &&
1162 "Legalizing the call start should have legalized this node!");
1163 return I->second;
1164 }
1165
1166 // Otherwise, the call start has been legalized and everything is going
1167 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001168 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001169 // Do not try to legalize the target-specific arguments (#1+), except for
1170 // an optional flag input.
1171 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1172 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001173 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001174 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001175 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001176 }
1177 } else {
1178 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1179 if (Tmp1 != Node->getOperand(0) ||
1180 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001181 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001182 Ops[0] = Tmp1;
1183 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001184 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001185 }
Chris Lattner6a542892006-01-24 05:48:21 +00001186 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001187 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001188 // This finishes up call legalization.
1189 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001190
1191 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1192 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1193 if (Node->getNumValues() == 2)
1194 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1195 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001196 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001197 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1198 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1199 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001200 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001201
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001202 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001203 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001204 switch (TLI.getOperationAction(Node->getOpcode(),
1205 Node->getValueType(0))) {
1206 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001207 case TargetLowering::Expand: {
1208 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1209 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1210 " not tell us which reg is the stack pointer!");
1211 SDOperand Chain = Tmp1.getOperand(0);
1212 SDOperand Size = Tmp2.getOperand(1);
1213 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1214 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1215 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001216 Tmp1 = LegalizeOp(Tmp1);
1217 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001218 break;
1219 }
1220 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001221 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1222 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001223 Tmp1 = LegalizeOp(Tmp3);
1224 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001225 }
Chris Lattner903d2782006-01-15 08:54:32 +00001226 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001227 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001228 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001229 }
Chris Lattner903d2782006-01-15 08:54:32 +00001230 // Since this op produce two values, make sure to remember that we
1231 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001232 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1233 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001234 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001235 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001236 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001237 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001238 bool Changed = false;
1239 // Legalize all of the operands of the inline asm, in case they are nodes
1240 // that need to be expanded or something. Note we skip the asm string and
1241 // all of the TargetConstant flags.
1242 SDOperand Op = LegalizeOp(Ops[0]);
1243 Changed = Op != Ops[0];
1244 Ops[0] = Op;
1245
1246 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1247 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1248 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1249 for (++i; NumVals; ++i, --NumVals) {
1250 SDOperand Op = LegalizeOp(Ops[i]);
1251 if (Op != Ops[i]) {
1252 Changed = true;
1253 Ops[i] = Op;
1254 }
1255 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001256 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001257
1258 if (HasInFlag) {
1259 Op = LegalizeOp(Ops.back());
1260 Changed |= Op != Ops.back();
1261 Ops.back() = Op;
1262 }
1263
1264 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001265 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001266
1267 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001268 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001269 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1270 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001271 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001272 case ISD::BR:
1273 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001274 // Ensure that libcalls are emitted before a branch.
1275 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1276 Tmp1 = LegalizeOp(Tmp1);
1277 LastCALLSEQ_END = DAG.getEntryNode();
1278
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001279 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001280 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001281 case ISD::BRIND:
1282 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1283 // Ensure that libcalls are emitted before a branch.
1284 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1285 Tmp1 = LegalizeOp(Tmp1);
1286 LastCALLSEQ_END = DAG.getEntryNode();
1287
1288 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1289 default: assert(0 && "Indirect target must be legal type (pointer)!");
1290 case Legal:
1291 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1292 break;
1293 }
1294 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1295 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001296 case ISD::BR_JT:
1297 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1298 // Ensure that libcalls are emitted before a branch.
1299 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1300 Tmp1 = LegalizeOp(Tmp1);
1301 LastCALLSEQ_END = DAG.getEntryNode();
1302
1303 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1305
1306 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1307 default: assert(0 && "This action is not supported yet!");
1308 case TargetLowering::Legal: break;
1309 case TargetLowering::Custom:
1310 Tmp1 = TLI.LowerOperation(Result, DAG);
1311 if (Tmp1.Val) Result = Tmp1;
1312 break;
1313 case TargetLowering::Expand: {
1314 SDOperand Chain = Result.getOperand(0);
1315 SDOperand Table = Result.getOperand(1);
1316 SDOperand Index = Result.getOperand(2);
1317
1318 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001319 MachineFunction &MF = DAG.getMachineFunction();
1320 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001321 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1322 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001323
1324 SDOperand LD;
1325 switch (EntrySize) {
1326 default: assert(0 && "Size of jump table not supported yet."); break;
1327 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1328 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1329 }
1330
1331 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001332 // For PIC, the sequence is:
1333 // BRIND(load(Jumptable + index) + RelocBase)
1334 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1335 SDOperand Reloc;
1336 if (TLI.usesGlobalOffsetTable())
1337 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1338 else
1339 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001340 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001341 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1342 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1343 } else {
1344 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1345 }
1346 }
1347 }
1348 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001349 case ISD::BRCOND:
1350 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001351 // Ensure that libcalls are emitted before a return.
1352 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1353 Tmp1 = LegalizeOp(Tmp1);
1354 LastCALLSEQ_END = DAG.getEntryNode();
1355
Chris Lattner47e92232005-01-18 19:27:06 +00001356 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1357 case Expand: assert(0 && "It's impossible to expand bools");
1358 case Legal:
1359 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1360 break;
1361 case Promote:
1362 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001363
1364 // The top bits of the promoted condition are not necessarily zero, ensure
1365 // that the value is properly zero extended.
1366 if (!TLI.MaskedValueIsZero(Tmp2,
1367 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1368 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001369 break;
1370 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001371
1372 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001373 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001374
1375 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1376 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001377 case TargetLowering::Legal: break;
1378 case TargetLowering::Custom:
1379 Tmp1 = TLI.LowerOperation(Result, DAG);
1380 if (Tmp1.Val) Result = Tmp1;
1381 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001382 case TargetLowering::Expand:
1383 // Expand brcond's setcc into its constituent parts and create a BR_CC
1384 // Node.
1385 if (Tmp2.getOpcode() == ISD::SETCC) {
1386 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1387 Tmp2.getOperand(0), Tmp2.getOperand(1),
1388 Node->getOperand(2));
1389 } else {
1390 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1391 DAG.getCondCode(ISD::SETNE), Tmp2,
1392 DAG.getConstant(0, Tmp2.getValueType()),
1393 Node->getOperand(2));
1394 }
1395 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001396 }
1397 break;
1398 case ISD::BR_CC:
1399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001400 // Ensure that libcalls are emitted before a branch.
1401 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1402 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001403 Tmp2 = Node->getOperand(2); // LHS
1404 Tmp3 = Node->getOperand(3); // RHS
1405 Tmp4 = Node->getOperand(1); // CC
1406
1407 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001408 LastCALLSEQ_END = DAG.getEntryNode();
1409
Nate Begeman750ac1b2006-02-01 07:19:44 +00001410 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1411 // the LHS is a legal SETCC itself. In this case, we need to compare
1412 // the result against zero to select between true and false values.
1413 if (Tmp3.Val == 0) {
1414 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1415 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001416 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001417
1418 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1419 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001420
Chris Lattner181b7a32005-12-17 23:46:46 +00001421 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1422 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001423 case TargetLowering::Legal: break;
1424 case TargetLowering::Custom:
1425 Tmp4 = TLI.LowerOperation(Result, DAG);
1426 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001427 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001428 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001429 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001430 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001431 LoadSDNode *LD = cast<LoadSDNode>(Node);
1432 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1433 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001434
Evan Cheng466685d2006-10-09 20:57:25 +00001435 ISD::LoadExtType ExtType = LD->getExtensionType();
1436 if (ExtType == ISD::NON_EXTLOAD) {
1437 MVT::ValueType VT = Node->getValueType(0);
1438 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1439 Tmp3 = Result.getValue(0);
1440 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001441
Evan Cheng466685d2006-10-09 20:57:25 +00001442 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1443 default: assert(0 && "This action is not supported yet!");
1444 case TargetLowering::Legal: break;
1445 case TargetLowering::Custom:
1446 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1447 if (Tmp1.Val) {
1448 Tmp3 = LegalizeOp(Tmp1);
1449 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001450 }
Evan Cheng466685d2006-10-09 20:57:25 +00001451 break;
1452 case TargetLowering::Promote: {
1453 // Only promote a load of vector type to another.
1454 assert(MVT::isVector(VT) && "Cannot promote this load!");
1455 // Change base type to a different vector type.
1456 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1457
1458 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1459 LD->getSrcValueOffset());
1460 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1461 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001462 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001463 }
Evan Cheng466685d2006-10-09 20:57:25 +00001464 }
1465 // Since loads produce two values, make sure to remember that we
1466 // legalized both of them.
1467 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1468 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1469 return Op.ResNo ? Tmp4 : Tmp3;
1470 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001471 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001472 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1473 default: assert(0 && "This action is not supported yet!");
1474 case TargetLowering::Promote:
1475 assert(SrcVT == MVT::i1 &&
1476 "Can only promote extending LOAD from i1 -> i8!");
1477 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1478 LD->getSrcValue(), LD->getSrcValueOffset(),
1479 MVT::i8);
1480 Tmp1 = Result.getValue(0);
1481 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001482 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001483 case TargetLowering::Custom:
1484 isCustom = true;
1485 // FALLTHROUGH
1486 case TargetLowering::Legal:
1487 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1488 Tmp1 = Result.getValue(0);
1489 Tmp2 = Result.getValue(1);
1490
1491 if (isCustom) {
1492 Tmp3 = TLI.LowerOperation(Result, DAG);
1493 if (Tmp3.Val) {
1494 Tmp1 = LegalizeOp(Tmp3);
1495 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1496 }
1497 }
1498 break;
1499 case TargetLowering::Expand:
1500 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1501 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1502 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1503 LD->getSrcValueOffset());
1504 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1505 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1506 Tmp2 = LegalizeOp(Load.getValue(1));
1507 break;
1508 }
1509 assert(ExtType != ISD::EXTLOAD && "EXTLOAD should always be supported!");
1510 // Turn the unsupported load into an EXTLOAD followed by an explicit
1511 // zero/sign extend inreg.
1512 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1513 Tmp1, Tmp2, LD->getSrcValue(),
1514 LD->getSrcValueOffset(), SrcVT);
1515 SDOperand ValRes;
1516 if (ExtType == ISD::SEXTLOAD)
1517 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1518 Result, DAG.getValueType(SrcVT));
1519 else
1520 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1521 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1522 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1523 break;
1524 }
1525 // Since loads produce two values, make sure to remember that we legalized
1526 // both of them.
1527 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1528 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1529 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001530 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001531 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001532 case ISD::EXTRACT_ELEMENT: {
1533 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1534 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001535 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001536 case Legal:
1537 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1538 // 1 -> Hi
1539 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1540 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1541 TLI.getShiftAmountTy()));
1542 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1543 } else {
1544 // 0 -> Lo
1545 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1546 Node->getOperand(0));
1547 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001548 break;
1549 case Expand:
1550 // Get both the low and high parts.
1551 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1552 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1553 Result = Tmp2; // 1 -> Hi
1554 else
1555 Result = Tmp1; // 0 -> Lo
1556 break;
1557 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001558 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001559 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001560
1561 case ISD::CopyToReg:
1562 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001563
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001564 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001565 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001566 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001567 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001568 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001569 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001570 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001571 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001572 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001573 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001574 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1575 Tmp3);
1576 } else {
1577 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001578 }
1579
1580 // Since this produces two values, make sure to remember that we legalized
1581 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001582 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001583 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001584 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001585 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001586 break;
1587
1588 case ISD::RET:
1589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001590
1591 // Ensure that libcalls are emitted before a return.
1592 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1593 Tmp1 = LegalizeOp(Tmp1);
1594 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001595
Chris Lattner3e928bb2005-01-07 07:47:09 +00001596 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001597 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001598 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001599 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001600 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001601 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001602 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001603 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001604 case Expand:
1605 if (Tmp2.getValueType() != MVT::Vector) {
1606 SDOperand Lo, Hi;
1607 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng13acce32006-12-11 19:27:14 +00001608 if (Hi.Val)
1609 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1610 else
1611 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001612 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001613 } else {
1614 SDNode *InVal = Tmp2.Val;
1615 unsigned NumElems =
1616 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1617 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1618
1619 // Figure out if there is a Packed type corresponding to this Vector
1620 // type. If so, convert to the packed type.
1621 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1622 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1623 // Turn this into a return of the packed type.
1624 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001625 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001626 } else if (NumElems == 1) {
1627 // Turn this into a return of the scalar type.
1628 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001629 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001630
1631 // FIXME: Returns of gcc generic vectors smaller than a legal type
1632 // should be returned in integer registers!
1633
Chris Lattnerf87324e2006-04-11 01:31:51 +00001634 // The scalarized value type may not be legal, e.g. it might require
1635 // promotion or expansion. Relegalize the return.
1636 Result = LegalizeOp(Result);
1637 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001638 // FIXME: Returns of gcc generic vectors larger than a legal vector
1639 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001640 SDOperand Lo, Hi;
1641 SplitVectorOp(Tmp2, Lo, Hi);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001642 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001643 Result = LegalizeOp(Result);
1644 }
1645 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001646 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001647 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001648 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001650 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001651 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001652 }
1653 break;
1654 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001655 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001656 break;
1657 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001658 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001659 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001660 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001661 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1662 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001663 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001664 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001665 break;
1666 case Expand: {
1667 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001668 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1669 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001670 ExpandOp(Node->getOperand(i), Lo, Hi);
1671 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001672 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001673 if (Hi.Val) {
1674 NewValues.push_back(Hi);
1675 NewValues.push_back(Node->getOperand(i+1));
1676 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001677 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001678 }
1679 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001680 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001681 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001682
1683 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001684 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001685 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001686 Result = DAG.getNode(ISD::RET, MVT::Other,
1687 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001688 break;
1689 }
1690 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001691
Chris Lattner6862dbc2006-01-29 21:02:23 +00001692 if (Result.getOpcode() == ISD::RET) {
1693 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1694 default: assert(0 && "This action is not supported yet!");
1695 case TargetLowering::Legal: break;
1696 case TargetLowering::Custom:
1697 Tmp1 = TLI.LowerOperation(Result, DAG);
1698 if (Tmp1.Val) Result = Tmp1;
1699 break;
1700 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001701 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001702 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001703 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001704 StoreSDNode *ST = cast<StoreSDNode>(Node);
1705 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1706 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001707
Evan Cheng8b2794a2006-10-13 21:14:26 +00001708 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001709 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1710 // FIXME: We shouldn't do this for TargetConstantFP's.
1711 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1712 // to phase ordering between legalized code and the dag combiner. This
1713 // probably means that we need to integrate dag combiner and legalizer
1714 // together.
1715 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1716 if (CFP->getValueType(0) == MVT::f32) {
1717 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1718 } else {
1719 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1720 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1721 }
1722 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1723 ST->getSrcValueOffset());
1724 break;
1725 }
1726
Evan Cheng8b2794a2006-10-13 21:14:26 +00001727 switch (getTypeAction(ST->getStoredVT())) {
1728 case Legal: {
1729 Tmp3 = LegalizeOp(ST->getValue());
1730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1731 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001732
Evan Cheng8b2794a2006-10-13 21:14:26 +00001733 MVT::ValueType VT = Tmp3.getValueType();
1734 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1735 default: assert(0 && "This action is not supported yet!");
1736 case TargetLowering::Legal: break;
1737 case TargetLowering::Custom:
1738 Tmp1 = TLI.LowerOperation(Result, DAG);
1739 if (Tmp1.Val) Result = Tmp1;
1740 break;
1741 case TargetLowering::Promote:
1742 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1743 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1744 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1745 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1746 ST->getSrcValue(), ST->getSrcValueOffset());
1747 break;
1748 }
1749 break;
1750 }
1751 case Promote:
1752 // Truncate the value and store the result.
1753 Tmp3 = PromoteOp(ST->getValue());
1754 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1755 ST->getSrcValueOffset(), ST->getStoredVT());
1756 break;
1757
1758 case Expand:
1759 unsigned IncrementSize = 0;
1760 SDOperand Lo, Hi;
1761
1762 // If this is a vector type, then we have to calculate the increment as
1763 // the product of the element size in bytes, and the number of elements
1764 // in the high half of the vector.
1765 if (ST->getValue().getValueType() == MVT::Vector) {
1766 SDNode *InVal = ST->getValue().Val;
1767 unsigned NumElems =
1768 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1769 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1770
1771 // Figure out if there is a Packed type corresponding to this Vector
1772 // type. If so, convert to the packed type.
1773 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1774 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1775 // Turn this into a normal store of the packed type.
1776 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1777 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1778 ST->getSrcValueOffset());
1779 Result = LegalizeOp(Result);
1780 break;
1781 } else if (NumElems == 1) {
1782 // Turn this into a normal store of the scalar type.
1783 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1784 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1785 ST->getSrcValueOffset());
1786 // The scalarized value type may not be legal, e.g. it might require
1787 // promotion or expansion. Relegalize the scalar store.
1788 Result = LegalizeOp(Result);
1789 break;
1790 } else {
1791 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1792 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1793 }
1794 } else {
1795 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001796 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001797
1798 if (!TLI.isLittleEndian())
1799 std::swap(Lo, Hi);
1800 }
1801
1802 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1803 ST->getSrcValueOffset());
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001804
1805 if (Hi.Val == NULL) {
1806 // Must be int <-> float one-to-one expansion.
1807 Result = Lo;
1808 break;
1809 }
1810
Evan Cheng8b2794a2006-10-13 21:14:26 +00001811 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1812 getIntPtrConstant(IncrementSize));
1813 assert(isTypeLegal(Tmp2.getValueType()) &&
1814 "Pointers must be legal!");
1815 // FIXME: This sets the srcvalue of both halves to be the same, which is
1816 // wrong.
1817 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1818 ST->getSrcValueOffset());
1819 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1820 break;
1821 }
1822 } else {
1823 // Truncating store
1824 assert(isTypeLegal(ST->getValue().getValueType()) &&
1825 "Cannot handle illegal TRUNCSTORE yet!");
1826 Tmp3 = LegalizeOp(ST->getValue());
1827
1828 // The only promote case we handle is TRUNCSTORE:i1 X into
1829 // -> TRUNCSTORE:i8 (and X, 1)
1830 if (ST->getStoredVT() == MVT::i1 &&
1831 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1832 // Promote the bool to a mask then store.
1833 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1834 DAG.getConstant(1, Tmp3.getValueType()));
1835 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1836 ST->getSrcValueOffset(), MVT::i8);
1837 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1838 Tmp2 != ST->getBasePtr()) {
1839 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1840 ST->getOffset());
1841 }
1842
1843 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1844 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001845 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001846 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001847 case TargetLowering::Custom:
1848 Tmp1 = TLI.LowerOperation(Result, DAG);
1849 if (Tmp1.Val) Result = Tmp1;
1850 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001851 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001852 }
1853 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001854 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001855 case ISD::PCMARKER:
1856 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001858 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001859 case ISD::STACKSAVE:
1860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001861 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1862 Tmp1 = Result.getValue(0);
1863 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001864
Chris Lattner140d53c2006-01-13 02:50:02 +00001865 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1866 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001867 case TargetLowering::Legal: break;
1868 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001869 Tmp3 = TLI.LowerOperation(Result, DAG);
1870 if (Tmp3.Val) {
1871 Tmp1 = LegalizeOp(Tmp3);
1872 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001873 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001874 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001875 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001876 // Expand to CopyFromReg if the target set
1877 // StackPointerRegisterToSaveRestore.
1878 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001879 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001880 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001881 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001882 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001883 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1884 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001885 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001886 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001887 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001888
1889 // Since stacksave produce two values, make sure to remember that we
1890 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001891 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1892 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1893 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001894
Chris Lattner140d53c2006-01-13 02:50:02 +00001895 case ISD::STACKRESTORE:
1896 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001899
1900 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1901 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001902 case TargetLowering::Legal: break;
1903 case TargetLowering::Custom:
1904 Tmp1 = TLI.LowerOperation(Result, DAG);
1905 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001906 break;
1907 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001908 // Expand to CopyToReg if the target set
1909 // StackPointerRegisterToSaveRestore.
1910 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1911 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1912 } else {
1913 Result = Tmp1;
1914 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001915 break;
1916 }
1917 break;
1918
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001919 case ISD::READCYCLECOUNTER:
1920 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001921 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00001922 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1923 Node->getValueType(0))) {
1924 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001925 case TargetLowering::Legal:
1926 Tmp1 = Result.getValue(0);
1927 Tmp2 = Result.getValue(1);
1928 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00001929 case TargetLowering::Custom:
1930 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001931 Tmp1 = LegalizeOp(Result.getValue(0));
1932 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00001933 break;
1934 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001935
1936 // Since rdcc produce two values, make sure to remember that we legalized
1937 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001938 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1939 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001940 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001941
Chris Lattner2ee743f2005-01-14 22:08:15 +00001942 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001943 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1944 case Expand: assert(0 && "It's impossible to expand bools");
1945 case Legal:
1946 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1947 break;
1948 case Promote:
1949 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00001950 // Make sure the condition is either zero or one.
1951 if (!TLI.MaskedValueIsZero(Tmp1,
1952 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
1953 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001954 break;
1955 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001956 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001957 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001958
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001959 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001960
Nate Begemanb942a3d2005-08-23 04:29:48 +00001961 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001962 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001963 case TargetLowering::Legal: break;
1964 case TargetLowering::Custom: {
1965 Tmp1 = TLI.LowerOperation(Result, DAG);
1966 if (Tmp1.Val) Result = Tmp1;
1967 break;
1968 }
Nate Begeman9373a812005-08-10 20:51:12 +00001969 case TargetLowering::Expand:
1970 if (Tmp1.getOpcode() == ISD::SETCC) {
1971 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1972 Tmp2, Tmp3,
1973 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1974 } else {
1975 Result = DAG.getSelectCC(Tmp1,
1976 DAG.getConstant(0, Tmp1.getValueType()),
1977 Tmp2, Tmp3, ISD::SETNE);
1978 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001979 break;
1980 case TargetLowering::Promote: {
1981 MVT::ValueType NVT =
1982 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1983 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001984 if (MVT::isVector(Tmp2.getValueType())) {
1985 ExtOp = ISD::BIT_CONVERT;
1986 TruncOp = ISD::BIT_CONVERT;
1987 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001988 ExtOp = ISD::ANY_EXTEND;
1989 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001990 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00001991 ExtOp = ISD::FP_EXTEND;
1992 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001993 }
1994 // Promote each of the values to the new type.
1995 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1996 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1997 // Perform the larger operation, then round down.
1998 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1999 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2000 break;
2001 }
2002 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002003 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002004 case ISD::SELECT_CC: {
2005 Tmp1 = Node->getOperand(0); // LHS
2006 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002007 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2008 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002009 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002010
Nate Begeman750ac1b2006-02-01 07:19:44 +00002011 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2012
2013 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2014 // the LHS is a legal SETCC itself. In this case, we need to compare
2015 // the result against zero to select between true and false values.
2016 if (Tmp2.Val == 0) {
2017 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2018 CC = DAG.getCondCode(ISD::SETNE);
2019 }
2020 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2021
2022 // Everything is legal, see if we should expand this op or something.
2023 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2024 default: assert(0 && "This action is not supported yet!");
2025 case TargetLowering::Legal: break;
2026 case TargetLowering::Custom:
2027 Tmp1 = TLI.LowerOperation(Result, DAG);
2028 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002029 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002030 }
2031 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002032 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002033 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002034 Tmp1 = Node->getOperand(0);
2035 Tmp2 = Node->getOperand(1);
2036 Tmp3 = Node->getOperand(2);
2037 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2038
2039 // If we had to Expand the SetCC operands into a SELECT node, then it may
2040 // not always be possible to return a true LHS & RHS. In this case, just
2041 // return the value we legalized, returned in the LHS
2042 if (Tmp2.Val == 0) {
2043 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002044 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002045 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002046
Chris Lattner73e142f2006-01-30 22:43:50 +00002047 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002048 default: assert(0 && "Cannot handle this action for SETCC yet!");
2049 case TargetLowering::Custom:
2050 isCustom = true;
2051 // FALLTHROUGH.
2052 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002053 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002054 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002055 Tmp4 = TLI.LowerOperation(Result, DAG);
2056 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002057 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002058 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002059 case TargetLowering::Promote: {
2060 // First step, figure out the appropriate operation to use.
2061 // Allow SETCC to not be supported for all legal data types
2062 // Mostly this targets FP
2063 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2064 MVT::ValueType OldVT = NewInTy;
2065
2066 // Scan for the appropriate larger type to use.
2067 while (1) {
2068 NewInTy = (MVT::ValueType)(NewInTy+1);
2069
2070 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2071 "Fell off of the edge of the integer world");
2072 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2073 "Fell off of the edge of the floating point world");
2074
2075 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002076 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002077 break;
2078 }
2079 if (MVT::isInteger(NewInTy))
2080 assert(0 && "Cannot promote Legal Integer SETCC yet");
2081 else {
2082 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2083 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2084 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002085 Tmp1 = LegalizeOp(Tmp1);
2086 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002088 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002089 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002090 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002091 case TargetLowering::Expand:
2092 // Expand a setcc node into a select_cc of the same condition, lhs, and
2093 // rhs that selects between const 1 (true) and const 0 (false).
2094 MVT::ValueType VT = Node->getValueType(0);
2095 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2096 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002097 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002098 break;
2099 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002100 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002101 case ISD::MEMSET:
2102 case ISD::MEMCPY:
2103 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002104 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002105 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2106
2107 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2108 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2109 case Expand: assert(0 && "Cannot expand a byte!");
2110 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002111 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002112 break;
2113 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002114 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002115 break;
2116 }
2117 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002118 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002119 }
Chris Lattner272455b2005-02-02 03:44:41 +00002120
2121 SDOperand Tmp4;
2122 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002123 case Expand: {
2124 // Length is too big, just take the lo-part of the length.
2125 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002126 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002127 break;
2128 }
Chris Lattnere5605212005-01-28 22:29:18 +00002129 case Legal:
2130 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002131 break;
2132 case Promote:
2133 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002134 break;
2135 }
2136
2137 SDOperand Tmp5;
2138 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2139 case Expand: assert(0 && "Cannot expand this yet!");
2140 case Legal:
2141 Tmp5 = LegalizeOp(Node->getOperand(4));
2142 break;
2143 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002144 Tmp5 = PromoteOp(Node->getOperand(4));
2145 break;
2146 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002147
2148 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2149 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002150 case TargetLowering::Custom:
2151 isCustom = true;
2152 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002153 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002155 if (isCustom) {
2156 Tmp1 = TLI.LowerOperation(Result, DAG);
2157 if (Tmp1.Val) Result = Tmp1;
2158 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002159 break;
2160 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002161 // Otherwise, the target does not support this operation. Lower the
2162 // operation to an explicit libcall as appropriate.
2163 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002164 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002165 TargetLowering::ArgListTy Args;
2166 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002167
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002168 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002169 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002170 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002171 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002172 // Extend the (previously legalized) ubyte argument to be an int value
2173 // for the call.
2174 if (Tmp3.getValueType() > MVT::i32)
2175 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2176 else
2177 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencer47857812006-12-31 05:55:36 +00002178 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
2179 Args.push_back(Entry);
2180 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2181 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002182
2183 FnName = "memset";
2184 } else if (Node->getOpcode() == ISD::MEMCPY ||
2185 Node->getOpcode() == ISD::MEMMOVE) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002186 Entry.Ty = IntPtrTy; Entry.isSigned = false;
2187 Entry.Node = Tmp2; Args.push_back(Entry);
2188 Entry.Node = Tmp3; Args.push_back(Entry);
2189 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002190 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2191 } else {
2192 assert(0 && "Unknown op!");
2193 }
Chris Lattner45982da2005-05-12 16:53:42 +00002194
Chris Lattnere1bd8222005-01-11 05:57:22 +00002195 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002196 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002197 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002198 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002199 break;
2200 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002201 }
2202 break;
2203 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002204
Chris Lattner5b359c62005-04-02 04:00:59 +00002205 case ISD::SHL_PARTS:
2206 case ISD::SRA_PARTS:
2207 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002208 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002209 bool Changed = false;
2210 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2211 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2212 Changed |= Ops.back() != Node->getOperand(i);
2213 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002214 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002215 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002216
Evan Cheng05a2d562006-01-09 18:31:59 +00002217 switch (TLI.getOperationAction(Node->getOpcode(),
2218 Node->getValueType(0))) {
2219 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002220 case TargetLowering::Legal: break;
2221 case TargetLowering::Custom:
2222 Tmp1 = TLI.LowerOperation(Result, DAG);
2223 if (Tmp1.Val) {
2224 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002225 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002226 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002227 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2228 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002229 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002230 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002231 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002232 return RetVal;
2233 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002234 break;
2235 }
2236
Chris Lattner2c8086f2005-04-02 05:00:07 +00002237 // Since these produce multiple values, make sure to remember that we
2238 // legalized all of them.
2239 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2240 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2241 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002242 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002243
2244 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002245 case ISD::ADD:
2246 case ISD::SUB:
2247 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002248 case ISD::MULHS:
2249 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002250 case ISD::UDIV:
2251 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002252 case ISD::AND:
2253 case ISD::OR:
2254 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002255 case ISD::SHL:
2256 case ISD::SRL:
2257 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002258 case ISD::FADD:
2259 case ISD::FSUB:
2260 case ISD::FMUL:
2261 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002262 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002263 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2264 case Expand: assert(0 && "Not possible");
2265 case Legal:
2266 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2267 break;
2268 case Promote:
2269 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2270 break;
2271 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002272
2273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002274
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002275 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002276 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002277 case TargetLowering::Legal: break;
2278 case TargetLowering::Custom:
2279 Tmp1 = TLI.LowerOperation(Result, DAG);
2280 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002281 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002282 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002283 if (Node->getValueType(0) == MVT::i32) {
2284 switch (Node->getOpcode()) {
2285 default: assert(0 && "Do not know how to expand this integer BinOp!");
2286 case ISD::UDIV:
2287 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002288 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2289 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002290 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002291 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002292 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002293 };
2294 break;
2295 }
2296
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002297 assert(MVT::isVector(Node->getValueType(0)) &&
2298 "Cannot expand this binary operator!");
2299 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002300 SmallVector<SDOperand, 8> Ops;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002301 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2302 MVT::ValueType PtrVT = TLI.getPointerTy();
2303 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2304 i != e; ++i) {
2305 SDOperand Idx = DAG.getConstant(i, PtrVT);
2306 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2307 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2308 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2309 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002310 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2311 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002312 break;
2313 }
Evan Chengcc987612006-04-12 21:20:24 +00002314 case TargetLowering::Promote: {
2315 switch (Node->getOpcode()) {
2316 default: assert(0 && "Do not know how to promote this BinOp!");
2317 case ISD::AND:
2318 case ISD::OR:
2319 case ISD::XOR: {
2320 MVT::ValueType OVT = Node->getValueType(0);
2321 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2322 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2323 // Bit convert each of the values to the new type.
2324 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2325 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2326 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2327 // Bit convert the result back the original type.
2328 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2329 break;
2330 }
2331 }
2332 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002333 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002334 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002335
2336 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2337 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2338 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2339 case Expand: assert(0 && "Not possible");
2340 case Legal:
2341 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2342 break;
2343 case Promote:
2344 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2345 break;
2346 }
2347
2348 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2349
2350 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2351 default: assert(0 && "Operation not supported");
2352 case TargetLowering::Custom:
2353 Tmp1 = TLI.LowerOperation(Result, DAG);
2354 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002355 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002356 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002357 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002358 // If this target supports fabs/fneg natively and select is cheap,
2359 // do this efficiently.
2360 if (!TLI.isSelectExpensive() &&
2361 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2362 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002363 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002364 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002365 // Get the sign bit of the RHS.
2366 MVT::ValueType IVT =
2367 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2368 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2369 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2370 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2371 // Get the absolute value of the result.
2372 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2373 // Select between the nabs and abs value based on the sign bit of
2374 // the input.
2375 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2376 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2377 AbsVal),
2378 AbsVal);
2379 Result = LegalizeOp(Result);
2380 break;
2381 }
2382
2383 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002384 MVT::ValueType NVT =
2385 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2386 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2387 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2388 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002389 break;
2390 }
Evan Cheng912095b2007-01-04 21:56:39 +00002391 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002392 break;
2393
Nate Begeman551bf3f2006-02-17 05:43:56 +00002394 case ISD::ADDC:
2395 case ISD::SUBC:
2396 Tmp1 = LegalizeOp(Node->getOperand(0));
2397 Tmp2 = LegalizeOp(Node->getOperand(1));
2398 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2399 // Since this produces two values, make sure to remember that we legalized
2400 // both of them.
2401 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2402 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2403 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002404
Nate Begeman551bf3f2006-02-17 05:43:56 +00002405 case ISD::ADDE:
2406 case ISD::SUBE:
2407 Tmp1 = LegalizeOp(Node->getOperand(0));
2408 Tmp2 = LegalizeOp(Node->getOperand(1));
2409 Tmp3 = LegalizeOp(Node->getOperand(2));
2410 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2411 // Since this produces two values, make sure to remember that we legalized
2412 // both of them.
2413 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2414 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2415 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002416
Nate Begeman419f8b62005-10-18 00:27:41 +00002417 case ISD::BUILD_PAIR: {
2418 MVT::ValueType PairTy = Node->getValueType(0);
2419 // TODO: handle the case where the Lo and Hi operands are not of legal type
2420 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2421 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2422 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002423 case TargetLowering::Promote:
2424 case TargetLowering::Custom:
2425 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002426 case TargetLowering::Legal:
2427 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2428 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2429 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002430 case TargetLowering::Expand:
2431 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2432 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2433 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2434 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2435 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002436 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002437 break;
2438 }
2439 break;
2440 }
2441
Nate Begemanc105e192005-04-06 00:23:54 +00002442 case ISD::UREM:
2443 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002444 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002445 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2446 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002447
Nate Begemanc105e192005-04-06 00:23:54 +00002448 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002449 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2450 case TargetLowering::Custom:
2451 isCustom = true;
2452 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002453 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002454 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002455 if (isCustom) {
2456 Tmp1 = TLI.LowerOperation(Result, DAG);
2457 if (Tmp1.Val) Result = Tmp1;
2458 }
Nate Begemanc105e192005-04-06 00:23:54 +00002459 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002460 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002461 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002462 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002463 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002464 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2465 TargetLowering::Legal) {
2466 // X % Y -> X-X/Y*Y
2467 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002468 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002469 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2470 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2471 } else {
2472 assert(Node->getValueType(0) == MVT::i32 &&
2473 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002474 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2475 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002476 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002477 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002478 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002479 } else {
2480 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002481 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2482 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002483 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002484 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2485 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002486 }
2487 break;
2488 }
2489 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002490 case ISD::VAARG: {
2491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2492 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2493
Chris Lattner5c62f332006-01-28 07:42:08 +00002494 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002495 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2496 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002497 case TargetLowering::Custom:
2498 isCustom = true;
2499 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002500 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002501 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2502 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002503 Tmp1 = Result.getValue(1);
2504
2505 if (isCustom) {
2506 Tmp2 = TLI.LowerOperation(Result, DAG);
2507 if (Tmp2.Val) {
2508 Result = LegalizeOp(Tmp2);
2509 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2510 }
2511 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002512 break;
2513 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002514 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002515 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002516 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002517 // Increment the pointer, VAList, to the next vaarg
2518 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2519 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2520 TLI.getPointerTy()));
2521 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002522 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2523 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002524 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002525 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002526 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002527 Result = LegalizeOp(Result);
2528 break;
2529 }
2530 }
2531 // Since VAARG produces two values, make sure to remember that we
2532 // legalized both of them.
2533 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002534 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2535 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002536 }
2537
2538 case ISD::VACOPY:
2539 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2540 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2541 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2542
2543 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2544 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002545 case TargetLowering::Custom:
2546 isCustom = true;
2547 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002548 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002549 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2550 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002551 if (isCustom) {
2552 Tmp1 = TLI.LowerOperation(Result, DAG);
2553 if (Tmp1.Val) Result = Tmp1;
2554 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002555 break;
2556 case TargetLowering::Expand:
2557 // This defaults to loading a pointer from the input and storing it to the
2558 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002559 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002560 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002561 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2562 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002563 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2564 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002565 break;
2566 }
2567 break;
2568
2569 case ISD::VAEND:
2570 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2571 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2572
2573 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2574 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002575 case TargetLowering::Custom:
2576 isCustom = true;
2577 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002578 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002580 if (isCustom) {
2581 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2582 if (Tmp1.Val) Result = Tmp1;
2583 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002584 break;
2585 case TargetLowering::Expand:
2586 Result = Tmp1; // Default to a no-op, return the chain
2587 break;
2588 }
2589 break;
2590
2591 case ISD::VASTART:
2592 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2593 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2594
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2596
Nate Begemanacc398c2006-01-25 18:21:52 +00002597 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2598 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002599 case TargetLowering::Legal: break;
2600 case TargetLowering::Custom:
2601 Tmp1 = TLI.LowerOperation(Result, DAG);
2602 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002603 break;
2604 }
2605 break;
2606
Nate Begeman35ef9132006-01-11 21:21:00 +00002607 case ISD::ROTL:
2608 case ISD::ROTR:
2609 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2610 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002611
2612 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2613 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002614 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002615 break;
2616
Nate Begemand88fc032006-01-14 03:14:10 +00002617 case ISD::BSWAP:
2618 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2619 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002620 case TargetLowering::Custom:
2621 assert(0 && "Cannot custom legalize this yet!");
2622 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002623 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002624 break;
2625 case TargetLowering::Promote: {
2626 MVT::ValueType OVT = Tmp1.getValueType();
2627 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2628 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002629
Chris Lattner456a93a2006-01-28 07:39:30 +00002630 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2631 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2632 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2633 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2634 break;
2635 }
2636 case TargetLowering::Expand:
2637 Result = ExpandBSWAP(Tmp1);
2638 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002639 }
2640 break;
2641
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002642 case ISD::CTPOP:
2643 case ISD::CTTZ:
2644 case ISD::CTLZ:
2645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2646 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002647 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002648 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002649 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002650 break;
2651 case TargetLowering::Promote: {
2652 MVT::ValueType OVT = Tmp1.getValueType();
2653 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002654
2655 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002656 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2657 // Perform the larger operation, then subtract if needed.
2658 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002659 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002660 case ISD::CTPOP:
2661 Result = Tmp1;
2662 break;
2663 case ISD::CTTZ:
2664 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002665 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2666 DAG.getConstant(getSizeInBits(NVT), NVT),
2667 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002668 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002669 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2670 break;
2671 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002672 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002673 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2674 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002675 getSizeInBits(OVT), NVT));
2676 break;
2677 }
2678 break;
2679 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002680 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002681 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002682 break;
2683 }
2684 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002685
Chris Lattner2c8086f2005-04-02 05:00:07 +00002686 // Unary operators
2687 case ISD::FABS:
2688 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002689 case ISD::FSQRT:
2690 case ISD::FSIN:
2691 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002692 Tmp1 = LegalizeOp(Node->getOperand(0));
2693 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002694 case TargetLowering::Promote:
2695 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002696 isCustom = true;
2697 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002698 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002699 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002700 if (isCustom) {
2701 Tmp1 = TLI.LowerOperation(Result, DAG);
2702 if (Tmp1.Val) Result = Tmp1;
2703 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002704 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002705 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002706 switch (Node->getOpcode()) {
2707 default: assert(0 && "Unreachable!");
2708 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002709 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2710 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002711 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002712 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002713 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002714 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2715 MVT::ValueType VT = Node->getValueType(0);
2716 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002717 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002718 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2719 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002720 break;
2721 }
2722 case ISD::FSQRT:
2723 case ISD::FSIN:
2724 case ISD::FCOS: {
2725 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002726 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002727 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002728 case ISD::FSQRT:
2729 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2730 break;
2731 case ISD::FSIN:
2732 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2733 break;
2734 case ISD::FCOS:
2735 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2736 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002737 default: assert(0 && "Unreachable!");
2738 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002739 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002740 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2741 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002742 break;
2743 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002744 }
2745 break;
2746 }
2747 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002748 case ISD::FPOWI: {
2749 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00002750 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2751 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002752 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002753 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2754 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002755 break;
2756 }
Chris Lattner35481892005-12-23 00:16:34 +00002757 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002758 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002759 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002760 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002761 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2762 Node->getOperand(0).getValueType())) {
2763 default: assert(0 && "Unknown operation action!");
2764 case TargetLowering::Expand:
2765 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2766 break;
2767 case TargetLowering::Legal:
2768 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002769 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002770 break;
2771 }
2772 }
2773 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002774 case ISD::VBIT_CONVERT: {
2775 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2776 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2777
2778 // The input has to be a vector type, we have to either scalarize it, pack
2779 // it, or convert it based on whether the input vector type is legal.
2780 SDNode *InVal = Node->getOperand(0).Val;
2781 unsigned NumElems =
2782 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2783 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2784
2785 // Figure out if there is a Packed type corresponding to this Vector
2786 // type. If so, convert to the packed type.
2787 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2788 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2789 // Turn this into a bit convert of the packed input.
2790 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2791 PackVectorOp(Node->getOperand(0), TVT));
2792 break;
2793 } else if (NumElems == 1) {
2794 // Turn this into a bit convert of the scalar input.
2795 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2796 PackVectorOp(Node->getOperand(0), EVT));
2797 break;
2798 } else {
2799 // FIXME: UNIMP! Store then reload
2800 assert(0 && "Cast from unsupported vector type not implemented yet!");
2801 }
2802 }
2803
Chris Lattner2c8086f2005-04-02 05:00:07 +00002804 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002805 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002806 case ISD::UINT_TO_FP: {
2807 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002808 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2809 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002810 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002811 Node->getOperand(0).getValueType())) {
2812 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002813 case TargetLowering::Custom:
2814 isCustom = true;
2815 // FALLTHROUGH
2816 case TargetLowering::Legal:
2817 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002818 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002819 if (isCustom) {
2820 Tmp1 = TLI.LowerOperation(Result, DAG);
2821 if (Tmp1.Val) Result = Tmp1;
2822 }
2823 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002824 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002825 Result = ExpandLegalINT_TO_FP(isSigned,
2826 LegalizeOp(Node->getOperand(0)),
2827 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002828 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002829 case TargetLowering::Promote:
2830 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2831 Node->getValueType(0),
2832 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002833 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002834 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002835 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002836 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002837 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2838 Node->getValueType(0), Node->getOperand(0));
2839 break;
2840 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002841 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002842 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002843 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2844 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002845 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002846 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2847 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002848 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002849 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2850 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002851 break;
2852 }
2853 break;
2854 }
2855 case ISD::TRUNCATE:
2856 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2857 case Legal:
2858 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002859 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002860 break;
2861 case Expand:
2862 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2863
2864 // Since the result is legal, we should just be able to truncate the low
2865 // part of the source.
2866 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2867 break;
2868 case Promote:
2869 Result = PromoteOp(Node->getOperand(0));
2870 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2871 break;
2872 }
2873 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002874
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002875 case ISD::FP_TO_SINT:
2876 case ISD::FP_TO_UINT:
2877 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2878 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002879 Tmp1 = LegalizeOp(Node->getOperand(0));
2880
Chris Lattner1618beb2005-07-29 00:11:56 +00002881 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2882 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002883 case TargetLowering::Custom:
2884 isCustom = true;
2885 // FALLTHROUGH
2886 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002887 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002888 if (isCustom) {
2889 Tmp1 = TLI.LowerOperation(Result, DAG);
2890 if (Tmp1.Val) Result = Tmp1;
2891 }
2892 break;
2893 case TargetLowering::Promote:
2894 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2895 Node->getOpcode() == ISD::FP_TO_SINT);
2896 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002897 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002898 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2899 SDOperand True, False;
2900 MVT::ValueType VT = Node->getOperand(0).getValueType();
2901 MVT::ValueType NVT = Node->getValueType(0);
2902 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2903 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2904 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2905 Node->getOperand(0), Tmp2, ISD::SETLT);
2906 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2907 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002908 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002909 Tmp2));
2910 False = DAG.getNode(ISD::XOR, NVT, False,
2911 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002912 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2913 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002914 } else {
2915 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2916 }
2917 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002918 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002919 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00002920 case Expand: {
2921 // Convert f32 / f64 to i32 / i64.
2922 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00002923 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00002924 switch (Node->getOpcode()) {
2925 case ISD::FP_TO_SINT:
2926 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002927 LC = (VT == MVT::i32)
2928 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002929 else
Evan Cheng56966222007-01-12 02:11:51 +00002930 LC = (VT == MVT::i32)
2931 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002932 break;
2933 case ISD::FP_TO_UINT:
2934 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002935 LC = (VT == MVT::i32)
2936 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002937 else
Evan Cheng56966222007-01-12 02:11:51 +00002938 LC = (VT == MVT::i32)
2939 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002940 break;
2941 default: assert(0 && "Unreachable!");
2942 }
2943 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002944 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2945 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00002946 break;
2947 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002948 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002949 Tmp1 = PromoteOp(Node->getOperand(0));
2950 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2951 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002952 break;
2953 }
2954 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002955
Chris Lattner13c78e22005-09-02 00:18:10 +00002956 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002957 case ISD::ZERO_EXTEND:
2958 case ISD::SIGN_EXTEND:
2959 case ISD::FP_EXTEND:
2960 case ISD::FP_ROUND:
2961 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002962 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002963 case Legal:
2964 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002965 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002966 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002967 case Promote:
2968 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002969 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002970 Tmp1 = PromoteOp(Node->getOperand(0));
2971 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002972 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002973 case ISD::ZERO_EXTEND:
2974 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002975 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002976 Result = DAG.getZeroExtendInReg(Result,
2977 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002978 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002979 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002980 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002981 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002982 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002983 Result,
2984 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002985 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002986 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002987 Result = PromoteOp(Node->getOperand(0));
2988 if (Result.getValueType() != Op.getValueType())
2989 // Dynamically dead while we have only 2 FP types.
2990 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2991 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002992 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002993 Result = PromoteOp(Node->getOperand(0));
2994 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2995 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002996 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002997 }
2998 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002999 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003000 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003001 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003002 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003003
3004 // If this operation is not supported, convert it to a shl/shr or load/store
3005 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003006 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3007 default: assert(0 && "This action not supported for this op yet!");
3008 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003009 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003010 break;
3011 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003012 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003013 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003014 // NOTE: we could fall back on load/store here too for targets without
3015 // SAR. However, it is doubtful that any exist.
3016 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3017 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003018 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003019 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3020 Node->getOperand(0), ShiftCst);
3021 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3022 Result, ShiftCst);
3023 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003024 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003025 // EXTLOAD pair, targetting a temporary location (a stack slot).
3026
3027 // NOTE: there is a choice here between constantly creating new stack
3028 // slots and always reusing the same one. We currently always create
3029 // new ones, as reuse may inhibit scheduling.
3030 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00003031 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
3032 unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003033 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003034 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00003035 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3036 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003037 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3038 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003039 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003040 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003041 } else {
3042 assert(0 && "Unknown op");
3043 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003044 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003045 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003046 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003047 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003048 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003049
Chris Lattner4ddd2832006-04-08 04:13:17 +00003050 assert(Result.getValueType() == Op.getValueType() &&
3051 "Bad legalization!");
3052
Chris Lattner456a93a2006-01-28 07:39:30 +00003053 // Make sure that the generated code is itself legal.
3054 if (Result != Op)
3055 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003056
Chris Lattner45982da2005-05-12 16:53:42 +00003057 // Note that LegalizeOp may be reentered even from single-use nodes, which
3058 // means that we always must cache transformed nodes.
3059 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003060 return Result;
3061}
3062
Chris Lattner8b6fa222005-01-15 22:16:26 +00003063/// PromoteOp - Given an operation that produces a value in an invalid type,
3064/// promote it to compute the value into a larger type. The produced value will
3065/// have the correct bits for the low portion of the register, but no guarantee
3066/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003067SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3068 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003069 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003070 assert(getTypeAction(VT) == Promote &&
3071 "Caller should expand or legalize operands that are not promotable!");
3072 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3073 "Cannot promote to smaller type!");
3074
Chris Lattner03c85462005-01-15 05:21:40 +00003075 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003076 SDOperand Result;
3077 SDNode *Node = Op.Val;
3078
Chris Lattner6fdcb252005-09-02 20:32:45 +00003079 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3080 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003081
Chris Lattner03c85462005-01-15 05:21:40 +00003082 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003083 case ISD::CopyFromReg:
3084 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003085 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003086#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00003087 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003088#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003089 assert(0 && "Do not know how to promote this operator!");
3090 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003091 case ISD::UNDEF:
3092 Result = DAG.getNode(ISD::UNDEF, NVT);
3093 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003094 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003095 if (VT != MVT::i1)
3096 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3097 else
3098 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003099 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3100 break;
3101 case ISD::ConstantFP:
3102 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3103 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3104 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003105
Chris Lattner82fbfb62005-01-18 02:59:52 +00003106 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003107 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003108 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3109 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003110 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003111
Chris Lattner03c85462005-01-15 05:21:40 +00003112 case ISD::TRUNCATE:
3113 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3114 case Legal:
3115 Result = LegalizeOp(Node->getOperand(0));
3116 assert(Result.getValueType() >= NVT &&
3117 "This truncation doesn't make sense!");
3118 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3119 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3120 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003121 case Promote:
3122 // The truncation is not required, because we don't guarantee anything
3123 // about high bits anyway.
3124 Result = PromoteOp(Node->getOperand(0));
3125 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003126 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003127 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3128 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003129 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003130 }
3131 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003132 case ISD::SIGN_EXTEND:
3133 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003134 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003135 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3136 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3137 case Legal:
3138 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003139 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003140 break;
3141 case Promote:
3142 // Promote the reg if it's smaller.
3143 Result = PromoteOp(Node->getOperand(0));
3144 // The high bits are not guaranteed to be anything. Insert an extend.
3145 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003146 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003147 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003148 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003149 Result = DAG.getZeroExtendInReg(Result,
3150 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003151 break;
3152 }
3153 break;
Chris Lattner35481892005-12-23 00:16:34 +00003154 case ISD::BIT_CONVERT:
3155 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3156 Result = PromoteOp(Result);
3157 break;
3158
Chris Lattner8b6fa222005-01-15 22:16:26 +00003159 case ISD::FP_EXTEND:
3160 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3161 case ISD::FP_ROUND:
3162 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3163 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3164 case Promote: assert(0 && "Unreachable with 2 FP types!");
3165 case Legal:
3166 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003167 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003168 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003169 break;
3170 }
3171 break;
3172
3173 case ISD::SINT_TO_FP:
3174 case ISD::UINT_TO_FP:
3175 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3176 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003177 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003178 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003179 break;
3180
3181 case Promote:
3182 Result = PromoteOp(Node->getOperand(0));
3183 if (Node->getOpcode() == ISD::SINT_TO_FP)
3184 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003185 Result,
3186 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003187 else
Chris Lattner23993562005-04-13 02:38:47 +00003188 Result = DAG.getZeroExtendInReg(Result,
3189 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003190 // No extra round required here.
3191 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003192 break;
3193 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003194 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3195 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003196 // Round if we cannot tolerate excess precision.
3197 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003198 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3199 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003200 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003201 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003202 break;
3203
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003204 case ISD::SIGN_EXTEND_INREG:
3205 Result = PromoteOp(Node->getOperand(0));
3206 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3207 Node->getOperand(1));
3208 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003209 case ISD::FP_TO_SINT:
3210 case ISD::FP_TO_UINT:
3211 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3212 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003213 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003214 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003215 break;
3216 case Promote:
3217 // The input result is prerounded, so we don't have to do anything
3218 // special.
3219 Tmp1 = PromoteOp(Node->getOperand(0));
3220 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003221 }
Nate Begemand2558e32005-08-14 01:20:53 +00003222 // If we're promoting a UINT to a larger size, check to see if the new node
3223 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3224 // we can use that instead. This allows us to generate better code for
3225 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3226 // legal, such as PowerPC.
3227 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003228 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003229 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3230 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003231 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3232 } else {
3233 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3234 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003235 break;
3236
Chris Lattner2c8086f2005-04-02 05:00:07 +00003237 case ISD::FABS:
3238 case ISD::FNEG:
3239 Tmp1 = PromoteOp(Node->getOperand(0));
3240 assert(Tmp1.getValueType() == NVT);
3241 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3242 // NOTE: we do not have to do any extra rounding here for
3243 // NoExcessFPPrecision, because we know the input will have the appropriate
3244 // precision, and these operations don't modify precision at all.
3245 break;
3246
Chris Lattnerda6ba872005-04-28 21:44:33 +00003247 case ISD::FSQRT:
3248 case ISD::FSIN:
3249 case ISD::FCOS:
3250 Tmp1 = PromoteOp(Node->getOperand(0));
3251 assert(Tmp1.getValueType() == NVT);
3252 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003253 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003254 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3255 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003256 break;
3257
Chris Lattner03c85462005-01-15 05:21:40 +00003258 case ISD::AND:
3259 case ISD::OR:
3260 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003261 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003262 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003263 case ISD::MUL:
3264 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003265 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003266 // that too is okay if they are integer operations.
3267 Tmp1 = PromoteOp(Node->getOperand(0));
3268 Tmp2 = PromoteOp(Node->getOperand(1));
3269 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3270 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003271 break;
3272 case ISD::FADD:
3273 case ISD::FSUB:
3274 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003275 Tmp1 = PromoteOp(Node->getOperand(0));
3276 Tmp2 = PromoteOp(Node->getOperand(1));
3277 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3278 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3279
3280 // Floating point operations will give excess precision that we may not be
3281 // able to tolerate. If we DO allow excess precision, just leave it,
3282 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003283 // FIXME: Why would we need to round FP ops more than integer ones?
3284 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003285 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003286 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3287 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003288 break;
3289
Chris Lattner8b6fa222005-01-15 22:16:26 +00003290 case ISD::SDIV:
3291 case ISD::SREM:
3292 // These operators require that their input be sign extended.
3293 Tmp1 = PromoteOp(Node->getOperand(0));
3294 Tmp2 = PromoteOp(Node->getOperand(1));
3295 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003296 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3297 DAG.getValueType(VT));
3298 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3299 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003300 }
3301 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3302
3303 // Perform FP_ROUND: this is probably overly pessimistic.
3304 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003305 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3306 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003307 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003308 case ISD::FDIV:
3309 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003310 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003311 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003312 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3313 case Legal:
3314 Tmp1 = LegalizeOp(Node->getOperand(0));
3315 break;
3316 case Promote:
3317 Tmp1 = PromoteOp(Node->getOperand(0));
3318 break;
3319 case Expand:
3320 assert(0 && "not implemented");
3321 }
3322 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3323 case Legal:
3324 Tmp2 = LegalizeOp(Node->getOperand(1));
3325 break;
3326 case Promote:
3327 Tmp2 = PromoteOp(Node->getOperand(1));
3328 break;
3329 case Expand:
3330 assert(0 && "not implemented");
3331 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003332 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3333
3334 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003335 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003336 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3337 DAG.getValueType(VT));
3338 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003339
3340 case ISD::UDIV:
3341 case ISD::UREM:
3342 // These operators require that their input be zero extended.
3343 Tmp1 = PromoteOp(Node->getOperand(0));
3344 Tmp2 = PromoteOp(Node->getOperand(1));
3345 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003346 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3347 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003348 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3349 break;
3350
3351 case ISD::SHL:
3352 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003353 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003354 break;
3355 case ISD::SRA:
3356 // The input value must be properly sign extended.
3357 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003358 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3359 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003360 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003361 break;
3362 case ISD::SRL:
3363 // The input value must be properly zero extended.
3364 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003365 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003366 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003367 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003368
3369 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003370 Tmp1 = Node->getOperand(0); // Get the chain.
3371 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003372 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3373 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3374 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3375 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003376 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003377 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003378 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003379 // Increment the pointer, VAList, to the next vaarg
3380 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3381 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3382 TLI.getPointerTy()));
3383 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003384 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3385 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003386 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003387 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003388 }
3389 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003390 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003391 break;
3392
Evan Cheng466685d2006-10-09 20:57:25 +00003393 case ISD::LOAD: {
3394 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003395 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3396 ? ISD::EXTLOAD : LD->getExtensionType();
3397 Result = DAG.getExtLoad(ExtType, NVT,
3398 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003399 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003400 LD->getLoadedVT());
Chris Lattner03c85462005-01-15 05:21:40 +00003401 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003402 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003403 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003404 }
Chris Lattner03c85462005-01-15 05:21:40 +00003405 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003406 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3407 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003408 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003409 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003410 case ISD::SELECT_CC:
3411 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3412 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3413 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003414 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003415 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003416 case ISD::BSWAP:
3417 Tmp1 = Node->getOperand(0);
3418 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3419 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3420 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3421 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3422 TLI.getShiftAmountTy()));
3423 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003424 case ISD::CTPOP:
3425 case ISD::CTTZ:
3426 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003427 // Zero extend the argument
3428 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003429 // Perform the larger operation, then subtract if needed.
3430 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003431 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003432 case ISD::CTPOP:
3433 Result = Tmp1;
3434 break;
3435 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003436 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003437 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003438 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003439 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003440 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003441 break;
3442 case ISD::CTLZ:
3443 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003444 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3445 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003446 getSizeInBits(VT), NVT));
3447 break;
3448 }
3449 break;
Chris Lattner15972212006-03-31 17:55:51 +00003450 case ISD::VEXTRACT_VECTOR_ELT:
3451 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3452 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003453 case ISD::EXTRACT_VECTOR_ELT:
3454 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3455 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003456 }
3457
3458 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003459
3460 // Make sure the result is itself legal.
3461 Result = LegalizeOp(Result);
3462
3463 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003464 AddPromotedOperand(Op, Result);
3465 return Result;
3466}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003467
Chris Lattner15972212006-03-31 17:55:51 +00003468/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3469/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3470/// on the vector type. The return type of this matches the element type of the
3471/// vector, which may not be legal for the target.
3472SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3473 // We know that operand #0 is the Vec vector. If the index is a constant
3474 // or if the invec is a supported hardware type, we can use it. Otherwise,
3475 // lower to a store then an indexed load.
3476 SDOperand Vec = Op.getOperand(0);
3477 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3478
3479 SDNode *InVal = Vec.Val;
3480 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3481 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3482
3483 // Figure out if there is a Packed type corresponding to this Vector
3484 // type. If so, convert to the packed type.
3485 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3486 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3487 // Turn this into a packed extract_vector_elt operation.
3488 Vec = PackVectorOp(Vec, TVT);
3489 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3490 } else if (NumElems == 1) {
3491 // This must be an access of the only element. Return it.
3492 return PackVectorOp(Vec, EVT);
3493 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3494 SDOperand Lo, Hi;
3495 SplitVectorOp(Vec, Lo, Hi);
3496 if (CIdx->getValue() < NumElems/2) {
3497 Vec = Lo;
3498 } else {
3499 Vec = Hi;
3500 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3501 }
3502
3503 // It's now an extract from the appropriate high or low part. Recurse.
3504 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3505 return LowerVEXTRACT_VECTOR_ELT(Op);
3506 } else {
3507 // Variable index case for extract element.
3508 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3509 assert(0 && "unimp!");
3510 return SDOperand();
3511 }
3512}
3513
Chris Lattner4aab2f42006-04-02 05:06:04 +00003514/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3515/// memory traffic.
3516SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3517 SDOperand Vector = Op.getOperand(0);
3518 SDOperand Idx = Op.getOperand(1);
3519
3520 // If the target doesn't support this, store the value to a temporary
3521 // stack slot, then LOAD the scalar element back out.
3522 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003523 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003524
3525 // Add the offset to the index.
3526 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3527 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3528 DAG.getConstant(EltSize, Idx.getValueType()));
3529 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3530
Evan Cheng466685d2006-10-09 20:57:25 +00003531 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003532}
3533
Chris Lattner15972212006-03-31 17:55:51 +00003534
Nate Begeman750ac1b2006-02-01 07:19:44 +00003535/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3536/// with condition CC on the current target. This usually involves legalizing
3537/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3538/// there may be no choice but to create a new SetCC node to represent the
3539/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3540/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3541void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3542 SDOperand &RHS,
3543 SDOperand &CC) {
3544 SDOperand Tmp1, Tmp2, Result;
3545
3546 switch (getTypeAction(LHS.getValueType())) {
3547 case Legal:
3548 Tmp1 = LegalizeOp(LHS); // LHS
3549 Tmp2 = LegalizeOp(RHS); // RHS
3550 break;
3551 case Promote:
3552 Tmp1 = PromoteOp(LHS); // LHS
3553 Tmp2 = PromoteOp(RHS); // RHS
3554
3555 // If this is an FP compare, the operands have already been extended.
3556 if (MVT::isInteger(LHS.getValueType())) {
3557 MVT::ValueType VT = LHS.getValueType();
3558 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3559
3560 // Otherwise, we have to insert explicit sign or zero extends. Note
3561 // that we could insert sign extends for ALL conditions, but zero extend
3562 // is cheaper on many machines (an AND instead of two shifts), so prefer
3563 // it.
3564 switch (cast<CondCodeSDNode>(CC)->get()) {
3565 default: assert(0 && "Unknown integer comparison!");
3566 case ISD::SETEQ:
3567 case ISD::SETNE:
3568 case ISD::SETUGE:
3569 case ISD::SETUGT:
3570 case ISD::SETULE:
3571 case ISD::SETULT:
3572 // ALL of these operations will work if we either sign or zero extend
3573 // the operands (including the unsigned comparisons!). Zero extend is
3574 // usually a simpler/cheaper operation, so prefer it.
3575 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3576 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3577 break;
3578 case ISD::SETGE:
3579 case ISD::SETGT:
3580 case ISD::SETLT:
3581 case ISD::SETLE:
3582 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3583 DAG.getValueType(VT));
3584 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3585 DAG.getValueType(VT));
3586 break;
3587 }
3588 }
3589 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003590 case Expand: {
3591 MVT::ValueType VT = LHS.getValueType();
3592 if (VT == MVT::f32 || VT == MVT::f64) {
3593 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003594 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner2d53a322006-12-15 07:36:19 +00003595 ISD::CondCode CC1, CC2 = ISD::SETCC_INVALID;
Evan Cheng2b49c502006-12-15 02:59:56 +00003596 switch (cast<CondCodeSDNode>(CC)->get()) {
3597 case ISD::SETEQ:
3598 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003599 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003600 CC1 = ISD::SETEQ;
3601 break;
3602 case ISD::SETNE:
3603 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003604 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003605 CC1 = ISD::SETNE;
3606 break;
3607 case ISD::SETGE:
3608 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003609 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003610 CC1 = ISD::SETGE;
3611 break;
3612 case ISD::SETLT:
3613 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003614 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003615 CC1 = ISD::SETLT;
3616 break;
3617 case ISD::SETLE:
3618 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003619 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003620 CC1 = ISD::SETLE;
3621 break;
3622 case ISD::SETGT:
3623 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003624 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003625 CC1 = ISD::SETGT;
3626 break;
3627 case ISD::SETUO:
3628 case ISD::SETO:
Evan Cheng56966222007-01-12 02:11:51 +00003629 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003630 CC1 = cast<CondCodeSDNode>(CC)->get() == ISD::SETO
3631 ? ISD::SETEQ : ISD::SETNE;
3632 break;
3633 default:
Evan Cheng56966222007-01-12 02:11:51 +00003634 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003635 CC1 = ISD::SETNE;
3636 switch (cast<CondCodeSDNode>(CC)->get()) {
3637 case ISD::SETONE:
3638 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003639 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003640 CC1 = ISD::SETLT;
3641 // Fallthrough
3642 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003643 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003644 CC2 = ISD::SETGT;
3645 break;
3646 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003647 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003648 CC2 = ISD::SETGE;
3649 break;
3650 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003651 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003652 CC2 = ISD::SETLT;
3653 break;
3654 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003655 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003656 CC2 = ISD::SETLE;
3657 break;
Evan Cheng56966222007-01-12 02:11:51 +00003658 case ISD::SETUEQ:
3659 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
3660 CC2 = ISD::SETEQ;
3661 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003662 default: assert(0 && "Unsupported FP setcc!");
3663 }
3664 }
3665
3666 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003667 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00003668 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003669 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003670 Tmp2 = DAG.getConstant(0, MVT::i32);
3671 CC = DAG.getCondCode(CC1);
Evan Cheng56966222007-01-12 02:11:51 +00003672 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003673 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00003674 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00003675 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003676 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003677 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
3678 DAG.getCondCode(CC2));
3679 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3680 Tmp2 = SDOperand();
3681 }
3682 LHS = Tmp1;
3683 RHS = Tmp2;
3684 return;
3685 }
3686
Nate Begeman750ac1b2006-02-01 07:19:44 +00003687 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3688 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003689 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003690 switch (cast<CondCodeSDNode>(CC)->get()) {
3691 case ISD::SETEQ:
3692 case ISD::SETNE:
3693 if (RHSLo == RHSHi)
3694 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3695 if (RHSCST->isAllOnesValue()) {
3696 // Comparison to -1.
3697 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3698 Tmp2 = RHSLo;
3699 break;
3700 }
3701
3702 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3703 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3704 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3705 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3706 break;
3707 default:
3708 // If this is a comparison of the sign bit, just look at the top part.
3709 // X > -1, x < 0
3710 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3711 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3712 CST->getValue() == 0) || // X < 0
3713 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3714 CST->isAllOnesValue())) { // X > -1
3715 Tmp1 = LHSHi;
3716 Tmp2 = RHSHi;
3717 break;
3718 }
3719
3720 // FIXME: This generated code sucks.
3721 ISD::CondCode LowCC;
3722 switch (cast<CondCodeSDNode>(CC)->get()) {
3723 default: assert(0 && "Unknown integer setcc!");
3724 case ISD::SETLT:
3725 case ISD::SETULT: LowCC = ISD::SETULT; break;
3726 case ISD::SETGT:
3727 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3728 case ISD::SETLE:
3729 case ISD::SETULE: LowCC = ISD::SETULE; break;
3730 case ISD::SETGE:
3731 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3732 }
3733
3734 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3735 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3736 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3737
3738 // NOTE: on targets without efficient SELECT of bools, we can always use
3739 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3740 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3741 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3742 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3743 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3744 Result, Tmp1, Tmp2));
3745 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003746 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003747 }
3748 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003749 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003750 LHS = Tmp1;
3751 RHS = Tmp2;
3752}
3753
Chris Lattner35481892005-12-23 00:16:34 +00003754/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003755/// The resultant code need not be legal. Note that SrcOp is the input operand
3756/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003757SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3758 SDOperand SrcOp) {
3759 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003760 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003761
3762 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003763 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003764 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003765 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003766}
3767
Chris Lattner4352cc92006-04-04 17:23:26 +00003768SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3769 // Create a vector sized/aligned stack slot, store the value to element #0,
3770 // then load the whole vector back out.
3771 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003772 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003773 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003774 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003775}
3776
3777
Chris Lattnerce872152006-03-19 06:31:19 +00003778/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3779/// support the operation, but do support the resultant packed vector type.
3780SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3781
3782 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003783 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003784 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003785 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003786 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003787 std::map<SDOperand, std::vector<unsigned> > Values;
3788 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003789 bool isConstant = true;
3790 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3791 SplatValue.getOpcode() != ISD::UNDEF)
3792 isConstant = false;
3793
Evan Cheng033e6812006-03-24 01:17:21 +00003794 for (unsigned i = 1; i < NumElems; ++i) {
3795 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003796 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003797 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003798 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003799 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003800 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003801
3802 // If this isn't a constant element or an undef, we can't use a constant
3803 // pool load.
3804 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3805 V.getOpcode() != ISD::UNDEF)
3806 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003807 }
3808
3809 if (isOnlyLowElement) {
3810 // If the low element is an undef too, then this whole things is an undef.
3811 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3812 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3813 // Otherwise, turn this into a scalar_to_vector node.
3814 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3815 Node->getOperand(0));
3816 }
3817
Chris Lattner2eb86532006-03-24 07:29:17 +00003818 // If all elements are constants, create a load from the constant pool.
3819 if (isConstant) {
3820 MVT::ValueType VT = Node->getValueType(0);
3821 const Type *OpNTy =
3822 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3823 std::vector<Constant*> CV;
3824 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3825 if (ConstantFPSDNode *V =
3826 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3827 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3828 } else if (ConstantSDNode *V =
3829 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003830 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003831 } else {
3832 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3833 CV.push_back(UndefValue::get(OpNTy));
3834 }
3835 }
3836 Constant *CP = ConstantPacked::get(CV);
3837 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00003838 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003839 }
3840
Chris Lattner87100e02006-03-20 01:52:29 +00003841 if (SplatValue.Val) { // Splat of one value?
3842 // Build the shuffle constant vector: <0, 0, 0, 0>
3843 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003844 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003845 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003846 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003847 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3848 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00003849
3850 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003851 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003852 // Get the splatted value into the low element of a vector register.
3853 SDOperand LowValVec =
3854 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3855
3856 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3857 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3858 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3859 SplatMask);
3860 }
3861 }
3862
Evan Cheng033e6812006-03-24 01:17:21 +00003863 // If there are only two unique elements, we may be able to turn this into a
3864 // vector shuffle.
3865 if (Values.size() == 2) {
3866 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3867 MVT::ValueType MaskVT =
3868 MVT::getIntVectorWithNumElements(NumElems);
3869 std::vector<SDOperand> MaskVec(NumElems);
3870 unsigned i = 0;
3871 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3872 E = Values.end(); I != E; ++I) {
3873 for (std::vector<unsigned>::iterator II = I->second.begin(),
3874 EE = I->second.end(); II != EE; ++II)
3875 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3876 i += NumElems;
3877 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003878 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3879 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003880
3881 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003882 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3883 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003884 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00003885 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3886 E = Values.end(); I != E; ++I) {
3887 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3888 I->first);
3889 Ops.push_back(Op);
3890 }
3891 Ops.push_back(ShuffleMask);
3892
3893 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003894 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3895 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003896 }
3897 }
Chris Lattnerce872152006-03-19 06:31:19 +00003898
3899 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3900 // aligned object on the stack, store each element into it, then load
3901 // the result as a vector.
3902 MVT::ValueType VT = Node->getValueType(0);
3903 // Create the stack frame object.
3904 SDOperand FIPtr = CreateStackTemporary(VT);
3905
3906 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003907 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003908 unsigned TypeByteSize =
3909 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00003910 // Store (in the right endianness) the elements to memory.
3911 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3912 // Ignore undef elements.
3913 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3914
Chris Lattner841c8822006-03-22 01:46:54 +00003915 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003916
3917 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3918 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3919
Evan Cheng786225a2006-10-05 23:01:46 +00003920 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003921 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00003922 }
3923
3924 SDOperand StoreChain;
3925 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003926 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3927 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00003928 else
3929 StoreChain = DAG.getEntryNode();
3930
3931 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003932 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00003933}
3934
3935/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3936/// specified value type.
3937SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3938 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3939 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
3940 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
3941 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3942}
3943
Chris Lattner5b359c62005-04-02 04:00:59 +00003944void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3945 SDOperand Op, SDOperand Amt,
3946 SDOperand &Lo, SDOperand &Hi) {
3947 // Expand the subcomponents.
3948 SDOperand LHSL, LHSH;
3949 ExpandOp(Op, LHSL, LHSH);
3950
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003951 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00003952 MVT::ValueType VT = LHSL.getValueType();
3953 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00003954 Hi = Lo.getValue(1);
3955}
3956
3957
Chris Lattnere34b3962005-01-19 04:19:40 +00003958/// ExpandShift - Try to find a clever way to expand this shift operation out to
3959/// smaller elements. If we can't find a way that is more efficient than a
3960/// libcall on this target, return false. Otherwise, return true with the
3961/// low-parts expanded into Lo and Hi.
3962bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3963 SDOperand &Lo, SDOperand &Hi) {
3964 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3965 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003966
Chris Lattnere34b3962005-01-19 04:19:40 +00003967 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003968 SDOperand ShAmt = LegalizeOp(Amt);
3969 MVT::ValueType ShTy = ShAmt.getValueType();
3970 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3971 unsigned NVTBits = MVT::getSizeInBits(NVT);
3972
3973 // Handle the case when Amt is an immediate. Other cases are currently broken
3974 // and are disabled.
3975 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3976 unsigned Cst = CN->getValue();
3977 // Expand the incoming operand to be shifted, so that we have its parts
3978 SDOperand InL, InH;
3979 ExpandOp(Op, InL, InH);
3980 switch(Opc) {
3981 case ISD::SHL:
3982 if (Cst > VTBits) {
3983 Lo = DAG.getConstant(0, NVT);
3984 Hi = DAG.getConstant(0, NVT);
3985 } else if (Cst > NVTBits) {
3986 Lo = DAG.getConstant(0, NVT);
3987 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00003988 } else if (Cst == NVTBits) {
3989 Lo = DAG.getConstant(0, NVT);
3990 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003991 } else {
3992 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
3993 Hi = DAG.getNode(ISD::OR, NVT,
3994 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
3995 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
3996 }
3997 return true;
3998 case ISD::SRL:
3999 if (Cst > VTBits) {
4000 Lo = DAG.getConstant(0, NVT);
4001 Hi = DAG.getConstant(0, NVT);
4002 } else if (Cst > NVTBits) {
4003 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4004 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004005 } else if (Cst == NVTBits) {
4006 Lo = InH;
4007 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004008 } else {
4009 Lo = DAG.getNode(ISD::OR, NVT,
4010 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4011 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4012 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4013 }
4014 return true;
4015 case ISD::SRA:
4016 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004017 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004018 DAG.getConstant(NVTBits-1, ShTy));
4019 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004020 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004021 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004022 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004023 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004024 } else if (Cst == NVTBits) {
4025 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004026 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004027 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004028 } else {
4029 Lo = DAG.getNode(ISD::OR, NVT,
4030 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4031 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4032 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4033 }
4034 return true;
4035 }
4036 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004037
4038 // Okay, the shift amount isn't constant. However, if we can tell that it is
4039 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4040 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4041 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4042
4043 // If we know that the high bit of the shift amount is one, then we can do
4044 // this as a couple of simple shifts.
4045 if (KnownOne & Mask) {
4046 // Mask out the high bit, which we know is set.
4047 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4048 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4049
4050 // Expand the incoming operand to be shifted, so that we have its parts
4051 SDOperand InL, InH;
4052 ExpandOp(Op, InL, InH);
4053 switch(Opc) {
4054 case ISD::SHL:
4055 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4056 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4057 return true;
4058 case ISD::SRL:
4059 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4060 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4061 return true;
4062 case ISD::SRA:
4063 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4064 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4065 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4066 return true;
4067 }
4068 }
4069
4070 // If we know that the high bit of the shift amount is zero, then we can do
4071 // this as a couple of simple shifts.
4072 if (KnownZero & Mask) {
4073 // Compute 32-amt.
4074 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4075 DAG.getConstant(NVTBits, Amt.getValueType()),
4076 Amt);
4077
4078 // Expand the incoming operand to be shifted, so that we have its parts
4079 SDOperand InL, InH;
4080 ExpandOp(Op, InL, InH);
4081 switch(Opc) {
4082 case ISD::SHL:
4083 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4084 Hi = DAG.getNode(ISD::OR, NVT,
4085 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4086 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4087 return true;
4088 case ISD::SRL:
4089 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4090 Lo = DAG.getNode(ISD::OR, NVT,
4091 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4092 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4093 return true;
4094 case ISD::SRA:
4095 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4096 Lo = DAG.getNode(ISD::OR, NVT,
4097 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4098 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4099 return true;
4100 }
4101 }
4102
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004103 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004104}
Chris Lattner77e77a62005-01-21 06:05:23 +00004105
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004106
Chris Lattner77e77a62005-01-21 06:05:23 +00004107// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4108// does not fit into a register, return the lo part and set the hi part to the
4109// by-reg argument. If it does fit into a single register, return the result
4110// and leave the Hi part unset.
4111SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004112 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004113 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4114 // The input chain to this libcall is the entry node of the function.
4115 // Legalizing the call will automatically add the previous call to the
4116 // dependence.
4117 SDOperand InChain = DAG.getEntryNode();
4118
Chris Lattner77e77a62005-01-21 06:05:23 +00004119 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004120 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004121 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4122 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4123 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004124 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
4125 Entry.isSigned = isSigned;
4126 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004127 }
4128 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004129
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004130 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004131 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004132 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004133 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004134 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004135
Chris Lattner6831a812006-02-13 09:18:02 +00004136 // Legalize the call sequence, starting with the chain. This will advance
4137 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4138 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4139 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004140 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004141 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004142 default: assert(0 && "Unknown thing");
4143 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004144 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004145 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004146 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004147 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004148 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004149 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004150 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004151}
4152
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004153
Chris Lattner77e77a62005-01-21 06:05:23 +00004154/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4155/// destination type is legal.
4156SDOperand SelectionDAGLegalize::
4157ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004158 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00004159 assert(getTypeAction(Source.getValueType()) == Expand &&
4160 "This is not an expansion!");
4161 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4162
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004163 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004164 assert(Source.getValueType() == MVT::i64 &&
4165 "This only works for 64-bit -> FP");
4166 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4167 // incoming integer is set. To handle this, we dynamically test to see if
4168 // it is set, and, if so, add a fudge factor.
4169 SDOperand Lo, Hi;
4170 ExpandOp(Source, Lo, Hi);
4171
Chris Lattner66de05b2005-05-13 04:45:13 +00004172 // If this is unsigned, and not supported, first perform the conversion to
4173 // signed, then adjust the result if the sign bit is set.
4174 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4175 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4176
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004177 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4178 DAG.getConstant(0, Hi.getValueType()),
4179 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004180 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4181 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4182 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004183 uint64_t FF = 0x5f800000ULL;
4184 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004185 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004186
Chris Lattner5839bf22005-08-26 17:15:30 +00004187 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004188 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4189 SDOperand FudgeInReg;
4190 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004191 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004192 else {
4193 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00004194 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004195 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004196 }
Chris Lattner473a9902005-09-29 06:44:39 +00004197 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004198 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004199
Chris Lattnera88a2602005-05-14 05:33:54 +00004200 // Check to see if the target has a custom way to lower this. If so, use it.
4201 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4202 default: assert(0 && "This action not implemented for this operation!");
4203 case TargetLowering::Legal:
4204 case TargetLowering::Expand:
4205 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004206 case TargetLowering::Custom: {
4207 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4208 Source), DAG);
4209 if (NV.Val)
4210 return LegalizeOp(NV);
4211 break; // The target decided this was legal after all
4212 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004213 }
4214
Chris Lattner13689e22005-05-12 07:00:44 +00004215 // Expand the source, then glue it back together for the call. We must expand
4216 // the source in case it is shared (this pass of legalize must traverse it).
4217 SDOperand SrcLo, SrcHi;
4218 ExpandOp(Source, SrcLo, SrcHi);
4219 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4220
Evan Cheng56966222007-01-12 02:11:51 +00004221 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004222 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004223 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004224 else {
4225 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004226 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004227 }
Chris Lattner6831a812006-02-13 09:18:02 +00004228
4229 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4230 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004231 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4232 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004233}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004234
Chris Lattner22cde6a2006-01-28 08:25:58 +00004235/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4236/// INT_TO_FP operation of the specified operand when the target requests that
4237/// we expand it. At this point, we know that the result and operand types are
4238/// legal for the target.
4239SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4240 SDOperand Op0,
4241 MVT::ValueType DestVT) {
4242 if (Op0.getValueType() == MVT::i32) {
4243 // simple 32-bit [signed|unsigned] integer to float/double expansion
4244
4245 // get the stack frame index of a 8 byte buffer
4246 MachineFunction &MF = DAG.getMachineFunction();
4247 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4248 // get address of 8 byte buffer
4249 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4250 // word offset constant for Hi/Lo address computation
4251 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4252 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004253 SDOperand Hi = StackSlot;
4254 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4255 if (TLI.isLittleEndian())
4256 std::swap(Hi, Lo);
4257
Chris Lattner22cde6a2006-01-28 08:25:58 +00004258 // if signed map to unsigned space
4259 SDOperand Op0Mapped;
4260 if (isSigned) {
4261 // constant used to invert sign bit (signed to unsigned mapping)
4262 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4263 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4264 } else {
4265 Op0Mapped = Op0;
4266 }
4267 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004268 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004269 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004270 // initial hi portion of constructed double
4271 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4272 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004273 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004274 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004275 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004276 // FP constant to bias correct the final result
4277 SDOperand Bias = DAG.getConstantFP(isSigned ?
4278 BitsToDouble(0x4330000080000000ULL)
4279 : BitsToDouble(0x4330000000000000ULL),
4280 MVT::f64);
4281 // subtract the bias
4282 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4283 // final result
4284 SDOperand Result;
4285 // handle final rounding
4286 if (DestVT == MVT::f64) {
4287 // do nothing
4288 Result = Sub;
4289 } else {
4290 // if f32 then cast to f32
4291 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4292 }
4293 return Result;
4294 }
4295 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4296 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4297
4298 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4299 DAG.getConstant(0, Op0.getValueType()),
4300 ISD::SETLT);
4301 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4302 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4303 SignSet, Four, Zero);
4304
4305 // If the sign bit of the integer is set, the large number will be treated
4306 // as a negative number. To counteract this, the dynamic code adds an
4307 // offset depending on the data type.
4308 uint64_t FF;
4309 switch (Op0.getValueType()) {
4310 default: assert(0 && "Unsupported integer type!");
4311 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4312 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4313 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4314 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4315 }
4316 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004317 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004318
4319 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4320 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4321 SDOperand FudgeInReg;
4322 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004323 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004324 else {
4325 assert(DestVT == MVT::f64 && "Unexpected conversion");
4326 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4327 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004328 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004329 }
4330
4331 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4332}
4333
4334/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4335/// *INT_TO_FP operation of the specified operand when the target requests that
4336/// we promote it. At this point, we know that the result and operand types are
4337/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4338/// operation that takes a larger input.
4339SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4340 MVT::ValueType DestVT,
4341 bool isSigned) {
4342 // First step, figure out the appropriate *INT_TO_FP operation to use.
4343 MVT::ValueType NewInTy = LegalOp.getValueType();
4344
4345 unsigned OpToUse = 0;
4346
4347 // Scan for the appropriate larger type to use.
4348 while (1) {
4349 NewInTy = (MVT::ValueType)(NewInTy+1);
4350 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4351
4352 // If the target supports SINT_TO_FP of this type, use it.
4353 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4354 default: break;
4355 case TargetLowering::Legal:
4356 if (!TLI.isTypeLegal(NewInTy))
4357 break; // Can't use this datatype.
4358 // FALL THROUGH.
4359 case TargetLowering::Custom:
4360 OpToUse = ISD::SINT_TO_FP;
4361 break;
4362 }
4363 if (OpToUse) break;
4364 if (isSigned) continue;
4365
4366 // If the target supports UINT_TO_FP of this type, use it.
4367 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4368 default: break;
4369 case TargetLowering::Legal:
4370 if (!TLI.isTypeLegal(NewInTy))
4371 break; // Can't use this datatype.
4372 // FALL THROUGH.
4373 case TargetLowering::Custom:
4374 OpToUse = ISD::UINT_TO_FP;
4375 break;
4376 }
4377 if (OpToUse) break;
4378
4379 // Otherwise, try a larger type.
4380 }
4381
4382 // Okay, we found the operation and type to use. Zero extend our input to the
4383 // desired type then run the operation on it.
4384 return DAG.getNode(OpToUse, DestVT,
4385 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4386 NewInTy, LegalOp));
4387}
4388
4389/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4390/// FP_TO_*INT operation of the specified operand when the target requests that
4391/// we promote it. At this point, we know that the result and operand types are
4392/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4393/// operation that returns a larger result.
4394SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4395 MVT::ValueType DestVT,
4396 bool isSigned) {
4397 // First step, figure out the appropriate FP_TO*INT operation to use.
4398 MVT::ValueType NewOutTy = DestVT;
4399
4400 unsigned OpToUse = 0;
4401
4402 // Scan for the appropriate larger type to use.
4403 while (1) {
4404 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4405 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4406
4407 // If the target supports FP_TO_SINT returning this type, use it.
4408 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4409 default: break;
4410 case TargetLowering::Legal:
4411 if (!TLI.isTypeLegal(NewOutTy))
4412 break; // Can't use this datatype.
4413 // FALL THROUGH.
4414 case TargetLowering::Custom:
4415 OpToUse = ISD::FP_TO_SINT;
4416 break;
4417 }
4418 if (OpToUse) break;
4419
4420 // If the target supports FP_TO_UINT of this type, use it.
4421 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4422 default: break;
4423 case TargetLowering::Legal:
4424 if (!TLI.isTypeLegal(NewOutTy))
4425 break; // Can't use this datatype.
4426 // FALL THROUGH.
4427 case TargetLowering::Custom:
4428 OpToUse = ISD::FP_TO_UINT;
4429 break;
4430 }
4431 if (OpToUse) break;
4432
4433 // Otherwise, try a larger type.
4434 }
4435
4436 // Okay, we found the operation and type to use. Truncate the result of the
4437 // extended FP_TO_*INT operation to the desired size.
4438 return DAG.getNode(ISD::TRUNCATE, DestVT,
4439 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4440}
4441
4442/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4443///
4444SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4445 MVT::ValueType VT = Op.getValueType();
4446 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4447 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4448 switch (VT) {
4449 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4450 case MVT::i16:
4451 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4452 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4453 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4454 case MVT::i32:
4455 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4456 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4457 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4458 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4459 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4460 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4461 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4462 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4463 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4464 case MVT::i64:
4465 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4466 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4467 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4468 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4469 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4470 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4471 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4472 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4473 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4474 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4475 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4476 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4477 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4478 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4479 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4480 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4481 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4482 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4483 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4484 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4485 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4486 }
4487}
4488
4489/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4490///
4491SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4492 switch (Opc) {
4493 default: assert(0 && "Cannot expand this yet!");
4494 case ISD::CTPOP: {
4495 static const uint64_t mask[6] = {
4496 0x5555555555555555ULL, 0x3333333333333333ULL,
4497 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4498 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4499 };
4500 MVT::ValueType VT = Op.getValueType();
4501 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4502 unsigned len = getSizeInBits(VT);
4503 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4504 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4505 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4506 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4507 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4508 DAG.getNode(ISD::AND, VT,
4509 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4510 }
4511 return Op;
4512 }
4513 case ISD::CTLZ: {
4514 // for now, we do this:
4515 // x = x | (x >> 1);
4516 // x = x | (x >> 2);
4517 // ...
4518 // x = x | (x >>16);
4519 // x = x | (x >>32); // for 64-bit input
4520 // return popcount(~x);
4521 //
4522 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4523 MVT::ValueType VT = Op.getValueType();
4524 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4525 unsigned len = getSizeInBits(VT);
4526 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4527 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4528 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4529 }
4530 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4531 return DAG.getNode(ISD::CTPOP, VT, Op);
4532 }
4533 case ISD::CTTZ: {
4534 // for now, we use: { return popcount(~x & (x - 1)); }
4535 // unless the target has ctlz but not ctpop, in which case we use:
4536 // { return 32 - nlz(~x & (x-1)); }
4537 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4538 MVT::ValueType VT = Op.getValueType();
4539 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4540 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4541 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4542 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4543 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4544 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4545 TLI.isOperationLegal(ISD::CTLZ, VT))
4546 return DAG.getNode(ISD::SUB, VT,
4547 DAG.getConstant(getSizeInBits(VT), VT),
4548 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4549 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4550 }
4551 }
4552}
Chris Lattnere34b3962005-01-19 04:19:40 +00004553
Chris Lattner3e928bb2005-01-07 07:47:09 +00004554/// ExpandOp - Expand the specified SDOperand into its two component pieces
4555/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4556/// LegalizeNodes map is filled in for any results that are not expanded, the
4557/// ExpandedNodes map is filled in for any results that are expanded, and the
4558/// Lo/Hi values are returned.
4559void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4560 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004561 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004562 SDNode *Node = Op.Val;
4563 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004564 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4565 VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004566 "Cannot expand to FP value or to larger int value!");
4567
Chris Lattner6fdcb252005-09-02 20:32:45 +00004568 // See if we already expanded it.
4569 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4570 = ExpandedNodes.find(Op);
4571 if (I != ExpandedNodes.end()) {
4572 Lo = I->second.first;
4573 Hi = I->second.second;
4574 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004575 }
4576
Chris Lattner3e928bb2005-01-07 07:47:09 +00004577 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004578 case ISD::CopyFromReg:
4579 assert(0 && "CopyFromReg must be legal!");
4580 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004581#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00004582 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004583#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004584 assert(0 && "Do not know how to expand this operator!");
4585 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004586 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004587 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004588 Lo = DAG.getNode(ISD::UNDEF, NVT);
4589 Hi = DAG.getNode(ISD::UNDEF, NVT);
4590 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004591 case ISD::Constant: {
4592 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4593 Lo = DAG.getConstant(Cst, NVT);
4594 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4595 break;
4596 }
Evan Cheng00495212006-12-12 21:32:44 +00004597 case ISD::ConstantFP: {
4598 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004599 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004600 if (getTypeAction(Lo.getValueType()) == Expand)
4601 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004602 break;
4603 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004604 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004605 // Return the operands.
4606 Lo = Node->getOperand(0);
4607 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004608 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004609
4610 case ISD::SIGN_EXTEND_INREG:
4611 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004612 // sext_inreg the low part if needed.
4613 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4614
4615 // The high part gets the sign extension from the lo-part. This handles
4616 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004617 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4618 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4619 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004620 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004621
Nate Begemand88fc032006-01-14 03:14:10 +00004622 case ISD::BSWAP: {
4623 ExpandOp(Node->getOperand(0), Lo, Hi);
4624 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4625 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4626 Lo = TempLo;
4627 break;
4628 }
4629
Chris Lattneredb1add2005-05-11 04:51:16 +00004630 case ISD::CTPOP:
4631 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004632 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4633 DAG.getNode(ISD::CTPOP, NVT, Lo),
4634 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004635 Hi = DAG.getConstant(0, NVT);
4636 break;
4637
Chris Lattner39a8f332005-05-12 19:05:01 +00004638 case ISD::CTLZ: {
4639 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004640 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004641 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4642 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004643 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4644 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004645 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4646 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4647
4648 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4649 Hi = DAG.getConstant(0, NVT);
4650 break;
4651 }
4652
4653 case ISD::CTTZ: {
4654 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004655 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004656 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4657 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004658 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4659 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004660 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4661 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4662
4663 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4664 Hi = DAG.getConstant(0, NVT);
4665 break;
4666 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004667
Nate Begemanacc398c2006-01-25 18:21:52 +00004668 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004669 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4670 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004671 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4672 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4673
4674 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004675 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004676 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4677 if (!TLI.isLittleEndian())
4678 std::swap(Lo, Hi);
4679 break;
4680 }
4681
Chris Lattner3e928bb2005-01-07 07:47:09 +00004682 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004683 LoadSDNode *LD = cast<LoadSDNode>(Node);
4684 SDOperand Ch = LD->getChain(); // Legalize the chain.
4685 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4686 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004687
Evan Cheng466685d2006-10-09 20:57:25 +00004688 if (ExtType == ISD::NON_EXTLOAD) {
4689 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng00495212006-12-12 21:32:44 +00004690 if (VT == MVT::f32 || VT == MVT::f64) {
4691 // f32->i32 or f64->i64 one to one expansion.
4692 // Remember that we legalized the chain.
4693 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004694 // Recursively expand the new load.
4695 if (getTypeAction(NVT) == Expand)
4696 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004697 break;
4698 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004699
Evan Cheng466685d2006-10-09 20:57:25 +00004700 // Increment the pointer to the other half.
4701 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4702 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4703 getIntPtrConstant(IncrementSize));
4704 // FIXME: This creates a bogus srcvalue!
4705 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004706
Evan Cheng466685d2006-10-09 20:57:25 +00004707 // Build a factor node to remember that this load is independent of the
4708 // other one.
4709 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4710 Hi.getValue(1));
4711
4712 // Remember that we legalized the chain.
4713 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4714 if (!TLI.isLittleEndian())
4715 std::swap(Lo, Hi);
4716 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004717 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004718
4719 if (VT == MVT::f64 && EVT == MVT::f32) {
4720 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4721 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4722 LD->getSrcValueOffset());
4723 // Remember that we legalized the chain.
4724 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4725 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4726 break;
4727 }
Evan Cheng466685d2006-10-09 20:57:25 +00004728
4729 if (EVT == NVT)
4730 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4731 LD->getSrcValueOffset());
4732 else
4733 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4734 LD->getSrcValueOffset(), EVT);
4735
4736 // Remember that we legalized the chain.
4737 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4738
4739 if (ExtType == ISD::SEXTLOAD) {
4740 // The high part is obtained by SRA'ing all but one of the bits of the
4741 // lo part.
4742 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4743 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4744 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4745 } else if (ExtType == ISD::ZEXTLOAD) {
4746 // The high part is just a zero.
4747 Hi = DAG.getConstant(0, NVT);
4748 } else /* if (ExtType == ISD::EXTLOAD) */ {
4749 // The high part is undefined.
4750 Hi = DAG.getNode(ISD::UNDEF, NVT);
4751 }
4752 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004753 break;
4754 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004755 case ISD::AND:
4756 case ISD::OR:
4757 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4758 SDOperand LL, LH, RL, RH;
4759 ExpandOp(Node->getOperand(0), LL, LH);
4760 ExpandOp(Node->getOperand(1), RL, RH);
4761 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4762 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4763 break;
4764 }
4765 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004766 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004767 ExpandOp(Node->getOperand(1), LL, LH);
4768 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004769 if (getTypeAction(NVT) == Expand)
4770 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004771 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004772 if (VT != MVT::f32)
4773 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004774 break;
4775 }
Nate Begeman9373a812005-08-10 20:51:12 +00004776 case ISD::SELECT_CC: {
4777 SDOperand TL, TH, FL, FH;
4778 ExpandOp(Node->getOperand(2), TL, TH);
4779 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004780 if (getTypeAction(NVT) == Expand)
4781 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004782 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4783 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004784 if (VT != MVT::f32)
4785 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4786 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004787 break;
4788 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004789 case ISD::ANY_EXTEND:
4790 // The low part is any extension of the input (which degenerates to a copy).
4791 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4792 // The high part is undefined.
4793 Hi = DAG.getNode(ISD::UNDEF, NVT);
4794 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004795 case ISD::SIGN_EXTEND: {
4796 // The low part is just a sign extension of the input (which degenerates to
4797 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004798 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004799
Chris Lattner3e928bb2005-01-07 07:47:09 +00004800 // The high part is obtained by SRA'ing all but one of the bits of the lo
4801 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004802 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004803 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4804 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004805 break;
4806 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004807 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004808 // The low part is just a zero extension of the input (which degenerates to
4809 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004810 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004811
Chris Lattner3e928bb2005-01-07 07:47:09 +00004812 // The high part is just a zero.
4813 Hi = DAG.getConstant(0, NVT);
4814 break;
Chris Lattner35481892005-12-23 00:16:34 +00004815
4816 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004817 SDOperand Tmp;
4818 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4819 // If the target wants to, allow it to lower this itself.
4820 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4821 case Expand: assert(0 && "cannot expand FP!");
4822 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4823 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4824 }
4825 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4826 }
4827
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004828 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00004829 if (VT == MVT::f32 || VT == MVT::f64) {
4830 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00004831 if (getTypeAction(NVT) == Expand)
4832 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00004833 break;
4834 }
4835
4836 // If source operand will be expanded to the same type as VT, i.e.
4837 // i64 <- f64, i32 <- f32, expand the source operand instead.
4838 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4839 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4840 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004841 break;
4842 }
4843
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004844 // Turn this into a load/store pair by default.
4845 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00004846 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004847
Chris Lattner35481892005-12-23 00:16:34 +00004848 ExpandOp(Tmp, Lo, Hi);
4849 break;
4850 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004851
Chris Lattner8137c9e2006-01-28 05:07:51 +00004852 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004853 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4854 TargetLowering::Custom &&
4855 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004856 Lo = TLI.LowerOperation(Op, DAG);
4857 assert(Lo.Val && "Node must be custom expanded!");
4858 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004859 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004860 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004861 break;
4862
Chris Lattner4e6c7462005-01-08 19:27:05 +00004863 // These operators cannot be expanded directly, emit them as calls to
4864 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00004865 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00004866 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004867 SDOperand Op;
4868 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4869 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004870 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4871 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004872 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004873
Chris Lattnerf20d1832005-07-30 01:40:57 +00004874 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4875
Chris Lattner80a3e942005-07-29 00:33:32 +00004876 // Now that the custom expander is done, expand the result, which is still
4877 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004878 if (Op.Val) {
4879 ExpandOp(Op, Lo, Hi);
4880 break;
4881 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004882 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004883
Evan Cheng56966222007-01-12 02:11:51 +00004884 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004885 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004886 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004887 else
Evan Cheng56966222007-01-12 02:11:51 +00004888 LC = RTLIB::FPTOSINT_F64_I64;
4889 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4890 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004891 break;
Evan Cheng56966222007-01-12 02:11:51 +00004892 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004893
Evan Cheng56966222007-01-12 02:11:51 +00004894 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00004895 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004896 SDOperand Op;
4897 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4898 case Expand: assert(0 && "cannot expand FP!");
4899 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4900 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4901 }
4902
4903 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4904
4905 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004906 if (Op.Val) {
4907 ExpandOp(Op, Lo, Hi);
4908 break;
4909 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004910 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004911
Evan Cheng56966222007-01-12 02:11:51 +00004912 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004913 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004914 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004915 else
Evan Cheng56966222007-01-12 02:11:51 +00004916 LC = RTLIB::FPTOUINT_F64_I64;
4917 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4918 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004919 break;
Evan Cheng56966222007-01-12 02:11:51 +00004920 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00004921
Evan Cheng05a2d562006-01-09 18:31:59 +00004922 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004923 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004924 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004925 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004926 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004927 Op = TLI.LowerOperation(Op, DAG);
4928 if (Op.Val) {
4929 // Now that the custom expander is done, expand the result, which is
4930 // still VT.
4931 ExpandOp(Op, Lo, Hi);
4932 break;
4933 }
4934 }
4935
Chris Lattner79980b02006-09-13 03:50:39 +00004936 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
4937 // this X << 1 as X+X.
4938 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
4939 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
4940 TLI.isOperationLegal(ISD::ADDE, NVT)) {
4941 SDOperand LoOps[2], HiOps[3];
4942 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
4943 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
4944 LoOps[1] = LoOps[0];
4945 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
4946
4947 HiOps[1] = HiOps[0];
4948 HiOps[2] = Lo.getValue(1);
4949 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
4950 break;
4951 }
4952 }
4953
Chris Lattnere34b3962005-01-19 04:19:40 +00004954 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004955 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004956 break;
Chris Lattner47599822005-04-02 03:38:53 +00004957
4958 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004959 TargetLowering::LegalizeAction Action =
4960 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4961 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4962 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004963 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004964 break;
4965 }
4966
Chris Lattnere34b3962005-01-19 04:19:40 +00004967 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00004968 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
4969 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004970 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004971 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004972
Evan Cheng05a2d562006-01-09 18:31:59 +00004973 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004974 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004975 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004976 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004977 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004978 Op = TLI.LowerOperation(Op, DAG);
4979 if (Op.Val) {
4980 // Now that the custom expander is done, expand the result, which is
4981 // still VT.
4982 ExpandOp(Op, Lo, Hi);
4983 break;
4984 }
4985 }
4986
Chris Lattnere34b3962005-01-19 04:19:40 +00004987 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004988 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004989 break;
Chris Lattner47599822005-04-02 03:38:53 +00004990
4991 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004992 TargetLowering::LegalizeAction Action =
4993 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
4994 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4995 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004996 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004997 break;
4998 }
4999
Chris Lattnere34b3962005-01-19 04:19:40 +00005000 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005001 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5002 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005003 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005004 }
5005
5006 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005007 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005008 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005009 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005010 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005011 Op = TLI.LowerOperation(Op, DAG);
5012 if (Op.Val) {
5013 // Now that the custom expander is done, expand the result, which is
5014 // still VT.
5015 ExpandOp(Op, Lo, Hi);
5016 break;
5017 }
5018 }
5019
Chris Lattnere34b3962005-01-19 04:19:40 +00005020 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005021 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005022 break;
Chris Lattner47599822005-04-02 03:38:53 +00005023
5024 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005025 TargetLowering::LegalizeAction Action =
5026 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5027 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5028 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005029 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005030 break;
5031 }
5032
Chris Lattnere34b3962005-01-19 04:19:40 +00005033 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005034 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5035 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005036 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005037 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005038
Misha Brukmanedf128a2005-04-21 22:36:52 +00005039 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005040 case ISD::SUB: {
5041 // If the target wants to custom expand this, let them.
5042 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5043 TargetLowering::Custom) {
5044 Op = TLI.LowerOperation(Op, DAG);
5045 if (Op.Val) {
5046 ExpandOp(Op, Lo, Hi);
5047 break;
5048 }
5049 }
5050
5051 // Expand the subcomponents.
5052 SDOperand LHSL, LHSH, RHSL, RHSH;
5053 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5054 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005055 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5056 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005057 LoOps[0] = LHSL;
5058 LoOps[1] = RHSL;
5059 HiOps[0] = LHSH;
5060 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005061 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005062 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005063 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005064 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005065 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005066 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005067 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005068 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005069 }
Chris Lattner84f67882005-01-20 18:52:28 +00005070 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005071 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005072 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005073 // If the target wants to custom expand this, let them.
5074 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005075 SDOperand New = TLI.LowerOperation(Op, DAG);
5076 if (New.Val) {
5077 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005078 break;
5079 }
5080 }
5081
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005082 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5083 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005084 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005085 SDOperand LL, LH, RL, RH;
5086 ExpandOp(Node->getOperand(0), LL, LH);
5087 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005088 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005089 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005090 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5091 // extended the sign bit of the low half through the upper half, and if so
5092 // emit a MULHS instead of the alternate sequence that is valid for any
5093 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005094 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005095 // is RH an extension of the sign bit of RL?
5096 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5097 RH.getOperand(1).getOpcode() == ISD::Constant &&
5098 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5099 // is LH an extension of the sign bit of LL?
5100 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5101 LH.getOperand(1).getOpcode() == ISD::Constant &&
5102 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005103 // Low part:
5104 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5105 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005106 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005107 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005108 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005109 // Low part:
5110 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5111
5112 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005113 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5114 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5115 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5116 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5117 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005118 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005119 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005120 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005121
Evan Cheng56966222007-01-12 02:11:51 +00005122 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5123 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005124 break;
5125 }
Evan Cheng56966222007-01-12 02:11:51 +00005126 case ISD::SDIV:
5127 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5128 break;
5129 case ISD::UDIV:
5130 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5131 break;
5132 case ISD::SREM:
5133 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5134 break;
5135 case ISD::UREM:
5136 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5137 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005138
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005139 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005140 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5141 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5142 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005143 break;
5144 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005145 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5146 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5147 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005148 break;
5149 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005150 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5151 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5152 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005153 break;
5154 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005155 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5156 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5157 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005158 break;
5159 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005160 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005161 break;
5162 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005163 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005164 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005165 case ISD::FSQRT:
5166 case ISD::FSIN:
5167 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005168 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005169 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005170 case ISD::FSQRT:
5171 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5172 break;
5173 case ISD::FSIN:
5174 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5175 break;
5176 case ISD::FCOS:
5177 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5178 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005179 default: assert(0 && "Unreachable!");
5180 }
Evan Cheng56966222007-01-12 02:11:51 +00005181 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005182 break;
5183 }
Evan Cheng966bf242006-12-16 00:52:40 +00005184 case ISD::FABS: {
5185 SDOperand Mask = (VT == MVT::f64)
5186 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5187 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5188 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5189 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5190 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5191 if (getTypeAction(NVT) == Expand)
5192 ExpandOp(Lo, Lo, Hi);
5193 break;
5194 }
5195 case ISD::FNEG: {
5196 SDOperand Mask = (VT == MVT::f64)
5197 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5198 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5199 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5200 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5201 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5202 if (getTypeAction(NVT) == Expand)
5203 ExpandOp(Lo, Lo, Hi);
5204 break;
5205 }
Evan Cheng912095b2007-01-04 21:56:39 +00005206 case ISD::FCOPYSIGN: {
5207 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5208 if (getTypeAction(NVT) == Expand)
5209 ExpandOp(Lo, Lo, Hi);
5210 break;
5211 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005212 case ISD::SINT_TO_FP:
5213 case ISD::UINT_TO_FP: {
5214 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5215 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005216 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005217 if (Node->getOperand(0).getValueType() == MVT::i64) {
5218 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005219 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005220 else
Evan Cheng56966222007-01-12 02:11:51 +00005221 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005222 } else {
5223 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005224 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005225 else
Evan Cheng56966222007-01-12 02:11:51 +00005226 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005227 }
5228
5229 // Promote the operand if needed.
5230 if (getTypeAction(SrcVT) == Promote) {
5231 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5232 Tmp = isSigned
5233 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5234 DAG.getValueType(SrcVT))
5235 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5236 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5237 }
Evan Cheng56966222007-01-12 02:11:51 +00005238 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00005239 break;
5240 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005241 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005242
Chris Lattner83397362005-12-21 18:02:52 +00005243 // Make sure the resultant values have been legalized themselves, unless this
5244 // is a type that requires multi-step expansion.
5245 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5246 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005247 if (Hi.Val)
5248 // Don't legalize the high part if it is expanded to a single node.
5249 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005250 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005251
5252 // Remember in a map if the values will be reused later.
5253 bool isNew =
5254 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5255 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005256}
5257
Chris Lattnerc7029802006-03-18 01:44:44 +00005258/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5259/// two smaller values of MVT::Vector type.
5260void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5261 SDOperand &Hi) {
5262 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5263 SDNode *Node = Op.Val;
5264 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5265 assert(NumElements > 1 && "Cannot split a single element vector!");
5266 unsigned NewNumElts = NumElements/2;
5267 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5268 SDOperand TypeNode = *(Node->op_end()-1);
5269
5270 // See if we already split it.
5271 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5272 = SplitNodes.find(Op);
5273 if (I != SplitNodes.end()) {
5274 Lo = I->second.first;
5275 Hi = I->second.second;
5276 return;
5277 }
5278
5279 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005280 default:
5281#ifndef NDEBUG
5282 Node->dump();
5283#endif
5284 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00005285 case ISD::VBUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005286 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5287 Node->op_begin()+NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005288 LoOps.push_back(NewNumEltsNode);
5289 LoOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005290 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005291
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005292 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5293 Node->op_end()-2);
Chris Lattnerc7029802006-03-18 01:44:44 +00005294 HiOps.push_back(NewNumEltsNode);
5295 HiOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005296 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005297 break;
5298 }
5299 case ISD::VADD:
5300 case ISD::VSUB:
5301 case ISD::VMUL:
5302 case ISD::VSDIV:
5303 case ISD::VUDIV:
5304 case ISD::VAND:
5305 case ISD::VOR:
5306 case ISD::VXOR: {
5307 SDOperand LL, LH, RL, RH;
5308 SplitVectorOp(Node->getOperand(0), LL, LH);
5309 SplitVectorOp(Node->getOperand(1), RL, RH);
5310
5311 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5312 NewNumEltsNode, TypeNode);
5313 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5314 NewNumEltsNode, TypeNode);
5315 break;
5316 }
5317 case ISD::VLOAD: {
5318 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5319 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5320 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5321
5322 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5323 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5324 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5325 getIntPtrConstant(IncrementSize));
5326 // FIXME: This creates a bogus srcvalue!
5327 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5328
5329 // Build a factor node to remember that this load is independent of the
5330 // other one.
5331 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5332 Hi.getValue(1));
5333
5334 // Remember that we legalized the chain.
5335 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005336 break;
5337 }
Chris Lattner7692eb42006-03-23 21:16:34 +00005338 case ISD::VBIT_CONVERT: {
5339 // We know the result is a vector. The input may be either a vector or a
5340 // scalar value.
5341 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5342 // Lower to a store/load. FIXME: this could be improved probably.
5343 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5344
Evan Cheng786225a2006-10-05 23:01:46 +00005345 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005346 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005347 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5348 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5349 SplitVectorOp(St, Lo, Hi);
5350 } else {
5351 // If the input is a vector type, we have to either scalarize it, pack it
5352 // or convert it based on whether the input vector type is legal.
5353 SDNode *InVal = Node->getOperand(0).Val;
5354 unsigned NumElems =
5355 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5356 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5357
5358 // If the input is from a single element vector, scalarize the vector,
5359 // then treat like a scalar.
5360 if (NumElems == 1) {
5361 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5362 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5363 Op.getOperand(1), Op.getOperand(2));
5364 SplitVectorOp(Scalar, Lo, Hi);
5365 } else {
5366 // Split the input vector.
5367 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5368
5369 // Convert each of the pieces now.
5370 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5371 NewNumEltsNode, TypeNode);
5372 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5373 NewNumEltsNode, TypeNode);
5374 }
5375 break;
5376 }
5377 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005378 }
5379
5380 // Remember in a map if the values will be reused later.
5381 bool isNew =
5382 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5383 assert(isNew && "Value already expanded?!?");
5384}
5385
5386
5387/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5388/// equivalent operation that returns a scalar (e.g. F32) or packed value
5389/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5390/// type for the result.
5391SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5392 MVT::ValueType NewVT) {
5393 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5394 SDNode *Node = Op.Val;
5395
5396 // See if we already packed it.
5397 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5398 if (I != PackedNodes.end()) return I->second;
5399
5400 SDOperand Result;
5401 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005402 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005403#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00005404 Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005405#endif
Chris Lattner2332b9f2006-03-19 01:17:20 +00005406 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005407 case ISD::VADD:
5408 case ISD::VSUB:
5409 case ISD::VMUL:
5410 case ISD::VSDIV:
5411 case ISD::VUDIV:
5412 case ISD::VAND:
5413 case ISD::VOR:
5414 case ISD::VXOR:
5415 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5416 NewVT,
5417 PackVectorOp(Node->getOperand(0), NewVT),
5418 PackVectorOp(Node->getOperand(1), NewVT));
5419 break;
5420 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00005421 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5422 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005423
Evan Cheng466685d2006-10-09 20:57:25 +00005424 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5425 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattnerc7029802006-03-18 01:44:44 +00005426
5427 // Remember that we legalized the chain.
5428 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5429 break;
5430 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00005431 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00005432 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005433 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00005434 Result = Node->getOperand(0);
5435 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005436 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00005437
5438 // If all elements of the build_vector are undefs, return an undef.
5439 bool AllUndef = true;
5440 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5441 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5442 AllUndef = false;
5443 break;
5444 }
5445 if (AllUndef) {
5446 Result = DAG.getNode(ISD::UNDEF, NewVT);
5447 } else {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005448 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5449 Node->getNumOperands()-2);
Chris Lattner17614ea2006-04-08 05:34:25 +00005450 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005451 }
5452 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00005453 case ISD::VINSERT_VECTOR_ELT:
5454 if (!MVT::isVector(NewVT)) {
5455 // Returning a scalar? Must be the inserted element.
5456 Result = Node->getOperand(1);
5457 } else {
5458 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5459 PackVectorOp(Node->getOperand(0), NewVT),
5460 Node->getOperand(1), Node->getOperand(2));
5461 }
5462 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00005463 case ISD::VVECTOR_SHUFFLE:
5464 if (!MVT::isVector(NewVT)) {
5465 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5466 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5467 if (cast<ConstantSDNode>(EltNum)->getValue())
5468 Result = PackVectorOp(Node->getOperand(1), NewVT);
5469 else
5470 Result = PackVectorOp(Node->getOperand(0), NewVT);
5471 } else {
5472 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5473 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5474 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5475 Node->getOperand(2).Val->op_end()-2);
5476 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005477 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5478 Node->getOperand(2).Val->op_begin(),
5479 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattner5b2316e2006-03-28 20:24:43 +00005480
5481 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5482 PackVectorOp(Node->getOperand(0), NewVT),
5483 PackVectorOp(Node->getOperand(1), NewVT), BV);
5484 }
5485 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00005486 case ISD::VBIT_CONVERT:
5487 if (Op.getOperand(0).getValueType() != MVT::Vector)
5488 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5489 else {
5490 // If the input is a vector type, we have to either scalarize it, pack it
5491 // or convert it based on whether the input vector type is legal.
5492 SDNode *InVal = Node->getOperand(0).Val;
5493 unsigned NumElems =
5494 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5495 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5496
5497 // Figure out if there is a Packed type corresponding to this Vector
5498 // type. If so, convert to the packed type.
5499 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5500 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5501 // Turn this into a bit convert of the packed input.
5502 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5503 PackVectorOp(Node->getOperand(0), TVT));
5504 break;
5505 } else if (NumElems == 1) {
5506 // Turn this into a bit convert of the scalar input.
5507 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5508 PackVectorOp(Node->getOperand(0), EVT));
5509 break;
5510 } else {
5511 // FIXME: UNIMP!
5512 assert(0 && "Cast from unsupported vector type not implemented yet!");
5513 }
5514 }
Evan Chengdb3c6262006-04-10 18:54:36 +00005515 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005516 case ISD::VSELECT:
5517 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5518 PackVectorOp(Op.getOperand(1), NewVT),
5519 PackVectorOp(Op.getOperand(2), NewVT));
5520 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005521 }
5522
5523 if (TLI.isTypeLegal(NewVT))
5524 Result = LegalizeOp(Result);
5525 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5526 assert(isNew && "Value already packed?");
5527 return Result;
5528}
5529
Chris Lattner3e928bb2005-01-07 07:47:09 +00005530
5531// SelectionDAG::Legalize - This is the entry point for the file.
5532//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005533void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005534 if (ViewLegalizeDAGs) viewGraph();
5535
Chris Lattner3e928bb2005-01-07 07:47:09 +00005536 /// run - This is the main entry point to this class.
5537 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005538 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005539}
5540