blob: c3289fc1e70b750a5f0369e932ee3617806cc4ec [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;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000655 case ISD::FRAMEADDR:
656 case ISD::RETURNADDR:
657 // The only option for these nodes is to custom lower them. If the target
658 // does not custom lower them, then return zero.
659 Tmp1 = TLI.LowerOperation(Op, DAG);
660 if (Tmp1.Val)
661 Result = Tmp1;
662 else
663 Result = DAG.getConstant(0, TLI.getPointerTy());
664 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000665 case ISD::AssertSext:
666 case ISD::AssertZext:
667 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000668 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000669 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000670 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000671 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000672 Result = Node->getOperand(Op.ResNo);
673 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000674 case ISD::CopyFromReg:
675 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000676 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000677 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000678 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000679 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000680 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000681 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000682 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000683 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
684 } else {
685 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
686 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000687 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
688 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000689 // Since CopyFromReg produces two values, make sure to remember that we
690 // legalized both of them.
691 AddLegalizedOperand(Op.getValue(0), Result);
692 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
693 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000694 case ISD::UNDEF: {
695 MVT::ValueType VT = Op.getValueType();
696 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000697 default: assert(0 && "This action is not supported yet!");
698 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000699 if (MVT::isInteger(VT))
700 Result = DAG.getConstant(0, VT);
701 else if (MVT::isFloatingPoint(VT))
702 Result = DAG.getConstantFP(0, VT);
703 else
704 assert(0 && "Unknown value type!");
705 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000706 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000707 break;
708 }
709 break;
710 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000711
Chris Lattner48b61a72006-03-28 00:40:33 +0000712 case ISD::INTRINSIC_W_CHAIN:
713 case ISD::INTRINSIC_WO_CHAIN:
714 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000715 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000716 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
717 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000718 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000719
720 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000721 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000722 TargetLowering::Custom) {
723 Tmp3 = TLI.LowerOperation(Result, DAG);
724 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000725 }
726
727 if (Result.Val->getNumValues() == 1) break;
728
729 // Must have return value and chain result.
730 assert(Result.Val->getNumValues() == 2 &&
731 "Cannot return more than two values!");
732
733 // Since loads produce two values, make sure to remember that we
734 // legalized both of them.
735 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
736 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
737 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000738 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000739
740 case ISD::LOCATION:
741 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
742 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
743
744 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
745 case TargetLowering::Promote:
746 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000747 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000748 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000749 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000750 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000751
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000752 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000753 const std::string &FName =
754 cast<StringSDNode>(Node->getOperand(3))->getValue();
755 const std::string &DirName =
756 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000757 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000758
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000759 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000760 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000761 SDOperand LineOp = Node->getOperand(1);
762 SDOperand ColOp = Node->getOperand(2);
763
764 if (useDEBUG_LOC) {
765 Ops.push_back(LineOp); // line #
766 Ops.push_back(ColOp); // col #
767 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000768 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000769 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000770 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
771 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000772 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000773 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000774 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000775 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000776 } else {
777 Result = Tmp1; // chain
778 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000779 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000780 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000781 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000782 if (Tmp1 != Node->getOperand(0) ||
783 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000784 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000785 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000786 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
787 Ops.push_back(Node->getOperand(1)); // line # must be legal.
788 Ops.push_back(Node->getOperand(2)); // col # must be legal.
789 } else {
790 // Otherwise promote them.
791 Ops.push_back(PromoteOp(Node->getOperand(1)));
792 Ops.push_back(PromoteOp(Node->getOperand(2)));
793 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000794 Ops.push_back(Node->getOperand(3)); // filename must be legal.
795 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000796 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000797 }
798 break;
799 }
800 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000801
802 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000803 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000804 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000805 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000806 case TargetLowering::Legal:
807 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
808 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
809 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
810 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000811 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000812 break;
813 }
814 break;
815
Jim Laskey1ee29252007-01-26 14:34:52 +0000816 case ISD::LABEL:
817 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
818 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000819 default: assert(0 && "This action is not supported yet!");
820 case TargetLowering::Legal:
821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
822 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000823 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000824 break;
825 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000826 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000827
Chris Lattner3e928bb2005-01-07 07:47:09 +0000828 case ISD::Constant:
829 // We know we don't need to expand constants here, constants only have one
830 // value and we check that it is fine above.
831
832 // FIXME: Maybe we should handle things like targets that don't support full
833 // 32-bit immediates?
834 break;
835 case ISD::ConstantFP: {
836 // Spill FP immediates to the constant pool if the target cannot directly
837 // codegen them. Targets often have some immediate values that can be
838 // efficiently generated into an FP register without a load. We explicitly
839 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000840 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
841
842 // Check to see if this FP immediate is already legal.
843 bool isLegal = false;
844 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
845 E = TLI.legal_fpimm_end(); I != E; ++I)
846 if (CFP->isExactlyValue(*I)) {
847 isLegal = true;
848 break;
849 }
850
Chris Lattner3181a772006-01-29 06:26:56 +0000851 // If this is a legal constant, turn it into a TargetConstantFP node.
852 if (isLegal) {
853 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
854 break;
855 }
856
857 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Custom:
860 Tmp3 = TLI.LowerOperation(Result, DAG);
861 if (Tmp3.Val) {
862 Result = Tmp3;
863 break;
864 }
865 // FALLTHROUGH
866 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +0000867 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000868 }
869 break;
870 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000871 case ISD::TokenFactor:
872 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000873 Tmp1 = LegalizeOp(Node->getOperand(0));
874 Tmp2 = LegalizeOp(Node->getOperand(1));
875 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
876 } else if (Node->getNumOperands() == 3) {
877 Tmp1 = LegalizeOp(Node->getOperand(0));
878 Tmp2 = LegalizeOp(Node->getOperand(1));
879 Tmp3 = LegalizeOp(Node->getOperand(2));
880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +0000881 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000882 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +0000883 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000884 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
885 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000886 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +0000887 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000888 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000889
890 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000891 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +0000892 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +0000893 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
894 assert(Tmp3.Val && "Target didn't custom lower this node!");
895 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
896 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +0000897
Chris Lattnerf4ec8172006-05-16 22:53:20 +0000898 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +0000899 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +0000900 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
901 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +0000902 if (Op.ResNo == i)
903 Tmp2 = Tmp1;
904 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
905 }
906 return Tmp2;
Chris Lattnerfdfded52006-04-12 16:20:43 +0000907
Chris Lattnerb2827b02006-03-19 00:52:58 +0000908 case ISD::BUILD_VECTOR:
909 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +0000910 default: assert(0 && "This action is not supported yet!");
911 case TargetLowering::Custom:
912 Tmp3 = TLI.LowerOperation(Result, DAG);
913 if (Tmp3.Val) {
914 Result = Tmp3;
915 break;
916 }
917 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +0000918 case TargetLowering::Expand:
919 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +0000920 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +0000921 }
Chris Lattner2332b9f2006-03-19 01:17:20 +0000922 break;
923 case ISD::INSERT_VECTOR_ELT:
924 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
925 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
926 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
927 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
928
929 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
930 Node->getValueType(0))) {
931 default: assert(0 && "This action is not supported yet!");
932 case TargetLowering::Legal:
933 break;
934 case TargetLowering::Custom:
935 Tmp3 = TLI.LowerOperation(Result, DAG);
936 if (Tmp3.Val) {
937 Result = Tmp3;
938 break;
939 }
940 // FALLTHROUGH
941 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +0000942 // If the insert index is a constant, codegen this as a scalar_to_vector,
943 // then a shuffle that inserts it into the right position in the vector.
944 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
945 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
946 Tmp1.getValueType(), Tmp2);
947
948 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
949 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
950 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
951
952 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
953 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
954 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000955 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +0000956 for (unsigned i = 0; i != NumElts; ++i) {
957 if (i != InsertPos->getValue())
958 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
959 else
960 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
961 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000962 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
963 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +0000964
965 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
966 Tmp1, ScVec, ShufMask);
967 Result = LegalizeOp(Result);
968 break;
969 }
970
Chris Lattner2332b9f2006-03-19 01:17:20 +0000971 // If the target doesn't support this, we have to spill the input vector
972 // to a temporary stack slot, update the element, then reload it. This is
973 // badness. We could also load the value into a vector register (either
974 // with a "move to register" or "extload into register" instruction, then
975 // permute it into place, if the idx is a constant and if the idx is
976 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000977 MVT::ValueType VT = Tmp1.getValueType();
978 MVT::ValueType EltVT = Tmp2.getValueType();
979 MVT::ValueType IdxVT = Tmp3.getValueType();
980 MVT::ValueType PtrVT = TLI.getPointerTy();
981 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +0000982 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000983 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000984
985 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000986 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
987 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +0000988 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +0000989 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
990 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
991 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +0000992 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +0000993 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +0000994 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +0000995 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +0000996 break;
997 }
998 }
999 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001000 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001001 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1002 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1003 break;
1004 }
1005
Chris Lattnerce872152006-03-19 06:31:19 +00001006 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1007 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1008 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1009 Node->getValueType(0))) {
1010 default: assert(0 && "This action is not supported yet!");
1011 case TargetLowering::Legal:
1012 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001013 case TargetLowering::Custom:
1014 Tmp3 = TLI.LowerOperation(Result, DAG);
1015 if (Tmp3.Val) {
1016 Result = Tmp3;
1017 break;
1018 }
1019 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001020 case TargetLowering::Expand:
1021 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001022 break;
1023 }
Chris Lattnerce872152006-03-19 06:31:19 +00001024 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001025 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001026 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1027 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1028 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1029
1030 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001031 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1032 default: assert(0 && "Unknown operation action!");
1033 case TargetLowering::Legal:
1034 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1035 "vector shuffle should not be created if not legal!");
1036 break;
1037 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001038 Tmp3 = TLI.LowerOperation(Result, DAG);
1039 if (Tmp3.Val) {
1040 Result = Tmp3;
1041 break;
1042 }
1043 // FALLTHROUGH
1044 case TargetLowering::Expand: {
1045 MVT::ValueType VT = Node->getValueType(0);
1046 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1047 MVT::ValueType PtrVT = TLI.getPointerTy();
1048 SDOperand Mask = Node->getOperand(2);
1049 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001050 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001051 for (unsigned i = 0; i != NumElems; ++i) {
1052 SDOperand Arg = Mask.getOperand(i);
1053 if (Arg.getOpcode() == ISD::UNDEF) {
1054 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1055 } else {
1056 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1057 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1058 if (Idx < NumElems)
1059 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1060 DAG.getConstant(Idx, PtrVT)));
1061 else
1062 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1063 DAG.getConstant(Idx - NumElems, PtrVT)));
1064 }
1065 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001066 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001067 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001068 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001069 case TargetLowering::Promote: {
1070 // Change base type to a different vector type.
1071 MVT::ValueType OVT = Node->getValueType(0);
1072 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1073
1074 // Cast the two input vectors.
1075 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1076 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1077
1078 // Convert the shuffle mask to the right # elements.
1079 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1080 assert(Tmp3.Val && "Shuffle not legal?");
1081 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1082 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1083 break;
1084 }
Chris Lattner87100e02006-03-20 01:52:29 +00001085 }
1086 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001087
1088 case ISD::EXTRACT_VECTOR_ELT:
1089 Tmp1 = LegalizeOp(Node->getOperand(0));
1090 Tmp2 = LegalizeOp(Node->getOperand(1));
1091 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnere35c2182006-03-21 21:02:03 +00001092
1093 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1094 Tmp1.getValueType())) {
1095 default: assert(0 && "This action is not supported yet!");
1096 case TargetLowering::Legal:
1097 break;
1098 case TargetLowering::Custom:
1099 Tmp3 = TLI.LowerOperation(Result, DAG);
1100 if (Tmp3.Val) {
1101 Result = Tmp3;
1102 break;
1103 }
1104 // FALLTHROUGH
Chris Lattner4aab2f42006-04-02 05:06:04 +00001105 case TargetLowering::Expand:
1106 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattnere35c2182006-03-21 21:02:03 +00001107 break;
1108 }
Chris Lattner384504c2006-03-21 20:44:12 +00001109 break;
1110
Chris Lattner15972212006-03-31 17:55:51 +00001111 case ISD::VEXTRACT_VECTOR_ELT:
1112 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner384504c2006-03-21 20:44:12 +00001113 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001114
Chris Lattner6831a812006-02-13 09:18:02 +00001115 case ISD::CALLSEQ_START: {
1116 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1117
1118 // Recursively Legalize all of the inputs of the call end that do not lead
1119 // to this call start. This ensures that any libcalls that need be inserted
1120 // are inserted *before* the CALLSEQ_START.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001121 {std::set<SDNode*> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001122 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001123 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1124 NodesLeadingTo);
1125 }
Chris Lattner6831a812006-02-13 09:18:02 +00001126
1127 // Now that we legalized all of the inputs (which may have inserted
1128 // libcalls) create the new CALLSEQ_START node.
1129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1130
1131 // Merge in the last call, to ensure that this call start after the last
1132 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001133 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001134 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1135 Tmp1 = LegalizeOp(Tmp1);
1136 }
Chris Lattner6831a812006-02-13 09:18:02 +00001137
1138 // Do not try to legalize the target-specific arguments (#1+).
1139 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001140 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001141 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001142 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001143 }
1144
1145 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001146 AddLegalizedOperand(Op.getValue(0), Result);
1147 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1148 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1149
Chris Lattner6831a812006-02-13 09:18:02 +00001150 // Now that the callseq_start and all of the non-call nodes above this call
1151 // sequence have been legalized, legalize the call itself. During this
1152 // process, no libcalls can/will be inserted, guaranteeing that no calls
1153 // can overlap.
1154 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1155 SDOperand InCallSEQ = LastCALLSEQ_END;
1156 // Note that we are selecting this call!
1157 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1158 IsLegalizingCall = true;
1159
1160 // Legalize the call, starting from the CALLSEQ_END.
1161 LegalizeOp(LastCALLSEQ_END);
1162 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1163 return Result;
1164 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001165 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001166 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1167 // will cause this node to be legalized as well as handling libcalls right.
1168 if (LastCALLSEQ_END.Val != Node) {
1169 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
1170 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
1171 assert(I != LegalizedNodes.end() &&
1172 "Legalizing the call start should have legalized this node!");
1173 return I->second;
1174 }
1175
1176 // Otherwise, the call start has been legalized and everything is going
1177 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001178 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001179 // Do not try to legalize the target-specific arguments (#1+), except for
1180 // an optional flag input.
1181 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1182 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001183 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001184 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001185 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001186 }
1187 } else {
1188 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1189 if (Tmp1 != Node->getOperand(0) ||
1190 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001191 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001192 Ops[0] = Tmp1;
1193 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001194 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001195 }
Chris Lattner6a542892006-01-24 05:48:21 +00001196 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001197 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001198 // This finishes up call legalization.
1199 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001200
1201 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1202 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1203 if (Node->getNumValues() == 2)
1204 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1205 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001206 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerfa404e82005-01-09 19:03:49 +00001207 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1208 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1209 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001211
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001212 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001213 Tmp2 = Result.getValue(1);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001214 switch (TLI.getOperationAction(Node->getOpcode(),
1215 Node->getValueType(0))) {
1216 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001217 case TargetLowering::Expand: {
1218 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1219 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1220 " not tell us which reg is the stack pointer!");
1221 SDOperand Chain = Tmp1.getOperand(0);
1222 SDOperand Size = Tmp2.getOperand(1);
1223 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1224 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1225 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001226 Tmp1 = LegalizeOp(Tmp1);
1227 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001228 break;
1229 }
1230 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001231 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1232 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001233 Tmp1 = LegalizeOp(Tmp3);
1234 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001235 }
Chris Lattner903d2782006-01-15 08:54:32 +00001236 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001237 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001238 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001239 }
Chris Lattner903d2782006-01-15 08:54:32 +00001240 // Since this op produce two values, make sure to remember that we
1241 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001242 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1243 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001244 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001245 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001246 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001247 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001248 bool Changed = false;
1249 // Legalize all of the operands of the inline asm, in case they are nodes
1250 // that need to be expanded or something. Note we skip the asm string and
1251 // all of the TargetConstant flags.
1252 SDOperand Op = LegalizeOp(Ops[0]);
1253 Changed = Op != Ops[0];
1254 Ops[0] = Op;
1255
1256 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1257 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1258 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1259 for (++i; NumVals; ++i, --NumVals) {
1260 SDOperand Op = LegalizeOp(Ops[i]);
1261 if (Op != Ops[i]) {
1262 Changed = true;
1263 Ops[i] = Op;
1264 }
1265 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001266 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001267
1268 if (HasInFlag) {
1269 Op = LegalizeOp(Ops.back());
1270 Changed |= Op != Ops.back();
1271 Ops.back() = Op;
1272 }
1273
1274 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001275 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001276
1277 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001278 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001279 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1280 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001281 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001282 case ISD::BR:
1283 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001284 // Ensure that libcalls are emitted before a branch.
1285 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1286 Tmp1 = LegalizeOp(Tmp1);
1287 LastCALLSEQ_END = DAG.getEntryNode();
1288
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001289 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001290 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001291 case ISD::BRIND:
1292 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1293 // Ensure that libcalls are emitted before a branch.
1294 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1295 Tmp1 = LegalizeOp(Tmp1);
1296 LastCALLSEQ_END = DAG.getEntryNode();
1297
1298 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1299 default: assert(0 && "Indirect target must be legal type (pointer)!");
1300 case Legal:
1301 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1302 break;
1303 }
1304 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1305 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001306 case ISD::BR_JT:
1307 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1308 // Ensure that libcalls are emitted before a branch.
1309 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1310 Tmp1 = LegalizeOp(Tmp1);
1311 LastCALLSEQ_END = DAG.getEntryNode();
1312
1313 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1314 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1315
1316 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1317 default: assert(0 && "This action is not supported yet!");
1318 case TargetLowering::Legal: break;
1319 case TargetLowering::Custom:
1320 Tmp1 = TLI.LowerOperation(Result, DAG);
1321 if (Tmp1.Val) Result = Tmp1;
1322 break;
1323 case TargetLowering::Expand: {
1324 SDOperand Chain = Result.getOperand(0);
1325 SDOperand Table = Result.getOperand(1);
1326 SDOperand Index = Result.getOperand(2);
1327
1328 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001329 MachineFunction &MF = DAG.getMachineFunction();
1330 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001331 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1332 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001333
1334 SDOperand LD;
1335 switch (EntrySize) {
1336 default: assert(0 && "Size of jump table not supported yet."); break;
1337 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1338 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1339 }
1340
1341 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001342 // For PIC, the sequence is:
1343 // BRIND(load(Jumptable + index) + RelocBase)
1344 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1345 SDOperand Reloc;
1346 if (TLI.usesGlobalOffsetTable())
1347 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1348 else
1349 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001350 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001351 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1352 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1353 } else {
1354 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1355 }
1356 }
1357 }
1358 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001359 case ISD::BRCOND:
1360 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001361 // Ensure that libcalls are emitted before a return.
1362 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1363 Tmp1 = LegalizeOp(Tmp1);
1364 LastCALLSEQ_END = DAG.getEntryNode();
1365
Chris Lattner47e92232005-01-18 19:27:06 +00001366 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1367 case Expand: assert(0 && "It's impossible to expand bools");
1368 case Legal:
1369 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1370 break;
1371 case Promote:
1372 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001373
1374 // The top bits of the promoted condition are not necessarily zero, ensure
1375 // that the value is properly zero extended.
1376 if (!TLI.MaskedValueIsZero(Tmp2,
1377 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1378 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001379 break;
1380 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001381
1382 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001383 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001384
1385 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1386 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001387 case TargetLowering::Legal: break;
1388 case TargetLowering::Custom:
1389 Tmp1 = TLI.LowerOperation(Result, DAG);
1390 if (Tmp1.Val) Result = Tmp1;
1391 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001392 case TargetLowering::Expand:
1393 // Expand brcond's setcc into its constituent parts and create a BR_CC
1394 // Node.
1395 if (Tmp2.getOpcode() == ISD::SETCC) {
1396 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1397 Tmp2.getOperand(0), Tmp2.getOperand(1),
1398 Node->getOperand(2));
1399 } else {
1400 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1401 DAG.getCondCode(ISD::SETNE), Tmp2,
1402 DAG.getConstant(0, Tmp2.getValueType()),
1403 Node->getOperand(2));
1404 }
1405 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001406 }
1407 break;
1408 case ISD::BR_CC:
1409 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001410 // Ensure that libcalls are emitted before a branch.
1411 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1412 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001413 Tmp2 = Node->getOperand(2); // LHS
1414 Tmp3 = Node->getOperand(3); // RHS
1415 Tmp4 = Node->getOperand(1); // CC
1416
1417 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001418 LastCALLSEQ_END = DAG.getEntryNode();
1419
Nate Begeman750ac1b2006-02-01 07:19:44 +00001420 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1421 // the LHS is a legal SETCC itself. In this case, we need to compare
1422 // the result against zero to select between true and false values.
1423 if (Tmp3.Val == 0) {
1424 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1425 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001426 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001427
1428 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1429 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001430
Chris Lattner181b7a32005-12-17 23:46:46 +00001431 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1432 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001433 case TargetLowering::Legal: break;
1434 case TargetLowering::Custom:
1435 Tmp4 = TLI.LowerOperation(Result, DAG);
1436 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001437 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001438 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001439 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001440 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001441 LoadSDNode *LD = cast<LoadSDNode>(Node);
1442 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1443 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001444
Evan Cheng466685d2006-10-09 20:57:25 +00001445 ISD::LoadExtType ExtType = LD->getExtensionType();
1446 if (ExtType == ISD::NON_EXTLOAD) {
1447 MVT::ValueType VT = Node->getValueType(0);
1448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1449 Tmp3 = Result.getValue(0);
1450 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001451
Evan Cheng466685d2006-10-09 20:57:25 +00001452 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1453 default: assert(0 && "This action is not supported yet!");
1454 case TargetLowering::Legal: break;
1455 case TargetLowering::Custom:
1456 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1457 if (Tmp1.Val) {
1458 Tmp3 = LegalizeOp(Tmp1);
1459 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001460 }
Evan Cheng466685d2006-10-09 20:57:25 +00001461 break;
1462 case TargetLowering::Promote: {
1463 // Only promote a load of vector type to another.
1464 assert(MVT::isVector(VT) && "Cannot promote this load!");
1465 // Change base type to a different vector type.
1466 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1467
1468 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1469 LD->getSrcValueOffset());
1470 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1471 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001472 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001473 }
Evan Cheng466685d2006-10-09 20:57:25 +00001474 }
1475 // Since loads produce two values, make sure to remember that we
1476 // legalized both of them.
1477 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1478 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1479 return Op.ResNo ? Tmp4 : Tmp3;
1480 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001481 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001482 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1483 default: assert(0 && "This action is not supported yet!");
1484 case TargetLowering::Promote:
1485 assert(SrcVT == MVT::i1 &&
1486 "Can only promote extending LOAD from i1 -> i8!");
1487 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1488 LD->getSrcValue(), LD->getSrcValueOffset(),
1489 MVT::i8);
1490 Tmp1 = Result.getValue(0);
1491 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001492 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001493 case TargetLowering::Custom:
1494 isCustom = true;
1495 // FALLTHROUGH
1496 case TargetLowering::Legal:
1497 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1498 Tmp1 = Result.getValue(0);
1499 Tmp2 = Result.getValue(1);
1500
1501 if (isCustom) {
1502 Tmp3 = TLI.LowerOperation(Result, DAG);
1503 if (Tmp3.Val) {
1504 Tmp1 = LegalizeOp(Tmp3);
1505 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1506 }
1507 }
1508 break;
1509 case TargetLowering::Expand:
1510 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1511 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1512 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1513 LD->getSrcValueOffset());
1514 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1515 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1516 Tmp2 = LegalizeOp(Load.getValue(1));
1517 break;
1518 }
1519 assert(ExtType != ISD::EXTLOAD && "EXTLOAD should always be supported!");
1520 // Turn the unsupported load into an EXTLOAD followed by an explicit
1521 // zero/sign extend inreg.
1522 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1523 Tmp1, Tmp2, LD->getSrcValue(),
1524 LD->getSrcValueOffset(), SrcVT);
1525 SDOperand ValRes;
1526 if (ExtType == ISD::SEXTLOAD)
1527 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1528 Result, DAG.getValueType(SrcVT));
1529 else
1530 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1531 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1532 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1533 break;
1534 }
1535 // Since loads produce two values, make sure to remember that we legalized
1536 // both of them.
1537 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1538 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1539 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001540 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001541 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001542 case ISD::EXTRACT_ELEMENT: {
1543 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1544 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001545 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001546 case Legal:
1547 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1548 // 1 -> Hi
1549 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1550 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1551 TLI.getShiftAmountTy()));
1552 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1553 } else {
1554 // 0 -> Lo
1555 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1556 Node->getOperand(0));
1557 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001558 break;
1559 case Expand:
1560 // Get both the low and high parts.
1561 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1562 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1563 Result = Tmp2; // 1 -> Hi
1564 else
1565 Result = Tmp1; // 0 -> Lo
1566 break;
1567 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001568 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001569 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001570
1571 case ISD::CopyToReg:
1572 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001573
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001574 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001575 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001576 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001577 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001578 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001579 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001580 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001581 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001582 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001583 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001584 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1585 Tmp3);
1586 } else {
1587 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001588 }
1589
1590 // Since this produces two values, make sure to remember that we legalized
1591 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001592 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001593 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001594 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001595 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001596 break;
1597
1598 case ISD::RET:
1599 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001600
1601 // Ensure that libcalls are emitted before a return.
1602 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1603 Tmp1 = LegalizeOp(Tmp1);
1604 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001605
Chris Lattner3e928bb2005-01-07 07:47:09 +00001606 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001607 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001608 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001609 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001610 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001611 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001612 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001613 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001614 case Expand:
1615 if (Tmp2.getValueType() != MVT::Vector) {
1616 SDOperand Lo, Hi;
1617 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng13acce32006-12-11 19:27:14 +00001618 if (Hi.Val)
1619 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1620 else
1621 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001622 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001623 } else {
1624 SDNode *InVal = Tmp2.Val;
1625 unsigned NumElems =
1626 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1627 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1628
1629 // Figure out if there is a Packed type corresponding to this Vector
1630 // type. If so, convert to the packed type.
1631 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1632 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1633 // Turn this into a return of the packed type.
1634 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001636 } else if (NumElems == 1) {
1637 // Turn this into a return of the scalar type.
1638 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001640
1641 // FIXME: Returns of gcc generic vectors smaller than a legal type
1642 // should be returned in integer registers!
1643
Chris Lattnerf87324e2006-04-11 01:31:51 +00001644 // The scalarized value type may not be legal, e.g. it might require
1645 // promotion or expansion. Relegalize the return.
1646 Result = LegalizeOp(Result);
1647 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001648 // FIXME: Returns of gcc generic vectors larger than a legal vector
1649 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001650 SDOperand Lo, Hi;
1651 SplitVectorOp(Tmp2, Lo, Hi);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001652 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001653 Result = LegalizeOp(Result);
1654 }
1655 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001656 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001657 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001658 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001659 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001660 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001661 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001662 }
1663 break;
1664 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001665 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001666 break;
1667 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001668 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001669 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001670 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001671 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1672 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001673 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001674 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001675 break;
1676 case Expand: {
1677 SDOperand Lo, Hi;
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001678 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1679 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001680 ExpandOp(Node->getOperand(i), Lo, Hi);
1681 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001682 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001683 if (Hi.Val) {
1684 NewValues.push_back(Hi);
1685 NewValues.push_back(Node->getOperand(i+1));
1686 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001687 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001688 }
1689 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001690 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001691 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001692
1693 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001694 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001695 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001696 Result = DAG.getNode(ISD::RET, MVT::Other,
1697 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001698 break;
1699 }
1700 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001701
Chris Lattner6862dbc2006-01-29 21:02:23 +00001702 if (Result.getOpcode() == ISD::RET) {
1703 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1704 default: assert(0 && "This action is not supported yet!");
1705 case TargetLowering::Legal: break;
1706 case TargetLowering::Custom:
1707 Tmp1 = TLI.LowerOperation(Result, DAG);
1708 if (Tmp1.Val) Result = Tmp1;
1709 break;
1710 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001711 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001712 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001713 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001714 StoreSDNode *ST = cast<StoreSDNode>(Node);
1715 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1716 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001717
Evan Cheng8b2794a2006-10-13 21:14:26 +00001718 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001719 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1720 // FIXME: We shouldn't do this for TargetConstantFP's.
1721 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1722 // to phase ordering between legalized code and the dag combiner. This
1723 // probably means that we need to integrate dag combiner and legalizer
1724 // together.
1725 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1726 if (CFP->getValueType(0) == MVT::f32) {
1727 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1728 } else {
1729 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1730 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1731 }
1732 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1733 ST->getSrcValueOffset());
1734 break;
1735 }
1736
Evan Cheng8b2794a2006-10-13 21:14:26 +00001737 switch (getTypeAction(ST->getStoredVT())) {
1738 case Legal: {
1739 Tmp3 = LegalizeOp(ST->getValue());
1740 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1741 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001742
Evan Cheng8b2794a2006-10-13 21:14:26 +00001743 MVT::ValueType VT = Tmp3.getValueType();
1744 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1745 default: assert(0 && "This action is not supported yet!");
1746 case TargetLowering::Legal: break;
1747 case TargetLowering::Custom:
1748 Tmp1 = TLI.LowerOperation(Result, DAG);
1749 if (Tmp1.Val) Result = Tmp1;
1750 break;
1751 case TargetLowering::Promote:
1752 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1753 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1754 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1755 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1756 ST->getSrcValue(), ST->getSrcValueOffset());
1757 break;
1758 }
1759 break;
1760 }
1761 case Promote:
1762 // Truncate the value and store the result.
1763 Tmp3 = PromoteOp(ST->getValue());
1764 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1765 ST->getSrcValueOffset(), ST->getStoredVT());
1766 break;
1767
1768 case Expand:
1769 unsigned IncrementSize = 0;
1770 SDOperand Lo, Hi;
1771
1772 // If this is a vector type, then we have to calculate the increment as
1773 // the product of the element size in bytes, and the number of elements
1774 // in the high half of the vector.
1775 if (ST->getValue().getValueType() == MVT::Vector) {
1776 SDNode *InVal = ST->getValue().Val;
1777 unsigned NumElems =
1778 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1779 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1780
1781 // Figure out if there is a Packed type corresponding to this Vector
1782 // type. If so, convert to the packed type.
1783 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1784 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1785 // Turn this into a normal store of the packed type.
1786 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1787 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1788 ST->getSrcValueOffset());
1789 Result = LegalizeOp(Result);
1790 break;
1791 } else if (NumElems == 1) {
1792 // Turn this into a normal store of the scalar type.
1793 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1794 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1795 ST->getSrcValueOffset());
1796 // The scalarized value type may not be legal, e.g. it might require
1797 // promotion or expansion. Relegalize the scalar store.
1798 Result = LegalizeOp(Result);
1799 break;
1800 } else {
1801 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1802 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1803 }
1804 } else {
1805 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001806 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001807
1808 if (!TLI.isLittleEndian())
1809 std::swap(Lo, Hi);
1810 }
1811
1812 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1813 ST->getSrcValueOffset());
Evan Cheng7b2b5c82006-12-12 19:53:13 +00001814
1815 if (Hi.Val == NULL) {
1816 // Must be int <-> float one-to-one expansion.
1817 Result = Lo;
1818 break;
1819 }
1820
Evan Cheng8b2794a2006-10-13 21:14:26 +00001821 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1822 getIntPtrConstant(IncrementSize));
1823 assert(isTypeLegal(Tmp2.getValueType()) &&
1824 "Pointers must be legal!");
1825 // FIXME: This sets the srcvalue of both halves to be the same, which is
1826 // wrong.
1827 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1828 ST->getSrcValueOffset());
1829 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1830 break;
1831 }
1832 } else {
1833 // Truncating store
1834 assert(isTypeLegal(ST->getValue().getValueType()) &&
1835 "Cannot handle illegal TRUNCSTORE yet!");
1836 Tmp3 = LegalizeOp(ST->getValue());
1837
1838 // The only promote case we handle is TRUNCSTORE:i1 X into
1839 // -> TRUNCSTORE:i8 (and X, 1)
1840 if (ST->getStoredVT() == MVT::i1 &&
1841 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1842 // Promote the bool to a mask then store.
1843 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1844 DAG.getConstant(1, Tmp3.getValueType()));
1845 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1846 ST->getSrcValueOffset(), MVT::i8);
1847 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1848 Tmp2 != ST->getBasePtr()) {
1849 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1850 ST->getOffset());
1851 }
1852
1853 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1854 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001855 default: assert(0 && "This action is not supported yet!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00001856 case TargetLowering::Legal: break;
Chris Lattner456a93a2006-01-28 07:39:30 +00001857 case TargetLowering::Custom:
1858 Tmp1 = TLI.LowerOperation(Result, DAG);
1859 if (Tmp1.Val) Result = Tmp1;
1860 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001861 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001862 }
1863 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001864 }
Andrew Lenharth95762122005-03-31 21:24:06 +00001865 case ISD::PCMARKER:
1866 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001867 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001868 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001869 case ISD::STACKSAVE:
1870 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001871 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1872 Tmp1 = Result.getValue(0);
1873 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001874
Chris Lattner140d53c2006-01-13 02:50:02 +00001875 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1876 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001877 case TargetLowering::Legal: break;
1878 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001879 Tmp3 = TLI.LowerOperation(Result, DAG);
1880 if (Tmp3.Val) {
1881 Tmp1 = LegalizeOp(Tmp3);
1882 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00001883 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001884 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001885 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001886 // Expand to CopyFromReg if the target set
1887 // StackPointerRegisterToSaveRestore.
1888 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001889 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001890 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001891 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001892 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001893 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1894 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001895 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001896 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00001897 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001898
1899 // Since stacksave produce two values, make sure to remember that we
1900 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001901 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1902 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1903 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001904
Chris Lattner140d53c2006-01-13 02:50:02 +00001905 case ISD::STACKRESTORE:
1906 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1907 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00001909
1910 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1911 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001912 case TargetLowering::Legal: break;
1913 case TargetLowering::Custom:
1914 Tmp1 = TLI.LowerOperation(Result, DAG);
1915 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00001916 break;
1917 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00001918 // Expand to CopyToReg if the target set
1919 // StackPointerRegisterToSaveRestore.
1920 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1921 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1922 } else {
1923 Result = Tmp1;
1924 }
Chris Lattner140d53c2006-01-13 02:50:02 +00001925 break;
1926 }
1927 break;
1928
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001929 case ISD::READCYCLECOUNTER:
1930 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001931 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00001932 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1933 Node->getValueType(0))) {
1934 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001935 case TargetLowering::Legal:
1936 Tmp1 = Result.getValue(0);
1937 Tmp2 = Result.getValue(1);
1938 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00001939 case TargetLowering::Custom:
1940 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001941 Tmp1 = LegalizeOp(Result.getValue(0));
1942 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00001943 break;
1944 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00001945
1946 // Since rdcc produce two values, make sure to remember that we legalized
1947 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00001948 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1949 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001950 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001951
Chris Lattner2ee743f2005-01-14 22:08:15 +00001952 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001953 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1954 case Expand: assert(0 && "It's impossible to expand bools");
1955 case Legal:
1956 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1957 break;
1958 case Promote:
1959 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00001960 // Make sure the condition is either zero or one.
1961 if (!TLI.MaskedValueIsZero(Tmp1,
1962 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
1963 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001964 break;
1965 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001966 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001967 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001968
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001969 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00001970
Nate Begemanb942a3d2005-08-23 04:29:48 +00001971 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001972 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001973 case TargetLowering::Legal: break;
1974 case TargetLowering::Custom: {
1975 Tmp1 = TLI.LowerOperation(Result, DAG);
1976 if (Tmp1.Val) Result = Tmp1;
1977 break;
1978 }
Nate Begeman9373a812005-08-10 20:51:12 +00001979 case TargetLowering::Expand:
1980 if (Tmp1.getOpcode() == ISD::SETCC) {
1981 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1982 Tmp2, Tmp3,
1983 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1984 } else {
1985 Result = DAG.getSelectCC(Tmp1,
1986 DAG.getConstant(0, Tmp1.getValueType()),
1987 Tmp2, Tmp3, ISD::SETNE);
1988 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001989 break;
1990 case TargetLowering::Promote: {
1991 MVT::ValueType NVT =
1992 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1993 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00001994 if (MVT::isVector(Tmp2.getValueType())) {
1995 ExtOp = ISD::BIT_CONVERT;
1996 TruncOp = ISD::BIT_CONVERT;
1997 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00001998 ExtOp = ISD::ANY_EXTEND;
1999 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002000 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002001 ExtOp = ISD::FP_EXTEND;
2002 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002003 }
2004 // Promote each of the values to the new type.
2005 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2006 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2007 // Perform the larger operation, then round down.
2008 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2009 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2010 break;
2011 }
2012 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002013 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002014 case ISD::SELECT_CC: {
2015 Tmp1 = Node->getOperand(0); // LHS
2016 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002017 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2018 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002019 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002020
Nate Begeman750ac1b2006-02-01 07:19:44 +00002021 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2022
2023 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2024 // the LHS is a legal SETCC itself. In this case, we need to compare
2025 // the result against zero to select between true and false values.
2026 if (Tmp2.Val == 0) {
2027 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2028 CC = DAG.getCondCode(ISD::SETNE);
2029 }
2030 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2031
2032 // Everything is legal, see if we should expand this op or something.
2033 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2034 default: assert(0 && "This action is not supported yet!");
2035 case TargetLowering::Legal: break;
2036 case TargetLowering::Custom:
2037 Tmp1 = TLI.LowerOperation(Result, DAG);
2038 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002039 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002040 }
2041 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002042 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002043 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002044 Tmp1 = Node->getOperand(0);
2045 Tmp2 = Node->getOperand(1);
2046 Tmp3 = Node->getOperand(2);
2047 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2048
2049 // If we had to Expand the SetCC operands into a SELECT node, then it may
2050 // not always be possible to return a true LHS & RHS. In this case, just
2051 // return the value we legalized, returned in the LHS
2052 if (Tmp2.Val == 0) {
2053 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002054 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002055 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002056
Chris Lattner73e142f2006-01-30 22:43:50 +00002057 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002058 default: assert(0 && "Cannot handle this action for SETCC yet!");
2059 case TargetLowering::Custom:
2060 isCustom = true;
2061 // FALLTHROUGH.
2062 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002063 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002064 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002065 Tmp4 = TLI.LowerOperation(Result, DAG);
2066 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002067 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002068 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002069 case TargetLowering::Promote: {
2070 // First step, figure out the appropriate operation to use.
2071 // Allow SETCC to not be supported for all legal data types
2072 // Mostly this targets FP
2073 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
2074 MVT::ValueType OldVT = NewInTy;
2075
2076 // Scan for the appropriate larger type to use.
2077 while (1) {
2078 NewInTy = (MVT::ValueType)(NewInTy+1);
2079
2080 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2081 "Fell off of the edge of the integer world");
2082 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2083 "Fell off of the edge of the floating point world");
2084
2085 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002086 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002087 break;
2088 }
2089 if (MVT::isInteger(NewInTy))
2090 assert(0 && "Cannot promote Legal Integer SETCC yet");
2091 else {
2092 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2093 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2094 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002095 Tmp1 = LegalizeOp(Tmp1);
2096 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002098 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002099 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002100 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002101 case TargetLowering::Expand:
2102 // Expand a setcc node into a select_cc of the same condition, lhs, and
2103 // rhs that selects between const 1 (true) and const 0 (false).
2104 MVT::ValueType VT = Node->getValueType(0);
2105 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2106 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002107 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002108 break;
2109 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002110 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002111 case ISD::MEMSET:
2112 case ISD::MEMCPY:
2113 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002114 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002115 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2116
2117 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2118 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2119 case Expand: assert(0 && "Cannot expand a byte!");
2120 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002121 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002122 break;
2123 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002124 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002125 break;
2126 }
2127 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002128 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002129 }
Chris Lattner272455b2005-02-02 03:44:41 +00002130
2131 SDOperand Tmp4;
2132 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002133 case Expand: {
2134 // Length is too big, just take the lo-part of the length.
2135 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002136 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002137 break;
2138 }
Chris Lattnere5605212005-01-28 22:29:18 +00002139 case Legal:
2140 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002141 break;
2142 case Promote:
2143 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002144 break;
2145 }
2146
2147 SDOperand Tmp5;
2148 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2149 case Expand: assert(0 && "Cannot expand this yet!");
2150 case Legal:
2151 Tmp5 = LegalizeOp(Node->getOperand(4));
2152 break;
2153 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002154 Tmp5 = PromoteOp(Node->getOperand(4));
2155 break;
2156 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002157
2158 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2159 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002160 case TargetLowering::Custom:
2161 isCustom = true;
2162 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002163 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002164 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002165 if (isCustom) {
2166 Tmp1 = TLI.LowerOperation(Result, DAG);
2167 if (Tmp1.Val) Result = Tmp1;
2168 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002169 break;
2170 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002171 // Otherwise, the target does not support this operation. Lower the
2172 // operation to an explicit libcall as appropriate.
2173 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002174 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002175 TargetLowering::ArgListTy Args;
2176 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002177
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002178 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002179 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencerb47b25c2007-01-03 04:22:32 +00002180 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Anton Korobeynikovac2b2cf2007-01-28 16:04:40 +00002181 Entry.isInReg = false;
Reid Spencer47857812006-12-31 05:55:36 +00002182 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002183 // Extend the (previously legalized) ubyte argument to be an int value
2184 // for the call.
2185 if (Tmp3.getValueType() > MVT::i32)
2186 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2187 else
2188 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencer47857812006-12-31 05:55:36 +00002189 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
Anton Korobeynikovac2b2cf2007-01-28 16:04:40 +00002190 Entry.isInReg = false;
Reid Spencer47857812006-12-31 05:55:36 +00002191 Args.push_back(Entry);
2192 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2193 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002194
2195 FnName = "memset";
2196 } else if (Node->getOpcode() == ISD::MEMCPY ||
2197 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002198 Entry.Ty = IntPtrTy; Entry.isSigned = false; Entry.isInReg = false;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002199 Entry.Node = Tmp2; Args.push_back(Entry);
2200 Entry.Node = Tmp3; Args.push_back(Entry);
2201 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002202 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2203 } else {
2204 assert(0 && "Unknown op!");
2205 }
Chris Lattner45982da2005-05-12 16:53:42 +00002206
Chris Lattnere1bd8222005-01-11 05:57:22 +00002207 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002208 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002209 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002210 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002211 break;
2212 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002213 }
2214 break;
2215 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002216
Chris Lattner5b359c62005-04-02 04:00:59 +00002217 case ISD::SHL_PARTS:
2218 case ISD::SRA_PARTS:
2219 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002220 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002221 bool Changed = false;
2222 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2223 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2224 Changed |= Ops.back() != Node->getOperand(i);
2225 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002226 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002227 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002228
Evan Cheng05a2d562006-01-09 18:31:59 +00002229 switch (TLI.getOperationAction(Node->getOpcode(),
2230 Node->getValueType(0))) {
2231 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002232 case TargetLowering::Legal: break;
2233 case TargetLowering::Custom:
2234 Tmp1 = TLI.LowerOperation(Result, DAG);
2235 if (Tmp1.Val) {
2236 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002237 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002238 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002239 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2240 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002241 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002242 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002243 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002244 return RetVal;
2245 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002246 break;
2247 }
2248
Chris Lattner2c8086f2005-04-02 05:00:07 +00002249 // Since these produce multiple values, make sure to remember that we
2250 // legalized all of them.
2251 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2252 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2253 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002254 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002255
2256 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002257 case ISD::ADD:
2258 case ISD::SUB:
2259 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002260 case ISD::MULHS:
2261 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002262 case ISD::UDIV:
2263 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002264 case ISD::AND:
2265 case ISD::OR:
2266 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002267 case ISD::SHL:
2268 case ISD::SRL:
2269 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002270 case ISD::FADD:
2271 case ISD::FSUB:
2272 case ISD::FMUL:
2273 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002274 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002275 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2276 case Expand: assert(0 && "Not possible");
2277 case Legal:
2278 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2279 break;
2280 case Promote:
2281 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2282 break;
2283 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002284
2285 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002286
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002287 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002288 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002289 case TargetLowering::Legal: break;
2290 case TargetLowering::Custom:
2291 Tmp1 = TLI.LowerOperation(Result, DAG);
2292 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002293 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002294 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002295 if (Node->getValueType(0) == MVT::i32) {
2296 switch (Node->getOpcode()) {
2297 default: assert(0 && "Do not know how to expand this integer BinOp!");
2298 case ISD::UDIV:
2299 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002300 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2301 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002302 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002303 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002304 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002305 };
2306 break;
2307 }
2308
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002309 assert(MVT::isVector(Node->getValueType(0)) &&
2310 "Cannot expand this binary operator!");
2311 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002312 SmallVector<SDOperand, 8> Ops;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002313 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2314 MVT::ValueType PtrVT = TLI.getPointerTy();
2315 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2316 i != e; ++i) {
2317 SDOperand Idx = DAG.getConstant(i, PtrVT);
2318 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2319 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2320 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2321 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002322 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2323 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002324 break;
2325 }
Evan Chengcc987612006-04-12 21:20:24 +00002326 case TargetLowering::Promote: {
2327 switch (Node->getOpcode()) {
2328 default: assert(0 && "Do not know how to promote this BinOp!");
2329 case ISD::AND:
2330 case ISD::OR:
2331 case ISD::XOR: {
2332 MVT::ValueType OVT = Node->getValueType(0);
2333 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2334 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2335 // Bit convert each of the values to the new type.
2336 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2337 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2338 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2339 // Bit convert the result back the original type.
2340 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2341 break;
2342 }
2343 }
2344 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002345 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002346 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002347
2348 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2349 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2350 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2351 case Expand: assert(0 && "Not possible");
2352 case Legal:
2353 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2354 break;
2355 case Promote:
2356 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2357 break;
2358 }
2359
2360 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2361
2362 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2363 default: assert(0 && "Operation not supported");
2364 case TargetLowering::Custom:
2365 Tmp1 = TLI.LowerOperation(Result, DAG);
2366 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002367 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002368 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002369 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002370 // If this target supports fabs/fneg natively and select is cheap,
2371 // do this efficiently.
2372 if (!TLI.isSelectExpensive() &&
2373 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2374 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002375 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002376 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002377 // Get the sign bit of the RHS.
2378 MVT::ValueType IVT =
2379 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2380 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2381 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2382 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2383 // Get the absolute value of the result.
2384 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2385 // Select between the nabs and abs value based on the sign bit of
2386 // the input.
2387 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2388 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2389 AbsVal),
2390 AbsVal);
2391 Result = LegalizeOp(Result);
2392 break;
2393 }
2394
2395 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002396 MVT::ValueType NVT =
2397 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2398 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2399 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2400 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002401 break;
2402 }
Evan Cheng912095b2007-01-04 21:56:39 +00002403 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002404 break;
2405
Nate Begeman551bf3f2006-02-17 05:43:56 +00002406 case ISD::ADDC:
2407 case ISD::SUBC:
2408 Tmp1 = LegalizeOp(Node->getOperand(0));
2409 Tmp2 = LegalizeOp(Node->getOperand(1));
2410 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
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;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002416
Nate Begeman551bf3f2006-02-17 05:43:56 +00002417 case ISD::ADDE:
2418 case ISD::SUBE:
2419 Tmp1 = LegalizeOp(Node->getOperand(0));
2420 Tmp2 = LegalizeOp(Node->getOperand(1));
2421 Tmp3 = LegalizeOp(Node->getOperand(2));
2422 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2423 // Since this produces two values, make sure to remember that we legalized
2424 // both of them.
2425 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2426 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2427 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002428
Nate Begeman419f8b62005-10-18 00:27:41 +00002429 case ISD::BUILD_PAIR: {
2430 MVT::ValueType PairTy = Node->getValueType(0);
2431 // TODO: handle the case where the Lo and Hi operands are not of legal type
2432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2433 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2434 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002435 case TargetLowering::Promote:
2436 case TargetLowering::Custom:
2437 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002438 case TargetLowering::Legal:
2439 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2440 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2441 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002442 case TargetLowering::Expand:
2443 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2444 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2445 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2446 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2447 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002448 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002449 break;
2450 }
2451 break;
2452 }
2453
Nate Begemanc105e192005-04-06 00:23:54 +00002454 case ISD::UREM:
2455 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002456 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002457 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2458 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002459
Nate Begemanc105e192005-04-06 00:23:54 +00002460 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002461 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2462 case TargetLowering::Custom:
2463 isCustom = true;
2464 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002465 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002466 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002467 if (isCustom) {
2468 Tmp1 = TLI.LowerOperation(Result, DAG);
2469 if (Tmp1.Val) Result = Tmp1;
2470 }
Nate Begemanc105e192005-04-06 00:23:54 +00002471 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002472 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002473 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002474 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002475 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002476 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2477 TargetLowering::Legal) {
2478 // X % Y -> X-X/Y*Y
2479 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002480 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002481 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2482 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2483 } else {
2484 assert(Node->getValueType(0) == MVT::i32 &&
2485 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002486 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2487 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002488 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002489 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002490 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002491 } else {
2492 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002493 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2494 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002495 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002496 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2497 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002498 }
2499 break;
2500 }
2501 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002502 case ISD::VAARG: {
2503 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2504 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2505
Chris Lattner5c62f332006-01-28 07:42:08 +00002506 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002507 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2508 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002509 case TargetLowering::Custom:
2510 isCustom = true;
2511 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002512 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002513 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2514 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002515 Tmp1 = Result.getValue(1);
2516
2517 if (isCustom) {
2518 Tmp2 = TLI.LowerOperation(Result, DAG);
2519 if (Tmp2.Val) {
2520 Result = LegalizeOp(Tmp2);
2521 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2522 }
2523 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002524 break;
2525 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002526 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002527 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002528 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002529 // Increment the pointer, VAList, to the next vaarg
2530 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2531 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2532 TLI.getPointerTy()));
2533 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002534 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2535 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002536 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002537 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002538 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002539 Result = LegalizeOp(Result);
2540 break;
2541 }
2542 }
2543 // Since VAARG produces two values, make sure to remember that we
2544 // legalized both of them.
2545 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002546 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2547 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002548 }
2549
2550 case ISD::VACOPY:
2551 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2552 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2553 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2554
2555 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2556 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002557 case TargetLowering::Custom:
2558 isCustom = true;
2559 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002560 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002561 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2562 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002563 if (isCustom) {
2564 Tmp1 = TLI.LowerOperation(Result, DAG);
2565 if (Tmp1.Val) Result = Tmp1;
2566 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002567 break;
2568 case TargetLowering::Expand:
2569 // This defaults to loading a pointer from the input and storing it to the
2570 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002571 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002572 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002573 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2574 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002575 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2576 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002577 break;
2578 }
2579 break;
2580
2581 case ISD::VAEND:
2582 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2583 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2584
2585 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2586 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002587 case TargetLowering::Custom:
2588 isCustom = true;
2589 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002590 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002591 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002592 if (isCustom) {
2593 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2594 if (Tmp1.Val) Result = Tmp1;
2595 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002596 break;
2597 case TargetLowering::Expand:
2598 Result = Tmp1; // Default to a no-op, return the chain
2599 break;
2600 }
2601 break;
2602
2603 case ISD::VASTART:
2604 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2605 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2606
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2608
Nate Begemanacc398c2006-01-25 18:21:52 +00002609 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2610 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002611 case TargetLowering::Legal: break;
2612 case TargetLowering::Custom:
2613 Tmp1 = TLI.LowerOperation(Result, DAG);
2614 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002615 break;
2616 }
2617 break;
2618
Nate Begeman35ef9132006-01-11 21:21:00 +00002619 case ISD::ROTL:
2620 case ISD::ROTR:
2621 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2622 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002623
2624 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2625 "Cannot handle this yet!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002626 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman35ef9132006-01-11 21:21:00 +00002627 break;
2628
Nate Begemand88fc032006-01-14 03:14:10 +00002629 case ISD::BSWAP:
2630 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2631 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002632 case TargetLowering::Custom:
2633 assert(0 && "Cannot custom legalize this yet!");
2634 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002635 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002636 break;
2637 case TargetLowering::Promote: {
2638 MVT::ValueType OVT = Tmp1.getValueType();
2639 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2640 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002641
Chris Lattner456a93a2006-01-28 07:39:30 +00002642 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2643 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2644 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2645 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2646 break;
2647 }
2648 case TargetLowering::Expand:
2649 Result = ExpandBSWAP(Tmp1);
2650 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002651 }
2652 break;
2653
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002654 case ISD::CTPOP:
2655 case ISD::CTTZ:
2656 case ISD::CTLZ:
2657 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2658 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002659 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002660 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002661 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002662 break;
2663 case TargetLowering::Promote: {
2664 MVT::ValueType OVT = Tmp1.getValueType();
2665 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002666
2667 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002668 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2669 // Perform the larger operation, then subtract if needed.
2670 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002671 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002672 case ISD::CTPOP:
2673 Result = Tmp1;
2674 break;
2675 case ISD::CTTZ:
2676 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002677 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2678 DAG.getConstant(getSizeInBits(NVT), NVT),
2679 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002680 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002681 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2682 break;
2683 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002684 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002685 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2686 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002687 getSizeInBits(OVT), NVT));
2688 break;
2689 }
2690 break;
2691 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002692 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002693 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002694 break;
2695 }
2696 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002697
Chris Lattner2c8086f2005-04-02 05:00:07 +00002698 // Unary operators
2699 case ISD::FABS:
2700 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002701 case ISD::FSQRT:
2702 case ISD::FSIN:
2703 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002704 Tmp1 = LegalizeOp(Node->getOperand(0));
2705 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002706 case TargetLowering::Promote:
2707 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002708 isCustom = true;
2709 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002710 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002711 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002712 if (isCustom) {
2713 Tmp1 = TLI.LowerOperation(Result, DAG);
2714 if (Tmp1.Val) Result = Tmp1;
2715 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002716 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002717 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002718 switch (Node->getOpcode()) {
2719 default: assert(0 && "Unreachable!");
2720 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002721 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2722 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002723 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002724 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002725 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002726 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2727 MVT::ValueType VT = Node->getValueType(0);
2728 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002729 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002730 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2731 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002732 break;
2733 }
2734 case ISD::FSQRT:
2735 case ISD::FSIN:
2736 case ISD::FCOS: {
2737 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002738 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002739 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002740 case ISD::FSQRT:
2741 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2742 break;
2743 case ISD::FSIN:
2744 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2745 break;
2746 case ISD::FCOS:
2747 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2748 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002749 default: assert(0 && "Unreachable!");
2750 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002751 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002752 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2753 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002754 break;
2755 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002756 }
2757 break;
2758 }
2759 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002760 case ISD::FPOWI: {
2761 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00002762 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2763 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002764 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002765 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2766 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002767 break;
2768 }
Chris Lattner35481892005-12-23 00:16:34 +00002769 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00002770 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00002771 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner67993f72006-01-23 07:30:46 +00002772 } else {
Chris Lattner35481892005-12-23 00:16:34 +00002773 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2774 Node->getOperand(0).getValueType())) {
2775 default: assert(0 && "Unknown operation action!");
2776 case TargetLowering::Expand:
2777 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2778 break;
2779 case TargetLowering::Legal:
2780 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002781 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00002782 break;
2783 }
2784 }
2785 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00002786 case ISD::VBIT_CONVERT: {
2787 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2788 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2789
2790 // The input has to be a vector type, we have to either scalarize it, pack
2791 // it, or convert it based on whether the input vector type is legal.
2792 SDNode *InVal = Node->getOperand(0).Val;
2793 unsigned NumElems =
2794 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2795 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2796
2797 // Figure out if there is a Packed type corresponding to this Vector
2798 // type. If so, convert to the packed type.
2799 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2800 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2801 // Turn this into a bit convert of the packed input.
2802 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2803 PackVectorOp(Node->getOperand(0), TVT));
2804 break;
2805 } else if (NumElems == 1) {
2806 // Turn this into a bit convert of the scalar input.
2807 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2808 PackVectorOp(Node->getOperand(0), EVT));
2809 break;
2810 } else {
2811 // FIXME: UNIMP! Store then reload
2812 assert(0 && "Cast from unsupported vector type not implemented yet!");
2813 }
2814 }
2815
Chris Lattner2c8086f2005-04-02 05:00:07 +00002816 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00002817 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002818 case ISD::UINT_TO_FP: {
2819 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002820 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2821 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002822 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002823 Node->getOperand(0).getValueType())) {
2824 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002825 case TargetLowering::Custom:
2826 isCustom = true;
2827 // FALLTHROUGH
2828 case TargetLowering::Legal:
2829 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002830 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002831 if (isCustom) {
2832 Tmp1 = TLI.LowerOperation(Result, DAG);
2833 if (Tmp1.Val) Result = Tmp1;
2834 }
2835 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002836 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00002837 Result = ExpandLegalINT_TO_FP(isSigned,
2838 LegalizeOp(Node->getOperand(0)),
2839 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002840 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002841 case TargetLowering::Promote:
2842 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2843 Node->getValueType(0),
2844 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002845 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00002846 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002847 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00002848 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002849 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2850 Node->getValueType(0), Node->getOperand(0));
2851 break;
2852 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002853 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002854 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002855 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2856 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002857 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002858 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2859 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002860 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002861 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2862 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002863 break;
2864 }
2865 break;
2866 }
2867 case ISD::TRUNCATE:
2868 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2869 case Legal:
2870 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002871 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002872 break;
2873 case Expand:
2874 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2875
2876 // Since the result is legal, we should just be able to truncate the low
2877 // part of the source.
2878 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2879 break;
2880 case Promote:
2881 Result = PromoteOp(Node->getOperand(0));
2882 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2883 break;
2884 }
2885 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002886
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002887 case ISD::FP_TO_SINT:
2888 case ISD::FP_TO_UINT:
2889 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2890 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002891 Tmp1 = LegalizeOp(Node->getOperand(0));
2892
Chris Lattner1618beb2005-07-29 00:11:56 +00002893 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2894 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002895 case TargetLowering::Custom:
2896 isCustom = true;
2897 // FALLTHROUGH
2898 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002899 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002900 if (isCustom) {
2901 Tmp1 = TLI.LowerOperation(Result, DAG);
2902 if (Tmp1.Val) Result = Tmp1;
2903 }
2904 break;
2905 case TargetLowering::Promote:
2906 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2907 Node->getOpcode() == ISD::FP_TO_SINT);
2908 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002909 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002910 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2911 SDOperand True, False;
2912 MVT::ValueType VT = Node->getOperand(0).getValueType();
2913 MVT::ValueType NVT = Node->getValueType(0);
2914 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2915 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2916 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2917 Node->getOperand(0), Tmp2, ISD::SETLT);
2918 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2919 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002920 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002921 Tmp2));
2922 False = DAG.getNode(ISD::XOR, NVT, False,
2923 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00002924 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2925 break;
Nate Begemand2558e32005-08-14 01:20:53 +00002926 } else {
2927 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2928 }
2929 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00002930 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002931 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00002932 case Expand: {
2933 // Convert f32 / f64 to i32 / i64.
2934 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00002935 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00002936 switch (Node->getOpcode()) {
2937 case ISD::FP_TO_SINT:
2938 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002939 LC = (VT == MVT::i32)
2940 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002941 else
Evan Cheng56966222007-01-12 02:11:51 +00002942 LC = (VT == MVT::i32)
2943 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002944 break;
2945 case ISD::FP_TO_UINT:
2946 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00002947 LC = (VT == MVT::i32)
2948 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002949 else
Evan Cheng56966222007-01-12 02:11:51 +00002950 LC = (VT == MVT::i32)
2951 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00002952 break;
2953 default: assert(0 && "Unreachable!");
2954 }
2955 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002956 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2957 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00002958 break;
2959 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002960 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002961 Tmp1 = PromoteOp(Node->getOperand(0));
2962 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2963 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002964 break;
2965 }
2966 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002967
Chris Lattner13c78e22005-09-02 00:18:10 +00002968 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002969 case ISD::ZERO_EXTEND:
2970 case ISD::SIGN_EXTEND:
2971 case ISD::FP_EXTEND:
2972 case ISD::FP_ROUND:
2973 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002974 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002975 case Legal:
2976 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002977 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002978 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002979 case Promote:
2980 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002981 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002982 Tmp1 = PromoteOp(Node->getOperand(0));
2983 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00002984 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002985 case ISD::ZERO_EXTEND:
2986 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002987 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002988 Result = DAG.getZeroExtendInReg(Result,
2989 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002990 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002991 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002992 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002993 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002994 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002995 Result,
2996 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002997 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002998 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002999 Result = PromoteOp(Node->getOperand(0));
3000 if (Result.getValueType() != Op.getValueType())
3001 // Dynamically dead while we have only 2 FP types.
3002 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3003 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003004 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003005 Result = PromoteOp(Node->getOperand(0));
3006 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3007 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003008 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003009 }
3010 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003011 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003012 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003013 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003014 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003015
3016 // If this operation is not supported, convert it to a shl/shr or load/store
3017 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003018 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3019 default: assert(0 && "This action not supported for this op yet!");
3020 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003021 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003022 break;
3023 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003024 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003025 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003026 // NOTE: we could fall back on load/store here too for targets without
3027 // SAR. However, it is doubtful that any exist.
3028 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3029 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003030 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003031 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3032 Node->getOperand(0), ShiftCst);
3033 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3034 Result, ShiftCst);
3035 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003036 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003037 // EXTLOAD pair, targetting a temporary location (a stack slot).
3038
3039 // NOTE: there is a choice here between constantly creating new stack
3040 // slots and always reusing the same one. We currently always create
3041 // new ones, as reuse may inhibit scheduling.
3042 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Andersona69571c2006-05-03 01:29:57 +00003043 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00003044 unsigned Align = TLI.getTargetData()->getTypeAlignmentPref(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003045 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003046 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00003047 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3048 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003049 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3050 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003051 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003052 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003053 } else {
3054 assert(0 && "Unknown op");
3055 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003056 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003057 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003058 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003059 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003060 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003061
Chris Lattner4ddd2832006-04-08 04:13:17 +00003062 assert(Result.getValueType() == Op.getValueType() &&
3063 "Bad legalization!");
3064
Chris Lattner456a93a2006-01-28 07:39:30 +00003065 // Make sure that the generated code is itself legal.
3066 if (Result != Op)
3067 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003068
Chris Lattner45982da2005-05-12 16:53:42 +00003069 // Note that LegalizeOp may be reentered even from single-use nodes, which
3070 // means that we always must cache transformed nodes.
3071 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003072 return Result;
3073}
3074
Chris Lattner8b6fa222005-01-15 22:16:26 +00003075/// PromoteOp - Given an operation that produces a value in an invalid type,
3076/// promote it to compute the value into a larger type. The produced value will
3077/// have the correct bits for the low portion of the register, but no guarantee
3078/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003079SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3080 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003081 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003082 assert(getTypeAction(VT) == Promote &&
3083 "Caller should expand or legalize operands that are not promotable!");
3084 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3085 "Cannot promote to smaller type!");
3086
Chris Lattner03c85462005-01-15 05:21:40 +00003087 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003088 SDOperand Result;
3089 SDNode *Node = Op.Val;
3090
Chris Lattner6fdcb252005-09-02 20:32:45 +00003091 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
3092 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003093
Chris Lattner03c85462005-01-15 05:21:40 +00003094 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003095 case ISD::CopyFromReg:
3096 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003097 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003098#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00003099 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003100#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003101 assert(0 && "Do not know how to promote this operator!");
3102 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003103 case ISD::UNDEF:
3104 Result = DAG.getNode(ISD::UNDEF, NVT);
3105 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003106 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003107 if (VT != MVT::i1)
3108 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3109 else
3110 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003111 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3112 break;
3113 case ISD::ConstantFP:
3114 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3115 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3116 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003117
Chris Lattner82fbfb62005-01-18 02:59:52 +00003118 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003119 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003120 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3121 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003122 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003123
Chris Lattner03c85462005-01-15 05:21:40 +00003124 case ISD::TRUNCATE:
3125 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3126 case Legal:
3127 Result = LegalizeOp(Node->getOperand(0));
3128 assert(Result.getValueType() >= NVT &&
3129 "This truncation doesn't make sense!");
3130 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3131 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3132 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003133 case Promote:
3134 // The truncation is not required, because we don't guarantee anything
3135 // about high bits anyway.
3136 Result = PromoteOp(Node->getOperand(0));
3137 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003138 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003139 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3140 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003141 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003142 }
3143 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003144 case ISD::SIGN_EXTEND:
3145 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003146 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003147 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3148 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3149 case Legal:
3150 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003151 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003152 break;
3153 case Promote:
3154 // Promote the reg if it's smaller.
3155 Result = PromoteOp(Node->getOperand(0));
3156 // The high bits are not guaranteed to be anything. Insert an extend.
3157 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003158 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003159 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003160 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003161 Result = DAG.getZeroExtendInReg(Result,
3162 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003163 break;
3164 }
3165 break;
Chris Lattner35481892005-12-23 00:16:34 +00003166 case ISD::BIT_CONVERT:
3167 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3168 Result = PromoteOp(Result);
3169 break;
3170
Chris Lattner8b6fa222005-01-15 22:16:26 +00003171 case ISD::FP_EXTEND:
3172 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3173 case ISD::FP_ROUND:
3174 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3175 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3176 case Promote: assert(0 && "Unreachable with 2 FP types!");
3177 case Legal:
3178 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003179 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003180 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003181 break;
3182 }
3183 break;
3184
3185 case ISD::SINT_TO_FP:
3186 case ISD::UINT_TO_FP:
3187 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3188 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003189 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003190 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003191 break;
3192
3193 case Promote:
3194 Result = PromoteOp(Node->getOperand(0));
3195 if (Node->getOpcode() == ISD::SINT_TO_FP)
3196 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003197 Result,
3198 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003199 else
Chris Lattner23993562005-04-13 02:38:47 +00003200 Result = DAG.getZeroExtendInReg(Result,
3201 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003202 // No extra round required here.
3203 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003204 break;
3205 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003206 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3207 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003208 // Round if we cannot tolerate excess precision.
3209 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003210 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3211 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003212 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003213 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003214 break;
3215
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003216 case ISD::SIGN_EXTEND_INREG:
3217 Result = PromoteOp(Node->getOperand(0));
3218 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3219 Node->getOperand(1));
3220 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003221 case ISD::FP_TO_SINT:
3222 case ISD::FP_TO_UINT:
3223 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3224 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003225 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003226 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003227 break;
3228 case Promote:
3229 // The input result is prerounded, so we don't have to do anything
3230 // special.
3231 Tmp1 = PromoteOp(Node->getOperand(0));
3232 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003233 }
Nate Begemand2558e32005-08-14 01:20:53 +00003234 // If we're promoting a UINT to a larger size, check to see if the new node
3235 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3236 // we can use that instead. This allows us to generate better code for
3237 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3238 // legal, such as PowerPC.
3239 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003240 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003241 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3242 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003243 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3244 } else {
3245 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3246 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003247 break;
3248
Chris Lattner2c8086f2005-04-02 05:00:07 +00003249 case ISD::FABS:
3250 case ISD::FNEG:
3251 Tmp1 = PromoteOp(Node->getOperand(0));
3252 assert(Tmp1.getValueType() == NVT);
3253 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3254 // NOTE: we do not have to do any extra rounding here for
3255 // NoExcessFPPrecision, because we know the input will have the appropriate
3256 // precision, and these operations don't modify precision at all.
3257 break;
3258
Chris Lattnerda6ba872005-04-28 21:44:33 +00003259 case ISD::FSQRT:
3260 case ISD::FSIN:
3261 case ISD::FCOS:
3262 Tmp1 = PromoteOp(Node->getOperand(0));
3263 assert(Tmp1.getValueType() == NVT);
3264 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003265 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003266 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3267 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003268 break;
3269
Chris Lattner03c85462005-01-15 05:21:40 +00003270 case ISD::AND:
3271 case ISD::OR:
3272 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003273 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003274 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003275 case ISD::MUL:
3276 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003277 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003278 // that too is okay if they are integer operations.
3279 Tmp1 = PromoteOp(Node->getOperand(0));
3280 Tmp2 = PromoteOp(Node->getOperand(1));
3281 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3282 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003283 break;
3284 case ISD::FADD:
3285 case ISD::FSUB:
3286 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003287 Tmp1 = PromoteOp(Node->getOperand(0));
3288 Tmp2 = PromoteOp(Node->getOperand(1));
3289 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3290 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3291
3292 // Floating point operations will give excess precision that we may not be
3293 // able to tolerate. If we DO allow excess precision, just leave it,
3294 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003295 // FIXME: Why would we need to round FP ops more than integer ones?
3296 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003297 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003298 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3299 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003300 break;
3301
Chris Lattner8b6fa222005-01-15 22:16:26 +00003302 case ISD::SDIV:
3303 case ISD::SREM:
3304 // These operators require that their input be sign extended.
3305 Tmp1 = PromoteOp(Node->getOperand(0));
3306 Tmp2 = PromoteOp(Node->getOperand(1));
3307 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003308 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3309 DAG.getValueType(VT));
3310 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3311 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003312 }
3313 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3314
3315 // Perform FP_ROUND: this is probably overly pessimistic.
3316 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003317 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3318 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003319 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003320 case ISD::FDIV:
3321 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003322 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003323 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003324 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3325 case Legal:
3326 Tmp1 = LegalizeOp(Node->getOperand(0));
3327 break;
3328 case Promote:
3329 Tmp1 = PromoteOp(Node->getOperand(0));
3330 break;
3331 case Expand:
3332 assert(0 && "not implemented");
3333 }
3334 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3335 case Legal:
3336 Tmp2 = LegalizeOp(Node->getOperand(1));
3337 break;
3338 case Promote:
3339 Tmp2 = PromoteOp(Node->getOperand(1));
3340 break;
3341 case Expand:
3342 assert(0 && "not implemented");
3343 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003344 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3345
3346 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003347 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003348 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3349 DAG.getValueType(VT));
3350 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003351
3352 case ISD::UDIV:
3353 case ISD::UREM:
3354 // These operators require that their input be zero extended.
3355 Tmp1 = PromoteOp(Node->getOperand(0));
3356 Tmp2 = PromoteOp(Node->getOperand(1));
3357 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003358 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3359 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003360 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3361 break;
3362
3363 case ISD::SHL:
3364 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003365 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003366 break;
3367 case ISD::SRA:
3368 // The input value must be properly sign extended.
3369 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003370 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3371 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003372 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003373 break;
3374 case ISD::SRL:
3375 // The input value must be properly zero extended.
3376 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003377 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003378 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003379 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003380
3381 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003382 Tmp1 = Node->getOperand(0); // Get the chain.
3383 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003384 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3385 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3386 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3387 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003388 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003389 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003390 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003391 // Increment the pointer, VAList, to the next vaarg
3392 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3393 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3394 TLI.getPointerTy()));
3395 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003396 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3397 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003398 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003399 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003400 }
3401 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003402 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003403 break;
3404
Evan Cheng466685d2006-10-09 20:57:25 +00003405 case ISD::LOAD: {
3406 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003407 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3408 ? ISD::EXTLOAD : LD->getExtensionType();
3409 Result = DAG.getExtLoad(ExtType, NVT,
3410 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003411 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Cheng2e49f092006-10-11 07:10:22 +00003412 LD->getLoadedVT());
Chris Lattner03c85462005-01-15 05:21:40 +00003413 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003414 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003415 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003416 }
Chris Lattner03c85462005-01-15 05:21:40 +00003417 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003418 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3419 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003420 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003421 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003422 case ISD::SELECT_CC:
3423 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3424 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3425 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003426 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003427 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003428 case ISD::BSWAP:
3429 Tmp1 = Node->getOperand(0);
3430 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3431 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3432 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3433 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3434 TLI.getShiftAmountTy()));
3435 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003436 case ISD::CTPOP:
3437 case ISD::CTTZ:
3438 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003439 // Zero extend the argument
3440 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003441 // Perform the larger operation, then subtract if needed.
3442 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003443 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003444 case ISD::CTPOP:
3445 Result = Tmp1;
3446 break;
3447 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003448 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003449 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003450 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003451 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner456a93a2006-01-28 07:39:30 +00003452 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003453 break;
3454 case ISD::CTLZ:
3455 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003456 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3457 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003458 getSizeInBits(VT), NVT));
3459 break;
3460 }
3461 break;
Chris Lattner15972212006-03-31 17:55:51 +00003462 case ISD::VEXTRACT_VECTOR_ELT:
3463 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3464 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003465 case ISD::EXTRACT_VECTOR_ELT:
3466 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3467 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003468 }
3469
3470 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003471
3472 // Make sure the result is itself legal.
3473 Result = LegalizeOp(Result);
3474
3475 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003476 AddPromotedOperand(Op, Result);
3477 return Result;
3478}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003479
Chris Lattner15972212006-03-31 17:55:51 +00003480/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3481/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3482/// on the vector type. The return type of this matches the element type of the
3483/// vector, which may not be legal for the target.
3484SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3485 // We know that operand #0 is the Vec vector. If the index is a constant
3486 // or if the invec is a supported hardware type, we can use it. Otherwise,
3487 // lower to a store then an indexed load.
3488 SDOperand Vec = Op.getOperand(0);
3489 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3490
3491 SDNode *InVal = Vec.Val;
3492 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3493 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3494
3495 // Figure out if there is a Packed type corresponding to this Vector
3496 // type. If so, convert to the packed type.
3497 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3498 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3499 // Turn this into a packed extract_vector_elt operation.
3500 Vec = PackVectorOp(Vec, TVT);
3501 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3502 } else if (NumElems == 1) {
3503 // This must be an access of the only element. Return it.
3504 return PackVectorOp(Vec, EVT);
3505 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3506 SDOperand Lo, Hi;
3507 SplitVectorOp(Vec, Lo, Hi);
3508 if (CIdx->getValue() < NumElems/2) {
3509 Vec = Lo;
3510 } else {
3511 Vec = Hi;
3512 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3513 }
3514
3515 // It's now an extract from the appropriate high or low part. Recurse.
3516 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3517 return LowerVEXTRACT_VECTOR_ELT(Op);
3518 } else {
3519 // Variable index case for extract element.
3520 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3521 assert(0 && "unimp!");
3522 return SDOperand();
3523 }
3524}
3525
Chris Lattner4aab2f42006-04-02 05:06:04 +00003526/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3527/// memory traffic.
3528SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3529 SDOperand Vector = Op.getOperand(0);
3530 SDOperand Idx = Op.getOperand(1);
3531
3532 // If the target doesn't support this, store the value to a temporary
3533 // stack slot, then LOAD the scalar element back out.
3534 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003535 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003536
3537 // Add the offset to the index.
3538 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3539 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3540 DAG.getConstant(EltSize, Idx.getValueType()));
3541 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3542
Evan Cheng466685d2006-10-09 20:57:25 +00003543 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner4aab2f42006-04-02 05:06:04 +00003544}
3545
Chris Lattner15972212006-03-31 17:55:51 +00003546
Nate Begeman750ac1b2006-02-01 07:19:44 +00003547/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3548/// with condition CC on the current target. This usually involves legalizing
3549/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3550/// there may be no choice but to create a new SetCC node to represent the
3551/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3552/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3553void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3554 SDOperand &RHS,
3555 SDOperand &CC) {
3556 SDOperand Tmp1, Tmp2, Result;
3557
3558 switch (getTypeAction(LHS.getValueType())) {
3559 case Legal:
3560 Tmp1 = LegalizeOp(LHS); // LHS
3561 Tmp2 = LegalizeOp(RHS); // RHS
3562 break;
3563 case Promote:
3564 Tmp1 = PromoteOp(LHS); // LHS
3565 Tmp2 = PromoteOp(RHS); // RHS
3566
3567 // If this is an FP compare, the operands have already been extended.
3568 if (MVT::isInteger(LHS.getValueType())) {
3569 MVT::ValueType VT = LHS.getValueType();
3570 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3571
3572 // Otherwise, we have to insert explicit sign or zero extends. Note
3573 // that we could insert sign extends for ALL conditions, but zero extend
3574 // is cheaper on many machines (an AND instead of two shifts), so prefer
3575 // it.
3576 switch (cast<CondCodeSDNode>(CC)->get()) {
3577 default: assert(0 && "Unknown integer comparison!");
3578 case ISD::SETEQ:
3579 case ISD::SETNE:
3580 case ISD::SETUGE:
3581 case ISD::SETUGT:
3582 case ISD::SETULE:
3583 case ISD::SETULT:
3584 // ALL of these operations will work if we either sign or zero extend
3585 // the operands (including the unsigned comparisons!). Zero extend is
3586 // usually a simpler/cheaper operation, so prefer it.
3587 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3588 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3589 break;
3590 case ISD::SETGE:
3591 case ISD::SETGT:
3592 case ISD::SETLT:
3593 case ISD::SETLE:
3594 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3595 DAG.getValueType(VT));
3596 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3597 DAG.getValueType(VT));
3598 break;
3599 }
3600 }
3601 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003602 case Expand: {
3603 MVT::ValueType VT = LHS.getValueType();
3604 if (VT == MVT::f32 || VT == MVT::f64) {
3605 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003606 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner2d53a322006-12-15 07:36:19 +00003607 ISD::CondCode CC1, CC2 = ISD::SETCC_INVALID;
Evan Cheng2b49c502006-12-15 02:59:56 +00003608 switch (cast<CondCodeSDNode>(CC)->get()) {
3609 case ISD::SETEQ:
3610 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003611 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003612 CC1 = ISD::SETEQ;
3613 break;
3614 case ISD::SETNE:
3615 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003616 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003617 CC1 = ISD::SETNE;
3618 break;
3619 case ISD::SETGE:
3620 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003621 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003622 CC1 = ISD::SETGE;
3623 break;
3624 case ISD::SETLT:
3625 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003626 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003627 CC1 = ISD::SETLT;
3628 break;
3629 case ISD::SETLE:
3630 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003631 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003632 CC1 = ISD::SETLE;
3633 break;
3634 case ISD::SETGT:
3635 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003636 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003637 CC1 = ISD::SETGT;
3638 break;
3639 case ISD::SETUO:
3640 case ISD::SETO:
Evan Cheng56966222007-01-12 02:11:51 +00003641 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003642 CC1 = cast<CondCodeSDNode>(CC)->get() == ISD::SETO
3643 ? ISD::SETEQ : ISD::SETNE;
3644 break;
3645 default:
Evan Cheng56966222007-01-12 02:11:51 +00003646 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003647 CC1 = ISD::SETNE;
3648 switch (cast<CondCodeSDNode>(CC)->get()) {
3649 case ISD::SETONE:
3650 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003651 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003652 CC1 = ISD::SETLT;
3653 // Fallthrough
3654 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003655 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003656 CC2 = ISD::SETGT;
3657 break;
3658 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003659 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003660 CC2 = ISD::SETGE;
3661 break;
3662 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003663 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003664 CC2 = ISD::SETLT;
3665 break;
3666 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003667 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003668 CC2 = ISD::SETLE;
3669 break;
Evan Cheng56966222007-01-12 02:11:51 +00003670 case ISD::SETUEQ:
3671 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
3672 CC2 = ISD::SETEQ;
3673 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003674 default: assert(0 && "Unsupported FP setcc!");
3675 }
3676 }
3677
3678 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003679 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00003680 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003681 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003682 Tmp2 = DAG.getConstant(0, MVT::i32);
3683 CC = DAG.getCondCode(CC1);
Evan Cheng56966222007-01-12 02:11:51 +00003684 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003685 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00003686 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00003687 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00003688 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00003689 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
3690 DAG.getCondCode(CC2));
3691 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3692 Tmp2 = SDOperand();
3693 }
3694 LHS = Tmp1;
3695 RHS = Tmp2;
3696 return;
3697 }
3698
Nate Begeman750ac1b2006-02-01 07:19:44 +00003699 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3700 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00003701 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003702 switch (cast<CondCodeSDNode>(CC)->get()) {
3703 case ISD::SETEQ:
3704 case ISD::SETNE:
3705 if (RHSLo == RHSHi)
3706 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3707 if (RHSCST->isAllOnesValue()) {
3708 // Comparison to -1.
3709 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3710 Tmp2 = RHSLo;
3711 break;
3712 }
3713
3714 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3715 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3716 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3717 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3718 break;
3719 default:
3720 // If this is a comparison of the sign bit, just look at the top part.
3721 // X > -1, x < 0
3722 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3723 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3724 CST->getValue() == 0) || // X < 0
3725 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3726 CST->isAllOnesValue())) { // X > -1
3727 Tmp1 = LHSHi;
3728 Tmp2 = RHSHi;
3729 break;
3730 }
3731
3732 // FIXME: This generated code sucks.
3733 ISD::CondCode LowCC;
3734 switch (cast<CondCodeSDNode>(CC)->get()) {
3735 default: assert(0 && "Unknown integer setcc!");
3736 case ISD::SETLT:
3737 case ISD::SETULT: LowCC = ISD::SETULT; break;
3738 case ISD::SETGT:
3739 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3740 case ISD::SETLE:
3741 case ISD::SETULE: LowCC = ISD::SETULE; break;
3742 case ISD::SETGE:
3743 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3744 }
3745
3746 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3747 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3748 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3749
3750 // NOTE: on targets without efficient SELECT of bools, we can always use
3751 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3752 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3753 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3754 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3755 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3756 Result, Tmp1, Tmp2));
3757 Tmp1 = Result;
Nate Begemanda06e9e2006-02-01 19:05:15 +00003758 Tmp2 = SDOperand();
Nate Begeman750ac1b2006-02-01 07:19:44 +00003759 }
3760 }
Evan Cheng2b49c502006-12-15 02:59:56 +00003761 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00003762 LHS = Tmp1;
3763 RHS = Tmp2;
3764}
3765
Chris Lattner35481892005-12-23 00:16:34 +00003766/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00003767/// The resultant code need not be legal. Note that SrcOp is the input operand
3768/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00003769SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3770 SDOperand SrcOp) {
3771 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00003772 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00003773
3774 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00003775 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003776 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003777 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00003778}
3779
Chris Lattner4352cc92006-04-04 17:23:26 +00003780SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3781 // Create a vector sized/aligned stack slot, store the value to element #0,
3782 // then load the whole vector back out.
3783 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00003784 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003785 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00003786 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00003787}
3788
3789
Chris Lattnerce872152006-03-19 06:31:19 +00003790/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3791/// support the operation, but do support the resultant packed vector type.
3792SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3793
3794 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00003795 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00003796 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00003797 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00003798 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00003799 std::map<SDOperand, std::vector<unsigned> > Values;
3800 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003801 bool isConstant = true;
3802 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3803 SplatValue.getOpcode() != ISD::UNDEF)
3804 isConstant = false;
3805
Evan Cheng033e6812006-03-24 01:17:21 +00003806 for (unsigned i = 1; i < NumElems; ++i) {
3807 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00003808 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00003809 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00003810 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00003811 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00003812 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003813
3814 // If this isn't a constant element or an undef, we can't use a constant
3815 // pool load.
3816 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3817 V.getOpcode() != ISD::UNDEF)
3818 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00003819 }
3820
3821 if (isOnlyLowElement) {
3822 // If the low element is an undef too, then this whole things is an undef.
3823 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3824 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3825 // Otherwise, turn this into a scalar_to_vector node.
3826 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3827 Node->getOperand(0));
3828 }
3829
Chris Lattner2eb86532006-03-24 07:29:17 +00003830 // If all elements are constants, create a load from the constant pool.
3831 if (isConstant) {
3832 MVT::ValueType VT = Node->getValueType(0);
3833 const Type *OpNTy =
3834 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3835 std::vector<Constant*> CV;
3836 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3837 if (ConstantFPSDNode *V =
3838 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3839 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3840 } else if (ConstantSDNode *V =
3841 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003842 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00003843 } else {
3844 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3845 CV.push_back(UndefValue::get(OpNTy));
3846 }
3847 }
3848 Constant *CP = ConstantPacked::get(CV);
3849 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00003850 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00003851 }
3852
Chris Lattner87100e02006-03-20 01:52:29 +00003853 if (SplatValue.Val) { // Splat of one value?
3854 // Build the shuffle constant vector: <0, 0, 0, 0>
3855 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00003856 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner87100e02006-03-20 01:52:29 +00003857 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00003858 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003859 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3860 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00003861
3862 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003863 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00003864 // Get the splatted value into the low element of a vector register.
3865 SDOperand LowValVec =
3866 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3867
3868 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3869 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3870 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3871 SplatMask);
3872 }
3873 }
3874
Evan Cheng033e6812006-03-24 01:17:21 +00003875 // If there are only two unique elements, we may be able to turn this into a
3876 // vector shuffle.
3877 if (Values.size() == 2) {
3878 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3879 MVT::ValueType MaskVT =
3880 MVT::getIntVectorWithNumElements(NumElems);
3881 std::vector<SDOperand> MaskVec(NumElems);
3882 unsigned i = 0;
3883 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3884 E = Values.end(); I != E; ++I) {
3885 for (std::vector<unsigned>::iterator II = I->second.begin(),
3886 EE = I->second.end(); II != EE; ++II)
3887 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3888 i += NumElems;
3889 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003890 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3891 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003892
3893 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00003894 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3895 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003896 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00003897 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3898 E = Values.end(); I != E; ++I) {
3899 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3900 I->first);
3901 Ops.push_back(Op);
3902 }
3903 Ops.push_back(ShuffleMask);
3904
3905 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003906 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3907 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00003908 }
3909 }
Chris Lattnerce872152006-03-19 06:31:19 +00003910
3911 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3912 // aligned object on the stack, store each element into it, then load
3913 // the result as a vector.
3914 MVT::ValueType VT = Node->getValueType(0);
3915 // Create the stack frame object.
3916 SDOperand FIPtr = CreateStackTemporary(VT);
3917
3918 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003919 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00003920 unsigned TypeByteSize =
3921 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00003922 // Store (in the right endianness) the elements to memory.
3923 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3924 // Ignore undef elements.
3925 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3926
Chris Lattner841c8822006-03-22 01:46:54 +00003927 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00003928
3929 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3930 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3931
Evan Cheng786225a2006-10-05 23:01:46 +00003932 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003933 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00003934 }
3935
3936 SDOperand StoreChain;
3937 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003938 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3939 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00003940 else
3941 StoreChain = DAG.getEntryNode();
3942
3943 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00003944 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00003945}
3946
3947/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3948/// specified value type.
3949SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3950 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3951 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00003952 const Type *Ty = MVT::getTypeForValueType(VT);
3953 unsigned StackAlign = (unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty);
3954 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00003955 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3956}
3957
Chris Lattner5b359c62005-04-02 04:00:59 +00003958void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3959 SDOperand Op, SDOperand Amt,
3960 SDOperand &Lo, SDOperand &Hi) {
3961 // Expand the subcomponents.
3962 SDOperand LHSL, LHSH;
3963 ExpandOp(Op, LHSL, LHSH);
3964
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003965 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00003966 MVT::ValueType VT = LHSL.getValueType();
3967 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00003968 Hi = Lo.getValue(1);
3969}
3970
3971
Chris Lattnere34b3962005-01-19 04:19:40 +00003972/// ExpandShift - Try to find a clever way to expand this shift operation out to
3973/// smaller elements. If we can't find a way that is more efficient than a
3974/// libcall on this target, return false. Otherwise, return true with the
3975/// low-parts expanded into Lo and Hi.
3976bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3977 SDOperand &Lo, SDOperand &Hi) {
3978 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3979 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003980
Chris Lattnere34b3962005-01-19 04:19:40 +00003981 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00003982 SDOperand ShAmt = LegalizeOp(Amt);
3983 MVT::ValueType ShTy = ShAmt.getValueType();
3984 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3985 unsigned NVTBits = MVT::getSizeInBits(NVT);
3986
3987 // Handle the case when Amt is an immediate. Other cases are currently broken
3988 // and are disabled.
3989 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3990 unsigned Cst = CN->getValue();
3991 // Expand the incoming operand to be shifted, so that we have its parts
3992 SDOperand InL, InH;
3993 ExpandOp(Op, InL, InH);
3994 switch(Opc) {
3995 case ISD::SHL:
3996 if (Cst > VTBits) {
3997 Lo = DAG.getConstant(0, NVT);
3998 Hi = DAG.getConstant(0, NVT);
3999 } else if (Cst > NVTBits) {
4000 Lo = DAG.getConstant(0, NVT);
4001 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004002 } else if (Cst == NVTBits) {
4003 Lo = DAG.getConstant(0, NVT);
4004 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004005 } else {
4006 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4007 Hi = DAG.getNode(ISD::OR, NVT,
4008 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4009 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4010 }
4011 return true;
4012 case ISD::SRL:
4013 if (Cst > VTBits) {
4014 Lo = DAG.getConstant(0, NVT);
4015 Hi = DAG.getConstant(0, NVT);
4016 } else if (Cst > NVTBits) {
4017 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4018 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004019 } else if (Cst == NVTBits) {
4020 Lo = InH;
4021 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004022 } else {
4023 Lo = DAG.getNode(ISD::OR, NVT,
4024 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4025 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4026 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4027 }
4028 return true;
4029 case ISD::SRA:
4030 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004031 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004032 DAG.getConstant(NVTBits-1, ShTy));
4033 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004034 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004035 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004036 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004037 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004038 } else if (Cst == NVTBits) {
4039 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004040 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004041 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004042 } else {
4043 Lo = DAG.getNode(ISD::OR, NVT,
4044 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4045 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4046 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4047 }
4048 return true;
4049 }
4050 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004051
4052 // Okay, the shift amount isn't constant. However, if we can tell that it is
4053 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4054 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4055 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4056
4057 // If we know that the high bit of the shift amount is one, then we can do
4058 // this as a couple of simple shifts.
4059 if (KnownOne & Mask) {
4060 // Mask out the high bit, which we know is set.
4061 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4062 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4063
4064 // Expand the incoming operand to be shifted, so that we have its parts
4065 SDOperand InL, InH;
4066 ExpandOp(Op, InL, InH);
4067 switch(Opc) {
4068 case ISD::SHL:
4069 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4070 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4071 return true;
4072 case ISD::SRL:
4073 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4074 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4075 return true;
4076 case ISD::SRA:
4077 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4078 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4079 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4080 return true;
4081 }
4082 }
4083
4084 // If we know that the high bit of the shift amount is zero, then we can do
4085 // this as a couple of simple shifts.
4086 if (KnownZero & Mask) {
4087 // Compute 32-amt.
4088 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4089 DAG.getConstant(NVTBits, Amt.getValueType()),
4090 Amt);
4091
4092 // Expand the incoming operand to be shifted, so that we have its parts
4093 SDOperand InL, InH;
4094 ExpandOp(Op, InL, InH);
4095 switch(Opc) {
4096 case ISD::SHL:
4097 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4098 Hi = DAG.getNode(ISD::OR, NVT,
4099 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4100 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4101 return true;
4102 case ISD::SRL:
4103 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4104 Lo = DAG.getNode(ISD::OR, NVT,
4105 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4106 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4107 return true;
4108 case ISD::SRA:
4109 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4110 Lo = DAG.getNode(ISD::OR, NVT,
4111 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4112 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4113 return true;
4114 }
4115 }
4116
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004117 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004118}
Chris Lattner77e77a62005-01-21 06:05:23 +00004119
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004120
Chris Lattner77e77a62005-01-21 06:05:23 +00004121// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4122// does not fit into a register, return the lo part and set the hi part to the
4123// by-reg argument. If it does fit into a single register, return the result
4124// and leave the Hi part unset.
4125SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004126 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004127 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4128 // The input chain to this libcall is the entry node of the function.
4129 // Legalizing the call will automatically add the previous call to the
4130 // dependence.
4131 SDOperand InChain = DAG.getEntryNode();
4132
Chris Lattner77e77a62005-01-21 06:05:23 +00004133 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004134 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004135 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4136 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4137 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004138 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004139 Entry.isSigned = isSigned; Entry.isInReg = false;
Reid Spencer47857812006-12-31 05:55:36 +00004140 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004141 }
4142 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004143
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004144 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004145 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004146 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004147 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004148 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004149
Chris Lattner6831a812006-02-13 09:18:02 +00004150 // Legalize the call sequence, starting with the chain. This will advance
4151 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4152 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4153 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004154 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004155 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004156 default: assert(0 && "Unknown thing");
4157 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004158 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004159 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004160 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004161 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004162 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004163 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004164 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004165}
4166
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004167
Chris Lattner77e77a62005-01-21 06:05:23 +00004168/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4169/// destination type is legal.
4170SDOperand SelectionDAGLegalize::
4171ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004172 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00004173 assert(getTypeAction(Source.getValueType()) == Expand &&
4174 "This is not an expansion!");
4175 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4176
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004177 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004178 assert(Source.getValueType() == MVT::i64 &&
4179 "This only works for 64-bit -> FP");
4180 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4181 // incoming integer is set. To handle this, we dynamically test to see if
4182 // it is set, and, if so, add a fudge factor.
4183 SDOperand Lo, Hi;
4184 ExpandOp(Source, Lo, Hi);
4185
Chris Lattner66de05b2005-05-13 04:45:13 +00004186 // If this is unsigned, and not supported, first perform the conversion to
4187 // signed, then adjust the result if the sign bit is set.
4188 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4189 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4190
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004191 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4192 DAG.getConstant(0, Hi.getValueType()),
4193 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004194 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4195 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4196 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004197 uint64_t FF = 0x5f800000ULL;
4198 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004199 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004200
Chris Lattner5839bf22005-08-26 17:15:30 +00004201 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004202 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4203 SDOperand FudgeInReg;
4204 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004205 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004206 else {
4207 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00004208 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004209 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004210 }
Chris Lattner473a9902005-09-29 06:44:39 +00004211 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004212 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004213
Chris Lattnera88a2602005-05-14 05:33:54 +00004214 // Check to see if the target has a custom way to lower this. If so, use it.
4215 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4216 default: assert(0 && "This action not implemented for this operation!");
4217 case TargetLowering::Legal:
4218 case TargetLowering::Expand:
4219 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004220 case TargetLowering::Custom: {
4221 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4222 Source), DAG);
4223 if (NV.Val)
4224 return LegalizeOp(NV);
4225 break; // The target decided this was legal after all
4226 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004227 }
4228
Chris Lattner13689e22005-05-12 07:00:44 +00004229 // Expand the source, then glue it back together for the call. We must expand
4230 // the source in case it is shared (this pass of legalize must traverse it).
4231 SDOperand SrcLo, SrcHi;
4232 ExpandOp(Source, SrcLo, SrcHi);
4233 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4234
Evan Cheng56966222007-01-12 02:11:51 +00004235 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004236 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004237 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004238 else {
4239 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004240 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004241 }
Chris Lattner6831a812006-02-13 09:18:02 +00004242
4243 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4244 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004245 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4246 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004247}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004248
Chris Lattner22cde6a2006-01-28 08:25:58 +00004249/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4250/// INT_TO_FP operation of the specified operand when the target requests that
4251/// we expand it. At this point, we know that the result and operand types are
4252/// legal for the target.
4253SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4254 SDOperand Op0,
4255 MVT::ValueType DestVT) {
4256 if (Op0.getValueType() == MVT::i32) {
4257 // simple 32-bit [signed|unsigned] integer to float/double expansion
4258
Chris Lattner58092e32007-01-20 22:35:55 +00004259 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004260 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004261 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4262 unsigned StackAlign =
4263 (unsigned)TLI.getTargetData()->getTypeAlignmentPref(F64Type);
4264 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004265 // get address of 8 byte buffer
4266 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4267 // word offset constant for Hi/Lo address computation
4268 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4269 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004270 SDOperand Hi = StackSlot;
4271 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4272 if (TLI.isLittleEndian())
4273 std::swap(Hi, Lo);
4274
Chris Lattner22cde6a2006-01-28 08:25:58 +00004275 // if signed map to unsigned space
4276 SDOperand Op0Mapped;
4277 if (isSigned) {
4278 // constant used to invert sign bit (signed to unsigned mapping)
4279 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4280 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4281 } else {
4282 Op0Mapped = Op0;
4283 }
4284 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004285 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004286 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004287 // initial hi portion of constructed double
4288 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4289 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004290 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004291 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004292 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004293 // FP constant to bias correct the final result
4294 SDOperand Bias = DAG.getConstantFP(isSigned ?
4295 BitsToDouble(0x4330000080000000ULL)
4296 : BitsToDouble(0x4330000000000000ULL),
4297 MVT::f64);
4298 // subtract the bias
4299 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4300 // final result
4301 SDOperand Result;
4302 // handle final rounding
4303 if (DestVT == MVT::f64) {
4304 // do nothing
4305 Result = Sub;
4306 } else {
4307 // if f32 then cast to f32
4308 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4309 }
4310 return Result;
4311 }
4312 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4313 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4314
4315 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4316 DAG.getConstant(0, Op0.getValueType()),
4317 ISD::SETLT);
4318 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4319 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4320 SignSet, Four, Zero);
4321
4322 // If the sign bit of the integer is set, the large number will be treated
4323 // as a negative number. To counteract this, the dynamic code adds an
4324 // offset depending on the data type.
4325 uint64_t FF;
4326 switch (Op0.getValueType()) {
4327 default: assert(0 && "Unsupported integer type!");
4328 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4329 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4330 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4331 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4332 }
4333 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004334 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004335
4336 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4337 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4338 SDOperand FudgeInReg;
4339 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004340 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004341 else {
4342 assert(DestVT == MVT::f64 && "Unexpected conversion");
4343 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4344 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004345 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004346 }
4347
4348 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4349}
4350
4351/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4352/// *INT_TO_FP operation of the specified operand when the target requests that
4353/// we promote it. At this point, we know that the result and operand types are
4354/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4355/// operation that takes a larger input.
4356SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4357 MVT::ValueType DestVT,
4358 bool isSigned) {
4359 // First step, figure out the appropriate *INT_TO_FP operation to use.
4360 MVT::ValueType NewInTy = LegalOp.getValueType();
4361
4362 unsigned OpToUse = 0;
4363
4364 // Scan for the appropriate larger type to use.
4365 while (1) {
4366 NewInTy = (MVT::ValueType)(NewInTy+1);
4367 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4368
4369 // If the target supports SINT_TO_FP of this type, use it.
4370 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4371 default: break;
4372 case TargetLowering::Legal:
4373 if (!TLI.isTypeLegal(NewInTy))
4374 break; // Can't use this datatype.
4375 // FALL THROUGH.
4376 case TargetLowering::Custom:
4377 OpToUse = ISD::SINT_TO_FP;
4378 break;
4379 }
4380 if (OpToUse) break;
4381 if (isSigned) continue;
4382
4383 // If the target supports UINT_TO_FP of this type, use it.
4384 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4385 default: break;
4386 case TargetLowering::Legal:
4387 if (!TLI.isTypeLegal(NewInTy))
4388 break; // Can't use this datatype.
4389 // FALL THROUGH.
4390 case TargetLowering::Custom:
4391 OpToUse = ISD::UINT_TO_FP;
4392 break;
4393 }
4394 if (OpToUse) break;
4395
4396 // Otherwise, try a larger type.
4397 }
4398
4399 // Okay, we found the operation and type to use. Zero extend our input to the
4400 // desired type then run the operation on it.
4401 return DAG.getNode(OpToUse, DestVT,
4402 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4403 NewInTy, LegalOp));
4404}
4405
4406/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4407/// FP_TO_*INT operation of the specified operand when the target requests that
4408/// we promote it. At this point, we know that the result and operand types are
4409/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4410/// operation that returns a larger result.
4411SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4412 MVT::ValueType DestVT,
4413 bool isSigned) {
4414 // First step, figure out the appropriate FP_TO*INT operation to use.
4415 MVT::ValueType NewOutTy = DestVT;
4416
4417 unsigned OpToUse = 0;
4418
4419 // Scan for the appropriate larger type to use.
4420 while (1) {
4421 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4422 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4423
4424 // If the target supports FP_TO_SINT returning this type, use it.
4425 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4426 default: break;
4427 case TargetLowering::Legal:
4428 if (!TLI.isTypeLegal(NewOutTy))
4429 break; // Can't use this datatype.
4430 // FALL THROUGH.
4431 case TargetLowering::Custom:
4432 OpToUse = ISD::FP_TO_SINT;
4433 break;
4434 }
4435 if (OpToUse) break;
4436
4437 // If the target supports FP_TO_UINT of this type, use it.
4438 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4439 default: break;
4440 case TargetLowering::Legal:
4441 if (!TLI.isTypeLegal(NewOutTy))
4442 break; // Can't use this datatype.
4443 // FALL THROUGH.
4444 case TargetLowering::Custom:
4445 OpToUse = ISD::FP_TO_UINT;
4446 break;
4447 }
4448 if (OpToUse) break;
4449
4450 // Otherwise, try a larger type.
4451 }
4452
4453 // Okay, we found the operation and type to use. Truncate the result of the
4454 // extended FP_TO_*INT operation to the desired size.
4455 return DAG.getNode(ISD::TRUNCATE, DestVT,
4456 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4457}
4458
4459/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4460///
4461SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4462 MVT::ValueType VT = Op.getValueType();
4463 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4464 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4465 switch (VT) {
4466 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4467 case MVT::i16:
4468 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4469 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4470 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4471 case MVT::i32:
4472 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4473 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4474 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4475 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4476 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4477 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4478 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4479 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4480 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4481 case MVT::i64:
4482 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4483 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4484 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4485 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4486 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4487 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4488 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4489 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4490 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4491 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4492 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4493 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4494 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4495 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4496 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4497 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4498 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4499 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4500 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4501 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4502 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4503 }
4504}
4505
4506/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4507///
4508SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4509 switch (Opc) {
4510 default: assert(0 && "Cannot expand this yet!");
4511 case ISD::CTPOP: {
4512 static const uint64_t mask[6] = {
4513 0x5555555555555555ULL, 0x3333333333333333ULL,
4514 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4515 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4516 };
4517 MVT::ValueType VT = Op.getValueType();
4518 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4519 unsigned len = getSizeInBits(VT);
4520 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4521 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4522 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4523 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4524 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4525 DAG.getNode(ISD::AND, VT,
4526 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4527 }
4528 return Op;
4529 }
4530 case ISD::CTLZ: {
4531 // for now, we do this:
4532 // x = x | (x >> 1);
4533 // x = x | (x >> 2);
4534 // ...
4535 // x = x | (x >>16);
4536 // x = x | (x >>32); // for 64-bit input
4537 // return popcount(~x);
4538 //
4539 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4540 MVT::ValueType VT = Op.getValueType();
4541 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4542 unsigned len = getSizeInBits(VT);
4543 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4544 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4545 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4546 }
4547 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4548 return DAG.getNode(ISD::CTPOP, VT, Op);
4549 }
4550 case ISD::CTTZ: {
4551 // for now, we use: { return popcount(~x & (x - 1)); }
4552 // unless the target has ctlz but not ctpop, in which case we use:
4553 // { return 32 - nlz(~x & (x-1)); }
4554 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4555 MVT::ValueType VT = Op.getValueType();
4556 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4557 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4558 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4559 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4560 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4561 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4562 TLI.isOperationLegal(ISD::CTLZ, VT))
4563 return DAG.getNode(ISD::SUB, VT,
4564 DAG.getConstant(getSizeInBits(VT), VT),
4565 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4566 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4567 }
4568 }
4569}
Chris Lattnere34b3962005-01-19 04:19:40 +00004570
Chris Lattner3e928bb2005-01-07 07:47:09 +00004571/// ExpandOp - Expand the specified SDOperand into its two component pieces
4572/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4573/// LegalizeNodes map is filled in for any results that are not expanded, the
4574/// ExpandedNodes map is filled in for any results that are expanded, and the
4575/// Lo/Hi values are returned.
4576void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4577 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004578 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004579 SDNode *Node = Op.Val;
4580 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004581 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4582 VT == MVT::Vector) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004583 "Cannot expand to FP value or to larger int value!");
4584
Chris Lattner6fdcb252005-09-02 20:32:45 +00004585 // See if we already expanded it.
4586 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
4587 = ExpandedNodes.find(Op);
4588 if (I != ExpandedNodes.end()) {
4589 Lo = I->second.first;
4590 Hi = I->second.second;
4591 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004592 }
4593
Chris Lattner3e928bb2005-01-07 07:47:09 +00004594 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004595 case ISD::CopyFromReg:
4596 assert(0 && "CopyFromReg must be legal!");
4597 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004598#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00004599 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004600#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004601 assert(0 && "Do not know how to expand this operator!");
4602 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004603 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004604 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004605 Lo = DAG.getNode(ISD::UNDEF, NVT);
4606 Hi = DAG.getNode(ISD::UNDEF, NVT);
4607 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004608 case ISD::Constant: {
4609 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4610 Lo = DAG.getConstant(Cst, NVT);
4611 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4612 break;
4613 }
Evan Cheng00495212006-12-12 21:32:44 +00004614 case ISD::ConstantFP: {
4615 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004616 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004617 if (getTypeAction(Lo.getValueType()) == Expand)
4618 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004619 break;
4620 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004621 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004622 // Return the operands.
4623 Lo = Node->getOperand(0);
4624 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004625 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004626
4627 case ISD::SIGN_EXTEND_INREG:
4628 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004629 // sext_inreg the low part if needed.
4630 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4631
4632 // The high part gets the sign extension from the lo-part. This handles
4633 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00004634 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4635 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4636 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00004637 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004638
Nate Begemand88fc032006-01-14 03:14:10 +00004639 case ISD::BSWAP: {
4640 ExpandOp(Node->getOperand(0), Lo, Hi);
4641 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4642 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4643 Lo = TempLo;
4644 break;
4645 }
4646
Chris Lattneredb1add2005-05-11 04:51:16 +00004647 case ISD::CTPOP:
4648 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00004649 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4650 DAG.getNode(ISD::CTPOP, NVT, Lo),
4651 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00004652 Hi = DAG.getConstant(0, NVT);
4653 break;
4654
Chris Lattner39a8f332005-05-12 19:05:01 +00004655 case ISD::CTLZ: {
4656 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004657 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004658 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4659 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004660 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4661 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004662 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4663 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4664
4665 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4666 Hi = DAG.getConstant(0, NVT);
4667 break;
4668 }
4669
4670 case ISD::CTTZ: {
4671 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00004672 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00004673 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4674 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004675 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4676 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00004677 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4678 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4679
4680 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4681 Hi = DAG.getConstant(0, NVT);
4682 break;
4683 }
Chris Lattneredb1add2005-05-11 04:51:16 +00004684
Nate Begemanacc398c2006-01-25 18:21:52 +00004685 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004686 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4687 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00004688 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4689 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4690
4691 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004692 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00004693 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4694 if (!TLI.isLittleEndian())
4695 std::swap(Lo, Hi);
4696 break;
4697 }
4698
Chris Lattner3e928bb2005-01-07 07:47:09 +00004699 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00004700 LoadSDNode *LD = cast<LoadSDNode>(Node);
4701 SDOperand Ch = LD->getChain(); // Legalize the chain.
4702 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4703 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattner3e928bb2005-01-07 07:47:09 +00004704
Evan Cheng466685d2006-10-09 20:57:25 +00004705 if (ExtType == ISD::NON_EXTLOAD) {
4706 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng00495212006-12-12 21:32:44 +00004707 if (VT == MVT::f32 || VT == MVT::f64) {
4708 // f32->i32 or f64->i64 one to one expansion.
4709 // Remember that we legalized the chain.
4710 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00004711 // Recursively expand the new load.
4712 if (getTypeAction(NVT) == Expand)
4713 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004714 break;
4715 }
Chris Lattnerec39a452005-01-19 18:02:17 +00004716
Evan Cheng466685d2006-10-09 20:57:25 +00004717 // Increment the pointer to the other half.
4718 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4719 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4720 getIntPtrConstant(IncrementSize));
4721 // FIXME: This creates a bogus srcvalue!
4722 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004723
Evan Cheng466685d2006-10-09 20:57:25 +00004724 // Build a factor node to remember that this load is independent of the
4725 // other one.
4726 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4727 Hi.getValue(1));
4728
4729 // Remember that we legalized the chain.
4730 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4731 if (!TLI.isLittleEndian())
4732 std::swap(Lo, Hi);
4733 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00004734 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00004735
4736 if (VT == MVT::f64 && EVT == MVT::f32) {
4737 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4738 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4739 LD->getSrcValueOffset());
4740 // Remember that we legalized the chain.
4741 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4742 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4743 break;
4744 }
Evan Cheng466685d2006-10-09 20:57:25 +00004745
4746 if (EVT == NVT)
4747 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4748 LD->getSrcValueOffset());
4749 else
4750 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4751 LD->getSrcValueOffset(), EVT);
4752
4753 // Remember that we legalized the chain.
4754 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4755
4756 if (ExtType == ISD::SEXTLOAD) {
4757 // The high part is obtained by SRA'ing all but one of the bits of the
4758 // lo part.
4759 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4760 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4761 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4762 } else if (ExtType == ISD::ZEXTLOAD) {
4763 // The high part is just a zero.
4764 Hi = DAG.getConstant(0, NVT);
4765 } else /* if (ExtType == ISD::EXTLOAD) */ {
4766 // The high part is undefined.
4767 Hi = DAG.getNode(ISD::UNDEF, NVT);
4768 }
4769 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004770 break;
4771 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004772 case ISD::AND:
4773 case ISD::OR:
4774 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4775 SDOperand LL, LH, RL, RH;
4776 ExpandOp(Node->getOperand(0), LL, LH);
4777 ExpandOp(Node->getOperand(1), RL, RH);
4778 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4779 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4780 break;
4781 }
4782 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00004783 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004784 ExpandOp(Node->getOperand(1), LL, LH);
4785 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00004786 if (getTypeAction(NVT) == Expand)
4787 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004788 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00004789 if (VT != MVT::f32)
4790 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004791 break;
4792 }
Nate Begeman9373a812005-08-10 20:51:12 +00004793 case ISD::SELECT_CC: {
4794 SDOperand TL, TH, FL, FH;
4795 ExpandOp(Node->getOperand(2), TL, TH);
4796 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00004797 if (getTypeAction(NVT) == Expand)
4798 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00004799 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4800 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00004801 if (VT != MVT::f32)
4802 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4803 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004804 break;
4805 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004806 case ISD::ANY_EXTEND:
4807 // The low part is any extension of the input (which degenerates to a copy).
4808 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4809 // The high part is undefined.
4810 Hi = DAG.getNode(ISD::UNDEF, NVT);
4811 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004812 case ISD::SIGN_EXTEND: {
4813 // The low part is just a sign extension of the input (which degenerates to
4814 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004815 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004816
Chris Lattner3e928bb2005-01-07 07:47:09 +00004817 // The high part is obtained by SRA'ing all but one of the bits of the lo
4818 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00004819 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00004820 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4821 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00004822 break;
4823 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00004824 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00004825 // The low part is just a zero extension of the input (which degenerates to
4826 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00004827 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004828
Chris Lattner3e928bb2005-01-07 07:47:09 +00004829 // The high part is just a zero.
4830 Hi = DAG.getConstant(0, NVT);
4831 break;
Chris Lattner35481892005-12-23 00:16:34 +00004832
4833 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004834 SDOperand Tmp;
4835 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4836 // If the target wants to, allow it to lower this itself.
4837 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4838 case Expand: assert(0 && "cannot expand FP!");
4839 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4840 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4841 }
4842 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4843 }
4844
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004845 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00004846 if (VT == MVT::f32 || VT == MVT::f64) {
4847 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00004848 if (getTypeAction(NVT) == Expand)
4849 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00004850 break;
4851 }
4852
4853 // If source operand will be expanded to the same type as VT, i.e.
4854 // i64 <- f64, i32 <- f32, expand the source operand instead.
4855 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4856 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4857 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004858 break;
4859 }
4860
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004861 // Turn this into a load/store pair by default.
4862 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00004863 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00004864
Chris Lattner35481892005-12-23 00:16:34 +00004865 ExpandOp(Tmp, Lo, Hi);
4866 break;
4867 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004868
Chris Lattner8137c9e2006-01-28 05:07:51 +00004869 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00004870 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4871 TargetLowering::Custom &&
4872 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004873 Lo = TLI.LowerOperation(Op, DAG);
4874 assert(Lo.Val && "Node must be custom expanded!");
4875 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00004876 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00004877 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00004878 break;
4879
Chris Lattner4e6c7462005-01-08 19:27:05 +00004880 // These operators cannot be expanded directly, emit them as calls to
4881 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00004882 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00004883 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00004884 SDOperand Op;
4885 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4886 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00004887 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4888 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00004889 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004890
Chris Lattnerf20d1832005-07-30 01:40:57 +00004891 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4892
Chris Lattner80a3e942005-07-29 00:33:32 +00004893 // Now that the custom expander is done, expand the result, which is still
4894 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00004895 if (Op.Val) {
4896 ExpandOp(Op, Lo, Hi);
4897 break;
4898 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004899 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004900
Evan Cheng56966222007-01-12 02:11:51 +00004901 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004902 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004903 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004904 else
Evan Cheng56966222007-01-12 02:11:51 +00004905 LC = RTLIB::FPTOSINT_F64_I64;
4906 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4907 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004908 break;
Evan Cheng56966222007-01-12 02:11:51 +00004909 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004910
Evan Cheng56966222007-01-12 02:11:51 +00004911 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00004912 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004913 SDOperand Op;
4914 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4915 case Expand: assert(0 && "cannot expand FP!");
4916 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4917 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4918 }
4919
4920 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4921
4922 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00004923 if (Op.Val) {
4924 ExpandOp(Op, Lo, Hi);
4925 break;
4926 }
Chris Lattner80a3e942005-07-29 00:33:32 +00004927 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004928
Evan Cheng56966222007-01-12 02:11:51 +00004929 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004930 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004931 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00004932 else
Evan Cheng56966222007-01-12 02:11:51 +00004933 LC = RTLIB::FPTOUINT_F64_I64;
4934 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4935 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00004936 break;
Evan Cheng56966222007-01-12 02:11:51 +00004937 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00004938
Evan Cheng05a2d562006-01-09 18:31:59 +00004939 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004940 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004941 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004942 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004943 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004944 Op = TLI.LowerOperation(Op, DAG);
4945 if (Op.Val) {
4946 // Now that the custom expander is done, expand the result, which is
4947 // still VT.
4948 ExpandOp(Op, Lo, Hi);
4949 break;
4950 }
4951 }
4952
Chris Lattner79980b02006-09-13 03:50:39 +00004953 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
4954 // this X << 1 as X+X.
4955 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
4956 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
4957 TLI.isOperationLegal(ISD::ADDE, NVT)) {
4958 SDOperand LoOps[2], HiOps[3];
4959 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
4960 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
4961 LoOps[1] = LoOps[0];
4962 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
4963
4964 HiOps[1] = HiOps[0];
4965 HiOps[2] = Lo.getValue(1);
4966 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
4967 break;
4968 }
4969 }
4970
Chris Lattnere34b3962005-01-19 04:19:40 +00004971 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00004972 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00004973 break;
Chris Lattner47599822005-04-02 03:38:53 +00004974
4975 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00004976 TargetLowering::LegalizeAction Action =
4977 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4978 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4979 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004980 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00004981 break;
4982 }
4983
Chris Lattnere34b3962005-01-19 04:19:40 +00004984 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00004985 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
4986 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00004987 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00004988 }
Chris Lattnere34b3962005-01-19 04:19:40 +00004989
Evan Cheng05a2d562006-01-09 18:31:59 +00004990 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00004991 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00004992 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00004993 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00004994 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00004995 Op = TLI.LowerOperation(Op, DAG);
4996 if (Op.Val) {
4997 // Now that the custom expander is done, expand the result, which is
4998 // still VT.
4999 ExpandOp(Op, Lo, Hi);
5000 break;
5001 }
5002 }
5003
Chris Lattnere34b3962005-01-19 04:19:40 +00005004 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005005 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005006 break;
Chris Lattner47599822005-04-02 03:38:53 +00005007
5008 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005009 TargetLowering::LegalizeAction Action =
5010 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5011 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5012 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005013 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005014 break;
5015 }
5016
Chris Lattnere34b3962005-01-19 04:19:40 +00005017 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005018 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5019 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005020 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005021 }
5022
5023 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005024 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005025 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005026 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005027 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005028 Op = TLI.LowerOperation(Op, DAG);
5029 if (Op.Val) {
5030 // Now that the custom expander is done, expand the result, which is
5031 // still VT.
5032 ExpandOp(Op, Lo, Hi);
5033 break;
5034 }
5035 }
5036
Chris Lattnere34b3962005-01-19 04:19:40 +00005037 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005038 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005039 break;
Chris Lattner47599822005-04-02 03:38:53 +00005040
5041 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005042 TargetLowering::LegalizeAction Action =
5043 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5044 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5045 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005046 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005047 break;
5048 }
5049
Chris Lattnere34b3962005-01-19 04:19:40 +00005050 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005051 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5052 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005053 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005054 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005055
Misha Brukmanedf128a2005-04-21 22:36:52 +00005056 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005057 case ISD::SUB: {
5058 // If the target wants to custom expand this, let them.
5059 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5060 TargetLowering::Custom) {
5061 Op = TLI.LowerOperation(Op, DAG);
5062 if (Op.Val) {
5063 ExpandOp(Op, Lo, Hi);
5064 break;
5065 }
5066 }
5067
5068 // Expand the subcomponents.
5069 SDOperand LHSL, LHSH, RHSL, RHSH;
5070 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5071 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005072 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5073 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005074 LoOps[0] = LHSL;
5075 LoOps[1] = RHSL;
5076 HiOps[0] = LHSH;
5077 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005078 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005079 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005080 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005081 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005082 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005083 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005084 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005085 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005086 }
Chris Lattner84f67882005-01-20 18:52:28 +00005087 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005088 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005089 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005090 // If the target wants to custom expand this, let them.
5091 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005092 SDOperand New = TLI.LowerOperation(Op, DAG);
5093 if (New.Val) {
5094 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005095 break;
5096 }
5097 }
5098
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005099 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5100 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005101 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005102 SDOperand LL, LH, RL, RH;
5103 ExpandOp(Node->getOperand(0), LL, LH);
5104 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005105 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005106 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005107 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5108 // extended the sign bit of the low half through the upper half, and if so
5109 // emit a MULHS instead of the alternate sequence that is valid for any
5110 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005111 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005112 // is RH an extension of the sign bit of RL?
5113 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5114 RH.getOperand(1).getOpcode() == ISD::Constant &&
5115 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5116 // is LH an extension of the sign bit of LL?
5117 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5118 LH.getOperand(1).getOpcode() == ISD::Constant &&
5119 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005120 // Low part:
5121 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5122 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005123 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005124 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005125 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005126 // Low part:
5127 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5128
5129 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005130 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5131 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5132 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5133 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5134 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005135 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005136 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005137 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005138
Evan Cheng56966222007-01-12 02:11:51 +00005139 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5140 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005141 break;
5142 }
Evan Cheng56966222007-01-12 02:11:51 +00005143 case ISD::SDIV:
5144 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5145 break;
5146 case ISD::UDIV:
5147 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5148 break;
5149 case ISD::SREM:
5150 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5151 break;
5152 case ISD::UREM:
5153 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5154 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005155
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005156 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005157 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5158 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5159 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005160 break;
5161 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005162 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5163 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5164 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005165 break;
5166 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005167 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5168 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5169 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005170 break;
5171 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005172 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5173 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5174 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005175 break;
5176 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005177 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005178 break;
5179 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005180 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005181 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005182 case ISD::FSQRT:
5183 case ISD::FSIN:
5184 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005185 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005186 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005187 case ISD::FSQRT:
5188 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5189 break;
5190 case ISD::FSIN:
5191 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5192 break;
5193 case ISD::FCOS:
5194 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5195 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005196 default: assert(0 && "Unreachable!");
5197 }
Evan Cheng56966222007-01-12 02:11:51 +00005198 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005199 break;
5200 }
Evan Cheng966bf242006-12-16 00:52:40 +00005201 case ISD::FABS: {
5202 SDOperand Mask = (VT == MVT::f64)
5203 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5204 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5205 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5206 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5207 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5208 if (getTypeAction(NVT) == Expand)
5209 ExpandOp(Lo, Lo, Hi);
5210 break;
5211 }
5212 case ISD::FNEG: {
5213 SDOperand Mask = (VT == MVT::f64)
5214 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5215 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5216 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5217 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5218 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5219 if (getTypeAction(NVT) == Expand)
5220 ExpandOp(Lo, Lo, Hi);
5221 break;
5222 }
Evan Cheng912095b2007-01-04 21:56:39 +00005223 case ISD::FCOPYSIGN: {
5224 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5225 if (getTypeAction(NVT) == Expand)
5226 ExpandOp(Lo, Lo, Hi);
5227 break;
5228 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005229 case ISD::SINT_TO_FP:
5230 case ISD::UINT_TO_FP: {
5231 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5232 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005233 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005234 if (Node->getOperand(0).getValueType() == MVT::i64) {
5235 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005236 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005237 else
Evan Cheng56966222007-01-12 02:11:51 +00005238 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005239 } else {
5240 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005241 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005242 else
Evan Cheng56966222007-01-12 02:11:51 +00005243 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005244 }
5245
5246 // Promote the operand if needed.
5247 if (getTypeAction(SrcVT) == Promote) {
5248 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5249 Tmp = isSigned
5250 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5251 DAG.getValueType(SrcVT))
5252 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5253 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5254 }
Evan Cheng56966222007-01-12 02:11:51 +00005255 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00005256 break;
5257 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005258 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005259
Chris Lattner83397362005-12-21 18:02:52 +00005260 // Make sure the resultant values have been legalized themselves, unless this
5261 // is a type that requires multi-step expansion.
5262 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5263 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005264 if (Hi.Val)
5265 // Don't legalize the high part if it is expanded to a single node.
5266 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005267 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005268
5269 // Remember in a map if the values will be reused later.
5270 bool isNew =
5271 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5272 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005273}
5274
Chris Lattnerc7029802006-03-18 01:44:44 +00005275/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5276/// two smaller values of MVT::Vector type.
5277void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5278 SDOperand &Hi) {
5279 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5280 SDNode *Node = Op.Val;
5281 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5282 assert(NumElements > 1 && "Cannot split a single element vector!");
5283 unsigned NewNumElts = NumElements/2;
5284 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5285 SDOperand TypeNode = *(Node->op_end()-1);
5286
5287 // See if we already split it.
5288 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5289 = SplitNodes.find(Op);
5290 if (I != SplitNodes.end()) {
5291 Lo = I->second.first;
5292 Hi = I->second.second;
5293 return;
5294 }
5295
5296 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005297 default:
5298#ifndef NDEBUG
5299 Node->dump();
5300#endif
5301 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerb2827b02006-03-19 00:52:58 +00005302 case ISD::VBUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005303 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5304 Node->op_begin()+NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005305 LoOps.push_back(NewNumEltsNode);
5306 LoOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005307 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005308
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005309 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5310 Node->op_end()-2);
Chris Lattnerc7029802006-03-18 01:44:44 +00005311 HiOps.push_back(NewNumEltsNode);
5312 HiOps.push_back(TypeNode);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005313 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005314 break;
5315 }
5316 case ISD::VADD:
5317 case ISD::VSUB:
5318 case ISD::VMUL:
5319 case ISD::VSDIV:
5320 case ISD::VUDIV:
5321 case ISD::VAND:
5322 case ISD::VOR:
5323 case ISD::VXOR: {
5324 SDOperand LL, LH, RL, RH;
5325 SplitVectorOp(Node->getOperand(0), LL, LH);
5326 SplitVectorOp(Node->getOperand(1), RL, RH);
5327
5328 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5329 NewNumEltsNode, TypeNode);
5330 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5331 NewNumEltsNode, TypeNode);
5332 break;
5333 }
5334 case ISD::VLOAD: {
5335 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5336 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5337 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5338
5339 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5340 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5341 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5342 getIntPtrConstant(IncrementSize));
5343 // FIXME: This creates a bogus srcvalue!
5344 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5345
5346 // Build a factor node to remember that this load is independent of the
5347 // other one.
5348 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5349 Hi.getValue(1));
5350
5351 // Remember that we legalized the chain.
5352 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005353 break;
5354 }
Chris Lattner7692eb42006-03-23 21:16:34 +00005355 case ISD::VBIT_CONVERT: {
5356 // We know the result is a vector. The input may be either a vector or a
5357 // scalar value.
5358 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5359 // Lower to a store/load. FIXME: this could be improved probably.
5360 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5361
Evan Cheng786225a2006-10-05 23:01:46 +00005362 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005363 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005364 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5365 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5366 SplitVectorOp(St, Lo, Hi);
5367 } else {
5368 // If the input is a vector type, we have to either scalarize it, pack it
5369 // or convert it based on whether the input vector type is legal.
5370 SDNode *InVal = Node->getOperand(0).Val;
5371 unsigned NumElems =
5372 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5373 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5374
5375 // If the input is from a single element vector, scalarize the vector,
5376 // then treat like a scalar.
5377 if (NumElems == 1) {
5378 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5379 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5380 Op.getOperand(1), Op.getOperand(2));
5381 SplitVectorOp(Scalar, Lo, Hi);
5382 } else {
5383 // Split the input vector.
5384 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5385
5386 // Convert each of the pieces now.
5387 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5388 NewNumEltsNode, TypeNode);
5389 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5390 NewNumEltsNode, TypeNode);
5391 }
5392 break;
5393 }
5394 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005395 }
5396
5397 // Remember in a map if the values will be reused later.
5398 bool isNew =
5399 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5400 assert(isNew && "Value already expanded?!?");
5401}
5402
5403
5404/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5405/// equivalent operation that returns a scalar (e.g. F32) or packed value
5406/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5407/// type for the result.
5408SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5409 MVT::ValueType NewVT) {
5410 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5411 SDNode *Node = Op.Val;
5412
5413 // See if we already packed it.
5414 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5415 if (I != PackedNodes.end()) return I->second;
5416
5417 SDOperand Result;
5418 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005419 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005420#ifndef NDEBUG
Bill Wendling832171c2006-12-07 20:04:42 +00005421 Node->dump(); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005422#endif
Chris Lattner2332b9f2006-03-19 01:17:20 +00005423 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005424 case ISD::VADD:
5425 case ISD::VSUB:
5426 case ISD::VMUL:
5427 case ISD::VSDIV:
5428 case ISD::VUDIV:
5429 case ISD::VAND:
5430 case ISD::VOR:
5431 case ISD::VXOR:
5432 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5433 NewVT,
5434 PackVectorOp(Node->getOperand(0), NewVT),
5435 PackVectorOp(Node->getOperand(1), NewVT));
5436 break;
5437 case ISD::VLOAD: {
Chris Lattner4794a6b2006-03-19 00:07:49 +00005438 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5439 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005440
Evan Cheng466685d2006-10-09 20:57:25 +00005441 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5442 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattnerc7029802006-03-18 01:44:44 +00005443
5444 // Remember that we legalized the chain.
5445 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5446 break;
5447 }
Chris Lattnerb2827b02006-03-19 00:52:58 +00005448 case ISD::VBUILD_VECTOR:
Chris Lattner70c2a612006-03-31 02:06:56 +00005449 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005450 // Returning a scalar?
Chris Lattnerc7029802006-03-18 01:44:44 +00005451 Result = Node->getOperand(0);
5452 } else {
Chris Lattnerb2827b02006-03-19 00:52:58 +00005453 // Returning a BUILD_VECTOR?
Chris Lattner17614ea2006-04-08 05:34:25 +00005454
5455 // If all elements of the build_vector are undefs, return an undef.
5456 bool AllUndef = true;
5457 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5458 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5459 AllUndef = false;
5460 break;
5461 }
5462 if (AllUndef) {
5463 Result = DAG.getNode(ISD::UNDEF, NewVT);
5464 } else {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005465 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5466 Node->getNumOperands()-2);
Chris Lattner17614ea2006-04-08 05:34:25 +00005467 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005468 }
5469 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00005470 case ISD::VINSERT_VECTOR_ELT:
5471 if (!MVT::isVector(NewVT)) {
5472 // Returning a scalar? Must be the inserted element.
5473 Result = Node->getOperand(1);
5474 } else {
5475 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5476 PackVectorOp(Node->getOperand(0), NewVT),
5477 Node->getOperand(1), Node->getOperand(2));
5478 }
5479 break;
Chris Lattner5b2316e2006-03-28 20:24:43 +00005480 case ISD::VVECTOR_SHUFFLE:
5481 if (!MVT::isVector(NewVT)) {
5482 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5483 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5484 if (cast<ConstantSDNode>(EltNum)->getValue())
5485 Result = PackVectorOp(Node->getOperand(1), NewVT);
5486 else
5487 Result = PackVectorOp(Node->getOperand(0), NewVT);
5488 } else {
5489 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5490 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5491 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5492 Node->getOperand(2).Val->op_end()-2);
5493 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005494 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5495 Node->getOperand(2).Val->op_begin(),
5496 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattner5b2316e2006-03-28 20:24:43 +00005497
5498 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5499 PackVectorOp(Node->getOperand(0), NewVT),
5500 PackVectorOp(Node->getOperand(1), NewVT), BV);
5501 }
5502 break;
Chris Lattnere25ca692006-03-22 20:09:35 +00005503 case ISD::VBIT_CONVERT:
5504 if (Op.getOperand(0).getValueType() != MVT::Vector)
5505 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5506 else {
5507 // If the input is a vector type, we have to either scalarize it, pack it
5508 // or convert it based on whether the input vector type is legal.
5509 SDNode *InVal = Node->getOperand(0).Val;
5510 unsigned NumElems =
5511 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5512 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5513
5514 // Figure out if there is a Packed type corresponding to this Vector
5515 // type. If so, convert to the packed type.
5516 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5517 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5518 // Turn this into a bit convert of the packed input.
5519 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5520 PackVectorOp(Node->getOperand(0), TVT));
5521 break;
5522 } else if (NumElems == 1) {
5523 // Turn this into a bit convert of the scalar input.
5524 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5525 PackVectorOp(Node->getOperand(0), EVT));
5526 break;
5527 } else {
5528 // FIXME: UNIMP!
5529 assert(0 && "Cast from unsupported vector type not implemented yet!");
5530 }
5531 }
Evan Chengdb3c6262006-04-10 18:54:36 +00005532 break;
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005533 case ISD::VSELECT:
5534 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5535 PackVectorOp(Op.getOperand(1), NewVT),
5536 PackVectorOp(Op.getOperand(2), NewVT));
5537 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005538 }
5539
5540 if (TLI.isTypeLegal(NewVT))
5541 Result = LegalizeOp(Result);
5542 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5543 assert(isNew && "Value already packed?");
5544 return Result;
5545}
5546
Chris Lattner3e928bb2005-01-07 07:47:09 +00005547
5548// SelectionDAG::Legalize - This is the entry point for the file.
5549//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005550void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005551 if (ViewLegalizeDAGs) viewGraph();
5552
Chris Lattner3e928bb2005-01-07 07:47:09 +00005553 /// run - This is the main entry point to this class.
5554 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005555 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005556}
5557