blob: 2c5b9dad42868b46b98eb2a7bcb849675a6e1252 [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"
Evan Cheng61bbbab2007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner79715142007-02-03 01:12:36 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000032#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000033using namespace llvm;
34
Chris Lattner5e46a192006-04-02 03:07:27 +000035#ifndef NDEBUG
36static cl::opt<bool>
37ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
38 cl::desc("Pop up a window to show dags before legalize"));
39#else
40static const bool ViewLegalizeDAGs = 0;
41#endif
42
Chris Lattner3e928bb2005-01-07 07:47:09 +000043//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000056class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000057 TargetLowering &TLI;
58 SelectionDAG &DAG;
59
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
61
62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
65 SDOperand LastCALLSEQ_END;
66
67 /// IsLegalizingCall - This member is used *only* for purposes of providing
68 /// helpful assertions that a libcall isn't created while another call is
69 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
71
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Chris Lattner6831a812006-02-13 09:18:02 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000086 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Chris Lattner03c85462005-01-15 05:21:40 +000088 /// PromotedNodes - For nodes that are below legal width, and that have more
89 /// than one use, this map indicates what promoted value to use. This allows
90 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000091 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// ExpandedNodes - For nodes that need to be expanded this map indicates
94 /// which which operands are the expanded version of the input. This allows
95 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000096 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattnerc7029802006-03-18 01:44:44 +000098 /// SplitNodes - For vector nodes that need to be split, this map indicates
99 /// which which operands are the split version of the input. This allows us
100 /// to avoid splitting the same node more than once.
101 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
102
Dan Gohman7f321562007-06-25 16:23:39 +0000103 /// ScalarizedNodes - For nodes that need to be converted from vector types to
104 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000105 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000106 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000107
Chris Lattner8afc48e2005-01-07 22:28:47 +0000108 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113 }
Chris Lattner03c85462005-01-15 05:21:40 +0000114 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000119 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000120
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121public:
122
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000123 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124
Chris Lattner3e928bb2005-01-07 07:47:09 +0000125 /// getTypeAction - Return how we should legalize values of this type, either
126 /// it is already legal or we need to expand it into multiple registers of
127 /// smaller integer type, or we need to promote it to a larger type.
128 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000129 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000130 }
131
132 /// isTypeLegal - Return true if this type is legal on this target.
133 ///
134 bool isTypeLegal(MVT::ValueType VT) const {
135 return getTypeAction(VT) == Legal;
136 }
137
Chris Lattner3e928bb2005-01-07 07:47:09 +0000138 void LegalizeDAG();
139
Chris Lattner456a93a2006-01-28 07:39:30 +0000140private:
Dan Gohman7f321562007-06-25 16:23:39 +0000141 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000142 /// appropriate for its type.
143 void HandleOp(SDOperand Op);
144
145 /// LegalizeOp - We know that the specified value has a legal type.
146 /// Recursively ensure that the operands have legal types, then return the
147 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000148 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000149
150 /// PromoteOp - Given an operation that produces a value in an invalid type,
151 /// promote it to compute the value into a larger type. The produced value
152 /// will have the correct bits for the low portion of the register, but no
153 /// guarantee is made about the top bits: it may be zero, sign-extended, or
154 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000155 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156
Chris Lattnerc7029802006-03-18 01:44:44 +0000157 /// ExpandOp - Expand the specified SDOperand into its two component pieces
158 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
159 /// the LegalizeNodes map is filled in for any results that are not expanded,
160 /// the ExpandedNodes map is filled in for any results that are expanded, and
161 /// the Lo/Hi values are returned. This applies to integer types and Vector
162 /// types.
163 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
164
Dan Gohman7f321562007-06-25 16:23:39 +0000165 /// SplitVectorOp - Given an operand of vector type, break it down into
166 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000167 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
168
Dan Gohman89b20c02007-06-27 14:06:22 +0000169 /// ScalarizeVectorOp - Given an operand of single-element vector type
170 /// (e.g. v1f32), convert it into the equivalent operation that returns a
171 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000172 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000173
Chris Lattner4352cc92006-04-04 17:23:26 +0000174 /// isShuffleLegal - Return true if a vector shuffle is legal with the
175 /// specified mask and type. Targets can specify exactly which masks they
176 /// support and the code generator is tasked with not creating illegal masks.
177 ///
178 /// Note that this will also return true for shuffles that are promoted to a
179 /// different type.
180 ///
181 /// If this is a legal shuffle, this method returns the (possibly promoted)
182 /// build_vector Mask. If it's not a legal shuffle, it returns null.
183 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
184
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000185 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000186 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000187
Nate Begeman750ac1b2006-02-01 07:19:44 +0000188 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
189
Chris Lattnerce872152006-03-19 06:31:19 +0000190 SDOperand CreateStackTemporary(MVT::ValueType VT);
191
Reid Spencer47857812006-12-31 05:55:36 +0000192 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000193 SDOperand &Hi);
194 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
195 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000196
Chris Lattner35481892005-12-23 00:16:34 +0000197 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000198 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000199 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000200 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
201 SDOperand LegalOp,
202 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000203 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
204 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000205 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
206 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000207
Chris Lattner456a93a2006-01-28 07:39:30 +0000208 SDOperand ExpandBSWAP(SDOperand Op);
209 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000210 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000212 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
213 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000214
Dan Gohman7f321562007-06-25 16:23:39 +0000215 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000216 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000217
Chris Lattner3e928bb2005-01-07 07:47:09 +0000218 SDOperand getIntPtrConstant(uint64_t Val) {
219 return DAG.getConstant(Val, TLI.getPointerTy());
220 }
221};
222}
223
Chris Lattner4352cc92006-04-04 17:23:26 +0000224/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
225/// specified mask and type. Targets can specify exactly which masks they
226/// support and the code generator is tasked with not creating illegal masks.
227///
228/// Note that this will also return true for shuffles that are promoted to a
229/// different type.
230SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
231 SDOperand Mask) const {
232 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
233 default: return 0;
234 case TargetLowering::Legal:
235 case TargetLowering::Custom:
236 break;
237 case TargetLowering::Promote: {
238 // If this is promoted to a different type, convert the shuffle mask and
239 // ask if it is legal in the promoted type!
240 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
241
242 // If we changed # elements, change the shuffle mask.
243 unsigned NumEltsGrowth =
244 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
245 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
246 if (NumEltsGrowth > 1) {
247 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000248 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000249 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
250 SDOperand InOp = Mask.getOperand(i);
251 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
252 if (InOp.getOpcode() == ISD::UNDEF)
253 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
254 else {
255 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
256 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
257 }
258 }
259 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000260 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000261 }
262 VT = NVT;
263 break;
264 }
265 }
266 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
267}
268
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000269SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
270 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
271 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000272 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000274}
275
Chris Lattnere10e6f72007-06-18 21:28:10 +0000276/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
277/// contains all of a nodes operands before it contains the node.
278static void ComputeTopDownOrdering(SelectionDAG &DAG,
279 SmallVector<SDNode*, 64> &Order) {
280
281 DenseMap<SDNode*, unsigned> Visited;
282 std::vector<SDNode*> Worklist;
283 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000284
Chris Lattnere10e6f72007-06-18 21:28:10 +0000285 // Compute ordering from all of the leaves in the graphs, those (like the
286 // entry node) that have no operands.
287 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
288 E = DAG.allnodes_end(); I != E; ++I) {
289 if (I->getNumOperands() == 0) {
290 Visited[I] = 0 - 1U;
291 Worklist.push_back(I);
292 }
Chris Lattner32fca002005-10-06 01:20:27 +0000293 }
294
Chris Lattnere10e6f72007-06-18 21:28:10 +0000295 while (!Worklist.empty()) {
296 SDNode *N = Worklist.back();
297 Worklist.pop_back();
298
299 if (++Visited[N] != N->getNumOperands())
300 continue; // Haven't visited all operands yet
301
302 Order.push_back(N);
303
304 // Now that we have N in, add anything that uses it if all of their operands
305 // are now done.
306 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
307 UI != E; ++UI)
308 Worklist.push_back(*UI);
309 }
310
311 assert(Order.size() == Visited.size() &&
312 Order.size() ==
313 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
314 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000315}
316
Chris Lattner1618beb2005-07-29 00:11:56 +0000317
Chris Lattner3e928bb2005-01-07 07:47:09 +0000318void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000319 LastCALLSEQ_END = DAG.getEntryNode();
320 IsLegalizingCall = false;
321
Chris Lattnerab510a72005-10-02 17:49:46 +0000322 // The legalize process is inherently a bottom-up recursive process (users
323 // legalize their uses before themselves). Given infinite stack space, we
324 // could just start legalizing on the root and traverse the whole graph. In
325 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000326 // blocks. To avoid this problem, compute an ordering of the nodes where each
327 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000328 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000329 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000330
Chris Lattnerc7029802006-03-18 01:44:44 +0000331 for (unsigned i = 0, e = Order.size(); i != e; ++i)
332 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000333
334 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000335 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000336 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
337 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000338
339 ExpandedNodes.clear();
340 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000341 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000342 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000343 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000344
345 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000346 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347}
348
Chris Lattner6831a812006-02-13 09:18:02 +0000349
350/// FindCallEndFromCallStart - Given a chained node that is part of a call
351/// sequence, find the CALLSEQ_END node that terminates the call sequence.
352static SDNode *FindCallEndFromCallStart(SDNode *Node) {
353 if (Node->getOpcode() == ISD::CALLSEQ_END)
354 return Node;
355 if (Node->use_empty())
356 return 0; // No CallSeqEnd
357
358 // The chain is usually at the end.
359 SDOperand TheChain(Node, Node->getNumValues()-1);
360 if (TheChain.getValueType() != MVT::Other) {
361 // Sometimes it's at the beginning.
362 TheChain = SDOperand(Node, 0);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Otherwise, hunt for it.
365 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
366 if (Node->getValueType(i) == MVT::Other) {
367 TheChain = SDOperand(Node, i);
368 break;
369 }
370
371 // Otherwise, we walked into a node without a chain.
372 if (TheChain.getValueType() != MVT::Other)
373 return 0;
374 }
375 }
376
377 for (SDNode::use_iterator UI = Node->use_begin(),
378 E = Node->use_end(); UI != E; ++UI) {
379
380 // Make sure to only follow users of our token chain.
381 SDNode *User = *UI;
382 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
383 if (User->getOperand(i) == TheChain)
384 if (SDNode *Result = FindCallEndFromCallStart(User))
385 return Result;
386 }
387 return 0;
388}
389
390/// FindCallStartFromCallEnd - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_START node that initiates the call sequence.
392static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
393 assert(Node && "Didn't find callseq_start for a call??");
394 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
395
396 assert(Node->getOperand(0).getValueType() == MVT::Other &&
397 "Node doesn't have a token chain argument!");
398 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
399}
400
401/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
402/// see if any uses can reach Dest. If no dest operands can get to dest,
403/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000404///
405/// Keep track of the nodes we fine that actually do lead to Dest in
406/// NodesLeadingTo. This avoids retraversing them exponential number of times.
407///
408bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000409 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000410 if (N == Dest) return true; // N certainly leads to Dest :)
411
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000412 // If we've already processed this node and it does lead to Dest, there is no
413 // need to reprocess it.
414 if (NodesLeadingTo.count(N)) return true;
415
Chris Lattner6831a812006-02-13 09:18:02 +0000416 // If the first result of this node has been already legalized, then it cannot
417 // reach N.
418 switch (getTypeAction(N->getValueType(0))) {
419 case Legal:
420 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
421 break;
422 case Promote:
423 if (PromotedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Expand:
426 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 }
429
430 // Okay, this node has not already been legalized. Check and legalize all
431 // operands. If none lead to Dest, then we can legalize this node.
432 bool OperandsLeadToDest = false;
433 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
434 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000435 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000436
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000437 if (OperandsLeadToDest) {
438 NodesLeadingTo.insert(N);
439 return true;
440 }
Chris Lattner6831a812006-02-13 09:18:02 +0000441
442 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000443 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000444 return false;
445}
446
Dan Gohman7f321562007-06-25 16:23:39 +0000447/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000448/// appropriate for its type.
449void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000450 MVT::ValueType VT = Op.getValueType();
451 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000452 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000453 case Legal: (void)LegalizeOp(Op); break;
454 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000455 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000456 if (!MVT::isVector(VT)) {
457 // If this is an illegal scalar, expand it into its two component
458 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000459 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000460 if (Op.getOpcode() == ISD::TargetConstant)
461 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000462 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000463 } else if (MVT::getVectorNumElements(VT) == 1) {
464 // If this is an illegal single element vector, convert it to a
465 // scalar operation.
466 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000467 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000468 // Otherwise, this is an illegal multiple element vector.
469 // Split it in half and legalize both parts.
470 SDOperand X, Y;
471 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000472 }
473 break;
474 }
475}
Chris Lattner6831a812006-02-13 09:18:02 +0000476
Evan Cheng9f877882006-12-13 20:57:08 +0000477/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
478/// a load from the constant pool.
479static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000480 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000481 bool Extend = false;
482
483 // If a FP immediate is precise when represented as a float and if the
484 // target can do an extending load from float to double, we put it into
485 // the constant pool as a float, even if it's is statically typed as a
486 // double.
487 MVT::ValueType VT = CFP->getValueType(0);
488 bool isDouble = VT == MVT::f64;
489 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000490 Type::FloatTy, CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000491 if (!UseCP) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000492 const APFloat& Val = LLVMC->getValueAPF();
Evan Cheng279101e2006-12-12 22:19:28 +0000493 return isDouble
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000494 ? DAG.getConstant(DoubleToBits(Val.convertToDouble()), MVT::i64)
495 : DAG.getConstant(FloatToBits(Val.convertToFloat()), MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000496 }
497
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000498 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000499 // Only do this if the target has a native EXTLOAD instruction from f32.
500 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
501 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
502 VT = MVT::f32;
503 Extend = true;
504 }
505
506 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
507 if (Extend) {
508 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
509 CPIdx, NULL, 0, MVT::f32);
510 } else {
511 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
512 }
513}
514
Chris Lattner6831a812006-02-13 09:18:02 +0000515
Evan Cheng912095b2007-01-04 21:56:39 +0000516/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
517/// operations.
518static
519SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
520 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000521 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000522 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000523 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
524 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000525 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000526
Evan Cheng912095b2007-01-04 21:56:39 +0000527 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000528 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000529 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
530 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000531 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000532 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000533 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000534 // Shift right or sign-extend it if the two operands have different types.
535 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
536 if (SizeDiff > 0) {
537 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
538 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
539 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
540 } else if (SizeDiff < 0)
541 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000542
543 // Clear the sign bit of first operand.
544 SDOperand Mask2 = (VT == MVT::f64)
545 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
546 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
547 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000548 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000549 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
550
551 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000552 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
553 return Result;
554}
555
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000556/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
557static
558SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
559 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000560 SDOperand Chain = ST->getChain();
561 SDOperand Ptr = ST->getBasePtr();
562 SDOperand Val = ST->getValue();
563 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000564 int Alignment = ST->getAlignment();
565 int SVOffset = ST->getSrcValueOffset();
566 if (MVT::isFloatingPoint(ST->getStoredVT())) {
567 // Expand to a bitconvert of the value to the integer type of the
568 // same size, then a (misaligned) int store.
569 MVT::ValueType intVT;
570 if (VT==MVT::f64)
571 intVT = MVT::i64;
572 else if (VT==MVT::f32)
573 intVT = MVT::i32;
574 else
575 assert(0 && "Unaligned load of unsupported floating point type");
576
577 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
578 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
579 SVOffset, ST->isVolatile(), Alignment);
580 }
581 assert(MVT::isInteger(ST->getStoredVT()) &&
582 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000583 // Get the half-size VT
584 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
585 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000586 int IncrementSize = NumBits / 8;
587
588 // Divide the stored value in two parts.
589 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
590 SDOperand Lo = Val;
591 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
592
593 // Store the two parts
594 SDOperand Store1, Store2;
595 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
596 ST->getSrcValue(), SVOffset, NewStoredVT,
597 ST->isVolatile(), Alignment);
598 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
599 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
600 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
601 ST->getSrcValue(), SVOffset + IncrementSize,
602 NewStoredVT, ST->isVolatile(), Alignment);
603
604 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
605}
606
607/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
608static
609SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
610 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000611 int SVOffset = LD->getSrcValueOffset();
612 SDOperand Chain = LD->getChain();
613 SDOperand Ptr = LD->getBasePtr();
614 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000615 MVT::ValueType LoadedVT = LD->getLoadedVT();
616 if (MVT::isFloatingPoint(VT)) {
617 // Expand to a (misaligned) integer load of the same size,
618 // then bitconvert to floating point.
619 MVT::ValueType intVT;
620 if (LoadedVT==MVT::f64)
621 intVT = MVT::i64;
622 else if (LoadedVT==MVT::f32)
623 intVT = MVT::i32;
624 else
625 assert(0 && "Unaligned load of unsupported floating point type");
626
627 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
628 SVOffset, LD->isVolatile(),
629 LD->getAlignment());
630 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
631 if (LoadedVT != VT)
632 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
633
634 SDOperand Ops[] = { Result, Chain };
635 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
636 Ops, 2);
637 }
638 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
639 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000640 int NumBits = MVT::getSizeInBits(NewLoadedVT);
641 int Alignment = LD->getAlignment();
642 int IncrementSize = NumBits / 8;
643 ISD::LoadExtType HiExtType = LD->getExtensionType();
644
645 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
646 if (HiExtType == ISD::NON_EXTLOAD)
647 HiExtType = ISD::ZEXTLOAD;
648
649 // Load the value in two parts
650 SDOperand Lo, Hi;
651 if (TLI.isLittleEndian()) {
652 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
653 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
654 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
655 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
656 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
657 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
658 Alignment);
659 } else {
660 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
661 NewLoadedVT,LD->isVolatile(), Alignment);
662 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
663 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
664 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
665 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
666 Alignment);
667 }
668
669 // aggregate the two parts
670 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
671 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
672 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
673
674 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
675 Hi.getValue(1));
676
677 SDOperand Ops[] = { Result, TF };
678 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
679}
Evan Cheng912095b2007-01-04 21:56:39 +0000680
Dan Gohmana3466152007-07-13 20:14:11 +0000681/// LegalizeOp - We know that the specified value has a legal type, and
682/// that its operands are legal. Now ensure that the operation itself
683/// is legal, recursively ensuring that the operands' operations remain
684/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000685SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000686 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
687 return Op;
688
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000689 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000690 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000691 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000692
Chris Lattner3e928bb2005-01-07 07:47:09 +0000693 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000694 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000695 if (Node->getNumValues() > 1) {
696 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000697 if (getTypeAction(Node->getValueType(i)) != Legal) {
698 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000699 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000700 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000701 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000702 }
703 }
704
Chris Lattner45982da2005-05-12 16:53:42 +0000705 // Note that LegalizeOp may be reentered even from single-use nodes, which
706 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000707 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000708 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000709
Nate Begeman9373a812005-08-10 20:51:12 +0000710 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000711 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000712 bool isCustom = false;
713
Chris Lattner3e928bb2005-01-07 07:47:09 +0000714 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000715 case ISD::FrameIndex:
716 case ISD::EntryToken:
717 case ISD::Register:
718 case ISD::BasicBlock:
719 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000720 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000721 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000722 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000723 case ISD::TargetConstantPool:
724 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000725 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000726 case ISD::TargetExternalSymbol:
727 case ISD::VALUETYPE:
728 case ISD::SRCVALUE:
729 case ISD::STRING:
730 case ISD::CONDCODE:
731 // Primitives must all be legal.
732 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
733 "This must be legal!");
734 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000735 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000736 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
737 // If this is a target node, legalize it by legalizing the operands then
738 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000739 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000740 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000741 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000742
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000743 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000744
745 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
746 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
747 return Result.getValue(Op.ResNo);
748 }
749 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000750#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000751 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000752#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000753 assert(0 && "Do not know how to legalize this operator!");
754 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000755 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000756 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000757 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000758 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000759 case ISD::ConstantPool:
760 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000761 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
762 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000763 case TargetLowering::Custom:
764 Tmp1 = TLI.LowerOperation(Op, DAG);
765 if (Tmp1.Val) Result = Tmp1;
766 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000767 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000768 break;
769 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000770 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000771 case ISD::FRAMEADDR:
772 case ISD::RETURNADDR:
773 // The only option for these nodes is to custom lower them. If the target
774 // does not custom lower them, then return zero.
775 Tmp1 = TLI.LowerOperation(Op, DAG);
776 if (Tmp1.Val)
777 Result = Tmp1;
778 else
779 Result = DAG.getConstant(0, TLI.getPointerTy());
780 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000781 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000782 MVT::ValueType VT = Node->getValueType(0);
783 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
784 default: assert(0 && "This action is not supported yet!");
785 case TargetLowering::Custom:
786 Result = TLI.LowerOperation(Op, DAG);
787 if (Result.Val) break;
788 // Fall Thru
789 case TargetLowering::Legal:
790 Result = DAG.getConstant(0, VT);
791 break;
792 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000793 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000794 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000795 case ISD::EXCEPTIONADDR: {
796 Tmp1 = LegalizeOp(Node->getOperand(0));
797 MVT::ValueType VT = Node->getValueType(0);
798 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
799 default: assert(0 && "This action is not supported yet!");
800 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000801 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000802 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
803 }
804 break;
805 case TargetLowering::Custom:
806 Result = TLI.LowerOperation(Op, DAG);
807 if (Result.Val) break;
808 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000809 case TargetLowering::Legal: {
810 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
811 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
812 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000813 break;
814 }
815 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000816 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000817 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000818 case ISD::EHSELECTION: {
819 Tmp1 = LegalizeOp(Node->getOperand(0));
820 Tmp2 = LegalizeOp(Node->getOperand(1));
821 MVT::ValueType VT = Node->getValueType(0);
822 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
823 default: assert(0 && "This action is not supported yet!");
824 case TargetLowering::Expand: {
825 unsigned Reg = TLI.getExceptionSelectorRegister();
826 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
827 }
828 break;
829 case TargetLowering::Custom:
830 Result = TLI.LowerOperation(Op, DAG);
831 if (Result.Val) break;
832 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000833 case TargetLowering::Legal: {
834 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
835 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
836 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000837 break;
838 }
839 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000840 }
Jim Laskey8782d482007-02-28 20:43:58 +0000841 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000842 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000843 MVT::ValueType VT = Node->getValueType(0);
844 // The only "good" option for this node is to custom lower it.
845 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
846 default: assert(0 && "This action is not supported at all!");
847 case TargetLowering::Custom:
848 Result = TLI.LowerOperation(Op, DAG);
849 if (Result.Val) break;
850 // Fall Thru
851 case TargetLowering::Legal:
852 // Target does not know, how to lower this, lower to noop
853 Result = LegalizeOp(Node->getOperand(0));
854 break;
855 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000856 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000857 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000858 case ISD::AssertSext:
859 case ISD::AssertZext:
860 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000861 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000862 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000863 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000864 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000865 Result = Node->getOperand(Op.ResNo);
866 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000867 case ISD::CopyFromReg:
868 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000869 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000870 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000871 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000872 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000873 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000874 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000875 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
877 } else {
878 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
879 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000880 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
881 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000882 // Since CopyFromReg produces two values, make sure to remember that we
883 // legalized both of them.
884 AddLegalizedOperand(Op.getValue(0), Result);
885 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
886 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000887 case ISD::UNDEF: {
888 MVT::ValueType VT = Op.getValueType();
889 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000890 default: assert(0 && "This action is not supported yet!");
891 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000892 if (MVT::isInteger(VT))
893 Result = DAG.getConstant(0, VT);
894 else if (MVT::isFloatingPoint(VT))
895 Result = DAG.getConstantFP(0, VT);
896 else
897 assert(0 && "Unknown value type!");
898 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000899 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000900 break;
901 }
902 break;
903 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000904
Chris Lattner48b61a72006-03-28 00:40:33 +0000905 case ISD::INTRINSIC_W_CHAIN:
906 case ISD::INTRINSIC_WO_CHAIN:
907 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000908 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000909 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
910 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000911 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000912
913 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000914 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000915 TargetLowering::Custom) {
916 Tmp3 = TLI.LowerOperation(Result, DAG);
917 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000918 }
919
920 if (Result.Val->getNumValues() == 1) break;
921
922 // Must have return value and chain result.
923 assert(Result.Val->getNumValues() == 2 &&
924 "Cannot return more than two values!");
925
926 // Since loads produce two values, make sure to remember that we
927 // legalized both of them.
928 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
929 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
930 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000931 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000932
933 case ISD::LOCATION:
934 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
935 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
936
937 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
938 case TargetLowering::Promote:
939 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000940 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000941 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000942 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000943 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000944
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000945 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000946 const std::string &FName =
947 cast<StringSDNode>(Node->getOperand(3))->getValue();
948 const std::string &DirName =
949 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000950 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000951
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000952 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000953 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000954 SDOperand LineOp = Node->getOperand(1);
955 SDOperand ColOp = Node->getOperand(2);
956
957 if (useDEBUG_LOC) {
958 Ops.push_back(LineOp); // line #
959 Ops.push_back(ColOp); // col #
960 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000961 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000962 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000963 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
964 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000965 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000966 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000967 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000968 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000969 } else {
970 Result = Tmp1; // chain
971 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000972 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000973 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000974 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000975 if (Tmp1 != Node->getOperand(0) ||
976 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000977 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000978 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000979 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
980 Ops.push_back(Node->getOperand(1)); // line # must be legal.
981 Ops.push_back(Node->getOperand(2)); // col # must be legal.
982 } else {
983 // Otherwise promote them.
984 Ops.push_back(PromoteOp(Node->getOperand(1)));
985 Ops.push_back(PromoteOp(Node->getOperand(2)));
986 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000987 Ops.push_back(Node->getOperand(3)); // filename must be legal.
988 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000989 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000990 }
991 break;
992 }
993 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000994
995 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000996 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000997 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000998 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000999 case TargetLowering::Legal:
1000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1001 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1002 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1003 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001005 break;
1006 }
1007 break;
1008
Jim Laskey1ee29252007-01-26 14:34:52 +00001009 case ISD::LABEL:
1010 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1011 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001012 default: assert(0 && "This action is not supported yet!");
1013 case TargetLowering::Legal:
1014 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1015 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001016 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001017 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001018 case TargetLowering::Expand:
1019 Result = LegalizeOp(Node->getOperand(0));
1020 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001021 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001022 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001023
Scott Michelc1513d22007-08-08 23:23:31 +00001024 case ISD::Constant: {
1025 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1026 unsigned opAction =
1027 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1028
Chris Lattner3e928bb2005-01-07 07:47:09 +00001029 // We know we don't need to expand constants here, constants only have one
1030 // value and we check that it is fine above.
1031
Scott Michelc1513d22007-08-08 23:23:31 +00001032 if (opAction == TargetLowering::Custom) {
1033 Tmp1 = TLI.LowerOperation(Result, DAG);
1034 if (Tmp1.Val)
1035 Result = Tmp1;
1036 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001037 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001038 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001039 case ISD::ConstantFP: {
1040 // Spill FP immediates to the constant pool if the target cannot directly
1041 // codegen them. Targets often have some immediate values that can be
1042 // efficiently generated into an FP register without a load. We explicitly
1043 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001044 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1045
1046 // Check to see if this FP immediate is already legal.
1047 bool isLegal = false;
1048 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1049 E = TLI.legal_fpimm_end(); I != E; ++I)
1050 if (CFP->isExactlyValue(*I)) {
1051 isLegal = true;
1052 break;
1053 }
1054
Chris Lattner3181a772006-01-29 06:26:56 +00001055 // If this is a legal constant, turn it into a TargetConstantFP node.
1056 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001057 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1058 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001059 break;
1060 }
1061
1062 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1063 default: assert(0 && "This action is not supported yet!");
1064 case TargetLowering::Custom:
1065 Tmp3 = TLI.LowerOperation(Result, DAG);
1066 if (Tmp3.Val) {
1067 Result = Tmp3;
1068 break;
1069 }
1070 // FALLTHROUGH
1071 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001072 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001073 }
1074 break;
1075 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001076 case ISD::TokenFactor:
1077 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001078 Tmp1 = LegalizeOp(Node->getOperand(0));
1079 Tmp2 = LegalizeOp(Node->getOperand(1));
1080 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1081 } else if (Node->getNumOperands() == 3) {
1082 Tmp1 = LegalizeOp(Node->getOperand(0));
1083 Tmp2 = LegalizeOp(Node->getOperand(1));
1084 Tmp3 = LegalizeOp(Node->getOperand(2));
1085 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001086 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001087 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001088 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001089 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1090 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001091 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001092 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001093 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001094
1095 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001096 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001097 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001098 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1099 assert(Tmp3.Val && "Target didn't custom lower this node!");
1100 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1101 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001102
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001103 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001104 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001105 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1106 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001107 if (Op.ResNo == i)
1108 Tmp2 = Tmp1;
1109 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1110 }
1111 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001112 case ISD::EXTRACT_SUBREG: {
1113 Tmp1 = LegalizeOp(Node->getOperand(0));
1114 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1115 assert(idx && "Operand must be a constant");
1116 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1117 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1118 }
1119 break;
1120 case ISD::INSERT_SUBREG: {
1121 Tmp1 = LegalizeOp(Node->getOperand(0));
1122 Tmp2 = LegalizeOp(Node->getOperand(1));
1123 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1124 assert(idx && "Operand must be a constant");
1125 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1127 }
1128 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001129 case ISD::BUILD_VECTOR:
1130 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001131 default: assert(0 && "This action is not supported yet!");
1132 case TargetLowering::Custom:
1133 Tmp3 = TLI.LowerOperation(Result, DAG);
1134 if (Tmp3.Val) {
1135 Result = Tmp3;
1136 break;
1137 }
1138 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001139 case TargetLowering::Expand:
1140 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001141 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001142 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001143 break;
1144 case ISD::INSERT_VECTOR_ELT:
1145 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1146 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1147 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1149
1150 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1151 Node->getValueType(0))) {
1152 default: assert(0 && "This action is not supported yet!");
1153 case TargetLowering::Legal:
1154 break;
1155 case TargetLowering::Custom:
1156 Tmp3 = TLI.LowerOperation(Result, DAG);
1157 if (Tmp3.Val) {
1158 Result = Tmp3;
1159 break;
1160 }
1161 // FALLTHROUGH
1162 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001163 // If the insert index is a constant, codegen this as a scalar_to_vector,
1164 // then a shuffle that inserts it into the right position in the vector.
1165 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1166 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1167 Tmp1.getValueType(), Tmp2);
1168
1169 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1170 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001171 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001172
1173 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1174 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1175 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001176 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001177 for (unsigned i = 0; i != NumElts; ++i) {
1178 if (i != InsertPos->getValue())
1179 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1180 else
1181 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1182 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001183 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1184 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001185
1186 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1187 Tmp1, ScVec, ShufMask);
1188 Result = LegalizeOp(Result);
1189 break;
1190 }
1191
Chris Lattner2332b9f2006-03-19 01:17:20 +00001192 // If the target doesn't support this, we have to spill the input vector
1193 // to a temporary stack slot, update the element, then reload it. This is
1194 // badness. We could also load the value into a vector register (either
1195 // with a "move to register" or "extload into register" instruction, then
1196 // permute it into place, if the idx is a constant and if the idx is
1197 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001198 MVT::ValueType VT = Tmp1.getValueType();
1199 MVT::ValueType EltVT = Tmp2.getValueType();
1200 MVT::ValueType IdxVT = Tmp3.getValueType();
1201 MVT::ValueType PtrVT = TLI.getPointerTy();
1202 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001203 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001204 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001205
1206 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001207 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1208 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001209 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001210 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1211 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1212 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001213 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001214 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001215 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001216 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001217 break;
1218 }
1219 }
1220 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001221 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001222 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1223 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1224 break;
1225 }
1226
Chris Lattnerce872152006-03-19 06:31:19 +00001227 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1228 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1229 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1230 Node->getValueType(0))) {
1231 default: assert(0 && "This action is not supported yet!");
1232 case TargetLowering::Legal:
1233 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001234 case TargetLowering::Custom:
1235 Tmp3 = TLI.LowerOperation(Result, DAG);
1236 if (Tmp3.Val) {
1237 Result = Tmp3;
1238 break;
1239 }
1240 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001241 case TargetLowering::Expand:
1242 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001243 break;
1244 }
Chris Lattnerce872152006-03-19 06:31:19 +00001245 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001246 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001247 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1248 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1250
1251 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001252 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1253 default: assert(0 && "Unknown operation action!");
1254 case TargetLowering::Legal:
1255 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1256 "vector shuffle should not be created if not legal!");
1257 break;
1258 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001259 Tmp3 = TLI.LowerOperation(Result, DAG);
1260 if (Tmp3.Val) {
1261 Result = Tmp3;
1262 break;
1263 }
1264 // FALLTHROUGH
1265 case TargetLowering::Expand: {
1266 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001267 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001268 MVT::ValueType PtrVT = TLI.getPointerTy();
1269 SDOperand Mask = Node->getOperand(2);
1270 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001271 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001272 for (unsigned i = 0; i != NumElems; ++i) {
1273 SDOperand Arg = Mask.getOperand(i);
1274 if (Arg.getOpcode() == ISD::UNDEF) {
1275 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1276 } else {
1277 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1278 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1279 if (Idx < NumElems)
1280 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1281 DAG.getConstant(Idx, PtrVT)));
1282 else
1283 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1284 DAG.getConstant(Idx - NumElems, PtrVT)));
1285 }
1286 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001287 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001288 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001289 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001290 case TargetLowering::Promote: {
1291 // Change base type to a different vector type.
1292 MVT::ValueType OVT = Node->getValueType(0);
1293 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1294
1295 // Cast the two input vectors.
1296 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1297 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1298
1299 // Convert the shuffle mask to the right # elements.
1300 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1301 assert(Tmp3.Val && "Shuffle not legal?");
1302 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1303 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1304 break;
1305 }
Chris Lattner87100e02006-03-20 01:52:29 +00001306 }
1307 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001308
1309 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001310 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001311 Tmp2 = LegalizeOp(Node->getOperand(1));
1312 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001313 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001314 break;
1315
Dan Gohman7f321562007-06-25 16:23:39 +00001316 case ISD::EXTRACT_SUBVECTOR:
1317 Tmp1 = Node->getOperand(0);
1318 Tmp2 = LegalizeOp(Node->getOperand(1));
1319 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1320 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001321 break;
1322
Chris Lattner6831a812006-02-13 09:18:02 +00001323 case ISD::CALLSEQ_START: {
1324 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1325
1326 // Recursively Legalize all of the inputs of the call end that do not lead
1327 // to this call start. This ensures that any libcalls that need be inserted
1328 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001329 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001330 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001331 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1332 NodesLeadingTo);
1333 }
Chris Lattner6831a812006-02-13 09:18:02 +00001334
1335 // Now that we legalized all of the inputs (which may have inserted
1336 // libcalls) create the new CALLSEQ_START node.
1337 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1338
1339 // Merge in the last call, to ensure that this call start after the last
1340 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001341 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001342 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1343 Tmp1 = LegalizeOp(Tmp1);
1344 }
Chris Lattner6831a812006-02-13 09:18:02 +00001345
1346 // Do not try to legalize the target-specific arguments (#1+).
1347 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001348 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001349 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001350 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001351 }
1352
1353 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001354 AddLegalizedOperand(Op.getValue(0), Result);
1355 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1356 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1357
Chris Lattner6831a812006-02-13 09:18:02 +00001358 // Now that the callseq_start and all of the non-call nodes above this call
1359 // sequence have been legalized, legalize the call itself. During this
1360 // process, no libcalls can/will be inserted, guaranteeing that no calls
1361 // can overlap.
1362 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1363 SDOperand InCallSEQ = LastCALLSEQ_END;
1364 // Note that we are selecting this call!
1365 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1366 IsLegalizingCall = true;
1367
1368 // Legalize the call, starting from the CALLSEQ_END.
1369 LegalizeOp(LastCALLSEQ_END);
1370 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1371 return Result;
1372 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001373 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001374 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1375 // will cause this node to be legalized as well as handling libcalls right.
1376 if (LastCALLSEQ_END.Val != Node) {
1377 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001378 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001379 assert(I != LegalizedNodes.end() &&
1380 "Legalizing the call start should have legalized this node!");
1381 return I->second;
1382 }
1383
1384 // Otherwise, the call start has been legalized and everything is going
1385 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001386 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001387 // Do not try to legalize the target-specific arguments (#1+), except for
1388 // an optional flag input.
1389 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1390 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001391 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001392 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001393 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001394 }
1395 } else {
1396 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1397 if (Tmp1 != Node->getOperand(0) ||
1398 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001399 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001400 Ops[0] = Tmp1;
1401 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001402 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001403 }
Chris Lattner6a542892006-01-24 05:48:21 +00001404 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001405 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001406 // This finishes up call legalization.
1407 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001408
1409 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1410 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1411 if (Node->getNumValues() == 2)
1412 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1413 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001414 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001415 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001416 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1417 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1418 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001419 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001420
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001421 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001422 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001423 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001424 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001425 case TargetLowering::Expand: {
1426 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1427 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1428 " not tell us which reg is the stack pointer!");
1429 SDOperand Chain = Tmp1.getOperand(0);
1430 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001431 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1432 Chain = SP.getValue(1);
1433 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1434 unsigned StackAlign =
1435 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1436 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001437 SP = DAG.getNode(ISD::AND, VT, SP,
1438 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001439 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1440 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001441 Tmp1 = LegalizeOp(Tmp1);
1442 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001443 break;
1444 }
1445 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001446 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1447 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001448 Tmp1 = LegalizeOp(Tmp3);
1449 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001450 }
Chris Lattner903d2782006-01-15 08:54:32 +00001451 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001452 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001453 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001454 }
Chris Lattner903d2782006-01-15 08:54:32 +00001455 // Since this op produce two values, make sure to remember that we
1456 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001457 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1458 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001459 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001460 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001461 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001462 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001463 bool Changed = false;
1464 // Legalize all of the operands of the inline asm, in case they are nodes
1465 // that need to be expanded or something. Note we skip the asm string and
1466 // all of the TargetConstant flags.
1467 SDOperand Op = LegalizeOp(Ops[0]);
1468 Changed = Op != Ops[0];
1469 Ops[0] = Op;
1470
1471 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1472 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1473 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1474 for (++i; NumVals; ++i, --NumVals) {
1475 SDOperand Op = LegalizeOp(Ops[i]);
1476 if (Op != Ops[i]) {
1477 Changed = true;
1478 Ops[i] = Op;
1479 }
1480 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001481 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001482
1483 if (HasInFlag) {
1484 Op = LegalizeOp(Ops.back());
1485 Changed |= Op != Ops.back();
1486 Ops.back() = Op;
1487 }
1488
1489 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001490 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001491
1492 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001493 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001494 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1495 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001496 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001497 case ISD::BR:
1498 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001499 // Ensure that libcalls are emitted before a branch.
1500 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1501 Tmp1 = LegalizeOp(Tmp1);
1502 LastCALLSEQ_END = DAG.getEntryNode();
1503
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001504 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001505 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001506 case ISD::BRIND:
1507 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1508 // Ensure that libcalls are emitted before a branch.
1509 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1510 Tmp1 = LegalizeOp(Tmp1);
1511 LastCALLSEQ_END = DAG.getEntryNode();
1512
1513 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1514 default: assert(0 && "Indirect target must be legal type (pointer)!");
1515 case Legal:
1516 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1517 break;
1518 }
1519 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1520 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001521 case ISD::BR_JT:
1522 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1523 // Ensure that libcalls are emitted before a branch.
1524 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1525 Tmp1 = LegalizeOp(Tmp1);
1526 LastCALLSEQ_END = DAG.getEntryNode();
1527
1528 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1529 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1530
1531 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1532 default: assert(0 && "This action is not supported yet!");
1533 case TargetLowering::Legal: break;
1534 case TargetLowering::Custom:
1535 Tmp1 = TLI.LowerOperation(Result, DAG);
1536 if (Tmp1.Val) Result = Tmp1;
1537 break;
1538 case TargetLowering::Expand: {
1539 SDOperand Chain = Result.getOperand(0);
1540 SDOperand Table = Result.getOperand(1);
1541 SDOperand Index = Result.getOperand(2);
1542
1543 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001544 MachineFunction &MF = DAG.getMachineFunction();
1545 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001546 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1547 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001548
1549 SDOperand LD;
1550 switch (EntrySize) {
1551 default: assert(0 && "Size of jump table not supported yet."); break;
1552 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1553 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1554 }
1555
1556 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001557 // For PIC, the sequence is:
1558 // BRIND(load(Jumptable + index) + RelocBase)
1559 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1560 SDOperand Reloc;
1561 if (TLI.usesGlobalOffsetTable())
1562 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1563 else
1564 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001565 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001566 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1567 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1568 } else {
1569 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1570 }
1571 }
1572 }
1573 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001574 case ISD::BRCOND:
1575 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001576 // Ensure that libcalls are emitted before a return.
1577 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1578 Tmp1 = LegalizeOp(Tmp1);
1579 LastCALLSEQ_END = DAG.getEntryNode();
1580
Chris Lattner47e92232005-01-18 19:27:06 +00001581 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1582 case Expand: assert(0 && "It's impossible to expand bools");
1583 case Legal:
1584 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1585 break;
1586 case Promote:
1587 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001588
1589 // The top bits of the promoted condition are not necessarily zero, ensure
1590 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001591 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001592 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1593 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001594 break;
1595 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001596
1597 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001598 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001599
1600 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1601 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001602 case TargetLowering::Legal: break;
1603 case TargetLowering::Custom:
1604 Tmp1 = TLI.LowerOperation(Result, DAG);
1605 if (Tmp1.Val) Result = Tmp1;
1606 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001607 case TargetLowering::Expand:
1608 // Expand brcond's setcc into its constituent parts and create a BR_CC
1609 // Node.
1610 if (Tmp2.getOpcode() == ISD::SETCC) {
1611 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1612 Tmp2.getOperand(0), Tmp2.getOperand(1),
1613 Node->getOperand(2));
1614 } else {
1615 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1616 DAG.getCondCode(ISD::SETNE), Tmp2,
1617 DAG.getConstant(0, Tmp2.getValueType()),
1618 Node->getOperand(2));
1619 }
1620 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001621 }
1622 break;
1623 case ISD::BR_CC:
1624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001625 // Ensure that libcalls are emitted before a branch.
1626 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1627 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001628 Tmp2 = Node->getOperand(2); // LHS
1629 Tmp3 = Node->getOperand(3); // RHS
1630 Tmp4 = Node->getOperand(1); // CC
1631
1632 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001633 LastCALLSEQ_END = DAG.getEntryNode();
1634
Nate Begeman750ac1b2006-02-01 07:19:44 +00001635 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1636 // the LHS is a legal SETCC itself. In this case, we need to compare
1637 // the result against zero to select between true and false values.
1638 if (Tmp3.Val == 0) {
1639 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1640 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001641 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001642
1643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1644 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001645
Chris Lattner181b7a32005-12-17 23:46:46 +00001646 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1647 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001648 case TargetLowering::Legal: break;
1649 case TargetLowering::Custom:
1650 Tmp4 = TLI.LowerOperation(Result, DAG);
1651 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001652 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001653 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001654 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001655 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001656 LoadSDNode *LD = cast<LoadSDNode>(Node);
1657 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1658 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001659
Evan Cheng466685d2006-10-09 20:57:25 +00001660 ISD::LoadExtType ExtType = LD->getExtensionType();
1661 if (ExtType == ISD::NON_EXTLOAD) {
1662 MVT::ValueType VT = Node->getValueType(0);
1663 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1664 Tmp3 = Result.getValue(0);
1665 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001666
Evan Cheng466685d2006-10-09 20:57:25 +00001667 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1668 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001669 case TargetLowering::Legal:
1670 // If this is an unaligned load and the target doesn't support it,
1671 // expand it.
1672 if (!TLI.allowsUnalignedMemoryAccesses()) {
1673 unsigned ABIAlignment = TLI.getTargetData()->
1674 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1675 if (LD->getAlignment() < ABIAlignment){
1676 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1677 TLI);
1678 Tmp3 = Result.getOperand(0);
1679 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001680 Tmp3 = LegalizeOp(Tmp3);
1681 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001682 }
1683 }
1684 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001685 case TargetLowering::Custom:
1686 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1687 if (Tmp1.Val) {
1688 Tmp3 = LegalizeOp(Tmp1);
1689 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001690 }
Evan Cheng466685d2006-10-09 20:57:25 +00001691 break;
1692 case TargetLowering::Promote: {
1693 // Only promote a load of vector type to another.
1694 assert(MVT::isVector(VT) && "Cannot promote this load!");
1695 // Change base type to a different vector type.
1696 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1697
1698 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001699 LD->getSrcValueOffset(),
1700 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001701 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1702 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001703 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001704 }
Evan Cheng466685d2006-10-09 20:57:25 +00001705 }
1706 // Since loads produce two values, make sure to remember that we
1707 // legalized both of them.
1708 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1709 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1710 return Op.ResNo ? Tmp4 : Tmp3;
1711 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001712 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001713 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1714 default: assert(0 && "This action is not supported yet!");
1715 case TargetLowering::Promote:
1716 assert(SrcVT == MVT::i1 &&
1717 "Can only promote extending LOAD from i1 -> i8!");
1718 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1719 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001720 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001721 Tmp1 = Result.getValue(0);
1722 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001723 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001724 case TargetLowering::Custom:
1725 isCustom = true;
1726 // FALLTHROUGH
1727 case TargetLowering::Legal:
1728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1729 Tmp1 = Result.getValue(0);
1730 Tmp2 = Result.getValue(1);
1731
1732 if (isCustom) {
1733 Tmp3 = TLI.LowerOperation(Result, DAG);
1734 if (Tmp3.Val) {
1735 Tmp1 = LegalizeOp(Tmp3);
1736 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1737 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001738 } else {
1739 // If this is an unaligned load and the target doesn't support it,
1740 // expand it.
1741 if (!TLI.allowsUnalignedMemoryAccesses()) {
1742 unsigned ABIAlignment = TLI.getTargetData()->
1743 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1744 if (LD->getAlignment() < ABIAlignment){
1745 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1746 TLI);
1747 Tmp1 = Result.getOperand(0);
1748 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001749 Tmp1 = LegalizeOp(Tmp1);
1750 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001751 }
1752 }
Evan Cheng466685d2006-10-09 20:57:25 +00001753 }
1754 break;
1755 case TargetLowering::Expand:
1756 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1757 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1758 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001759 LD->getSrcValueOffset(),
1760 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001761 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1762 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1763 Tmp2 = LegalizeOp(Load.getValue(1));
1764 break;
1765 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001766 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001767 // Turn the unsupported load into an EXTLOAD followed by an explicit
1768 // zero/sign extend inreg.
1769 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1770 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001771 LD->getSrcValueOffset(), SrcVT,
1772 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001773 SDOperand ValRes;
1774 if (ExtType == ISD::SEXTLOAD)
1775 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1776 Result, DAG.getValueType(SrcVT));
1777 else
1778 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1779 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1780 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1781 break;
1782 }
1783 // Since loads produce two values, make sure to remember that we legalized
1784 // both of them.
1785 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1786 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1787 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001788 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001789 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001790 case ISD::EXTRACT_ELEMENT: {
1791 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1792 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001793 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001794 case Legal:
1795 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1796 // 1 -> Hi
1797 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1798 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1799 TLI.getShiftAmountTy()));
1800 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1801 } else {
1802 // 0 -> Lo
1803 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1804 Node->getOperand(0));
1805 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001806 break;
1807 case Expand:
1808 // Get both the low and high parts.
1809 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1810 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1811 Result = Tmp2; // 1 -> Hi
1812 else
1813 Result = Tmp1; // 0 -> Lo
1814 break;
1815 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001816 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001817 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001818
1819 case ISD::CopyToReg:
1820 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001821
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001822 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001823 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001824 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001825 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001826 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001827 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001828 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001829 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001830 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001831 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001832 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1833 Tmp3);
1834 } else {
1835 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001836 }
1837
1838 // Since this produces two values, make sure to remember that we legalized
1839 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001840 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001841 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001842 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001843 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001844 break;
1845
1846 case ISD::RET:
1847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001848
1849 // Ensure that libcalls are emitted before a return.
1850 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1851 Tmp1 = LegalizeOp(Tmp1);
1852 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001853
Chris Lattner3e928bb2005-01-07 07:47:09 +00001854 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001855 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001856 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001857 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001858 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001859 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001860 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001861 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001862 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001863 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001864 SDOperand Lo, Hi;
1865 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001866
1867 // Big endian systems want the hi reg first.
1868 if (!TLI.isLittleEndian())
1869 std::swap(Lo, Hi);
1870
Evan Cheng13acce32006-12-11 19:27:14 +00001871 if (Hi.Val)
1872 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1873 else
1874 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001875 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001876 } else {
1877 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001878 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1879 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001880
Dan Gohman7f321562007-06-25 16:23:39 +00001881 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001882 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001883 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001884 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001885 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001886 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001887 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001888 } else if (NumElems == 1) {
1889 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001890 Tmp2 = ScalarizeVectorOp(Tmp2);
1891 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001892 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001893
1894 // FIXME: Returns of gcc generic vectors smaller than a legal type
1895 // should be returned in integer registers!
1896
Chris Lattnerf87324e2006-04-11 01:31:51 +00001897 // The scalarized value type may not be legal, e.g. it might require
1898 // promotion or expansion. Relegalize the return.
1899 Result = LegalizeOp(Result);
1900 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001901 // FIXME: Returns of gcc generic vectors larger than a legal vector
1902 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001903 SDOperand Lo, Hi;
1904 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001905 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001906 Result = LegalizeOp(Result);
1907 }
1908 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001909 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001910 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001911 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001912 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001913 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001914 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001915 }
1916 break;
1917 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001918 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001919 break;
1920 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001921 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001922 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001923 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001924 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1925 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001926 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001927 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001928 break;
1929 case Expand: {
1930 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001931 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001932 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001933 ExpandOp(Node->getOperand(i), Lo, Hi);
1934 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001935 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001936 if (Hi.Val) {
1937 NewValues.push_back(Hi);
1938 NewValues.push_back(Node->getOperand(i+1));
1939 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001940 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001941 }
1942 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001943 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001944 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001945
1946 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001947 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001948 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001949 Result = DAG.getNode(ISD::RET, MVT::Other,
1950 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001951 break;
1952 }
1953 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001954
Chris Lattner6862dbc2006-01-29 21:02:23 +00001955 if (Result.getOpcode() == ISD::RET) {
1956 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1957 default: assert(0 && "This action is not supported yet!");
1958 case TargetLowering::Legal: break;
1959 case TargetLowering::Custom:
1960 Tmp1 = TLI.LowerOperation(Result, DAG);
1961 if (Tmp1.Val) Result = Tmp1;
1962 break;
1963 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001964 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001965 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001966 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001967 StoreSDNode *ST = cast<StoreSDNode>(Node);
1968 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1969 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001970 int SVOffset = ST->getSrcValueOffset();
1971 unsigned Alignment = ST->getAlignment();
1972 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001973
Evan Cheng8b2794a2006-10-13 21:14:26 +00001974 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001975 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1976 // FIXME: We shouldn't do this for TargetConstantFP's.
1977 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1978 // to phase ordering between legalized code and the dag combiner. This
1979 // probably means that we need to integrate dag combiner and legalizer
1980 // together.
1981 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1982 if (CFP->getValueType(0) == MVT::f32) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001983 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValueAPF().
1984 convertToFloat()), MVT::i32);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001985 } else {
1986 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001987 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValueAPF().
1988 convertToDouble()), MVT::i64);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001989 }
1990 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001991 SVOffset, isVolatile, Alignment);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001992 break;
1993 }
1994
Evan Cheng8b2794a2006-10-13 21:14:26 +00001995 switch (getTypeAction(ST->getStoredVT())) {
1996 case Legal: {
1997 Tmp3 = LegalizeOp(ST->getValue());
1998 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1999 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002000
Evan Cheng8b2794a2006-10-13 21:14:26 +00002001 MVT::ValueType VT = Tmp3.getValueType();
2002 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2003 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002004 case TargetLowering::Legal:
2005 // If this is an unaligned store and the target doesn't support it,
2006 // expand it.
2007 if (!TLI.allowsUnalignedMemoryAccesses()) {
2008 unsigned ABIAlignment = TLI.getTargetData()->
2009 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2010 if (ST->getAlignment() < ABIAlignment)
2011 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2012 TLI);
2013 }
2014 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002015 case TargetLowering::Custom:
2016 Tmp1 = TLI.LowerOperation(Result, DAG);
2017 if (Tmp1.Val) Result = Tmp1;
2018 break;
2019 case TargetLowering::Promote:
2020 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2021 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2022 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2023 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002024 ST->getSrcValue(), SVOffset, isVolatile,
2025 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002026 break;
2027 }
2028 break;
2029 }
2030 case Promote:
2031 // Truncate the value and store the result.
2032 Tmp3 = PromoteOp(ST->getValue());
2033 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002034 SVOffset, ST->getStoredVT(),
2035 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002036 break;
2037
2038 case Expand:
2039 unsigned IncrementSize = 0;
2040 SDOperand Lo, Hi;
2041
2042 // If this is a vector type, then we have to calculate the increment as
2043 // the product of the element size in bytes, and the number of elements
2044 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002045 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002046 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00002047 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2048 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002049
Dan Gohman7f321562007-06-25 16:23:39 +00002050 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002051 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002052 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002053 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002054 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002055 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002056 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002057 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002058 Result = LegalizeOp(Result);
2059 break;
2060 } else if (NumElems == 1) {
2061 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002062 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002063 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002064 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002065 // The scalarized value type may not be legal, e.g. it might require
2066 // promotion or expansion. Relegalize the scalar store.
2067 Result = LegalizeOp(Result);
2068 break;
2069 } else {
2070 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2071 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2072 }
2073 } else {
2074 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002075 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002076
2077 if (!TLI.isLittleEndian())
2078 std::swap(Lo, Hi);
2079 }
2080
2081 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002082 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002083
2084 if (Hi.Val == NULL) {
2085 // Must be int <-> float one-to-one expansion.
2086 Result = Lo;
2087 break;
2088 }
2089
Evan Cheng8b2794a2006-10-13 21:14:26 +00002090 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2091 getIntPtrConstant(IncrementSize));
2092 assert(isTypeLegal(Tmp2.getValueType()) &&
2093 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002094 SVOffset += IncrementSize;
2095 if (Alignment > IncrementSize)
2096 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002097 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002098 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002099 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2100 break;
2101 }
2102 } else {
2103 // Truncating store
2104 assert(isTypeLegal(ST->getValue().getValueType()) &&
2105 "Cannot handle illegal TRUNCSTORE yet!");
2106 Tmp3 = LegalizeOp(ST->getValue());
2107
2108 // The only promote case we handle is TRUNCSTORE:i1 X into
2109 // -> TRUNCSTORE:i8 (and X, 1)
2110 if (ST->getStoredVT() == MVT::i1 &&
2111 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2112 // Promote the bool to a mask then store.
2113 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2114 DAG.getConstant(1, Tmp3.getValueType()));
2115 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002116 SVOffset, MVT::i8,
2117 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002118 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2119 Tmp2 != ST->getBasePtr()) {
2120 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2121 ST->getOffset());
2122 }
2123
2124 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2125 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002126 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002127 case TargetLowering::Legal:
2128 // If this is an unaligned store and the target doesn't support it,
2129 // expand it.
2130 if (!TLI.allowsUnalignedMemoryAccesses()) {
2131 unsigned ABIAlignment = TLI.getTargetData()->
2132 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2133 if (ST->getAlignment() < ABIAlignment)
2134 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2135 TLI);
2136 }
2137 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002138 case TargetLowering::Custom:
2139 Tmp1 = TLI.LowerOperation(Result, DAG);
2140 if (Tmp1.Val) Result = Tmp1;
2141 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002142 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002143 }
2144 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002145 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002146 case ISD::PCMARKER:
2147 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002149 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002150 case ISD::STACKSAVE:
2151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002152 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2153 Tmp1 = Result.getValue(0);
2154 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002155
Chris Lattner140d53c2006-01-13 02:50:02 +00002156 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2157 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002158 case TargetLowering::Legal: break;
2159 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002160 Tmp3 = TLI.LowerOperation(Result, DAG);
2161 if (Tmp3.Val) {
2162 Tmp1 = LegalizeOp(Tmp3);
2163 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002164 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002165 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002166 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002167 // Expand to CopyFromReg if the target set
2168 // StackPointerRegisterToSaveRestore.
2169 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002170 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002171 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002172 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002173 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002174 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2175 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002176 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002177 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002178 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002179
2180 // Since stacksave produce two values, make sure to remember that we
2181 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002182 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2183 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2184 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002185
Chris Lattner140d53c2006-01-13 02:50:02 +00002186 case ISD::STACKRESTORE:
2187 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2188 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002190
2191 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2192 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002193 case TargetLowering::Legal: break;
2194 case TargetLowering::Custom:
2195 Tmp1 = TLI.LowerOperation(Result, DAG);
2196 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002197 break;
2198 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002199 // Expand to CopyToReg if the target set
2200 // StackPointerRegisterToSaveRestore.
2201 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2202 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2203 } else {
2204 Result = Tmp1;
2205 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002206 break;
2207 }
2208 break;
2209
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002210 case ISD::READCYCLECOUNTER:
2211 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002212 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002213 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2214 Node->getValueType(0))) {
2215 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002216 case TargetLowering::Legal:
2217 Tmp1 = Result.getValue(0);
2218 Tmp2 = Result.getValue(1);
2219 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002220 case TargetLowering::Custom:
2221 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002222 Tmp1 = LegalizeOp(Result.getValue(0));
2223 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002224 break;
2225 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002226
2227 // Since rdcc produce two values, make sure to remember that we legalized
2228 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002229 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2230 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002231 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002232
Chris Lattner2ee743f2005-01-14 22:08:15 +00002233 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002234 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2235 case Expand: assert(0 && "It's impossible to expand bools");
2236 case Legal:
2237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2238 break;
2239 case Promote:
2240 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002241 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002242 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002243 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2244 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002245 break;
2246 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002247 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002248 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002249
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002250 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002251
Nate Begemanb942a3d2005-08-23 04:29:48 +00002252 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002253 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002254 case TargetLowering::Legal: break;
2255 case TargetLowering::Custom: {
2256 Tmp1 = TLI.LowerOperation(Result, DAG);
2257 if (Tmp1.Val) Result = Tmp1;
2258 break;
2259 }
Nate Begeman9373a812005-08-10 20:51:12 +00002260 case TargetLowering::Expand:
2261 if (Tmp1.getOpcode() == ISD::SETCC) {
2262 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2263 Tmp2, Tmp3,
2264 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2265 } else {
2266 Result = DAG.getSelectCC(Tmp1,
2267 DAG.getConstant(0, Tmp1.getValueType()),
2268 Tmp2, Tmp3, ISD::SETNE);
2269 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002270 break;
2271 case TargetLowering::Promote: {
2272 MVT::ValueType NVT =
2273 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2274 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002275 if (MVT::isVector(Tmp2.getValueType())) {
2276 ExtOp = ISD::BIT_CONVERT;
2277 TruncOp = ISD::BIT_CONVERT;
2278 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002279 ExtOp = ISD::ANY_EXTEND;
2280 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002281 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002282 ExtOp = ISD::FP_EXTEND;
2283 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002284 }
2285 // Promote each of the values to the new type.
2286 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2287 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2288 // Perform the larger operation, then round down.
2289 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2290 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2291 break;
2292 }
2293 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002294 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002295 case ISD::SELECT_CC: {
2296 Tmp1 = Node->getOperand(0); // LHS
2297 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002298 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2299 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002300 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002301
Nate Begeman750ac1b2006-02-01 07:19:44 +00002302 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2303
2304 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2305 // the LHS is a legal SETCC itself. In this case, we need to compare
2306 // the result against zero to select between true and false values.
2307 if (Tmp2.Val == 0) {
2308 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2309 CC = DAG.getCondCode(ISD::SETNE);
2310 }
2311 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2312
2313 // Everything is legal, see if we should expand this op or something.
2314 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2315 default: assert(0 && "This action is not supported yet!");
2316 case TargetLowering::Legal: break;
2317 case TargetLowering::Custom:
2318 Tmp1 = TLI.LowerOperation(Result, DAG);
2319 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002320 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002321 }
2322 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002323 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002324 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002325 Tmp1 = Node->getOperand(0);
2326 Tmp2 = Node->getOperand(1);
2327 Tmp3 = Node->getOperand(2);
2328 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2329
2330 // If we had to Expand the SetCC operands into a SELECT node, then it may
2331 // not always be possible to return a true LHS & RHS. In this case, just
2332 // return the value we legalized, returned in the LHS
2333 if (Tmp2.Val == 0) {
2334 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002335 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002336 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002337
Chris Lattner73e142f2006-01-30 22:43:50 +00002338 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002339 default: assert(0 && "Cannot handle this action for SETCC yet!");
2340 case TargetLowering::Custom:
2341 isCustom = true;
2342 // FALLTHROUGH.
2343 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002345 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002346 Tmp4 = TLI.LowerOperation(Result, DAG);
2347 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002348 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002349 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002350 case TargetLowering::Promote: {
2351 // First step, figure out the appropriate operation to use.
2352 // Allow SETCC to not be supported for all legal data types
2353 // Mostly this targets FP
2354 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002355 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002356
2357 // Scan for the appropriate larger type to use.
2358 while (1) {
2359 NewInTy = (MVT::ValueType)(NewInTy+1);
2360
2361 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2362 "Fell off of the edge of the integer world");
2363 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2364 "Fell off of the edge of the floating point world");
2365
2366 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002367 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002368 break;
2369 }
2370 if (MVT::isInteger(NewInTy))
2371 assert(0 && "Cannot promote Legal Integer SETCC yet");
2372 else {
2373 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2374 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2375 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002376 Tmp1 = LegalizeOp(Tmp1);
2377 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002378 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002379 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002380 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002381 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002382 case TargetLowering::Expand:
2383 // Expand a setcc node into a select_cc of the same condition, lhs, and
2384 // rhs that selects between const 1 (true) and const 0 (false).
2385 MVT::ValueType VT = Node->getValueType(0);
2386 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2387 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002388 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002389 break;
2390 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002391 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002392 case ISD::MEMSET:
2393 case ISD::MEMCPY:
2394 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002396 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2397
2398 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2399 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2400 case Expand: assert(0 && "Cannot expand a byte!");
2401 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002402 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002403 break;
2404 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002405 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002406 break;
2407 }
2408 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002409 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002410 }
Chris Lattner272455b2005-02-02 03:44:41 +00002411
2412 SDOperand Tmp4;
2413 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002414 case Expand: {
2415 // Length is too big, just take the lo-part of the length.
2416 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002417 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002418 break;
2419 }
Chris Lattnere5605212005-01-28 22:29:18 +00002420 case Legal:
2421 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002422 break;
2423 case Promote:
2424 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002425 break;
2426 }
2427
2428 SDOperand Tmp5;
2429 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2430 case Expand: assert(0 && "Cannot expand this yet!");
2431 case Legal:
2432 Tmp5 = LegalizeOp(Node->getOperand(4));
2433 break;
2434 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002435 Tmp5 = PromoteOp(Node->getOperand(4));
2436 break;
2437 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002438
2439 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2440 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002441 case TargetLowering::Custom:
2442 isCustom = true;
2443 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002444 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002445 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002446 if (isCustom) {
2447 Tmp1 = TLI.LowerOperation(Result, DAG);
2448 if (Tmp1.Val) Result = Tmp1;
2449 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002450 break;
2451 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002452 // Otherwise, the target does not support this operation. Lower the
2453 // operation to an explicit libcall as appropriate.
2454 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002455 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002456 TargetLowering::ArgListTy Args;
2457 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002458
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002459 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002460 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002461 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002462 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002463 // Extend the (previously legalized) ubyte argument to be an int value
2464 // for the call.
2465 if (Tmp3.getValueType() > MVT::i32)
2466 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2467 else
2468 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002469 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002470 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002471 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002472 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002473
2474 FnName = "memset";
2475 } else if (Node->getOpcode() == ISD::MEMCPY ||
2476 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002477 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002478 Entry.Node = Tmp2; Args.push_back(Entry);
2479 Entry.Node = Tmp3; Args.push_back(Entry);
2480 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002481 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2482 } else {
2483 assert(0 && "Unknown op!");
2484 }
Chris Lattner45982da2005-05-12 16:53:42 +00002485
Chris Lattnere1bd8222005-01-11 05:57:22 +00002486 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002487 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002488 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002489 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002490 break;
2491 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002492 }
2493 break;
2494 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002495
Chris Lattner5b359c62005-04-02 04:00:59 +00002496 case ISD::SHL_PARTS:
2497 case ISD::SRA_PARTS:
2498 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002499 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002500 bool Changed = false;
2501 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2502 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2503 Changed |= Ops.back() != Node->getOperand(i);
2504 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002505 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002506 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002507
Evan Cheng05a2d562006-01-09 18:31:59 +00002508 switch (TLI.getOperationAction(Node->getOpcode(),
2509 Node->getValueType(0))) {
2510 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002511 case TargetLowering::Legal: break;
2512 case TargetLowering::Custom:
2513 Tmp1 = TLI.LowerOperation(Result, DAG);
2514 if (Tmp1.Val) {
2515 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002516 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002517 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002518 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2519 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002520 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002521 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002522 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002523 return RetVal;
2524 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002525 break;
2526 }
2527
Chris Lattner2c8086f2005-04-02 05:00:07 +00002528 // Since these produce multiple values, make sure to remember that we
2529 // legalized all of them.
2530 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2531 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2532 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002533 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002534
2535 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002536 case ISD::ADD:
2537 case ISD::SUB:
2538 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002539 case ISD::MULHS:
2540 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002541 case ISD::UDIV:
2542 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002543 case ISD::AND:
2544 case ISD::OR:
2545 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002546 case ISD::SHL:
2547 case ISD::SRL:
2548 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002549 case ISD::FADD:
2550 case ISD::FSUB:
2551 case ISD::FMUL:
2552 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002553 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002554 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2555 case Expand: assert(0 && "Not possible");
2556 case Legal:
2557 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2558 break;
2559 case Promote:
2560 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2561 break;
2562 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002563
2564 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002565
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002566 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002567 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002568 case TargetLowering::Legal: break;
2569 case TargetLowering::Custom:
2570 Tmp1 = TLI.LowerOperation(Result, DAG);
2571 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002572 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002573 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002574 if (Node->getValueType(0) == MVT::i32) {
2575 switch (Node->getOpcode()) {
2576 default: assert(0 && "Do not know how to expand this integer BinOp!");
2577 case ISD::UDIV:
2578 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002579 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2580 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002581 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002582 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002583 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002584 };
2585 break;
2586 }
2587
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002588 assert(MVT::isVector(Node->getValueType(0)) &&
2589 "Cannot expand this binary operator!");
2590 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002591 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002592 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002593 MVT::ValueType PtrVT = TLI.getPointerTy();
2594 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2595 i != e; ++i) {
2596 SDOperand Idx = DAG.getConstant(i, PtrVT);
2597 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2598 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2599 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2600 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002601 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2602 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002603 break;
2604 }
Evan Chengcc987612006-04-12 21:20:24 +00002605 case TargetLowering::Promote: {
2606 switch (Node->getOpcode()) {
2607 default: assert(0 && "Do not know how to promote this BinOp!");
2608 case ISD::AND:
2609 case ISD::OR:
2610 case ISD::XOR: {
2611 MVT::ValueType OVT = Node->getValueType(0);
2612 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2613 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2614 // Bit convert each of the values to the new type.
2615 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2616 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2617 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2618 // Bit convert the result back the original type.
2619 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2620 break;
2621 }
2622 }
2623 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002624 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002625 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002626
2627 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2628 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2629 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2630 case Expand: assert(0 && "Not possible");
2631 case Legal:
2632 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2633 break;
2634 case Promote:
2635 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2636 break;
2637 }
2638
2639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2640
2641 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2642 default: assert(0 && "Operation not supported");
2643 case TargetLowering::Custom:
2644 Tmp1 = TLI.LowerOperation(Result, DAG);
2645 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002646 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002647 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002648 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002649 // If this target supports fabs/fneg natively and select is cheap,
2650 // do this efficiently.
2651 if (!TLI.isSelectExpensive() &&
2652 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2653 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002654 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002655 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002656 // Get the sign bit of the RHS.
2657 MVT::ValueType IVT =
2658 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2659 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2660 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2661 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2662 // Get the absolute value of the result.
2663 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2664 // Select between the nabs and abs value based on the sign bit of
2665 // the input.
2666 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2667 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2668 AbsVal),
2669 AbsVal);
2670 Result = LegalizeOp(Result);
2671 break;
2672 }
2673
2674 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002675 MVT::ValueType NVT =
2676 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2677 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2678 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2679 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002680 break;
2681 }
Evan Cheng912095b2007-01-04 21:56:39 +00002682 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002683 break;
2684
Nate Begeman551bf3f2006-02-17 05:43:56 +00002685 case ISD::ADDC:
2686 case ISD::SUBC:
2687 Tmp1 = LegalizeOp(Node->getOperand(0));
2688 Tmp2 = LegalizeOp(Node->getOperand(1));
2689 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2690 // Since this produces two values, make sure to remember that we legalized
2691 // both of them.
2692 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2693 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2694 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002695
Nate Begeman551bf3f2006-02-17 05:43:56 +00002696 case ISD::ADDE:
2697 case ISD::SUBE:
2698 Tmp1 = LegalizeOp(Node->getOperand(0));
2699 Tmp2 = LegalizeOp(Node->getOperand(1));
2700 Tmp3 = LegalizeOp(Node->getOperand(2));
2701 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2702 // Since this produces two values, make sure to remember that we legalized
2703 // both of them.
2704 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2705 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2706 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002707
Nate Begeman419f8b62005-10-18 00:27:41 +00002708 case ISD::BUILD_PAIR: {
2709 MVT::ValueType PairTy = Node->getValueType(0);
2710 // TODO: handle the case where the Lo and Hi operands are not of legal type
2711 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2712 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2713 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002714 case TargetLowering::Promote:
2715 case TargetLowering::Custom:
2716 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002717 case TargetLowering::Legal:
2718 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2719 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2720 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002721 case TargetLowering::Expand:
2722 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2723 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2724 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2725 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2726 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002727 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002728 break;
2729 }
2730 break;
2731 }
2732
Nate Begemanc105e192005-04-06 00:23:54 +00002733 case ISD::UREM:
2734 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002735 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002736 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2737 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002738
Nate Begemanc105e192005-04-06 00:23:54 +00002739 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002740 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2741 case TargetLowering::Custom:
2742 isCustom = true;
2743 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002744 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002745 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002746 if (isCustom) {
2747 Tmp1 = TLI.LowerOperation(Result, DAG);
2748 if (Tmp1.Val) Result = Tmp1;
2749 }
Nate Begemanc105e192005-04-06 00:23:54 +00002750 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002751 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002752 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002753 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002754 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002755 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2756 TargetLowering::Legal) {
2757 // X % Y -> X-X/Y*Y
2758 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002759 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002760 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2761 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2762 } else {
2763 assert(Node->getValueType(0) == MVT::i32 &&
2764 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002765 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2766 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002767 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002768 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002769 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002770 } else {
2771 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002772 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2773 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002774 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002775 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2776 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002777 }
2778 break;
2779 }
2780 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002781 case ISD::VAARG: {
2782 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2783 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2784
Chris Lattner5c62f332006-01-28 07:42:08 +00002785 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002786 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2787 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002788 case TargetLowering::Custom:
2789 isCustom = true;
2790 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002791 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002792 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2793 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002794 Tmp1 = Result.getValue(1);
2795
2796 if (isCustom) {
2797 Tmp2 = TLI.LowerOperation(Result, DAG);
2798 if (Tmp2.Val) {
2799 Result = LegalizeOp(Tmp2);
2800 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2801 }
2802 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002803 break;
2804 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002805 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002806 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002807 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002808 // Increment the pointer, VAList, to the next vaarg
2809 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2810 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2811 TLI.getPointerTy()));
2812 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002813 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2814 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002815 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002816 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002817 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002818 Result = LegalizeOp(Result);
2819 break;
2820 }
2821 }
2822 // Since VAARG produces two values, make sure to remember that we
2823 // legalized both of them.
2824 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002825 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2826 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002827 }
2828
2829 case ISD::VACOPY:
2830 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2831 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2832 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2833
2834 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2835 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002836 case TargetLowering::Custom:
2837 isCustom = true;
2838 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002839 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002840 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2841 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002842 if (isCustom) {
2843 Tmp1 = TLI.LowerOperation(Result, DAG);
2844 if (Tmp1.Val) Result = Tmp1;
2845 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002846 break;
2847 case TargetLowering::Expand:
2848 // This defaults to loading a pointer from the input and storing it to the
2849 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002850 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002851 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002852 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2853 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002854 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2855 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002856 break;
2857 }
2858 break;
2859
2860 case ISD::VAEND:
2861 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2862 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2863
2864 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2865 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002866 case TargetLowering::Custom:
2867 isCustom = true;
2868 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002869 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002870 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002871 if (isCustom) {
2872 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2873 if (Tmp1.Val) Result = Tmp1;
2874 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002875 break;
2876 case TargetLowering::Expand:
2877 Result = Tmp1; // Default to a no-op, return the chain
2878 break;
2879 }
2880 break;
2881
2882 case ISD::VASTART:
2883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2884 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2885
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002886 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2887
Nate Begemanacc398c2006-01-25 18:21:52 +00002888 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2889 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002890 case TargetLowering::Legal: break;
2891 case TargetLowering::Custom:
2892 Tmp1 = TLI.LowerOperation(Result, DAG);
2893 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002894 break;
2895 }
2896 break;
2897
Nate Begeman35ef9132006-01-11 21:21:00 +00002898 case ISD::ROTL:
2899 case ISD::ROTR:
2900 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2901 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002902 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002903 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2904 default:
2905 assert(0 && "ROTL/ROTR legalize operation not supported");
2906 break;
2907 case TargetLowering::Legal:
2908 break;
2909 case TargetLowering::Custom:
2910 Tmp1 = TLI.LowerOperation(Result, DAG);
2911 if (Tmp1.Val) Result = Tmp1;
2912 break;
2913 case TargetLowering::Promote:
2914 assert(0 && "Do not know how to promote ROTL/ROTR");
2915 break;
2916 case TargetLowering::Expand:
2917 assert(0 && "Do not know how to expand ROTL/ROTR");
2918 break;
2919 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002920 break;
2921
Nate Begemand88fc032006-01-14 03:14:10 +00002922 case ISD::BSWAP:
2923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2924 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002925 case TargetLowering::Custom:
2926 assert(0 && "Cannot custom legalize this yet!");
2927 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002928 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002929 break;
2930 case TargetLowering::Promote: {
2931 MVT::ValueType OVT = Tmp1.getValueType();
2932 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002933 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002934
Chris Lattner456a93a2006-01-28 07:39:30 +00002935 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2936 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2937 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2938 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2939 break;
2940 }
2941 case TargetLowering::Expand:
2942 Result = ExpandBSWAP(Tmp1);
2943 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002944 }
2945 break;
2946
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002947 case ISD::CTPOP:
2948 case ISD::CTTZ:
2949 case ISD::CTLZ:
2950 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2951 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00002952 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002953 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002954 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00002955 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00002956 TargetLowering::Custom) {
2957 Tmp1 = TLI.LowerOperation(Result, DAG);
2958 if (Tmp1.Val) {
2959 Result = Tmp1;
2960 }
Scott Michel910b66d2007-07-30 21:00:31 +00002961 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002962 break;
2963 case TargetLowering::Promote: {
2964 MVT::ValueType OVT = Tmp1.getValueType();
2965 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002966
2967 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002968 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2969 // Perform the larger operation, then subtract if needed.
2970 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002971 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002972 case ISD::CTPOP:
2973 Result = Tmp1;
2974 break;
2975 case ISD::CTTZ:
2976 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002977 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002978 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002979 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002980 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00002981 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002982 break;
2983 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002984 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002985 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002986 DAG.getConstant(MVT::getSizeInBits(NVT) -
2987 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002988 break;
2989 }
2990 break;
2991 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002992 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002993 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002994 break;
2995 }
2996 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002997
Chris Lattner2c8086f2005-04-02 05:00:07 +00002998 // Unary operators
2999 case ISD::FABS:
3000 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003001 case ISD::FSQRT:
3002 case ISD::FSIN:
3003 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003004 Tmp1 = LegalizeOp(Node->getOperand(0));
3005 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003006 case TargetLowering::Promote:
3007 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003008 isCustom = true;
3009 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003010 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003011 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003012 if (isCustom) {
3013 Tmp1 = TLI.LowerOperation(Result, DAG);
3014 if (Tmp1.Val) Result = Tmp1;
3015 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003016 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003017 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003018 switch (Node->getOpcode()) {
3019 default: assert(0 && "Unreachable!");
3020 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003021 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3022 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003023 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003024 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003025 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003026 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3027 MVT::ValueType VT = Node->getValueType(0);
3028 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003029 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003030 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3031 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003032 break;
3033 }
3034 case ISD::FSQRT:
3035 case ISD::FSIN:
3036 case ISD::FCOS: {
3037 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00003038 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003039 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003040 case ISD::FSQRT:
3041 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
3042 break;
3043 case ISD::FSIN:
3044 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3045 break;
3046 case ISD::FCOS:
3047 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3048 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003049 default: assert(0 && "Unreachable!");
3050 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003051 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003052 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3053 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003054 break;
3055 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003056 }
3057 break;
3058 }
3059 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003060 case ISD::FPOWI: {
3061 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00003062 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
3063 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003064 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003065 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3066 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003067 break;
3068 }
Chris Lattner35481892005-12-23 00:16:34 +00003069 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003070 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003071 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003072 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3073 // The input has to be a vector type, we have to either scalarize it, pack
3074 // it, or convert it based on whether the input vector type is legal.
3075 SDNode *InVal = Node->getOperand(0).Val;
3076 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3077 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3078
3079 // Figure out if there is a simple type corresponding to this Vector
3080 // type. If so, convert to the vector type.
3081 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3082 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003083 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003084 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3085 LegalizeOp(Node->getOperand(0)));
3086 break;
3087 } else if (NumElems == 1) {
3088 // Turn this into a bit convert of the scalar input.
3089 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3090 ScalarizeVectorOp(Node->getOperand(0)));
3091 break;
3092 } else {
3093 // FIXME: UNIMP! Store then reload
3094 assert(0 && "Cast from unsupported vector type not implemented yet!");
3095 }
Chris Lattner67993f72006-01-23 07:30:46 +00003096 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003097 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3098 Node->getOperand(0).getValueType())) {
3099 default: assert(0 && "Unknown operation action!");
3100 case TargetLowering::Expand:
3101 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3102 break;
3103 case TargetLowering::Legal:
3104 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003105 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003106 break;
3107 }
3108 }
3109 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003110
Chris Lattner2c8086f2005-04-02 05:00:07 +00003111 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003112 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003113 case ISD::UINT_TO_FP: {
3114 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3116 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003117 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003118 Node->getOperand(0).getValueType())) {
3119 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003120 case TargetLowering::Custom:
3121 isCustom = true;
3122 // FALLTHROUGH
3123 case TargetLowering::Legal:
3124 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003125 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003126 if (isCustom) {
3127 Tmp1 = TLI.LowerOperation(Result, DAG);
3128 if (Tmp1.Val) Result = Tmp1;
3129 }
3130 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003131 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003132 Result = ExpandLegalINT_TO_FP(isSigned,
3133 LegalizeOp(Node->getOperand(0)),
3134 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003135 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003136 case TargetLowering::Promote:
3137 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3138 Node->getValueType(0),
3139 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003140 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003141 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003142 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003143 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003144 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3145 Node->getValueType(0), Node->getOperand(0));
3146 break;
3147 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003148 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003149 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003150 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3151 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003152 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003153 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3154 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003155 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003156 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3157 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003158 break;
3159 }
3160 break;
3161 }
3162 case ISD::TRUNCATE:
3163 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3164 case Legal:
3165 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003166 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003167 break;
3168 case Expand:
3169 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3170
3171 // Since the result is legal, we should just be able to truncate the low
3172 // part of the source.
3173 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3174 break;
3175 case Promote:
3176 Result = PromoteOp(Node->getOperand(0));
3177 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3178 break;
3179 }
3180 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003181
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003182 case ISD::FP_TO_SINT:
3183 case ISD::FP_TO_UINT:
3184 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3185 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003186 Tmp1 = LegalizeOp(Node->getOperand(0));
3187
Chris Lattner1618beb2005-07-29 00:11:56 +00003188 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3189 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003190 case TargetLowering::Custom:
3191 isCustom = true;
3192 // FALLTHROUGH
3193 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003194 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003195 if (isCustom) {
3196 Tmp1 = TLI.LowerOperation(Result, DAG);
3197 if (Tmp1.Val) Result = Tmp1;
3198 }
3199 break;
3200 case TargetLowering::Promote:
3201 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3202 Node->getOpcode() == ISD::FP_TO_SINT);
3203 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003204 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003205 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3206 SDOperand True, False;
3207 MVT::ValueType VT = Node->getOperand(0).getValueType();
3208 MVT::ValueType NVT = Node->getValueType(0);
3209 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3210 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3211 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3212 Node->getOperand(0), Tmp2, ISD::SETLT);
3213 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3214 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003215 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003216 Tmp2));
3217 False = DAG.getNode(ISD::XOR, NVT, False,
3218 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003219 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3220 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003221 } else {
3222 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3223 }
3224 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003225 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003226 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003227 case Expand: {
3228 // Convert f32 / f64 to i32 / i64.
3229 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00003230 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003231 switch (Node->getOpcode()) {
3232 case ISD::FP_TO_SINT:
3233 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003234 LC = (VT == MVT::i32)
3235 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003236 else
Evan Cheng56966222007-01-12 02:11:51 +00003237 LC = (VT == MVT::i32)
3238 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003239 break;
3240 case ISD::FP_TO_UINT:
3241 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003242 LC = (VT == MVT::i32)
3243 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003244 else
Evan Cheng56966222007-01-12 02:11:51 +00003245 LC = (VT == MVT::i32)
3246 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003247 break;
3248 default: assert(0 && "Unreachable!");
3249 }
3250 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003251 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3252 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003253 break;
3254 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003255 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003256 Tmp1 = PromoteOp(Node->getOperand(0));
3257 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3258 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003259 break;
3260 }
3261 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003262
Dale Johannesenab081c72007-08-09 17:27:48 +00003263 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003264 case ISD::FP_ROUND: {
3265 MVT::ValueType newVT = Op.getValueType();
3266 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3267 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenab081c72007-08-09 17:27:48 +00003268 // The only way we can lower this is to turn it into a STORE,
Dale Johannesen5411a392007-08-09 01:04:01 +00003269 // LOAD pair, targetting a temporary location (a stack slot).
3270
3271 // NOTE: there is a choice here between constantly creating new stack
3272 // slots and always reusing the same one. We currently always create
3273 // new ones, as reuse may inhibit scheduling.
Dale Johannesenab081c72007-08-09 17:27:48 +00003274 MVT::ValueType slotVT =
3275 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3276 const Type *Ty = MVT::getTypeForValueType(slotVT);
Dale Johannesen5411a392007-08-09 01:04:01 +00003277 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3278 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3279 MachineFunction &MF = DAG.getMachineFunction();
3280 int SSFI =
3281 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3282 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesenab081c72007-08-09 17:27:48 +00003283 if (Node->getOpcode() == ISD::FP_EXTEND) {
3284 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3285 StackSlot, NULL, 0);
3286 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3287 Result, StackSlot, NULL, 0, oldVT);
3288 } else {
3289 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3290 StackSlot, NULL, 0, newVT);
3291 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3292 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003293 break;
3294 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003295 }
3296 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003297 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003298 case ISD::ZERO_EXTEND:
3299 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003300 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003301 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003302 case Legal:
3303 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003304 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003305 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003306 case Promote:
3307 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003308 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003309 Tmp1 = PromoteOp(Node->getOperand(0));
3310 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003311 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003312 case ISD::ZERO_EXTEND:
3313 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003314 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003315 Result = DAG.getZeroExtendInReg(Result,
3316 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003317 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003318 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003319 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003320 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003321 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003322 Result,
3323 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003324 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003325 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003326 Result = PromoteOp(Node->getOperand(0));
3327 if (Result.getValueType() != Op.getValueType())
3328 // Dynamically dead while we have only 2 FP types.
3329 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3330 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003331 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003332 Result = PromoteOp(Node->getOperand(0));
3333 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3334 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003335 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003336 }
3337 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003338 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003339 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003340 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003341 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003342
3343 // If this operation is not supported, convert it to a shl/shr or load/store
3344 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003345 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3346 default: assert(0 && "This action not supported for this op yet!");
3347 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003348 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003349 break;
3350 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003351 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003352 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003353 // NOTE: we could fall back on load/store here too for targets without
3354 // SAR. However, it is doubtful that any exist.
3355 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3356 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003357 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003358 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3359 Node->getOperand(0), ShiftCst);
3360 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3361 Result, ShiftCst);
3362 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003363 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003364 // EXTLOAD pair, targetting a temporary location (a stack slot).
3365
3366 // NOTE: there is a choice here between constantly creating new stack
3367 // slots and always reusing the same one. We currently always create
3368 // new ones, as reuse may inhibit scheduling.
3369 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003370 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003371 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003372 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003373 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003374 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003375 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003376 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3377 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003378 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003379 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003380 } else {
3381 assert(0 && "Unknown op");
3382 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003383 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003384 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003385 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003386 }
Duncan Sands36397f52007-07-27 12:58:54 +00003387 case ISD::ADJUST_TRAMP: {
3388 Tmp1 = LegalizeOp(Node->getOperand(0));
3389 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3390 default: assert(0 && "This action is not supported yet!");
3391 case TargetLowering::Custom:
3392 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3393 Result = TLI.LowerOperation(Result, DAG);
3394 if (Result.Val) break;
3395 // FALL THROUGH
3396 case TargetLowering::Expand:
3397 Result = Tmp1;
3398 break;
3399 }
3400 break;
3401 }
3402 case ISD::TRAMPOLINE: {
3403 SDOperand Ops[6];
3404 for (unsigned i = 0; i != 6; ++i)
3405 Ops[i] = LegalizeOp(Node->getOperand(i));
3406 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3407 // The only option for this node is to custom lower it.
3408 Result = TLI.LowerOperation(Result, DAG);
3409 assert(Result.Val && "Should always custom lower!");
3410 break;
3411 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003412 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003413
Chris Lattner4ddd2832006-04-08 04:13:17 +00003414 assert(Result.getValueType() == Op.getValueType() &&
3415 "Bad legalization!");
3416
Chris Lattner456a93a2006-01-28 07:39:30 +00003417 // Make sure that the generated code is itself legal.
3418 if (Result != Op)
3419 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003420
Chris Lattner45982da2005-05-12 16:53:42 +00003421 // Note that LegalizeOp may be reentered even from single-use nodes, which
3422 // means that we always must cache transformed nodes.
3423 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003424 return Result;
3425}
3426
Chris Lattner8b6fa222005-01-15 22:16:26 +00003427/// PromoteOp - Given an operation that produces a value in an invalid type,
3428/// promote it to compute the value into a larger type. The produced value will
3429/// have the correct bits for the low portion of the register, but no guarantee
3430/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003431SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3432 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003433 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003434 assert(getTypeAction(VT) == Promote &&
3435 "Caller should expand or legalize operands that are not promotable!");
3436 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3437 "Cannot promote to smaller type!");
3438
Chris Lattner03c85462005-01-15 05:21:40 +00003439 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003440 SDOperand Result;
3441 SDNode *Node = Op.Val;
3442
Chris Lattner40030bf2007-02-04 01:17:38 +00003443 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003444 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003445
Chris Lattner03c85462005-01-15 05:21:40 +00003446 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003447 case ISD::CopyFromReg:
3448 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003449 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003450#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003451 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003452#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003453 assert(0 && "Do not know how to promote this operator!");
3454 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003455 case ISD::UNDEF:
3456 Result = DAG.getNode(ISD::UNDEF, NVT);
3457 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003458 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003459 if (VT != MVT::i1)
3460 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3461 else
3462 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003463 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3464 break;
3465 case ISD::ConstantFP:
3466 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3467 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3468 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003469
Chris Lattner82fbfb62005-01-18 02:59:52 +00003470 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003471 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003472 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3473 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003474 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003475
Chris Lattner03c85462005-01-15 05:21:40 +00003476 case ISD::TRUNCATE:
3477 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3478 case Legal:
3479 Result = LegalizeOp(Node->getOperand(0));
3480 assert(Result.getValueType() >= NVT &&
3481 "This truncation doesn't make sense!");
3482 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3483 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3484 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003485 case Promote:
3486 // The truncation is not required, because we don't guarantee anything
3487 // about high bits anyway.
3488 Result = PromoteOp(Node->getOperand(0));
3489 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003490 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003491 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3492 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003493 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003494 }
3495 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003496 case ISD::SIGN_EXTEND:
3497 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003498 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003499 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3500 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3501 case Legal:
3502 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003503 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003504 break;
3505 case Promote:
3506 // Promote the reg if it's smaller.
3507 Result = PromoteOp(Node->getOperand(0));
3508 // The high bits are not guaranteed to be anything. Insert an extend.
3509 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003510 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003511 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003512 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003513 Result = DAG.getZeroExtendInReg(Result,
3514 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003515 break;
3516 }
3517 break;
Chris Lattner35481892005-12-23 00:16:34 +00003518 case ISD::BIT_CONVERT:
3519 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3520 Result = PromoteOp(Result);
3521 break;
3522
Chris Lattner8b6fa222005-01-15 22:16:26 +00003523 case ISD::FP_EXTEND:
3524 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3525 case ISD::FP_ROUND:
3526 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3527 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3528 case Promote: assert(0 && "Unreachable with 2 FP types!");
3529 case Legal:
3530 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003531 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003532 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003533 break;
3534 }
3535 break;
3536
3537 case ISD::SINT_TO_FP:
3538 case ISD::UINT_TO_FP:
3539 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3540 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003541 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003542 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003543 break;
3544
3545 case Promote:
3546 Result = PromoteOp(Node->getOperand(0));
3547 if (Node->getOpcode() == ISD::SINT_TO_FP)
3548 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003549 Result,
3550 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003551 else
Chris Lattner23993562005-04-13 02:38:47 +00003552 Result = DAG.getZeroExtendInReg(Result,
3553 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003554 // No extra round required here.
3555 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003556 break;
3557 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003558 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3559 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003560 // Round if we cannot tolerate excess precision.
3561 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003562 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3563 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003564 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003565 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003566 break;
3567
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003568 case ISD::SIGN_EXTEND_INREG:
3569 Result = PromoteOp(Node->getOperand(0));
3570 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3571 Node->getOperand(1));
3572 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003573 case ISD::FP_TO_SINT:
3574 case ISD::FP_TO_UINT:
3575 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3576 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003577 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003578 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003579 break;
3580 case Promote:
3581 // The input result is prerounded, so we don't have to do anything
3582 // special.
3583 Tmp1 = PromoteOp(Node->getOperand(0));
3584 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003585 }
Nate Begemand2558e32005-08-14 01:20:53 +00003586 // If we're promoting a UINT to a larger size, check to see if the new node
3587 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3588 // we can use that instead. This allows us to generate better code for
3589 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3590 // legal, such as PowerPC.
3591 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003592 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003593 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3594 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003595 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3596 } else {
3597 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3598 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003599 break;
3600
Chris Lattner2c8086f2005-04-02 05:00:07 +00003601 case ISD::FABS:
3602 case ISD::FNEG:
3603 Tmp1 = PromoteOp(Node->getOperand(0));
3604 assert(Tmp1.getValueType() == NVT);
3605 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3606 // NOTE: we do not have to do any extra rounding here for
3607 // NoExcessFPPrecision, because we know the input will have the appropriate
3608 // precision, and these operations don't modify precision at all.
3609 break;
3610
Chris Lattnerda6ba872005-04-28 21:44:33 +00003611 case ISD::FSQRT:
3612 case ISD::FSIN:
3613 case ISD::FCOS:
3614 Tmp1 = PromoteOp(Node->getOperand(0));
3615 assert(Tmp1.getValueType() == NVT);
3616 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003617 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003618 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3619 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003620 break;
3621
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003622 case ISD::FPOWI: {
3623 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3624 // directly as well, which may be better.
3625 Tmp1 = PromoteOp(Node->getOperand(0));
3626 assert(Tmp1.getValueType() == NVT);
3627 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3628 if (NoExcessFPPrecision)
3629 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3630 DAG.getValueType(VT));
3631 break;
3632 }
3633
Chris Lattner03c85462005-01-15 05:21:40 +00003634 case ISD::AND:
3635 case ISD::OR:
3636 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003637 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003638 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003639 case ISD::MUL:
3640 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003641 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003642 // that too is okay if they are integer operations.
3643 Tmp1 = PromoteOp(Node->getOperand(0));
3644 Tmp2 = PromoteOp(Node->getOperand(1));
3645 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3646 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003647 break;
3648 case ISD::FADD:
3649 case ISD::FSUB:
3650 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003651 Tmp1 = PromoteOp(Node->getOperand(0));
3652 Tmp2 = PromoteOp(Node->getOperand(1));
3653 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3654 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3655
3656 // Floating point operations will give excess precision that we may not be
3657 // able to tolerate. If we DO allow excess precision, just leave it,
3658 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003659 // FIXME: Why would we need to round FP ops more than integer ones?
3660 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003661 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003662 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3663 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003664 break;
3665
Chris Lattner8b6fa222005-01-15 22:16:26 +00003666 case ISD::SDIV:
3667 case ISD::SREM:
3668 // These operators require that their input be sign extended.
3669 Tmp1 = PromoteOp(Node->getOperand(0));
3670 Tmp2 = PromoteOp(Node->getOperand(1));
3671 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003672 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3673 DAG.getValueType(VT));
3674 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3675 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003676 }
3677 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3678
3679 // Perform FP_ROUND: this is probably overly pessimistic.
3680 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003681 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3682 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003683 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003684 case ISD::FDIV:
3685 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003686 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003687 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003688 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3689 case Legal:
3690 Tmp1 = LegalizeOp(Node->getOperand(0));
3691 break;
3692 case Promote:
3693 Tmp1 = PromoteOp(Node->getOperand(0));
3694 break;
3695 case Expand:
3696 assert(0 && "not implemented");
3697 }
3698 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3699 case Legal:
3700 Tmp2 = LegalizeOp(Node->getOperand(1));
3701 break;
3702 case Promote:
3703 Tmp2 = PromoteOp(Node->getOperand(1));
3704 break;
3705 case Expand:
3706 assert(0 && "not implemented");
3707 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003708 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3709
3710 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003711 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003712 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3713 DAG.getValueType(VT));
3714 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003715
3716 case ISD::UDIV:
3717 case ISD::UREM:
3718 // These operators require that their input be zero extended.
3719 Tmp1 = PromoteOp(Node->getOperand(0));
3720 Tmp2 = PromoteOp(Node->getOperand(1));
3721 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003722 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3723 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003724 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3725 break;
3726
3727 case ISD::SHL:
3728 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003729 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003730 break;
3731 case ISD::SRA:
3732 // The input value must be properly sign extended.
3733 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003734 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3735 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003736 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003737 break;
3738 case ISD::SRL:
3739 // The input value must be properly zero extended.
3740 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003741 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003742 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003743 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003744
3745 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003746 Tmp1 = Node->getOperand(0); // Get the chain.
3747 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003748 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3749 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3750 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3751 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003752 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003753 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003754 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003755 // Increment the pointer, VAList, to the next vaarg
3756 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3757 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3758 TLI.getPointerTy()));
3759 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003760 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3761 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003762 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003763 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003764 }
3765 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003766 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003767 break;
3768
Evan Cheng466685d2006-10-09 20:57:25 +00003769 case ISD::LOAD: {
3770 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003771 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3772 ? ISD::EXTLOAD : LD->getExtensionType();
3773 Result = DAG.getExtLoad(ExtType, NVT,
3774 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003775 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003776 LD->getLoadedVT(),
3777 LD->isVolatile(),
3778 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003779 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003780 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003781 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003782 }
Chris Lattner03c85462005-01-15 05:21:40 +00003783 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003784 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3785 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003786 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003787 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003788 case ISD::SELECT_CC:
3789 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3790 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3791 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003792 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003793 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003794 case ISD::BSWAP:
3795 Tmp1 = Node->getOperand(0);
3796 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3797 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3798 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003799 DAG.getConstant(MVT::getSizeInBits(NVT) -
3800 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003801 TLI.getShiftAmountTy()));
3802 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003803 case ISD::CTPOP:
3804 case ISD::CTTZ:
3805 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003806 // Zero extend the argument
3807 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003808 // Perform the larger operation, then subtract if needed.
3809 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003810 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003811 case ISD::CTPOP:
3812 Result = Tmp1;
3813 break;
3814 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003815 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003816 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003817 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3818 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003819 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003820 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003821 break;
3822 case ISD::CTLZ:
3823 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003824 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003825 DAG.getConstant(MVT::getSizeInBits(NVT) -
3826 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003827 break;
3828 }
3829 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003830 case ISD::EXTRACT_SUBVECTOR:
3831 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003832 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003833 case ISD::EXTRACT_VECTOR_ELT:
3834 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3835 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003836 }
3837
3838 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003839
3840 // Make sure the result is itself legal.
3841 Result = LegalizeOp(Result);
3842
3843 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003844 AddPromotedOperand(Op, Result);
3845 return Result;
3846}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003847
Dan Gohman7f321562007-06-25 16:23:39 +00003848/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3849/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3850/// based on the vector type. The return type of this matches the element type
3851/// of the vector, which may not be legal for the target.
3852SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003853 // We know that operand #0 is the Vec vector. If the index is a constant
3854 // or if the invec is a supported hardware type, we can use it. Otherwise,
3855 // lower to a store then an indexed load.
3856 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003857 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003858
3859 SDNode *InVal = Vec.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00003860 MVT::ValueType TVT = InVal->getValueType(0);
3861 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003862
Dan Gohman7f321562007-06-25 16:23:39 +00003863 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3864 default: assert(0 && "This action is not supported yet!");
3865 case TargetLowering::Custom: {
3866 Vec = LegalizeOp(Vec);
3867 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3868 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3869 if (Tmp3.Val)
3870 return Tmp3;
3871 break;
3872 }
3873 case TargetLowering::Legal:
3874 if (isTypeLegal(TVT)) {
3875 Vec = LegalizeOp(Vec);
3876 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00003877 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00003878 }
3879 break;
3880 case TargetLowering::Expand:
3881 break;
3882 }
3883
3884 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003885 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003886 Op = ScalarizeVectorOp(Vec);
3887 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3888 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003889 SDOperand Lo, Hi;
3890 SplitVectorOp(Vec, Lo, Hi);
3891 if (CIdx->getValue() < NumElems/2) {
3892 Vec = Lo;
3893 } else {
3894 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003895 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3896 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003897 }
Dan Gohman7f321562007-06-25 16:23:39 +00003898
Chris Lattner15972212006-03-31 17:55:51 +00003899 // It's now an extract from the appropriate high or low part. Recurse.
3900 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003901 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003902 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003903 // Store the value to a temporary stack slot, then LOAD the scalar
3904 // element back out.
3905 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3906 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3907
3908 // Add the offset to the index.
3909 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3910 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3911 DAG.getConstant(EltSize, Idx.getValueType()));
3912 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3913
3914 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003915 }
Dan Gohman7f321562007-06-25 16:23:39 +00003916 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003917}
3918
Dan Gohman7f321562007-06-25 16:23:39 +00003919/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003920/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003921SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003922 // We know that operand #0 is the Vec vector. For now we assume the index
3923 // is a constant and that the extracted result is a supported hardware type.
3924 SDOperand Vec = Op.getOperand(0);
3925 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3926
Dan Gohman7f321562007-06-25 16:23:39 +00003927 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003928
3929 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3930 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003931 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003932 }
3933
3934 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3935 SDOperand Lo, Hi;
3936 SplitVectorOp(Vec, Lo, Hi);
3937 if (CIdx->getValue() < NumElems/2) {
3938 Vec = Lo;
3939 } else {
3940 Vec = Hi;
3941 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3942 }
3943
3944 // It's now an extract from the appropriate high or low part. Recurse.
3945 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003946 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003947}
3948
Nate Begeman750ac1b2006-02-01 07:19:44 +00003949/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3950/// with condition CC on the current target. This usually involves legalizing
3951/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3952/// there may be no choice but to create a new SetCC node to represent the
3953/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3954/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3955void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3956 SDOperand &RHS,
3957 SDOperand &CC) {
3958 SDOperand Tmp1, Tmp2, Result;
3959
3960 switch (getTypeAction(LHS.getValueType())) {
3961 case Legal:
3962 Tmp1 = LegalizeOp(LHS); // LHS
3963 Tmp2 = LegalizeOp(RHS); // RHS
3964 break;
3965 case Promote:
3966 Tmp1 = PromoteOp(LHS); // LHS
3967 Tmp2 = PromoteOp(RHS); // RHS
3968
3969 // If this is an FP compare, the operands have already been extended.
3970 if (MVT::isInteger(LHS.getValueType())) {
3971 MVT::ValueType VT = LHS.getValueType();
3972 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3973
3974 // Otherwise, we have to insert explicit sign or zero extends. Note
3975 // that we could insert sign extends for ALL conditions, but zero extend
3976 // is cheaper on many machines (an AND instead of two shifts), so prefer
3977 // it.
3978 switch (cast<CondCodeSDNode>(CC)->get()) {
3979 default: assert(0 && "Unknown integer comparison!");
3980 case ISD::SETEQ:
3981 case ISD::SETNE:
3982 case ISD::SETUGE:
3983 case ISD::SETUGT:
3984 case ISD::SETULE:
3985 case ISD::SETULT:
3986 // ALL of these operations will work if we either sign or zero extend
3987 // the operands (including the unsigned comparisons!). Zero extend is
3988 // usually a simpler/cheaper operation, so prefer it.
3989 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3990 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3991 break;
3992 case ISD::SETGE:
3993 case ISD::SETGT:
3994 case ISD::SETLT:
3995 case ISD::SETLE:
3996 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3997 DAG.getValueType(VT));
3998 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3999 DAG.getValueType(VT));
4000 break;
4001 }
4002 }
4003 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004004 case Expand: {
4005 MVT::ValueType VT = LHS.getValueType();
4006 if (VT == MVT::f32 || VT == MVT::f64) {
4007 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004008 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004009 switch (cast<CondCodeSDNode>(CC)->get()) {
4010 case ISD::SETEQ:
4011 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004012 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004013 break;
4014 case ISD::SETNE:
4015 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004016 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004017 break;
4018 case ISD::SETGE:
4019 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004020 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004021 break;
4022 case ISD::SETLT:
4023 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004024 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004025 break;
4026 case ISD::SETLE:
4027 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004028 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004029 break;
4030 case ISD::SETGT:
4031 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004032 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004033 break;
4034 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004035 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4036 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004037 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004038 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004039 break;
4040 default:
Evan Cheng56966222007-01-12 02:11:51 +00004041 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004042 switch (cast<CondCodeSDNode>(CC)->get()) {
4043 case ISD::SETONE:
4044 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004045 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004046 // Fallthrough
4047 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004048 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004049 break;
4050 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004051 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004052 break;
4053 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004054 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004055 break;
4056 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004057 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004058 break;
Evan Cheng56966222007-01-12 02:11:51 +00004059 case ISD::SETUEQ:
4060 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004061 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004062 default: assert(0 && "Unsupported FP setcc!");
4063 }
4064 }
4065
4066 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004067 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004068 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004069 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004070 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004071 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004072 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004073 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004074 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004075 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004076 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004077 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004078 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004079 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4080 Tmp2 = SDOperand();
4081 }
4082 LHS = Tmp1;
4083 RHS = Tmp2;
4084 return;
4085 }
4086
Nate Begeman750ac1b2006-02-01 07:19:44 +00004087 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4088 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00004089 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004090 switch (cast<CondCodeSDNode>(CC)->get()) {
4091 case ISD::SETEQ:
4092 case ISD::SETNE:
4093 if (RHSLo == RHSHi)
4094 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4095 if (RHSCST->isAllOnesValue()) {
4096 // Comparison to -1.
4097 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4098 Tmp2 = RHSLo;
4099 break;
4100 }
4101
4102 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4103 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4104 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4105 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4106 break;
4107 default:
4108 // If this is a comparison of the sign bit, just look at the top part.
4109 // X > -1, x < 0
4110 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4111 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4112 CST->getValue() == 0) || // X < 0
4113 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4114 CST->isAllOnesValue())) { // X > -1
4115 Tmp1 = LHSHi;
4116 Tmp2 = RHSHi;
4117 break;
4118 }
4119
4120 // FIXME: This generated code sucks.
4121 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004122 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4123 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004124 default: assert(0 && "Unknown integer setcc!");
4125 case ISD::SETLT:
4126 case ISD::SETULT: LowCC = ISD::SETULT; break;
4127 case ISD::SETGT:
4128 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4129 case ISD::SETLE:
4130 case ISD::SETULE: LowCC = ISD::SETULE; break;
4131 case ISD::SETGE:
4132 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4133 }
4134
4135 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4136 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4137 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4138
4139 // NOTE: on targets without efficient SELECT of bools, we can always use
4140 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004141 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4142 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4143 false, DagCombineInfo);
4144 if (!Tmp1.Val)
4145 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4146 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4147 CCCode, false, DagCombineInfo);
4148 if (!Tmp2.Val)
4149 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4150
4151 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4152 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4153 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4154 (Tmp2C && Tmp2C->getValue() == 0 &&
4155 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4156 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4157 (Tmp2C && Tmp2C->getValue() == 1 &&
4158 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4159 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4160 // low part is known false, returns high part.
4161 // For LE / GE, if high part is known false, ignore the low part.
4162 // For LT / GT, if high part is known true, ignore the low part.
4163 Tmp1 = Tmp2;
4164 Tmp2 = SDOperand();
4165 } else {
4166 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4167 ISD::SETEQ, false, DagCombineInfo);
4168 if (!Result.Val)
4169 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4170 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4171 Result, Tmp1, Tmp2));
4172 Tmp1 = Result;
4173 Tmp2 = SDOperand();
4174 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004175 }
4176 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004177 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004178 LHS = Tmp1;
4179 RHS = Tmp2;
4180}
4181
Chris Lattner35481892005-12-23 00:16:34 +00004182/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004183/// The resultant code need not be legal. Note that SrcOp is the input operand
4184/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004185SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4186 SDOperand SrcOp) {
4187 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00004188 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004189
4190 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004191 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004192 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004193 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004194}
4195
Chris Lattner4352cc92006-04-04 17:23:26 +00004196SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4197 // Create a vector sized/aligned stack slot, store the value to element #0,
4198 // then load the whole vector back out.
4199 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004200 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004201 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004202 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004203}
4204
4205
Chris Lattnerce872152006-03-19 06:31:19 +00004206/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004207/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004208SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4209
4210 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004211 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004212 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004213 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004214 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004215 std::map<SDOperand, std::vector<unsigned> > Values;
4216 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004217 bool isConstant = true;
4218 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4219 SplatValue.getOpcode() != ISD::UNDEF)
4220 isConstant = false;
4221
Evan Cheng033e6812006-03-24 01:17:21 +00004222 for (unsigned i = 1; i < NumElems; ++i) {
4223 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004224 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004225 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004226 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004227 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004228 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004229
4230 // If this isn't a constant element or an undef, we can't use a constant
4231 // pool load.
4232 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4233 V.getOpcode() != ISD::UNDEF)
4234 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004235 }
4236
4237 if (isOnlyLowElement) {
4238 // If the low element is an undef too, then this whole things is an undef.
4239 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4240 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4241 // Otherwise, turn this into a scalar_to_vector node.
4242 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4243 Node->getOperand(0));
4244 }
4245
Chris Lattner2eb86532006-03-24 07:29:17 +00004246 // If all elements are constants, create a load from the constant pool.
4247 if (isConstant) {
4248 MVT::ValueType VT = Node->getValueType(0);
4249 const Type *OpNTy =
4250 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4251 std::vector<Constant*> CV;
4252 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4253 if (ConstantFPSDNode *V =
4254 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004255 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004256 } else if (ConstantSDNode *V =
4257 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004258 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004259 } else {
4260 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4261 CV.push_back(UndefValue::get(OpNTy));
4262 }
4263 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004264 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004265 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004266 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004267 }
4268
Chris Lattner87100e02006-03-20 01:52:29 +00004269 if (SplatValue.Val) { // Splat of one value?
4270 // Build the shuffle constant vector: <0, 0, 0, 0>
4271 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004272 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004273 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004274 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004275 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4276 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004277
4278 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004279 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004280 // Get the splatted value into the low element of a vector register.
4281 SDOperand LowValVec =
4282 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4283
4284 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4285 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4286 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4287 SplatMask);
4288 }
4289 }
4290
Evan Cheng033e6812006-03-24 01:17:21 +00004291 // If there are only two unique elements, we may be able to turn this into a
4292 // vector shuffle.
4293 if (Values.size() == 2) {
4294 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4295 MVT::ValueType MaskVT =
4296 MVT::getIntVectorWithNumElements(NumElems);
4297 std::vector<SDOperand> MaskVec(NumElems);
4298 unsigned i = 0;
4299 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4300 E = Values.end(); I != E; ++I) {
4301 for (std::vector<unsigned>::iterator II = I->second.begin(),
4302 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004303 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004304 i += NumElems;
4305 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004306 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4307 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004308
4309 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004310 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4311 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004312 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004313 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4314 E = Values.end(); I != E; ++I) {
4315 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4316 I->first);
4317 Ops.push_back(Op);
4318 }
4319 Ops.push_back(ShuffleMask);
4320
4321 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004322 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4323 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004324 }
4325 }
Chris Lattnerce872152006-03-19 06:31:19 +00004326
4327 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4328 // aligned object on the stack, store each element into it, then load
4329 // the result as a vector.
4330 MVT::ValueType VT = Node->getValueType(0);
4331 // Create the stack frame object.
4332 SDOperand FIPtr = CreateStackTemporary(VT);
4333
4334 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004335 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004336 unsigned TypeByteSize =
4337 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004338 // Store (in the right endianness) the elements to memory.
4339 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4340 // Ignore undef elements.
4341 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4342
Chris Lattner841c8822006-03-22 01:46:54 +00004343 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004344
4345 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4346 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4347
Evan Cheng786225a2006-10-05 23:01:46 +00004348 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004349 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004350 }
4351
4352 SDOperand StoreChain;
4353 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004354 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4355 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004356 else
4357 StoreChain = DAG.getEntryNode();
4358
4359 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004360 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004361}
4362
4363/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4364/// specified value type.
4365SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4366 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4367 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004368 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004369 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004370 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004371 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4372}
4373
Chris Lattner5b359c62005-04-02 04:00:59 +00004374void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4375 SDOperand Op, SDOperand Amt,
4376 SDOperand &Lo, SDOperand &Hi) {
4377 // Expand the subcomponents.
4378 SDOperand LHSL, LHSH;
4379 ExpandOp(Op, LHSL, LHSH);
4380
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004381 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004382 MVT::ValueType VT = LHSL.getValueType();
4383 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004384 Hi = Lo.getValue(1);
4385}
4386
4387
Chris Lattnere34b3962005-01-19 04:19:40 +00004388/// ExpandShift - Try to find a clever way to expand this shift operation out to
4389/// smaller elements. If we can't find a way that is more efficient than a
4390/// libcall on this target, return false. Otherwise, return true with the
4391/// low-parts expanded into Lo and Hi.
4392bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4393 SDOperand &Lo, SDOperand &Hi) {
4394 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4395 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004396
Chris Lattnere34b3962005-01-19 04:19:40 +00004397 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004398 SDOperand ShAmt = LegalizeOp(Amt);
4399 MVT::ValueType ShTy = ShAmt.getValueType();
4400 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4401 unsigned NVTBits = MVT::getSizeInBits(NVT);
4402
4403 // Handle the case when Amt is an immediate. Other cases are currently broken
4404 // and are disabled.
4405 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4406 unsigned Cst = CN->getValue();
4407 // Expand the incoming operand to be shifted, so that we have its parts
4408 SDOperand InL, InH;
4409 ExpandOp(Op, InL, InH);
4410 switch(Opc) {
4411 case ISD::SHL:
4412 if (Cst > VTBits) {
4413 Lo = DAG.getConstant(0, NVT);
4414 Hi = DAG.getConstant(0, NVT);
4415 } else if (Cst > NVTBits) {
4416 Lo = DAG.getConstant(0, NVT);
4417 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004418 } else if (Cst == NVTBits) {
4419 Lo = DAG.getConstant(0, NVT);
4420 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004421 } else {
4422 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4423 Hi = DAG.getNode(ISD::OR, NVT,
4424 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4425 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4426 }
4427 return true;
4428 case ISD::SRL:
4429 if (Cst > VTBits) {
4430 Lo = DAG.getConstant(0, NVT);
4431 Hi = DAG.getConstant(0, NVT);
4432 } else if (Cst > NVTBits) {
4433 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4434 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004435 } else if (Cst == NVTBits) {
4436 Lo = InH;
4437 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004438 } else {
4439 Lo = DAG.getNode(ISD::OR, NVT,
4440 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4441 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4442 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4443 }
4444 return true;
4445 case ISD::SRA:
4446 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004447 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004448 DAG.getConstant(NVTBits-1, ShTy));
4449 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004450 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004451 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004452 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004453 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004454 } else if (Cst == NVTBits) {
4455 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004456 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004457 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004458 } else {
4459 Lo = DAG.getNode(ISD::OR, NVT,
4460 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4461 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4462 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4463 }
4464 return true;
4465 }
4466 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004467
4468 // Okay, the shift amount isn't constant. However, if we can tell that it is
4469 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4470 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004471 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004472
4473 // If we know that the high bit of the shift amount is one, then we can do
4474 // this as a couple of simple shifts.
4475 if (KnownOne & Mask) {
4476 // Mask out the high bit, which we know is set.
4477 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4478 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4479
4480 // Expand the incoming operand to be shifted, so that we have its parts
4481 SDOperand InL, InH;
4482 ExpandOp(Op, InL, InH);
4483 switch(Opc) {
4484 case ISD::SHL:
4485 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4486 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4487 return true;
4488 case ISD::SRL:
4489 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4490 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4491 return true;
4492 case ISD::SRA:
4493 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4494 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4495 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4496 return true;
4497 }
4498 }
4499
4500 // If we know that the high bit of the shift amount is zero, then we can do
4501 // this as a couple of simple shifts.
4502 if (KnownZero & Mask) {
4503 // Compute 32-amt.
4504 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4505 DAG.getConstant(NVTBits, Amt.getValueType()),
4506 Amt);
4507
4508 // Expand the incoming operand to be shifted, so that we have its parts
4509 SDOperand InL, InH;
4510 ExpandOp(Op, InL, InH);
4511 switch(Opc) {
4512 case ISD::SHL:
4513 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4514 Hi = DAG.getNode(ISD::OR, NVT,
4515 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4516 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4517 return true;
4518 case ISD::SRL:
4519 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4520 Lo = DAG.getNode(ISD::OR, NVT,
4521 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4522 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4523 return true;
4524 case ISD::SRA:
4525 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4526 Lo = DAG.getNode(ISD::OR, NVT,
4527 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4528 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4529 return true;
4530 }
4531 }
4532
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004533 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004534}
Chris Lattner77e77a62005-01-21 06:05:23 +00004535
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004536
Chris Lattner77e77a62005-01-21 06:05:23 +00004537// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4538// does not fit into a register, return the lo part and set the hi part to the
4539// by-reg argument. If it does fit into a single register, return the result
4540// and leave the Hi part unset.
4541SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004542 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004543 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4544 // The input chain to this libcall is the entry node of the function.
4545 // Legalizing the call will automatically add the previous call to the
4546 // dependence.
4547 SDOperand InChain = DAG.getEntryNode();
4548
Chris Lattner77e77a62005-01-21 06:05:23 +00004549 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004550 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004551 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4552 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4553 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004554 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004555 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004556 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004557 }
4558 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004559
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004560 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004561 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004562 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004563 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004564 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004565
Chris Lattner6831a812006-02-13 09:18:02 +00004566 // Legalize the call sequence, starting with the chain. This will advance
4567 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4568 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4569 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004570 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004571 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004572 default: assert(0 && "Unknown thing");
4573 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004574 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004575 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004576 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004577 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004578 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004579 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004580 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004581}
4582
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004583
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004584/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4585///
Chris Lattner77e77a62005-01-21 06:05:23 +00004586SDOperand SelectionDAGLegalize::
4587ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004588 assert(getTypeAction(Source.getValueType()) == Expand &&
4589 "This is not an expansion!");
4590 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4591
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004592 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004593 assert(Source.getValueType() == MVT::i64 &&
4594 "This only works for 64-bit -> FP");
4595 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4596 // incoming integer is set. To handle this, we dynamically test to see if
4597 // it is set, and, if so, add a fudge factor.
4598 SDOperand Lo, Hi;
4599 ExpandOp(Source, Lo, Hi);
4600
Chris Lattner66de05b2005-05-13 04:45:13 +00004601 // If this is unsigned, and not supported, first perform the conversion to
4602 // signed, then adjust the result if the sign bit is set.
4603 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4604 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4605
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004606 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4607 DAG.getConstant(0, Hi.getValueType()),
4608 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004609 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4610 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4611 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004612 uint64_t FF = 0x5f800000ULL;
4613 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004614 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004615
Chris Lattner5839bf22005-08-26 17:15:30 +00004616 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004617 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4618 SDOperand FudgeInReg;
4619 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004620 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004621 else {
4622 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004623 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattner5f056bf2005-07-10 01:55:33 +00004624 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004625 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004626 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004627 MVT::ValueType SCVT = SignedConv.getValueType();
4628 if (SCVT != DestTy) {
4629 // Destination type needs to be expanded as well. The FADD now we are
4630 // constructing will be expanded into a libcall.
4631 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4632 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4633 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4634 SignedConv, SignedConv.getValue(1));
4635 }
4636 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4637 }
Chris Lattner473a9902005-09-29 06:44:39 +00004638 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004639 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004640
Chris Lattnera88a2602005-05-14 05:33:54 +00004641 // Check to see if the target has a custom way to lower this. If so, use it.
4642 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4643 default: assert(0 && "This action not implemented for this operation!");
4644 case TargetLowering::Legal:
4645 case TargetLowering::Expand:
4646 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004647 case TargetLowering::Custom: {
4648 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4649 Source), DAG);
4650 if (NV.Val)
4651 return LegalizeOp(NV);
4652 break; // The target decided this was legal after all
4653 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004654 }
4655
Chris Lattner13689e22005-05-12 07:00:44 +00004656 // Expand the source, then glue it back together for the call. We must expand
4657 // the source in case it is shared (this pass of legalize must traverse it).
4658 SDOperand SrcLo, SrcHi;
4659 ExpandOp(Source, SrcLo, SrcHi);
4660 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4661
Evan Cheng56966222007-01-12 02:11:51 +00004662 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004663 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004664 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004665 else {
4666 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004667 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004668 }
Chris Lattner6831a812006-02-13 09:18:02 +00004669
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004670 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004671 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4672 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004673 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4674 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004675}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004676
Chris Lattner22cde6a2006-01-28 08:25:58 +00004677/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4678/// INT_TO_FP operation of the specified operand when the target requests that
4679/// we expand it. At this point, we know that the result and operand types are
4680/// legal for the target.
4681SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4682 SDOperand Op0,
4683 MVT::ValueType DestVT) {
4684 if (Op0.getValueType() == MVT::i32) {
4685 // simple 32-bit [signed|unsigned] integer to float/double expansion
4686
Chris Lattner58092e32007-01-20 22:35:55 +00004687 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004688 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004689 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4690 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004691 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004692 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004693 // get address of 8 byte buffer
4694 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4695 // word offset constant for Hi/Lo address computation
4696 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4697 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004698 SDOperand Hi = StackSlot;
4699 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4700 if (TLI.isLittleEndian())
4701 std::swap(Hi, Lo);
4702
Chris Lattner22cde6a2006-01-28 08:25:58 +00004703 // if signed map to unsigned space
4704 SDOperand Op0Mapped;
4705 if (isSigned) {
4706 // constant used to invert sign bit (signed to unsigned mapping)
4707 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4708 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4709 } else {
4710 Op0Mapped = Op0;
4711 }
4712 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004713 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004714 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004715 // initial hi portion of constructed double
4716 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4717 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004718 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004719 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004720 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004721 // FP constant to bias correct the final result
4722 SDOperand Bias = DAG.getConstantFP(isSigned ?
4723 BitsToDouble(0x4330000080000000ULL)
4724 : BitsToDouble(0x4330000000000000ULL),
4725 MVT::f64);
4726 // subtract the bias
4727 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4728 // final result
4729 SDOperand Result;
4730 // handle final rounding
4731 if (DestVT == MVT::f64) {
4732 // do nothing
4733 Result = Sub;
4734 } else {
4735 // if f32 then cast to f32
4736 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4737 }
4738 return Result;
4739 }
4740 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4741 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4742
4743 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4744 DAG.getConstant(0, Op0.getValueType()),
4745 ISD::SETLT);
4746 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4747 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4748 SignSet, Four, Zero);
4749
4750 // If the sign bit of the integer is set, the large number will be treated
4751 // as a negative number. To counteract this, the dynamic code adds an
4752 // offset depending on the data type.
4753 uint64_t FF;
4754 switch (Op0.getValueType()) {
4755 default: assert(0 && "Unsupported integer type!");
4756 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4757 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4758 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4759 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4760 }
4761 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004762 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004763
4764 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4765 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4766 SDOperand FudgeInReg;
4767 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004768 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004769 else {
4770 assert(DestVT == MVT::f64 && "Unexpected conversion");
4771 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4772 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004773 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004774 }
4775
4776 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4777}
4778
4779/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4780/// *INT_TO_FP operation of the specified operand when the target requests that
4781/// we promote it. At this point, we know that the result and operand types are
4782/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4783/// operation that takes a larger input.
4784SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4785 MVT::ValueType DestVT,
4786 bool isSigned) {
4787 // First step, figure out the appropriate *INT_TO_FP operation to use.
4788 MVT::ValueType NewInTy = LegalOp.getValueType();
4789
4790 unsigned OpToUse = 0;
4791
4792 // Scan for the appropriate larger type to use.
4793 while (1) {
4794 NewInTy = (MVT::ValueType)(NewInTy+1);
4795 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4796
4797 // If the target supports SINT_TO_FP of this type, use it.
4798 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4799 default: break;
4800 case TargetLowering::Legal:
4801 if (!TLI.isTypeLegal(NewInTy))
4802 break; // Can't use this datatype.
4803 // FALL THROUGH.
4804 case TargetLowering::Custom:
4805 OpToUse = ISD::SINT_TO_FP;
4806 break;
4807 }
4808 if (OpToUse) break;
4809 if (isSigned) continue;
4810
4811 // If the target supports UINT_TO_FP of this type, use it.
4812 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4813 default: break;
4814 case TargetLowering::Legal:
4815 if (!TLI.isTypeLegal(NewInTy))
4816 break; // Can't use this datatype.
4817 // FALL THROUGH.
4818 case TargetLowering::Custom:
4819 OpToUse = ISD::UINT_TO_FP;
4820 break;
4821 }
4822 if (OpToUse) break;
4823
4824 // Otherwise, try a larger type.
4825 }
4826
4827 // Okay, we found the operation and type to use. Zero extend our input to the
4828 // desired type then run the operation on it.
4829 return DAG.getNode(OpToUse, DestVT,
4830 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4831 NewInTy, LegalOp));
4832}
4833
4834/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4835/// FP_TO_*INT operation of the specified operand when the target requests that
4836/// we promote it. At this point, we know that the result and operand types are
4837/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4838/// operation that returns a larger result.
4839SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4840 MVT::ValueType DestVT,
4841 bool isSigned) {
4842 // First step, figure out the appropriate FP_TO*INT operation to use.
4843 MVT::ValueType NewOutTy = DestVT;
4844
4845 unsigned OpToUse = 0;
4846
4847 // Scan for the appropriate larger type to use.
4848 while (1) {
4849 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4850 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4851
4852 // If the target supports FP_TO_SINT returning this type, use it.
4853 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4854 default: break;
4855 case TargetLowering::Legal:
4856 if (!TLI.isTypeLegal(NewOutTy))
4857 break; // Can't use this datatype.
4858 // FALL THROUGH.
4859 case TargetLowering::Custom:
4860 OpToUse = ISD::FP_TO_SINT;
4861 break;
4862 }
4863 if (OpToUse) break;
4864
4865 // If the target supports FP_TO_UINT of this type, use it.
4866 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4867 default: break;
4868 case TargetLowering::Legal:
4869 if (!TLI.isTypeLegal(NewOutTy))
4870 break; // Can't use this datatype.
4871 // FALL THROUGH.
4872 case TargetLowering::Custom:
4873 OpToUse = ISD::FP_TO_UINT;
4874 break;
4875 }
4876 if (OpToUse) break;
4877
4878 // Otherwise, try a larger type.
4879 }
4880
4881 // Okay, we found the operation and type to use. Truncate the result of the
4882 // extended FP_TO_*INT operation to the desired size.
4883 return DAG.getNode(ISD::TRUNCATE, DestVT,
4884 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4885}
4886
4887/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4888///
4889SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4890 MVT::ValueType VT = Op.getValueType();
4891 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4892 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4893 switch (VT) {
4894 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4895 case MVT::i16:
4896 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4897 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4898 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4899 case MVT::i32:
4900 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4901 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4902 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4903 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4904 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4905 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4906 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4907 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4908 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4909 case MVT::i64:
4910 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4911 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4912 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4913 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4914 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4915 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4916 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4917 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4918 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4919 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4920 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4921 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4922 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4923 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4924 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4925 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4926 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4927 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4928 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4929 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4930 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4931 }
4932}
4933
4934/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4935///
4936SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4937 switch (Opc) {
4938 default: assert(0 && "Cannot expand this yet!");
4939 case ISD::CTPOP: {
4940 static const uint64_t mask[6] = {
4941 0x5555555555555555ULL, 0x3333333333333333ULL,
4942 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4943 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4944 };
4945 MVT::ValueType VT = Op.getValueType();
4946 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004947 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004948 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4949 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4950 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4951 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4952 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4953 DAG.getNode(ISD::AND, VT,
4954 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4955 }
4956 return Op;
4957 }
4958 case ISD::CTLZ: {
4959 // for now, we do this:
4960 // x = x | (x >> 1);
4961 // x = x | (x >> 2);
4962 // ...
4963 // x = x | (x >>16);
4964 // x = x | (x >>32); // for 64-bit input
4965 // return popcount(~x);
4966 //
4967 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4968 MVT::ValueType VT = Op.getValueType();
4969 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004970 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004971 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4972 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4973 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4974 }
4975 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4976 return DAG.getNode(ISD::CTPOP, VT, Op);
4977 }
4978 case ISD::CTTZ: {
4979 // for now, we use: { return popcount(~x & (x - 1)); }
4980 // unless the target has ctlz but not ctpop, in which case we use:
4981 // { return 32 - nlz(~x & (x-1)); }
4982 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4983 MVT::ValueType VT = Op.getValueType();
4984 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4985 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4986 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4987 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4988 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4989 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4990 TLI.isOperationLegal(ISD::CTLZ, VT))
4991 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004992 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004993 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4994 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4995 }
4996 }
4997}
Chris Lattnere34b3962005-01-19 04:19:40 +00004998
Chris Lattner3e928bb2005-01-07 07:47:09 +00004999/// ExpandOp - Expand the specified SDOperand into its two component pieces
5000/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5001/// LegalizeNodes map is filled in for any results that are not expanded, the
5002/// ExpandedNodes map is filled in for any results that are expanded, and the
5003/// Lo/Hi values are returned.
5004void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5005 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005006 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005007 SDNode *Node = Op.Val;
5008 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005009 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005010 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005011 "Cannot expand to FP value or to larger int value!");
5012
Chris Lattner6fdcb252005-09-02 20:32:45 +00005013 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005014 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005015 = ExpandedNodes.find(Op);
5016 if (I != ExpandedNodes.end()) {
5017 Lo = I->second.first;
5018 Hi = I->second.second;
5019 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005020 }
5021
Chris Lattner3e928bb2005-01-07 07:47:09 +00005022 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005023 case ISD::CopyFromReg:
5024 assert(0 && "CopyFromReg must be legal!");
5025 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005026#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005027 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005028#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005029 assert(0 && "Do not know how to expand this operator!");
5030 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005031 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005032 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005033 Lo = DAG.getNode(ISD::UNDEF, NVT);
5034 Hi = DAG.getNode(ISD::UNDEF, NVT);
5035 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005036 case ISD::Constant: {
5037 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5038 Lo = DAG.getConstant(Cst, NVT);
5039 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5040 break;
5041 }
Evan Cheng00495212006-12-12 21:32:44 +00005042 case ISD::ConstantFP: {
5043 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00005044 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005045 if (getTypeAction(Lo.getValueType()) == Expand)
5046 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005047 break;
5048 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005049 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005050 // Return the operands.
5051 Lo = Node->getOperand(0);
5052 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005053 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005054
5055 case ISD::SIGN_EXTEND_INREG:
5056 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005057 // sext_inreg the low part if needed.
5058 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5059
5060 // The high part gets the sign extension from the lo-part. This handles
5061 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005062 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5063 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5064 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005065 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005066
Nate Begemand88fc032006-01-14 03:14:10 +00005067 case ISD::BSWAP: {
5068 ExpandOp(Node->getOperand(0), Lo, Hi);
5069 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5070 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5071 Lo = TempLo;
5072 break;
5073 }
5074
Chris Lattneredb1add2005-05-11 04:51:16 +00005075 case ISD::CTPOP:
5076 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005077 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5078 DAG.getNode(ISD::CTPOP, NVT, Lo),
5079 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005080 Hi = DAG.getConstant(0, NVT);
5081 break;
5082
Chris Lattner39a8f332005-05-12 19:05:01 +00005083 case ISD::CTLZ: {
5084 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005085 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005086 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5087 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005088 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5089 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005090 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5091 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5092
5093 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5094 Hi = DAG.getConstant(0, NVT);
5095 break;
5096 }
5097
5098 case ISD::CTTZ: {
5099 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005100 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005101 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5102 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005103 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5104 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005105 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5106 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5107
5108 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5109 Hi = DAG.getConstant(0, NVT);
5110 break;
5111 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005112
Nate Begemanacc398c2006-01-25 18:21:52 +00005113 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005114 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5115 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005116 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5117 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5118
5119 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005120 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005121 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5122 if (!TLI.isLittleEndian())
5123 std::swap(Lo, Hi);
5124 break;
5125 }
5126
Chris Lattner3e928bb2005-01-07 07:47:09 +00005127 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005128 LoadSDNode *LD = cast<LoadSDNode>(Node);
5129 SDOperand Ch = LD->getChain(); // Legalize the chain.
5130 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5131 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005132 int SVOffset = LD->getSrcValueOffset();
5133 unsigned Alignment = LD->getAlignment();
5134 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005135
Evan Cheng466685d2006-10-09 20:57:25 +00005136 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005137 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5138 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005139 if (VT == MVT::f32 || VT == MVT::f64) {
5140 // f32->i32 or f64->i64 one to one expansion.
5141 // Remember that we legalized the chain.
5142 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005143 // Recursively expand the new load.
5144 if (getTypeAction(NVT) == Expand)
5145 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005146 break;
5147 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005148
Evan Cheng466685d2006-10-09 20:57:25 +00005149 // Increment the pointer to the other half.
5150 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5151 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5152 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005153 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005154 if (Alignment > IncrementSize)
5155 Alignment = IncrementSize;
5156 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5157 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005158
Evan Cheng466685d2006-10-09 20:57:25 +00005159 // Build a factor node to remember that this load is independent of the
5160 // other one.
5161 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5162 Hi.getValue(1));
5163
5164 // Remember that we legalized the chain.
5165 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5166 if (!TLI.isLittleEndian())
5167 std::swap(Lo, Hi);
5168 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005169 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005170
5171 if (VT == MVT::f64 && EVT == MVT::f32) {
5172 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5173 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005174 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005175 // Remember that we legalized the chain.
5176 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5177 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5178 break;
5179 }
Evan Cheng466685d2006-10-09 20:57:25 +00005180
5181 if (EVT == NVT)
5182 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005183 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005184 else
5185 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005186 SVOffset, EVT, isVolatile,
5187 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005188
5189 // Remember that we legalized the chain.
5190 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5191
5192 if (ExtType == ISD::SEXTLOAD) {
5193 // The high part is obtained by SRA'ing all but one of the bits of the
5194 // lo part.
5195 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5196 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5197 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5198 } else if (ExtType == ISD::ZEXTLOAD) {
5199 // The high part is just a zero.
5200 Hi = DAG.getConstant(0, NVT);
5201 } else /* if (ExtType == ISD::EXTLOAD) */ {
5202 // The high part is undefined.
5203 Hi = DAG.getNode(ISD::UNDEF, NVT);
5204 }
5205 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005206 break;
5207 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005208 case ISD::AND:
5209 case ISD::OR:
5210 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5211 SDOperand LL, LH, RL, RH;
5212 ExpandOp(Node->getOperand(0), LL, LH);
5213 ExpandOp(Node->getOperand(1), RL, RH);
5214 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5215 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5216 break;
5217 }
5218 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005219 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005220 ExpandOp(Node->getOperand(1), LL, LH);
5221 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005222 if (getTypeAction(NVT) == Expand)
5223 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005224 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005225 if (VT != MVT::f32)
5226 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005227 break;
5228 }
Nate Begeman9373a812005-08-10 20:51:12 +00005229 case ISD::SELECT_CC: {
5230 SDOperand TL, TH, FL, FH;
5231 ExpandOp(Node->getOperand(2), TL, TH);
5232 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005233 if (getTypeAction(NVT) == Expand)
5234 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005235 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5236 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005237 if (VT != MVT::f32)
5238 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5239 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005240 break;
5241 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005242 case ISD::ANY_EXTEND:
5243 // The low part is any extension of the input (which degenerates to a copy).
5244 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5245 // The high part is undefined.
5246 Hi = DAG.getNode(ISD::UNDEF, NVT);
5247 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005248 case ISD::SIGN_EXTEND: {
5249 // The low part is just a sign extension of the input (which degenerates to
5250 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005251 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005252
Chris Lattner3e928bb2005-01-07 07:47:09 +00005253 // The high part is obtained by SRA'ing all but one of the bits of the lo
5254 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005255 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005256 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5257 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005258 break;
5259 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005260 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005261 // The low part is just a zero extension of the input (which degenerates to
5262 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005263 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005264
Chris Lattner3e928bb2005-01-07 07:47:09 +00005265 // The high part is just a zero.
5266 Hi = DAG.getConstant(0, NVT);
5267 break;
Chris Lattner35481892005-12-23 00:16:34 +00005268
Chris Lattner4c948eb2007-02-13 23:55:16 +00005269 case ISD::TRUNCATE: {
5270 // The input value must be larger than this value. Expand *it*.
5271 SDOperand NewLo;
5272 ExpandOp(Node->getOperand(0), NewLo, Hi);
5273
5274 // The low part is now either the right size, or it is closer. If not the
5275 // right size, make an illegal truncate so we recursively expand it.
5276 if (NewLo.getValueType() != Node->getValueType(0))
5277 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5278 ExpandOp(NewLo, Lo, Hi);
5279 break;
5280 }
5281
Chris Lattner35481892005-12-23 00:16:34 +00005282 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005283 SDOperand Tmp;
5284 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5285 // If the target wants to, allow it to lower this itself.
5286 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5287 case Expand: assert(0 && "cannot expand FP!");
5288 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5289 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5290 }
5291 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5292 }
5293
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005294 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005295 if (VT == MVT::f32 || VT == MVT::f64) {
5296 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005297 if (getTypeAction(NVT) == Expand)
5298 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005299 break;
5300 }
5301
5302 // If source operand will be expanded to the same type as VT, i.e.
5303 // i64 <- f64, i32 <- f32, expand the source operand instead.
5304 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5305 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5306 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005307 break;
5308 }
5309
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005310 // Turn this into a load/store pair by default.
5311 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005312 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005313
Chris Lattner35481892005-12-23 00:16:34 +00005314 ExpandOp(Tmp, Lo, Hi);
5315 break;
5316 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005317
Chris Lattner8137c9e2006-01-28 05:07:51 +00005318 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005319 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5320 TargetLowering::Custom &&
5321 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005322 Lo = TLI.LowerOperation(Op, DAG);
5323 assert(Lo.Val && "Node must be custom expanded!");
5324 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005325 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005326 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005327 break;
5328
Chris Lattner4e6c7462005-01-08 19:27:05 +00005329 // These operators cannot be expanded directly, emit them as calls to
5330 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005331 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005332 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005333 SDOperand Op;
5334 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5335 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005336 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5337 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005338 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005339
Chris Lattnerf20d1832005-07-30 01:40:57 +00005340 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5341
Chris Lattner80a3e942005-07-29 00:33:32 +00005342 // Now that the custom expander is done, expand the result, which is still
5343 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005344 if (Op.Val) {
5345 ExpandOp(Op, Lo, Hi);
5346 break;
5347 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005348 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005349
Evan Cheng56966222007-01-12 02:11:51 +00005350 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005351 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005352 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005353 else
Evan Cheng56966222007-01-12 02:11:51 +00005354 LC = RTLIB::FPTOSINT_F64_I64;
5355 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5356 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005357 break;
Evan Cheng56966222007-01-12 02:11:51 +00005358 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005359
Evan Cheng56966222007-01-12 02:11:51 +00005360 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005361 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005362 SDOperand Op;
5363 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5364 case Expand: assert(0 && "cannot expand FP!");
5365 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5366 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5367 }
5368
5369 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5370
5371 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005372 if (Op.Val) {
5373 ExpandOp(Op, Lo, Hi);
5374 break;
5375 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005376 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005377
Evan Cheng56966222007-01-12 02:11:51 +00005378 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005379 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005380 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005381 else
Evan Cheng56966222007-01-12 02:11:51 +00005382 LC = RTLIB::FPTOUINT_F64_I64;
5383 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5384 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005385 break;
Evan Cheng56966222007-01-12 02:11:51 +00005386 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005387
Evan Cheng05a2d562006-01-09 18:31:59 +00005388 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005389 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005390 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005391 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005392 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005393 Op = TLI.LowerOperation(Op, DAG);
5394 if (Op.Val) {
5395 // Now that the custom expander is done, expand the result, which is
5396 // still VT.
5397 ExpandOp(Op, Lo, Hi);
5398 break;
5399 }
5400 }
5401
Chris Lattner79980b02006-09-13 03:50:39 +00005402 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5403 // this X << 1 as X+X.
5404 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5405 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5406 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5407 SDOperand LoOps[2], HiOps[3];
5408 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5409 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5410 LoOps[1] = LoOps[0];
5411 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5412
5413 HiOps[1] = HiOps[0];
5414 HiOps[2] = Lo.getValue(1);
5415 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5416 break;
5417 }
5418 }
5419
Chris Lattnere34b3962005-01-19 04:19:40 +00005420 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005421 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005422 break;
Chris Lattner47599822005-04-02 03:38:53 +00005423
5424 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005425 TargetLowering::LegalizeAction Action =
5426 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5427 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5428 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005429 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005430 break;
5431 }
5432
Chris Lattnere34b3962005-01-19 04:19:40 +00005433 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005434 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5435 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005436 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005437 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005438
Evan Cheng05a2d562006-01-09 18:31:59 +00005439 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005440 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005441 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005442 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005443 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005444 Op = TLI.LowerOperation(Op, DAG);
5445 if (Op.Val) {
5446 // Now that the custom expander is done, expand the result, which is
5447 // still VT.
5448 ExpandOp(Op, Lo, Hi);
5449 break;
5450 }
5451 }
5452
Chris Lattnere34b3962005-01-19 04:19:40 +00005453 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005454 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005455 break;
Chris Lattner47599822005-04-02 03:38:53 +00005456
5457 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005458 TargetLowering::LegalizeAction Action =
5459 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5460 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5461 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005462 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005463 break;
5464 }
5465
Chris Lattnere34b3962005-01-19 04:19:40 +00005466 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005467 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5468 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005469 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005470 }
5471
5472 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005473 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005474 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005475 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005476 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005477 Op = TLI.LowerOperation(Op, DAG);
5478 if (Op.Val) {
5479 // Now that the custom expander is done, expand the result, which is
5480 // still VT.
5481 ExpandOp(Op, Lo, Hi);
5482 break;
5483 }
5484 }
5485
Chris Lattnere34b3962005-01-19 04:19:40 +00005486 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005487 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005488 break;
Chris Lattner47599822005-04-02 03:38:53 +00005489
5490 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005491 TargetLowering::LegalizeAction Action =
5492 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5493 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5494 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005495 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005496 break;
5497 }
5498
Chris Lattnere34b3962005-01-19 04:19:40 +00005499 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005500 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5501 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005502 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005503 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005504
Misha Brukmanedf128a2005-04-21 22:36:52 +00005505 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005506 case ISD::SUB: {
5507 // If the target wants to custom expand this, let them.
5508 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5509 TargetLowering::Custom) {
5510 Op = TLI.LowerOperation(Op, DAG);
5511 if (Op.Val) {
5512 ExpandOp(Op, Lo, Hi);
5513 break;
5514 }
5515 }
5516
5517 // Expand the subcomponents.
5518 SDOperand LHSL, LHSH, RHSL, RHSH;
5519 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5520 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005521 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5522 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005523 LoOps[0] = LHSL;
5524 LoOps[1] = RHSL;
5525 HiOps[0] = LHSH;
5526 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005527 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005528 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005529 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005530 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005531 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005532 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005533 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005534 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005535 }
Chris Lattner84f67882005-01-20 18:52:28 +00005536 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005537 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005538
5539 case ISD::ADDC:
5540 case ISD::SUBC: {
5541 // Expand the subcomponents.
5542 SDOperand LHSL, LHSH, RHSL, RHSH;
5543 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5544 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5545 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5546 SDOperand LoOps[2] = { LHSL, RHSL };
5547 SDOperand HiOps[3] = { LHSH, RHSH };
5548
5549 if (Node->getOpcode() == ISD::ADDC) {
5550 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5551 HiOps[2] = Lo.getValue(1);
5552 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5553 } else {
5554 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5555 HiOps[2] = Lo.getValue(1);
5556 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5557 }
5558 // Remember that we legalized the flag.
5559 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5560 break;
5561 }
5562 case ISD::ADDE:
5563 case ISD::SUBE: {
5564 // Expand the subcomponents.
5565 SDOperand LHSL, LHSH, RHSL, RHSH;
5566 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5567 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5568 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5569 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5570 SDOperand HiOps[3] = { LHSH, RHSH };
5571
5572 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5573 HiOps[2] = Lo.getValue(1);
5574 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5575
5576 // Remember that we legalized the flag.
5577 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5578 break;
5579 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005580 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005581 // If the target wants to custom expand this, let them.
5582 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005583 SDOperand New = TLI.LowerOperation(Op, DAG);
5584 if (New.Val) {
5585 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005586 break;
5587 }
5588 }
5589
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005590 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5591 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005592 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005593 SDOperand LL, LH, RL, RH;
5594 ExpandOp(Node->getOperand(0), LL, LH);
5595 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005596 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005597 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005598 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5599 // extended the sign bit of the low half through the upper half, and if so
5600 // emit a MULHS instead of the alternate sequence that is valid for any
5601 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005602 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005603 // is RH an extension of the sign bit of RL?
5604 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5605 RH.getOperand(1).getOpcode() == ISD::Constant &&
5606 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5607 // is LH an extension of the sign bit of LL?
5608 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5609 LH.getOperand(1).getOpcode() == ISD::Constant &&
5610 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005611 // Low part:
5612 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5613 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005614 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005615 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005616 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005617 // Low part:
5618 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5619
5620 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005621 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5622 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5623 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5624 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5625 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005626 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005627 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005628 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005629
Evan Cheng56966222007-01-12 02:11:51 +00005630 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5631 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005632 break;
5633 }
Evan Cheng56966222007-01-12 02:11:51 +00005634 case ISD::SDIV:
5635 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5636 break;
5637 case ISD::UDIV:
5638 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5639 break;
5640 case ISD::SREM:
5641 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5642 break;
5643 case ISD::UREM:
5644 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5645 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005646
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005647 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005648 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5649 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5650 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005651 break;
5652 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005653 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5654 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5655 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005656 break;
5657 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005658 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5659 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5660 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005661 break;
5662 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005663 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5664 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5665 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005666 break;
5667 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005668 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005669 break;
5670 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005671 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005672 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005673 case ISD::FPOWI:
5674 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5675 ? RTLIB::POWI_F32 : RTLIB::POWI_F64),
5676 Node, false, Hi);
5677 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005678 case ISD::FSQRT:
5679 case ISD::FSIN:
5680 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005681 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005682 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005683 case ISD::FSQRT:
5684 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5685 break;
5686 case ISD::FSIN:
5687 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5688 break;
5689 case ISD::FCOS:
5690 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5691 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005692 default: assert(0 && "Unreachable!");
5693 }
Evan Cheng56966222007-01-12 02:11:51 +00005694 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005695 break;
5696 }
Evan Cheng966bf242006-12-16 00:52:40 +00005697 case ISD::FABS: {
5698 SDOperand Mask = (VT == MVT::f64)
5699 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5700 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5701 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5702 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5703 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5704 if (getTypeAction(NVT) == Expand)
5705 ExpandOp(Lo, Lo, Hi);
5706 break;
5707 }
5708 case ISD::FNEG: {
5709 SDOperand Mask = (VT == MVT::f64)
5710 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5711 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5712 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5713 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5714 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5715 if (getTypeAction(NVT) == Expand)
5716 ExpandOp(Lo, Lo, Hi);
5717 break;
5718 }
Evan Cheng912095b2007-01-04 21:56:39 +00005719 case ISD::FCOPYSIGN: {
5720 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5721 if (getTypeAction(NVT) == Expand)
5722 ExpandOp(Lo, Lo, Hi);
5723 break;
5724 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005725 case ISD::SINT_TO_FP:
5726 case ISD::UINT_TO_FP: {
5727 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5728 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005729 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005730 if (Node->getOperand(0).getValueType() == MVT::i64) {
5731 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005732 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005733 else
Evan Cheng56966222007-01-12 02:11:51 +00005734 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005735 } else {
5736 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005737 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005738 else
Evan Cheng56966222007-01-12 02:11:51 +00005739 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005740 }
5741
5742 // Promote the operand if needed.
5743 if (getTypeAction(SrcVT) == Promote) {
5744 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5745 Tmp = isSigned
5746 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5747 DAG.getValueType(SrcVT))
5748 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5749 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5750 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005751
5752 const char *LibCall = TLI.getLibcallName(LC);
5753 if (LibCall)
5754 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5755 else {
5756 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5757 Node->getOperand(0));
5758 if (getTypeAction(Lo.getValueType()) == Expand)
5759 ExpandOp(Lo, Lo, Hi);
5760 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005761 break;
5762 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005763 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005764
Chris Lattner83397362005-12-21 18:02:52 +00005765 // Make sure the resultant values have been legalized themselves, unless this
5766 // is a type that requires multi-step expansion.
5767 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5768 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005769 if (Hi.Val)
5770 // Don't legalize the high part if it is expanded to a single node.
5771 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005772 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005773
5774 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005775 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005776 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005777}
5778
Dan Gohman7f321562007-06-25 16:23:39 +00005779/// SplitVectorOp - Given an operand of vector type, break it down into
5780/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005781void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5782 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005783 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005784 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005785 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattnerc7029802006-03-18 01:44:44 +00005786 assert(NumElements > 1 && "Cannot split a single element vector!");
5787 unsigned NewNumElts = NumElements/2;
Dan Gohman7f321562007-06-25 16:23:39 +00005788 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5789 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005790
5791 // See if we already split it.
5792 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5793 = SplitNodes.find(Op);
5794 if (I != SplitNodes.end()) {
5795 Lo = I->second.first;
5796 Hi = I->second.second;
5797 return;
5798 }
5799
5800 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005801 default:
5802#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005803 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005804#endif
5805 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005806 case ISD::BUILD_PAIR:
5807 Lo = Node->getOperand(0);
5808 Hi = Node->getOperand(1);
5809 break;
5810 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005811 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5812 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005813 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005814
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005815 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005816 Node->op_end());
5817 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005818 break;
5819 }
Dan Gohman7f321562007-06-25 16:23:39 +00005820 case ISD::CONCAT_VECTORS: {
5821 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5822 if (NewNumSubvectors == 1) {
5823 Lo = Node->getOperand(0);
5824 Hi = Node->getOperand(1);
5825 } else {
5826 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5827 Node->op_begin()+NewNumSubvectors);
5828 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005829
Dan Gohman7f321562007-06-25 16:23:39 +00005830 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5831 Node->op_end());
5832 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5833 }
Dan Gohman65956352007-06-13 15:12:02 +00005834 break;
5835 }
Dan Gohman7f321562007-06-25 16:23:39 +00005836 case ISD::ADD:
5837 case ISD::SUB:
5838 case ISD::MUL:
5839 case ISD::FADD:
5840 case ISD::FSUB:
5841 case ISD::FMUL:
5842 case ISD::SDIV:
5843 case ISD::UDIV:
5844 case ISD::FDIV:
5845 case ISD::AND:
5846 case ISD::OR:
5847 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005848 SDOperand LL, LH, RL, RH;
5849 SplitVectorOp(Node->getOperand(0), LL, LH);
5850 SplitVectorOp(Node->getOperand(1), RL, RH);
5851
Dan Gohman7f321562007-06-25 16:23:39 +00005852 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5853 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005854 break;
5855 }
Dan Gohman7f321562007-06-25 16:23:39 +00005856 case ISD::LOAD: {
5857 LoadSDNode *LD = cast<LoadSDNode>(Node);
5858 SDOperand Ch = LD->getChain();
5859 SDOperand Ptr = LD->getBasePtr();
5860 const Value *SV = LD->getSrcValue();
5861 int SVOffset = LD->getSrcValueOffset();
5862 unsigned Alignment = LD->getAlignment();
5863 bool isVolatile = LD->isVolatile();
5864
5865 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5866 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005867 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5868 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005869 SVOffset += IncrementSize;
5870 if (Alignment > IncrementSize)
5871 Alignment = IncrementSize;
5872 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005873
5874 // Build a factor node to remember that this load is independent of the
5875 // other one.
5876 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5877 Hi.getValue(1));
5878
5879 // Remember that we legalized the chain.
5880 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005881 break;
5882 }
Dan Gohman7f321562007-06-25 16:23:39 +00005883 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005884 // We know the result is a vector. The input may be either a vector or a
5885 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005886 SDOperand InOp = Node->getOperand(0);
5887 if (!MVT::isVector(InOp.getValueType()) ||
5888 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5889 // The input is a scalar or single-element vector.
5890 // Lower to a store/load so that it can be split.
5891 // FIXME: this could be improved probably.
5892 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005893
Evan Cheng786225a2006-10-05 23:01:46 +00005894 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005895 InOp, Ptr, NULL, 0);
5896 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005897 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005898 // Split the vector and convert each of the pieces now.
5899 SplitVectorOp(InOp, Lo, Hi);
5900 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5901 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5902 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005903 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005904 }
5905
5906 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005907 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005908 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005909 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005910}
5911
5912
Dan Gohman89b20c02007-06-27 14:06:22 +00005913/// ScalarizeVectorOp - Given an operand of single-element vector type
5914/// (e.g. v1f32), convert it into the equivalent operation that returns a
5915/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005916SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5917 assert(MVT::isVector(Op.getValueType()) &&
5918 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005919 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005920 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5921 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005922
Dan Gohman7f321562007-06-25 16:23:39 +00005923 // See if we already scalarized it.
5924 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5925 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005926
5927 SDOperand Result;
5928 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005929 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005930#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005931 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005932#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005933 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5934 case ISD::ADD:
5935 case ISD::FADD:
5936 case ISD::SUB:
5937 case ISD::FSUB:
5938 case ISD::MUL:
5939 case ISD::FMUL:
5940 case ISD::SDIV:
5941 case ISD::UDIV:
5942 case ISD::FDIV:
5943 case ISD::SREM:
5944 case ISD::UREM:
5945 case ISD::FREM:
5946 case ISD::AND:
5947 case ISD::OR:
5948 case ISD::XOR:
5949 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005950 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005951 ScalarizeVectorOp(Node->getOperand(0)),
5952 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005953 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005954 case ISD::FNEG:
5955 case ISD::FABS:
5956 case ISD::FSQRT:
5957 case ISD::FSIN:
5958 case ISD::FCOS:
5959 Result = DAG.getNode(Node->getOpcode(),
5960 NewVT,
5961 ScalarizeVectorOp(Node->getOperand(0)));
5962 break;
5963 case ISD::LOAD: {
5964 LoadSDNode *LD = cast<LoadSDNode>(Node);
5965 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5966 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005967
Dan Gohman7f321562007-06-25 16:23:39 +00005968 const Value *SV = LD->getSrcValue();
5969 int SVOffset = LD->getSrcValueOffset();
5970 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5971 LD->isVolatile(), LD->getAlignment());
5972
Chris Lattnerc7029802006-03-18 01:44:44 +00005973 // Remember that we legalized the chain.
5974 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5975 break;
5976 }
Dan Gohman7f321562007-06-25 16:23:39 +00005977 case ISD::BUILD_VECTOR:
5978 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00005979 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005980 case ISD::INSERT_VECTOR_ELT:
5981 // Returning the inserted scalar element.
5982 Result = Node->getOperand(1);
5983 break;
5984 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00005985 assert(Node->getOperand(0).getValueType() == NewVT &&
5986 "Concat of non-legal vectors not yet supported!");
5987 Result = Node->getOperand(0);
5988 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005989 case ISD::VECTOR_SHUFFLE: {
5990 // Figure out if the scalar is the LHS or RHS and return it.
5991 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5992 if (cast<ConstantSDNode>(EltNum)->getValue())
5993 Result = ScalarizeVectorOp(Node->getOperand(1));
5994 else
5995 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00005996 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005997 }
5998 case ISD::EXTRACT_SUBVECTOR:
5999 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006000 assert(Result.getValueType() == NewVT);
6001 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006002 case ISD::BIT_CONVERT:
6003 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006004 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006005 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006006 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006007 ScalarizeVectorOp(Op.getOperand(1)),
6008 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006009 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006010 }
6011
6012 if (TLI.isTypeLegal(NewVT))
6013 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006014 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6015 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006016 return Result;
6017}
6018
Chris Lattner3e928bb2005-01-07 07:47:09 +00006019
6020// SelectionDAG::Legalize - This is the entry point for the file.
6021//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006022void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006023 if (ViewLegalizeDAGs) viewGraph();
6024
Chris Lattner3e928bb2005-01-07 07:47:09 +00006025 /// run - This is the main entry point to this class.
6026 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006027 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006028}
6029