blob: b5c160a1a77df1af00de132e302b01b554f3b198 [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;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000489 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000490 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000491 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000492 if (VT!=MVT::f64 && VT!=MVT::f32)
493 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000494 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
495 isDouble ? MVT::i64 : 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.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000500 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000501 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
502 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
503 VT = MVT::f32;
504 Extend = true;
505 }
506
507 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
508 if (Extend) {
509 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
510 CPIdx, NULL, 0, MVT::f32);
511 } else {
512 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
513 }
514}
515
Chris Lattner6831a812006-02-13 09:18:02 +0000516
Evan Cheng912095b2007-01-04 21:56:39 +0000517/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
518/// operations.
519static
520SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
521 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000522 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000523 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000524 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
525 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000526 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000527
Evan Cheng912095b2007-01-04 21:56:39 +0000528 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000529 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000530 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
531 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000532 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000533 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000534 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000535 // Shift right or sign-extend it if the two operands have different types.
536 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
537 if (SizeDiff > 0) {
538 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
539 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
540 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
541 } else if (SizeDiff < 0)
542 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000543
544 // Clear the sign bit of first operand.
545 SDOperand Mask2 = (VT == MVT::f64)
546 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
547 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
548 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000549 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000550 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
551
552 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000553 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
554 return Result;
555}
556
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000557/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
558static
559SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
560 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000561 SDOperand Chain = ST->getChain();
562 SDOperand Ptr = ST->getBasePtr();
563 SDOperand Val = ST->getValue();
564 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000565 int Alignment = ST->getAlignment();
566 int SVOffset = ST->getSrcValueOffset();
567 if (MVT::isFloatingPoint(ST->getStoredVT())) {
568 // Expand to a bitconvert of the value to the integer type of the
569 // same size, then a (misaligned) int store.
570 MVT::ValueType intVT;
571 if (VT==MVT::f64)
572 intVT = MVT::i64;
573 else if (VT==MVT::f32)
574 intVT = MVT::i32;
575 else
576 assert(0 && "Unaligned load of unsupported floating point type");
577
578 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
579 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
580 SVOffset, ST->isVolatile(), Alignment);
581 }
582 assert(MVT::isInteger(ST->getStoredVT()) &&
583 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000584 // Get the half-size VT
585 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
586 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000587 int IncrementSize = NumBits / 8;
588
589 // Divide the stored value in two parts.
590 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
591 SDOperand Lo = Val;
592 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
593
594 // Store the two parts
595 SDOperand Store1, Store2;
596 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
597 ST->getSrcValue(), SVOffset, NewStoredVT,
598 ST->isVolatile(), Alignment);
599 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
600 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
601 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
602 ST->getSrcValue(), SVOffset + IncrementSize,
603 NewStoredVT, ST->isVolatile(), Alignment);
604
605 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
606}
607
608/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
609static
610SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
611 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000612 int SVOffset = LD->getSrcValueOffset();
613 SDOperand Chain = LD->getChain();
614 SDOperand Ptr = LD->getBasePtr();
615 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000616 MVT::ValueType LoadedVT = LD->getLoadedVT();
617 if (MVT::isFloatingPoint(VT)) {
618 // Expand to a (misaligned) integer load of the same size,
619 // then bitconvert to floating point.
620 MVT::ValueType intVT;
621 if (LoadedVT==MVT::f64)
622 intVT = MVT::i64;
623 else if (LoadedVT==MVT::f32)
624 intVT = MVT::i32;
625 else
626 assert(0 && "Unaligned load of unsupported floating point type");
627
628 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
629 SVOffset, LD->isVolatile(),
630 LD->getAlignment());
631 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
632 if (LoadedVT != VT)
633 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
634
635 SDOperand Ops[] = { Result, Chain };
636 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
637 Ops, 2);
638 }
639 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
640 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000641 int NumBits = MVT::getSizeInBits(NewLoadedVT);
642 int Alignment = LD->getAlignment();
643 int IncrementSize = NumBits / 8;
644 ISD::LoadExtType HiExtType = LD->getExtensionType();
645
646 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
647 if (HiExtType == ISD::NON_EXTLOAD)
648 HiExtType = ISD::ZEXTLOAD;
649
650 // Load the value in two parts
651 SDOperand Lo, Hi;
652 if (TLI.isLittleEndian()) {
653 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
654 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
655 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
656 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
657 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
658 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
659 Alignment);
660 } else {
661 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
662 NewLoadedVT,LD->isVolatile(), Alignment);
663 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
664 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
665 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
666 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
667 Alignment);
668 }
669
670 // aggregate the two parts
671 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
672 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
673 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
674
675 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
676 Hi.getValue(1));
677
678 SDOperand Ops[] = { Result, TF };
679 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
680}
Evan Cheng912095b2007-01-04 21:56:39 +0000681
Dan Gohmana3466152007-07-13 20:14:11 +0000682/// LegalizeOp - We know that the specified value has a legal type, and
683/// that its operands are legal. Now ensure that the operation itself
684/// is legal, recursively ensuring that the operands' operations remain
685/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000686SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000687 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
688 return Op;
689
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000690 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000691 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000692 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000693
Chris Lattner3e928bb2005-01-07 07:47:09 +0000694 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000695 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000696 if (Node->getNumValues() > 1) {
697 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000698 if (getTypeAction(Node->getValueType(i)) != Legal) {
699 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000700 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000701 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000702 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000703 }
704 }
705
Chris Lattner45982da2005-05-12 16:53:42 +0000706 // Note that LegalizeOp may be reentered even from single-use nodes, which
707 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000708 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000709 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000710
Nate Begeman9373a812005-08-10 20:51:12 +0000711 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000712 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000713 bool isCustom = false;
714
Chris Lattner3e928bb2005-01-07 07:47:09 +0000715 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000716 case ISD::FrameIndex:
717 case ISD::EntryToken:
718 case ISD::Register:
719 case ISD::BasicBlock:
720 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000721 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000722 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000723 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000724 case ISD::TargetConstantPool:
725 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000726 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000727 case ISD::TargetExternalSymbol:
728 case ISD::VALUETYPE:
729 case ISD::SRCVALUE:
730 case ISD::STRING:
731 case ISD::CONDCODE:
732 // Primitives must all be legal.
733 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
734 "This must be legal!");
735 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000736 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000737 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
738 // If this is a target node, legalize it by legalizing the operands then
739 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000740 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000741 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000742 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000743
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000744 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000745
746 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
747 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
748 return Result.getValue(Op.ResNo);
749 }
750 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000751#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000752 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000753#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000754 assert(0 && "Do not know how to legalize this operator!");
755 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000756 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000757 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000758 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000759 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000760 case ISD::ConstantPool:
761 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000762 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
763 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000764 case TargetLowering::Custom:
765 Tmp1 = TLI.LowerOperation(Op, DAG);
766 if (Tmp1.Val) Result = Tmp1;
767 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000768 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000769 break;
770 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000771 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000772 case ISD::FRAMEADDR:
773 case ISD::RETURNADDR:
774 // The only option for these nodes is to custom lower them. If the target
775 // does not custom lower them, then return zero.
776 Tmp1 = TLI.LowerOperation(Op, DAG);
777 if (Tmp1.Val)
778 Result = Tmp1;
779 else
780 Result = DAG.getConstant(0, TLI.getPointerTy());
781 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000782 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000783 MVT::ValueType VT = Node->getValueType(0);
784 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
785 default: assert(0 && "This action is not supported yet!");
786 case TargetLowering::Custom:
787 Result = TLI.LowerOperation(Op, DAG);
788 if (Result.Val) break;
789 // Fall Thru
790 case TargetLowering::Legal:
791 Result = DAG.getConstant(0, VT);
792 break;
793 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000794 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000795 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000796 case ISD::EXCEPTIONADDR: {
797 Tmp1 = LegalizeOp(Node->getOperand(0));
798 MVT::ValueType VT = Node->getValueType(0);
799 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
800 default: assert(0 && "This action is not supported yet!");
801 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000802 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000803 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
804 }
805 break;
806 case TargetLowering::Custom:
807 Result = TLI.LowerOperation(Op, DAG);
808 if (Result.Val) break;
809 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000810 case TargetLowering::Legal: {
811 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
812 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
813 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000814 break;
815 }
816 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000817 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000818 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000819 case ISD::EHSELECTION: {
820 Tmp1 = LegalizeOp(Node->getOperand(0));
821 Tmp2 = LegalizeOp(Node->getOperand(1));
822 MVT::ValueType VT = Node->getValueType(0);
823 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
824 default: assert(0 && "This action is not supported yet!");
825 case TargetLowering::Expand: {
826 unsigned Reg = TLI.getExceptionSelectorRegister();
827 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
828 }
829 break;
830 case TargetLowering::Custom:
831 Result = TLI.LowerOperation(Op, DAG);
832 if (Result.Val) break;
833 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000834 case TargetLowering::Legal: {
835 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
836 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
837 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000838 break;
839 }
840 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000841 }
Jim Laskey8782d482007-02-28 20:43:58 +0000842 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000843 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000844 MVT::ValueType VT = Node->getValueType(0);
845 // The only "good" option for this node is to custom lower it.
846 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
847 default: assert(0 && "This action is not supported at all!");
848 case TargetLowering::Custom:
849 Result = TLI.LowerOperation(Op, DAG);
850 if (Result.Val) break;
851 // Fall Thru
852 case TargetLowering::Legal:
853 // Target does not know, how to lower this, lower to noop
854 Result = LegalizeOp(Node->getOperand(0));
855 break;
856 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000857 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000858 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000859 case ISD::AssertSext:
860 case ISD::AssertZext:
861 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000862 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000863 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000864 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000865 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000866 Result = Node->getOperand(Op.ResNo);
867 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000868 case ISD::CopyFromReg:
869 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000870 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000871 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000873 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000874 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000875 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000876 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
878 } else {
879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
880 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000881 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
882 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000883 // Since CopyFromReg produces two values, make sure to remember that we
884 // legalized both of them.
885 AddLegalizedOperand(Op.getValue(0), Result);
886 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
887 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000888 case ISD::UNDEF: {
889 MVT::ValueType VT = Op.getValueType();
890 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000891 default: assert(0 && "This action is not supported yet!");
892 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000893 if (MVT::isInteger(VT))
894 Result = DAG.getConstant(0, VT);
895 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000896 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
897 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000898 else
899 assert(0 && "Unknown value type!");
900 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000901 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000902 break;
903 }
904 break;
905 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000906
Chris Lattner48b61a72006-03-28 00:40:33 +0000907 case ISD::INTRINSIC_W_CHAIN:
908 case ISD::INTRINSIC_WO_CHAIN:
909 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000910 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000911 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
912 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000913 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000914
915 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000916 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000917 TargetLowering::Custom) {
918 Tmp3 = TLI.LowerOperation(Result, DAG);
919 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000920 }
921
922 if (Result.Val->getNumValues() == 1) break;
923
924 // Must have return value and chain result.
925 assert(Result.Val->getNumValues() == 2 &&
926 "Cannot return more than two values!");
927
928 // Since loads produce two values, make sure to remember that we
929 // legalized both of them.
930 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
931 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
932 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000933 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000934
935 case ISD::LOCATION:
936 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
937 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
938
939 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
940 case TargetLowering::Promote:
941 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000942 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000943 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000944 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000945 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000946
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000947 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000948 const std::string &FName =
949 cast<StringSDNode>(Node->getOperand(3))->getValue();
950 const std::string &DirName =
951 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000952 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000953
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000954 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000955 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000956 SDOperand LineOp = Node->getOperand(1);
957 SDOperand ColOp = Node->getOperand(2);
958
959 if (useDEBUG_LOC) {
960 Ops.push_back(LineOp); // line #
961 Ops.push_back(ColOp); // col #
962 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000963 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000964 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000965 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
966 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000967 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000968 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000969 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000970 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000971 } else {
972 Result = Tmp1; // chain
973 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000974 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000975 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000976 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000977 if (Tmp1 != Node->getOperand(0) ||
978 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000979 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000980 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000981 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
982 Ops.push_back(Node->getOperand(1)); // line # must be legal.
983 Ops.push_back(Node->getOperand(2)); // col # must be legal.
984 } else {
985 // Otherwise promote them.
986 Ops.push_back(PromoteOp(Node->getOperand(1)));
987 Ops.push_back(PromoteOp(Node->getOperand(2)));
988 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000989 Ops.push_back(Node->getOperand(3)); // filename must be legal.
990 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000991 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000992 }
993 break;
994 }
995 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000996
997 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000998 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000999 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001000 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001001 case TargetLowering::Legal:
1002 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1003 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1004 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1005 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001007 break;
1008 }
1009 break;
1010
Jim Laskey1ee29252007-01-26 14:34:52 +00001011 case ISD::LABEL:
1012 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1013 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001014 default: assert(0 && "This action is not supported yet!");
1015 case TargetLowering::Legal:
1016 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1017 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001018 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001019 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001020 case TargetLowering::Expand:
1021 Result = LegalizeOp(Node->getOperand(0));
1022 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001023 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001024 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001025
Scott Michelc1513d22007-08-08 23:23:31 +00001026 case ISD::Constant: {
1027 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1028 unsigned opAction =
1029 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1030
Chris Lattner3e928bb2005-01-07 07:47:09 +00001031 // We know we don't need to expand constants here, constants only have one
1032 // value and we check that it is fine above.
1033
Scott Michelc1513d22007-08-08 23:23:31 +00001034 if (opAction == TargetLowering::Custom) {
1035 Tmp1 = TLI.LowerOperation(Result, DAG);
1036 if (Tmp1.Val)
1037 Result = Tmp1;
1038 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001039 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001040 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001041 case ISD::ConstantFP: {
1042 // Spill FP immediates to the constant pool if the target cannot directly
1043 // codegen them. Targets often have some immediate values that can be
1044 // efficiently generated into an FP register without a load. We explicitly
1045 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001046 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1047
1048 // Check to see if this FP immediate is already legal.
1049 bool isLegal = false;
1050 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1051 E = TLI.legal_fpimm_end(); I != E; ++I)
1052 if (CFP->isExactlyValue(*I)) {
1053 isLegal = true;
1054 break;
1055 }
1056
Chris Lattner3181a772006-01-29 06:26:56 +00001057 // If this is a legal constant, turn it into a TargetConstantFP node.
1058 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001059 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1060 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001061 break;
1062 }
1063
1064 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1065 default: assert(0 && "This action is not supported yet!");
1066 case TargetLowering::Custom:
1067 Tmp3 = TLI.LowerOperation(Result, DAG);
1068 if (Tmp3.Val) {
1069 Result = Tmp3;
1070 break;
1071 }
1072 // FALLTHROUGH
1073 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001074 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001075 }
1076 break;
1077 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001078 case ISD::TokenFactor:
1079 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001080 Tmp1 = LegalizeOp(Node->getOperand(0));
1081 Tmp2 = LegalizeOp(Node->getOperand(1));
1082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1083 } else if (Node->getNumOperands() == 3) {
1084 Tmp1 = LegalizeOp(Node->getOperand(0));
1085 Tmp2 = LegalizeOp(Node->getOperand(1));
1086 Tmp3 = LegalizeOp(Node->getOperand(2));
1087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001088 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001089 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001090 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001091 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1092 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001093 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001094 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001095 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001096
1097 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001098 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001099 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001100 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1101 assert(Tmp3.Val && "Target didn't custom lower this node!");
1102 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1103 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001104
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001105 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001106 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001107 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1108 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001109 if (Op.ResNo == i)
1110 Tmp2 = Tmp1;
1111 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1112 }
1113 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001114 case ISD::EXTRACT_SUBREG: {
1115 Tmp1 = LegalizeOp(Node->getOperand(0));
1116 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1117 assert(idx && "Operand must be a constant");
1118 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1119 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1120 }
1121 break;
1122 case ISD::INSERT_SUBREG: {
1123 Tmp1 = LegalizeOp(Node->getOperand(0));
1124 Tmp2 = LegalizeOp(Node->getOperand(1));
1125 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1126 assert(idx && "Operand must be a constant");
1127 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1129 }
1130 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001131 case ISD::BUILD_VECTOR:
1132 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001133 default: assert(0 && "This action is not supported yet!");
1134 case TargetLowering::Custom:
1135 Tmp3 = TLI.LowerOperation(Result, DAG);
1136 if (Tmp3.Val) {
1137 Result = Tmp3;
1138 break;
1139 }
1140 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001141 case TargetLowering::Expand:
1142 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001143 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001144 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001145 break;
1146 case ISD::INSERT_VECTOR_ELT:
1147 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1148 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1149 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1151
1152 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1153 Node->getValueType(0))) {
1154 default: assert(0 && "This action is not supported yet!");
1155 case TargetLowering::Legal:
1156 break;
1157 case TargetLowering::Custom:
1158 Tmp3 = TLI.LowerOperation(Result, DAG);
1159 if (Tmp3.Val) {
1160 Result = Tmp3;
1161 break;
1162 }
1163 // FALLTHROUGH
1164 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001165 // If the insert index is a constant, codegen this as a scalar_to_vector,
1166 // then a shuffle that inserts it into the right position in the vector.
1167 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1168 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1169 Tmp1.getValueType(), Tmp2);
1170
1171 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1172 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001173 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001174
1175 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1176 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1177 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001178 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001179 for (unsigned i = 0; i != NumElts; ++i) {
1180 if (i != InsertPos->getValue())
1181 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1182 else
1183 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1184 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001185 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1186 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001187
1188 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1189 Tmp1, ScVec, ShufMask);
1190 Result = LegalizeOp(Result);
1191 break;
1192 }
1193
Chris Lattner2332b9f2006-03-19 01:17:20 +00001194 // If the target doesn't support this, we have to spill the input vector
1195 // to a temporary stack slot, update the element, then reload it. This is
1196 // badness. We could also load the value into a vector register (either
1197 // with a "move to register" or "extload into register" instruction, then
1198 // permute it into place, if the idx is a constant and if the idx is
1199 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001200 MVT::ValueType VT = Tmp1.getValueType();
1201 MVT::ValueType EltVT = Tmp2.getValueType();
1202 MVT::ValueType IdxVT = Tmp3.getValueType();
1203 MVT::ValueType PtrVT = TLI.getPointerTy();
1204 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001205 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001206 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001207
1208 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001209 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1210 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001211 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001212 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1213 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1214 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001215 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001216 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001217 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001218 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001219 break;
1220 }
1221 }
1222 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001223 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001224 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1225 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1226 break;
1227 }
1228
Chris Lattnerce872152006-03-19 06:31:19 +00001229 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1230 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1231 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1232 Node->getValueType(0))) {
1233 default: assert(0 && "This action is not supported yet!");
1234 case TargetLowering::Legal:
1235 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001236 case TargetLowering::Custom:
1237 Tmp3 = TLI.LowerOperation(Result, DAG);
1238 if (Tmp3.Val) {
1239 Result = Tmp3;
1240 break;
1241 }
1242 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001243 case TargetLowering::Expand:
1244 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001245 break;
1246 }
Chris Lattnerce872152006-03-19 06:31:19 +00001247 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001248 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001249 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1250 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1251 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1252
1253 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001254 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1255 default: assert(0 && "Unknown operation action!");
1256 case TargetLowering::Legal:
1257 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1258 "vector shuffle should not be created if not legal!");
1259 break;
1260 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001261 Tmp3 = TLI.LowerOperation(Result, DAG);
1262 if (Tmp3.Val) {
1263 Result = Tmp3;
1264 break;
1265 }
1266 // FALLTHROUGH
1267 case TargetLowering::Expand: {
1268 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001269 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001270 MVT::ValueType PtrVT = TLI.getPointerTy();
1271 SDOperand Mask = Node->getOperand(2);
1272 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001273 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001274 for (unsigned i = 0; i != NumElems; ++i) {
1275 SDOperand Arg = Mask.getOperand(i);
1276 if (Arg.getOpcode() == ISD::UNDEF) {
1277 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1278 } else {
1279 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1280 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1281 if (Idx < NumElems)
1282 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1283 DAG.getConstant(Idx, PtrVT)));
1284 else
1285 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1286 DAG.getConstant(Idx - NumElems, PtrVT)));
1287 }
1288 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001289 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001290 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001291 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001292 case TargetLowering::Promote: {
1293 // Change base type to a different vector type.
1294 MVT::ValueType OVT = Node->getValueType(0);
1295 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1296
1297 // Cast the two input vectors.
1298 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1299 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1300
1301 // Convert the shuffle mask to the right # elements.
1302 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1303 assert(Tmp3.Val && "Shuffle not legal?");
1304 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1305 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1306 break;
1307 }
Chris Lattner87100e02006-03-20 01:52:29 +00001308 }
1309 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001310
1311 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001312 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001313 Tmp2 = LegalizeOp(Node->getOperand(1));
1314 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001315 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001316 break;
1317
Dan Gohman7f321562007-06-25 16:23:39 +00001318 case ISD::EXTRACT_SUBVECTOR:
1319 Tmp1 = Node->getOperand(0);
1320 Tmp2 = LegalizeOp(Node->getOperand(1));
1321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1322 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001323 break;
1324
Chris Lattner6831a812006-02-13 09:18:02 +00001325 case ISD::CALLSEQ_START: {
1326 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1327
1328 // Recursively Legalize all of the inputs of the call end that do not lead
1329 // to this call start. This ensures that any libcalls that need be inserted
1330 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001331 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001332 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001333 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1334 NodesLeadingTo);
1335 }
Chris Lattner6831a812006-02-13 09:18:02 +00001336
1337 // Now that we legalized all of the inputs (which may have inserted
1338 // libcalls) create the new CALLSEQ_START node.
1339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1340
1341 // Merge in the last call, to ensure that this call start after the last
1342 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001343 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001344 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1345 Tmp1 = LegalizeOp(Tmp1);
1346 }
Chris Lattner6831a812006-02-13 09:18:02 +00001347
1348 // Do not try to legalize the target-specific arguments (#1+).
1349 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001350 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001351 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001352 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001353 }
1354
1355 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001356 AddLegalizedOperand(Op.getValue(0), Result);
1357 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1358 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1359
Chris Lattner6831a812006-02-13 09:18:02 +00001360 // Now that the callseq_start and all of the non-call nodes above this call
1361 // sequence have been legalized, legalize the call itself. During this
1362 // process, no libcalls can/will be inserted, guaranteeing that no calls
1363 // can overlap.
1364 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1365 SDOperand InCallSEQ = LastCALLSEQ_END;
1366 // Note that we are selecting this call!
1367 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1368 IsLegalizingCall = true;
1369
1370 // Legalize the call, starting from the CALLSEQ_END.
1371 LegalizeOp(LastCALLSEQ_END);
1372 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1373 return Result;
1374 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001375 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001376 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1377 // will cause this node to be legalized as well as handling libcalls right.
1378 if (LastCALLSEQ_END.Val != Node) {
1379 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001380 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001381 assert(I != LegalizedNodes.end() &&
1382 "Legalizing the call start should have legalized this node!");
1383 return I->second;
1384 }
1385
1386 // Otherwise, the call start has been legalized and everything is going
1387 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001388 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001389 // Do not try to legalize the target-specific arguments (#1+), except for
1390 // an optional flag input.
1391 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1392 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001393 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001394 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001395 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001396 }
1397 } else {
1398 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1399 if (Tmp1 != Node->getOperand(0) ||
1400 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001401 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001402 Ops[0] = Tmp1;
1403 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001404 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001405 }
Chris Lattner6a542892006-01-24 05:48:21 +00001406 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001407 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001408 // This finishes up call legalization.
1409 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001410
1411 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1412 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1413 if (Node->getNumValues() == 2)
1414 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1415 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001416 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001417 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001418 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1419 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1420 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001422
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001423 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001424 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001425 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001426 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001427 case TargetLowering::Expand: {
1428 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1429 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1430 " not tell us which reg is the stack pointer!");
1431 SDOperand Chain = Tmp1.getOperand(0);
1432 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001433 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1434 Chain = SP.getValue(1);
1435 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1436 unsigned StackAlign =
1437 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1438 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001439 SP = DAG.getNode(ISD::AND, VT, SP,
1440 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001441 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1442 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001443 Tmp1 = LegalizeOp(Tmp1);
1444 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001445 break;
1446 }
1447 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001448 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1449 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001450 Tmp1 = LegalizeOp(Tmp3);
1451 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001452 }
Chris Lattner903d2782006-01-15 08:54:32 +00001453 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001454 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001455 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001456 }
Chris Lattner903d2782006-01-15 08:54:32 +00001457 // Since this op produce two values, make sure to remember that we
1458 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001459 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1460 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001461 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001462 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001463 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001464 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001465 bool Changed = false;
1466 // Legalize all of the operands of the inline asm, in case they are nodes
1467 // that need to be expanded or something. Note we skip the asm string and
1468 // all of the TargetConstant flags.
1469 SDOperand Op = LegalizeOp(Ops[0]);
1470 Changed = Op != Ops[0];
1471 Ops[0] = Op;
1472
1473 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1474 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1475 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1476 for (++i; NumVals; ++i, --NumVals) {
1477 SDOperand Op = LegalizeOp(Ops[i]);
1478 if (Op != Ops[i]) {
1479 Changed = true;
1480 Ops[i] = Op;
1481 }
1482 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001483 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001484
1485 if (HasInFlag) {
1486 Op = LegalizeOp(Ops.back());
1487 Changed |= Op != Ops.back();
1488 Ops.back() = Op;
1489 }
1490
1491 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001492 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001493
1494 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001495 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001496 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1497 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001498 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001499 case ISD::BR:
1500 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001501 // Ensure that libcalls are emitted before a branch.
1502 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1503 Tmp1 = LegalizeOp(Tmp1);
1504 LastCALLSEQ_END = DAG.getEntryNode();
1505
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001506 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001507 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001508 case ISD::BRIND:
1509 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1510 // Ensure that libcalls are emitted before a branch.
1511 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1512 Tmp1 = LegalizeOp(Tmp1);
1513 LastCALLSEQ_END = DAG.getEntryNode();
1514
1515 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1516 default: assert(0 && "Indirect target must be legal type (pointer)!");
1517 case Legal:
1518 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1519 break;
1520 }
1521 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1522 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001523 case ISD::BR_JT:
1524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1525 // Ensure that libcalls are emitted before a branch.
1526 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1527 Tmp1 = LegalizeOp(Tmp1);
1528 LastCALLSEQ_END = DAG.getEntryNode();
1529
1530 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1531 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1532
1533 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1534 default: assert(0 && "This action is not supported yet!");
1535 case TargetLowering::Legal: break;
1536 case TargetLowering::Custom:
1537 Tmp1 = TLI.LowerOperation(Result, DAG);
1538 if (Tmp1.Val) Result = Tmp1;
1539 break;
1540 case TargetLowering::Expand: {
1541 SDOperand Chain = Result.getOperand(0);
1542 SDOperand Table = Result.getOperand(1);
1543 SDOperand Index = Result.getOperand(2);
1544
1545 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001546 MachineFunction &MF = DAG.getMachineFunction();
1547 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001548 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1549 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001550
1551 SDOperand LD;
1552 switch (EntrySize) {
1553 default: assert(0 && "Size of jump table not supported yet."); break;
1554 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1555 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1556 }
1557
1558 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001559 // For PIC, the sequence is:
1560 // BRIND(load(Jumptable + index) + RelocBase)
1561 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1562 SDOperand Reloc;
1563 if (TLI.usesGlobalOffsetTable())
1564 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1565 else
1566 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001567 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001568 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1569 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1570 } else {
1571 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1572 }
1573 }
1574 }
1575 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001576 case ISD::BRCOND:
1577 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001578 // Ensure that libcalls are emitted before a return.
1579 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1580 Tmp1 = LegalizeOp(Tmp1);
1581 LastCALLSEQ_END = DAG.getEntryNode();
1582
Chris Lattner47e92232005-01-18 19:27:06 +00001583 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1584 case Expand: assert(0 && "It's impossible to expand bools");
1585 case Legal:
1586 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1587 break;
1588 case Promote:
1589 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001590
1591 // The top bits of the promoted condition are not necessarily zero, ensure
1592 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001593 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001594 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1595 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001596 break;
1597 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001598
1599 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001601
1602 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1603 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001604 case TargetLowering::Legal: break;
1605 case TargetLowering::Custom:
1606 Tmp1 = TLI.LowerOperation(Result, DAG);
1607 if (Tmp1.Val) Result = Tmp1;
1608 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001609 case TargetLowering::Expand:
1610 // Expand brcond's setcc into its constituent parts and create a BR_CC
1611 // Node.
1612 if (Tmp2.getOpcode() == ISD::SETCC) {
1613 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1614 Tmp2.getOperand(0), Tmp2.getOperand(1),
1615 Node->getOperand(2));
1616 } else {
1617 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1618 DAG.getCondCode(ISD::SETNE), Tmp2,
1619 DAG.getConstant(0, Tmp2.getValueType()),
1620 Node->getOperand(2));
1621 }
1622 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001623 }
1624 break;
1625 case ISD::BR_CC:
1626 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001627 // Ensure that libcalls are emitted before a branch.
1628 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1629 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001630 Tmp2 = Node->getOperand(2); // LHS
1631 Tmp3 = Node->getOperand(3); // RHS
1632 Tmp4 = Node->getOperand(1); // CC
1633
1634 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001635 LastCALLSEQ_END = DAG.getEntryNode();
1636
Nate Begeman750ac1b2006-02-01 07:19:44 +00001637 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1638 // the LHS is a legal SETCC itself. In this case, we need to compare
1639 // the result against zero to select between true and false values.
1640 if (Tmp3.Val == 0) {
1641 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1642 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001643 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001644
1645 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1646 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001647
Chris Lattner181b7a32005-12-17 23:46:46 +00001648 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1649 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001650 case TargetLowering::Legal: break;
1651 case TargetLowering::Custom:
1652 Tmp4 = TLI.LowerOperation(Result, DAG);
1653 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001654 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001655 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001656 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001657 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001658 LoadSDNode *LD = cast<LoadSDNode>(Node);
1659 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1660 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001661
Evan Cheng466685d2006-10-09 20:57:25 +00001662 ISD::LoadExtType ExtType = LD->getExtensionType();
1663 if (ExtType == ISD::NON_EXTLOAD) {
1664 MVT::ValueType VT = Node->getValueType(0);
1665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1666 Tmp3 = Result.getValue(0);
1667 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001668
Evan Cheng466685d2006-10-09 20:57:25 +00001669 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1670 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001671 case TargetLowering::Legal:
1672 // If this is an unaligned load and the target doesn't support it,
1673 // expand it.
1674 if (!TLI.allowsUnalignedMemoryAccesses()) {
1675 unsigned ABIAlignment = TLI.getTargetData()->
1676 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1677 if (LD->getAlignment() < ABIAlignment){
1678 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1679 TLI);
1680 Tmp3 = Result.getOperand(0);
1681 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001682 Tmp3 = LegalizeOp(Tmp3);
1683 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001684 }
1685 }
1686 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001687 case TargetLowering::Custom:
1688 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1689 if (Tmp1.Val) {
1690 Tmp3 = LegalizeOp(Tmp1);
1691 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001692 }
Evan Cheng466685d2006-10-09 20:57:25 +00001693 break;
1694 case TargetLowering::Promote: {
1695 // Only promote a load of vector type to another.
1696 assert(MVT::isVector(VT) && "Cannot promote this load!");
1697 // Change base type to a different vector type.
1698 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1699
1700 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001701 LD->getSrcValueOffset(),
1702 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001703 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1704 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001705 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001706 }
Evan Cheng466685d2006-10-09 20:57:25 +00001707 }
1708 // Since loads produce two values, make sure to remember that we
1709 // legalized both of them.
1710 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1711 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1712 return Op.ResNo ? Tmp4 : Tmp3;
1713 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001714 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001715 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1716 default: assert(0 && "This action is not supported yet!");
1717 case TargetLowering::Promote:
1718 assert(SrcVT == MVT::i1 &&
1719 "Can only promote extending LOAD from i1 -> i8!");
1720 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1721 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001722 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001723 Tmp1 = Result.getValue(0);
1724 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001725 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001726 case TargetLowering::Custom:
1727 isCustom = true;
1728 // FALLTHROUGH
1729 case TargetLowering::Legal:
1730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1731 Tmp1 = Result.getValue(0);
1732 Tmp2 = Result.getValue(1);
1733
1734 if (isCustom) {
1735 Tmp3 = TLI.LowerOperation(Result, DAG);
1736 if (Tmp3.Val) {
1737 Tmp1 = LegalizeOp(Tmp3);
1738 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1739 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001740 } else {
1741 // If this is an unaligned load and the target doesn't support it,
1742 // expand it.
1743 if (!TLI.allowsUnalignedMemoryAccesses()) {
1744 unsigned ABIAlignment = TLI.getTargetData()->
1745 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1746 if (LD->getAlignment() < ABIAlignment){
1747 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1748 TLI);
1749 Tmp1 = Result.getOperand(0);
1750 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001751 Tmp1 = LegalizeOp(Tmp1);
1752 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001753 }
1754 }
Evan Cheng466685d2006-10-09 20:57:25 +00001755 }
1756 break;
1757 case TargetLowering::Expand:
1758 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1759 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1760 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001761 LD->getSrcValueOffset(),
1762 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001763 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1764 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1765 Tmp2 = LegalizeOp(Load.getValue(1));
1766 break;
1767 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001768 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001769 // Turn the unsupported load into an EXTLOAD followed by an explicit
1770 // zero/sign extend inreg.
1771 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1772 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001773 LD->getSrcValueOffset(), SrcVT,
1774 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001775 SDOperand ValRes;
1776 if (ExtType == ISD::SEXTLOAD)
1777 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1778 Result, DAG.getValueType(SrcVT));
1779 else
1780 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1781 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1782 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1783 break;
1784 }
1785 // Since loads produce two values, make sure to remember that we legalized
1786 // both of them.
1787 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1788 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1789 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001790 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001791 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001792 case ISD::EXTRACT_ELEMENT: {
1793 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1794 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001795 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001796 case Legal:
1797 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1798 // 1 -> Hi
1799 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1800 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1801 TLI.getShiftAmountTy()));
1802 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1803 } else {
1804 // 0 -> Lo
1805 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1806 Node->getOperand(0));
1807 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001808 break;
1809 case Expand:
1810 // Get both the low and high parts.
1811 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1812 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1813 Result = Tmp2; // 1 -> Hi
1814 else
1815 Result = Tmp1; // 0 -> Lo
1816 break;
1817 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001818 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001819 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001820
1821 case ISD::CopyToReg:
1822 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001823
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001824 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001825 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001826 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001827 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001828 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001829 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001830 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001831 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001832 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001833 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001834 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1835 Tmp3);
1836 } else {
1837 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001838 }
1839
1840 // Since this produces two values, make sure to remember that we legalized
1841 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001842 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001843 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001844 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001845 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001846 break;
1847
1848 case ISD::RET:
1849 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001850
1851 // Ensure that libcalls are emitted before a return.
1852 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1853 Tmp1 = LegalizeOp(Tmp1);
1854 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001855
Chris Lattner3e928bb2005-01-07 07:47:09 +00001856 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001857 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001858 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001859 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001860 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001861 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001862 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001863 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001864 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001865 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001866 SDOperand Lo, Hi;
1867 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001868
1869 // Big endian systems want the hi reg first.
1870 if (!TLI.isLittleEndian())
1871 std::swap(Lo, Hi);
1872
Evan Cheng13acce32006-12-11 19:27:14 +00001873 if (Hi.Val)
1874 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1875 else
1876 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001877 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001878 } else {
1879 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001880 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1881 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001882
Dan Gohman7f321562007-06-25 16:23:39 +00001883 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001884 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001885 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001886 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001887 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001888 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001889 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001890 } else if (NumElems == 1) {
1891 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001892 Tmp2 = ScalarizeVectorOp(Tmp2);
1893 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001894 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001895
1896 // FIXME: Returns of gcc generic vectors smaller than a legal type
1897 // should be returned in integer registers!
1898
Chris Lattnerf87324e2006-04-11 01:31:51 +00001899 // The scalarized value type may not be legal, e.g. it might require
1900 // promotion or expansion. Relegalize the return.
1901 Result = LegalizeOp(Result);
1902 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001903 // FIXME: Returns of gcc generic vectors larger than a legal vector
1904 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001905 SDOperand Lo, Hi;
1906 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001907 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001908 Result = LegalizeOp(Result);
1909 }
1910 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001911 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001912 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001913 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001915 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001916 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001917 }
1918 break;
1919 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001920 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001921 break;
1922 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001923 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001924 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001925 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001926 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1927 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001928 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001929 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001930 break;
1931 case Expand: {
1932 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001933 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001934 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001935 ExpandOp(Node->getOperand(i), Lo, Hi);
1936 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001937 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001938 if (Hi.Val) {
1939 NewValues.push_back(Hi);
1940 NewValues.push_back(Node->getOperand(i+1));
1941 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001942 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001943 }
1944 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001945 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001946 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001947
1948 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001949 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001950 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001951 Result = DAG.getNode(ISD::RET, MVT::Other,
1952 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001953 break;
1954 }
1955 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001956
Chris Lattner6862dbc2006-01-29 21:02:23 +00001957 if (Result.getOpcode() == ISD::RET) {
1958 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1959 default: assert(0 && "This action is not supported yet!");
1960 case TargetLowering::Legal: break;
1961 case TargetLowering::Custom:
1962 Tmp1 = TLI.LowerOperation(Result, DAG);
1963 if (Tmp1.Val) Result = Tmp1;
1964 break;
1965 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001966 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001967 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001968 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001969 StoreSDNode *ST = cast<StoreSDNode>(Node);
1970 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1971 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001972 int SVOffset = ST->getSrcValueOffset();
1973 unsigned Alignment = ST->getAlignment();
1974 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001975
Evan Cheng8b2794a2006-10-13 21:14:26 +00001976 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001977 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1978 // FIXME: We shouldn't do this for TargetConstantFP's.
1979 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1980 // to phase ordering between legalized code and the dag combiner. This
1981 // probably means that we need to integrate dag combiner and legalizer
1982 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001983 // We generally can't do this one for long doubles.
1984 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001985 if (CFP->getValueType(0) == MVT::f32) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001986 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
1987 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00001988 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001989 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1990 SVOffset, isVolatile, Alignment);
1991 break;
1992 } else if (CFP->getValueType(0) == MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001993 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
1994 getZExtValue(), MVT::i64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00001995 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1996 SVOffset, isVolatile, Alignment);
1997 break;
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001998 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001999 }
2000
Evan Cheng8b2794a2006-10-13 21:14:26 +00002001 switch (getTypeAction(ST->getStoredVT())) {
2002 case Legal: {
2003 Tmp3 = LegalizeOp(ST->getValue());
2004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2005 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002006
Evan Cheng8b2794a2006-10-13 21:14:26 +00002007 MVT::ValueType VT = Tmp3.getValueType();
2008 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2009 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002010 case TargetLowering::Legal:
2011 // If this is an unaligned store and the target doesn't support it,
2012 // expand it.
2013 if (!TLI.allowsUnalignedMemoryAccesses()) {
2014 unsigned ABIAlignment = TLI.getTargetData()->
2015 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2016 if (ST->getAlignment() < ABIAlignment)
2017 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2018 TLI);
2019 }
2020 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002021 case TargetLowering::Custom:
2022 Tmp1 = TLI.LowerOperation(Result, DAG);
2023 if (Tmp1.Val) Result = Tmp1;
2024 break;
2025 case TargetLowering::Promote:
2026 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2027 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2028 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2029 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002030 ST->getSrcValue(), SVOffset, isVolatile,
2031 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002032 break;
2033 }
2034 break;
2035 }
2036 case Promote:
2037 // Truncate the value and store the result.
2038 Tmp3 = PromoteOp(ST->getValue());
2039 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002040 SVOffset, ST->getStoredVT(),
2041 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002042 break;
2043
2044 case Expand:
2045 unsigned IncrementSize = 0;
2046 SDOperand Lo, Hi;
2047
2048 // If this is a vector type, then we have to calculate the increment as
2049 // the product of the element size in bytes, and the number of elements
2050 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002051 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002052 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00002053 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2054 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002055
Dan Gohman7f321562007-06-25 16:23:39 +00002056 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002057 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002058 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002059 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002060 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002061 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002062 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002063 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002064 Result = LegalizeOp(Result);
2065 break;
2066 } else if (NumElems == 1) {
2067 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002068 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002069 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002070 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002071 // The scalarized value type may not be legal, e.g. it might require
2072 // promotion or expansion. Relegalize the scalar store.
2073 Result = LegalizeOp(Result);
2074 break;
2075 } else {
2076 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2077 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2078 }
2079 } else {
2080 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002081 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002082
2083 if (!TLI.isLittleEndian())
2084 std::swap(Lo, Hi);
2085 }
2086
2087 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002088 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002089
2090 if (Hi.Val == NULL) {
2091 // Must be int <-> float one-to-one expansion.
2092 Result = Lo;
2093 break;
2094 }
2095
Evan Cheng8b2794a2006-10-13 21:14:26 +00002096 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2097 getIntPtrConstant(IncrementSize));
2098 assert(isTypeLegal(Tmp2.getValueType()) &&
2099 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002100 SVOffset += IncrementSize;
2101 if (Alignment > IncrementSize)
2102 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002103 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002104 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002105 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2106 break;
2107 }
2108 } else {
2109 // Truncating store
2110 assert(isTypeLegal(ST->getValue().getValueType()) &&
2111 "Cannot handle illegal TRUNCSTORE yet!");
2112 Tmp3 = LegalizeOp(ST->getValue());
2113
2114 // The only promote case we handle is TRUNCSTORE:i1 X into
2115 // -> TRUNCSTORE:i8 (and X, 1)
2116 if (ST->getStoredVT() == MVT::i1 &&
2117 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2118 // Promote the bool to a mask then store.
2119 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2120 DAG.getConstant(1, Tmp3.getValueType()));
2121 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002122 SVOffset, MVT::i8,
2123 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002124 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2125 Tmp2 != ST->getBasePtr()) {
2126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2127 ST->getOffset());
2128 }
2129
2130 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2131 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002132 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002133 case TargetLowering::Legal:
2134 // If this is an unaligned store and the target doesn't support it,
2135 // expand it.
2136 if (!TLI.allowsUnalignedMemoryAccesses()) {
2137 unsigned ABIAlignment = TLI.getTargetData()->
2138 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2139 if (ST->getAlignment() < ABIAlignment)
2140 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2141 TLI);
2142 }
2143 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002144 case TargetLowering::Custom:
2145 Tmp1 = TLI.LowerOperation(Result, DAG);
2146 if (Tmp1.Val) Result = Tmp1;
2147 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002148 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002149 }
2150 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002151 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002152 case ISD::PCMARKER:
2153 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002155 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002156 case ISD::STACKSAVE:
2157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002158 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2159 Tmp1 = Result.getValue(0);
2160 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002161
Chris Lattner140d53c2006-01-13 02:50:02 +00002162 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2163 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002164 case TargetLowering::Legal: break;
2165 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002166 Tmp3 = TLI.LowerOperation(Result, DAG);
2167 if (Tmp3.Val) {
2168 Tmp1 = LegalizeOp(Tmp3);
2169 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002170 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002171 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002172 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002173 // Expand to CopyFromReg if the target set
2174 // StackPointerRegisterToSaveRestore.
2175 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002176 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002177 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002178 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002179 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002180 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2181 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002182 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002183 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002184 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002185
2186 // Since stacksave produce two values, make sure to remember that we
2187 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002188 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2189 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2190 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002191
Chris Lattner140d53c2006-01-13 02:50:02 +00002192 case ISD::STACKRESTORE:
2193 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2194 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002196
2197 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2198 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002199 case TargetLowering::Legal: break;
2200 case TargetLowering::Custom:
2201 Tmp1 = TLI.LowerOperation(Result, DAG);
2202 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002203 break;
2204 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002205 // Expand to CopyToReg if the target set
2206 // StackPointerRegisterToSaveRestore.
2207 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2208 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2209 } else {
2210 Result = Tmp1;
2211 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002212 break;
2213 }
2214 break;
2215
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002216 case ISD::READCYCLECOUNTER:
2217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002218 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002219 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2220 Node->getValueType(0))) {
2221 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002222 case TargetLowering::Legal:
2223 Tmp1 = Result.getValue(0);
2224 Tmp2 = Result.getValue(1);
2225 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002226 case TargetLowering::Custom:
2227 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002228 Tmp1 = LegalizeOp(Result.getValue(0));
2229 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002230 break;
2231 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002232
2233 // Since rdcc produce two values, make sure to remember that we legalized
2234 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002235 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2236 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002237 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002238
Chris Lattner2ee743f2005-01-14 22:08:15 +00002239 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002240 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2241 case Expand: assert(0 && "It's impossible to expand bools");
2242 case Legal:
2243 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2244 break;
2245 case Promote:
2246 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002247 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002248 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002249 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2250 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002251 break;
2252 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002253 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002254 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002255
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002256 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002257
Nate Begemanb942a3d2005-08-23 04:29:48 +00002258 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002259 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002260 case TargetLowering::Legal: break;
2261 case TargetLowering::Custom: {
2262 Tmp1 = TLI.LowerOperation(Result, DAG);
2263 if (Tmp1.Val) Result = Tmp1;
2264 break;
2265 }
Nate Begeman9373a812005-08-10 20:51:12 +00002266 case TargetLowering::Expand:
2267 if (Tmp1.getOpcode() == ISD::SETCC) {
2268 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2269 Tmp2, Tmp3,
2270 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2271 } else {
2272 Result = DAG.getSelectCC(Tmp1,
2273 DAG.getConstant(0, Tmp1.getValueType()),
2274 Tmp2, Tmp3, ISD::SETNE);
2275 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002276 break;
2277 case TargetLowering::Promote: {
2278 MVT::ValueType NVT =
2279 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2280 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002281 if (MVT::isVector(Tmp2.getValueType())) {
2282 ExtOp = ISD::BIT_CONVERT;
2283 TruncOp = ISD::BIT_CONVERT;
2284 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002285 ExtOp = ISD::ANY_EXTEND;
2286 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002287 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002288 ExtOp = ISD::FP_EXTEND;
2289 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002290 }
2291 // Promote each of the values to the new type.
2292 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2293 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2294 // Perform the larger operation, then round down.
2295 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2296 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2297 break;
2298 }
2299 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002300 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002301 case ISD::SELECT_CC: {
2302 Tmp1 = Node->getOperand(0); // LHS
2303 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002304 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2305 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002306 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002307
Nate Begeman750ac1b2006-02-01 07:19:44 +00002308 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2309
2310 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2311 // the LHS is a legal SETCC itself. In this case, we need to compare
2312 // the result against zero to select between true and false values.
2313 if (Tmp2.Val == 0) {
2314 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2315 CC = DAG.getCondCode(ISD::SETNE);
2316 }
2317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2318
2319 // Everything is legal, see if we should expand this op or something.
2320 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2321 default: assert(0 && "This action is not supported yet!");
2322 case TargetLowering::Legal: break;
2323 case TargetLowering::Custom:
2324 Tmp1 = TLI.LowerOperation(Result, DAG);
2325 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002326 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002327 }
2328 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002329 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002330 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002331 Tmp1 = Node->getOperand(0);
2332 Tmp2 = Node->getOperand(1);
2333 Tmp3 = Node->getOperand(2);
2334 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2335
2336 // If we had to Expand the SetCC operands into a SELECT node, then it may
2337 // not always be possible to return a true LHS & RHS. In this case, just
2338 // return the value we legalized, returned in the LHS
2339 if (Tmp2.Val == 0) {
2340 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002341 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002342 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002343
Chris Lattner73e142f2006-01-30 22:43:50 +00002344 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002345 default: assert(0 && "Cannot handle this action for SETCC yet!");
2346 case TargetLowering::Custom:
2347 isCustom = true;
2348 // FALLTHROUGH.
2349 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002351 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002352 Tmp4 = TLI.LowerOperation(Result, DAG);
2353 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002354 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002355 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002356 case TargetLowering::Promote: {
2357 // First step, figure out the appropriate operation to use.
2358 // Allow SETCC to not be supported for all legal data types
2359 // Mostly this targets FP
2360 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002361 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002362
2363 // Scan for the appropriate larger type to use.
2364 while (1) {
2365 NewInTy = (MVT::ValueType)(NewInTy+1);
2366
2367 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2368 "Fell off of the edge of the integer world");
2369 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2370 "Fell off of the edge of the floating point world");
2371
2372 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002373 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002374 break;
2375 }
2376 if (MVT::isInteger(NewInTy))
2377 assert(0 && "Cannot promote Legal Integer SETCC yet");
2378 else {
2379 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2380 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2381 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002382 Tmp1 = LegalizeOp(Tmp1);
2383 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002384 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002385 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002386 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002387 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002388 case TargetLowering::Expand:
2389 // Expand a setcc node into a select_cc of the same condition, lhs, and
2390 // rhs that selects between const 1 (true) and const 0 (false).
2391 MVT::ValueType VT = Node->getValueType(0);
2392 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2393 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002394 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002395 break;
2396 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002397 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002398 case ISD::MEMSET:
2399 case ISD::MEMCPY:
2400 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002401 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002402 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2403
2404 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2405 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2406 case Expand: assert(0 && "Cannot expand a byte!");
2407 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002408 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002409 break;
2410 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002411 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002412 break;
2413 }
2414 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002415 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002416 }
Chris Lattner272455b2005-02-02 03:44:41 +00002417
2418 SDOperand Tmp4;
2419 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002420 case Expand: {
2421 // Length is too big, just take the lo-part of the length.
2422 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002423 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002424 break;
2425 }
Chris Lattnere5605212005-01-28 22:29:18 +00002426 case Legal:
2427 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002428 break;
2429 case Promote:
2430 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002431 break;
2432 }
2433
2434 SDOperand Tmp5;
2435 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2436 case Expand: assert(0 && "Cannot expand this yet!");
2437 case Legal:
2438 Tmp5 = LegalizeOp(Node->getOperand(4));
2439 break;
2440 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002441 Tmp5 = PromoteOp(Node->getOperand(4));
2442 break;
2443 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002444
2445 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2446 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002447 case TargetLowering::Custom:
2448 isCustom = true;
2449 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002450 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002452 if (isCustom) {
2453 Tmp1 = TLI.LowerOperation(Result, DAG);
2454 if (Tmp1.Val) Result = Tmp1;
2455 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002456 break;
2457 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002458 // Otherwise, the target does not support this operation. Lower the
2459 // operation to an explicit libcall as appropriate.
2460 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002461 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002462 TargetLowering::ArgListTy Args;
2463 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002464
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002465 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002466 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002467 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002468 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002469 // Extend the (previously legalized) ubyte argument to be an int value
2470 // for the call.
2471 if (Tmp3.getValueType() > MVT::i32)
2472 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2473 else
2474 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002475 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002476 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002477 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002478 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002479
2480 FnName = "memset";
2481 } else if (Node->getOpcode() == ISD::MEMCPY ||
2482 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002483 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002484 Entry.Node = Tmp2; Args.push_back(Entry);
2485 Entry.Node = Tmp3; Args.push_back(Entry);
2486 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002487 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2488 } else {
2489 assert(0 && "Unknown op!");
2490 }
Chris Lattner45982da2005-05-12 16:53:42 +00002491
Chris Lattnere1bd8222005-01-11 05:57:22 +00002492 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002493 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002494 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002495 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002496 break;
2497 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002498 }
2499 break;
2500 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002501
Chris Lattner5b359c62005-04-02 04:00:59 +00002502 case ISD::SHL_PARTS:
2503 case ISD::SRA_PARTS:
2504 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002505 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002506 bool Changed = false;
2507 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2508 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2509 Changed |= Ops.back() != Node->getOperand(i);
2510 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002511 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002512 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002513
Evan Cheng05a2d562006-01-09 18:31:59 +00002514 switch (TLI.getOperationAction(Node->getOpcode(),
2515 Node->getValueType(0))) {
2516 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002517 case TargetLowering::Legal: break;
2518 case TargetLowering::Custom:
2519 Tmp1 = TLI.LowerOperation(Result, DAG);
2520 if (Tmp1.Val) {
2521 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002523 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002524 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2525 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002526 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002527 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002528 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002529 return RetVal;
2530 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002531 break;
2532 }
2533
Chris Lattner2c8086f2005-04-02 05:00:07 +00002534 // Since these produce multiple values, make sure to remember that we
2535 // legalized all of them.
2536 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2537 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2538 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002539 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002540
2541 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002542 case ISD::ADD:
2543 case ISD::SUB:
2544 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002545 case ISD::MULHS:
2546 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002547 case ISD::UDIV:
2548 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002549 case ISD::AND:
2550 case ISD::OR:
2551 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002552 case ISD::SHL:
2553 case ISD::SRL:
2554 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002555 case ISD::FADD:
2556 case ISD::FSUB:
2557 case ISD::FMUL:
2558 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002559 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002560 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2561 case Expand: assert(0 && "Not possible");
2562 case Legal:
2563 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2564 break;
2565 case Promote:
2566 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2567 break;
2568 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002569
2570 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002571
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002572 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002573 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002574 case TargetLowering::Legal: break;
2575 case TargetLowering::Custom:
2576 Tmp1 = TLI.LowerOperation(Result, DAG);
2577 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002578 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002579 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002580 if (Node->getValueType(0) == MVT::i32) {
2581 switch (Node->getOpcode()) {
2582 default: assert(0 && "Do not know how to expand this integer BinOp!");
2583 case ISD::UDIV:
2584 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002585 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2586 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002587 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002588 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002589 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002590 };
2591 break;
2592 }
2593
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002594 assert(MVT::isVector(Node->getValueType(0)) &&
2595 "Cannot expand this binary operator!");
2596 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002597 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002598 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002599 MVT::ValueType PtrVT = TLI.getPointerTy();
2600 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2601 i != e; ++i) {
2602 SDOperand Idx = DAG.getConstant(i, PtrVT);
2603 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2604 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2605 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2606 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002607 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2608 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002609 break;
2610 }
Evan Chengcc987612006-04-12 21:20:24 +00002611 case TargetLowering::Promote: {
2612 switch (Node->getOpcode()) {
2613 default: assert(0 && "Do not know how to promote this BinOp!");
2614 case ISD::AND:
2615 case ISD::OR:
2616 case ISD::XOR: {
2617 MVT::ValueType OVT = Node->getValueType(0);
2618 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2619 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2620 // Bit convert each of the values to the new type.
2621 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2622 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2623 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2624 // Bit convert the result back the original type.
2625 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2626 break;
2627 }
2628 }
2629 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002630 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002631 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002632
2633 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2634 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2635 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2636 case Expand: assert(0 && "Not possible");
2637 case Legal:
2638 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2639 break;
2640 case Promote:
2641 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2642 break;
2643 }
2644
2645 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2646
2647 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2648 default: assert(0 && "Operation not supported");
2649 case TargetLowering::Custom:
2650 Tmp1 = TLI.LowerOperation(Result, DAG);
2651 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002652 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002653 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002654 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002655 // If this target supports fabs/fneg natively and select is cheap,
2656 // do this efficiently.
2657 if (!TLI.isSelectExpensive() &&
2658 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2659 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002660 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002661 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002662 // Get the sign bit of the RHS.
2663 MVT::ValueType IVT =
2664 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2665 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2666 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2667 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2668 // Get the absolute value of the result.
2669 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2670 // Select between the nabs and abs value based on the sign bit of
2671 // the input.
2672 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2673 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2674 AbsVal),
2675 AbsVal);
2676 Result = LegalizeOp(Result);
2677 break;
2678 }
2679
2680 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002681 MVT::ValueType NVT =
2682 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2683 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2684 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2685 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002686 break;
2687 }
Evan Cheng912095b2007-01-04 21:56:39 +00002688 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002689 break;
2690
Nate Begeman551bf3f2006-02-17 05:43:56 +00002691 case ISD::ADDC:
2692 case ISD::SUBC:
2693 Tmp1 = LegalizeOp(Node->getOperand(0));
2694 Tmp2 = LegalizeOp(Node->getOperand(1));
2695 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2696 // Since this produces two values, make sure to remember that we legalized
2697 // both of them.
2698 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2699 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2700 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002701
Nate Begeman551bf3f2006-02-17 05:43:56 +00002702 case ISD::ADDE:
2703 case ISD::SUBE:
2704 Tmp1 = LegalizeOp(Node->getOperand(0));
2705 Tmp2 = LegalizeOp(Node->getOperand(1));
2706 Tmp3 = LegalizeOp(Node->getOperand(2));
2707 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2708 // Since this produces two values, make sure to remember that we legalized
2709 // both of them.
2710 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2711 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2712 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002713
Nate Begeman419f8b62005-10-18 00:27:41 +00002714 case ISD::BUILD_PAIR: {
2715 MVT::ValueType PairTy = Node->getValueType(0);
2716 // TODO: handle the case where the Lo and Hi operands are not of legal type
2717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2718 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2719 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002720 case TargetLowering::Promote:
2721 case TargetLowering::Custom:
2722 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002723 case TargetLowering::Legal:
2724 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2725 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2726 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002727 case TargetLowering::Expand:
2728 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2729 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2730 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2731 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2732 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002733 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002734 break;
2735 }
2736 break;
2737 }
2738
Nate Begemanc105e192005-04-06 00:23:54 +00002739 case ISD::UREM:
2740 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002741 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002742 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2743 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002744
Nate Begemanc105e192005-04-06 00:23:54 +00002745 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002746 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2747 case TargetLowering::Custom:
2748 isCustom = true;
2749 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002750 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002751 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002752 if (isCustom) {
2753 Tmp1 = TLI.LowerOperation(Result, DAG);
2754 if (Tmp1.Val) Result = Tmp1;
2755 }
Nate Begemanc105e192005-04-06 00:23:54 +00002756 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002757 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002758 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002759 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002760 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002761 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2762 TargetLowering::Legal) {
2763 // X % Y -> X-X/Y*Y
2764 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002765 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002766 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2767 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2768 } else {
2769 assert(Node->getValueType(0) == MVT::i32 &&
2770 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002771 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2772 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002773 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002774 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002775 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002776 } else {
2777 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002778 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2779 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002780 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002781 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2782 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002783 }
2784 break;
2785 }
2786 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002787 case ISD::VAARG: {
2788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2789 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2790
Chris Lattner5c62f332006-01-28 07:42:08 +00002791 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002792 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2793 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002794 case TargetLowering::Custom:
2795 isCustom = true;
2796 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002797 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002798 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2799 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002800 Tmp1 = Result.getValue(1);
2801
2802 if (isCustom) {
2803 Tmp2 = TLI.LowerOperation(Result, DAG);
2804 if (Tmp2.Val) {
2805 Result = LegalizeOp(Tmp2);
2806 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2807 }
2808 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002809 break;
2810 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002811 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002812 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002813 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002814 // Increment the pointer, VAList, to the next vaarg
2815 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2816 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2817 TLI.getPointerTy()));
2818 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002819 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2820 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002821 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002822 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002823 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002824 Result = LegalizeOp(Result);
2825 break;
2826 }
2827 }
2828 // Since VAARG produces two values, make sure to remember that we
2829 // legalized both of them.
2830 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002831 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2832 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002833 }
2834
2835 case ISD::VACOPY:
2836 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2837 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2838 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2839
2840 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2841 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002842 case TargetLowering::Custom:
2843 isCustom = true;
2844 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002845 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002846 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2847 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002848 if (isCustom) {
2849 Tmp1 = TLI.LowerOperation(Result, DAG);
2850 if (Tmp1.Val) Result = Tmp1;
2851 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002852 break;
2853 case TargetLowering::Expand:
2854 // This defaults to loading a pointer from the input and storing it to the
2855 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002856 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002857 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002858 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2859 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002860 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2861 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002862 break;
2863 }
2864 break;
2865
2866 case ISD::VAEND:
2867 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2868 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2869
2870 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2871 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002872 case TargetLowering::Custom:
2873 isCustom = true;
2874 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002875 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002877 if (isCustom) {
2878 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2879 if (Tmp1.Val) Result = Tmp1;
2880 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002881 break;
2882 case TargetLowering::Expand:
2883 Result = Tmp1; // Default to a no-op, return the chain
2884 break;
2885 }
2886 break;
2887
2888 case ISD::VASTART:
2889 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2890 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2891
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002892 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2893
Nate Begemanacc398c2006-01-25 18:21:52 +00002894 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2895 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002896 case TargetLowering::Legal: break;
2897 case TargetLowering::Custom:
2898 Tmp1 = TLI.LowerOperation(Result, DAG);
2899 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002900 break;
2901 }
2902 break;
2903
Nate Begeman35ef9132006-01-11 21:21:00 +00002904 case ISD::ROTL:
2905 case ISD::ROTR:
2906 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2907 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002909 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2910 default:
2911 assert(0 && "ROTL/ROTR legalize operation not supported");
2912 break;
2913 case TargetLowering::Legal:
2914 break;
2915 case TargetLowering::Custom:
2916 Tmp1 = TLI.LowerOperation(Result, DAG);
2917 if (Tmp1.Val) Result = Tmp1;
2918 break;
2919 case TargetLowering::Promote:
2920 assert(0 && "Do not know how to promote ROTL/ROTR");
2921 break;
2922 case TargetLowering::Expand:
2923 assert(0 && "Do not know how to expand ROTL/ROTR");
2924 break;
2925 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002926 break;
2927
Nate Begemand88fc032006-01-14 03:14:10 +00002928 case ISD::BSWAP:
2929 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2930 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002931 case TargetLowering::Custom:
2932 assert(0 && "Cannot custom legalize this yet!");
2933 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002934 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002935 break;
2936 case TargetLowering::Promote: {
2937 MVT::ValueType OVT = Tmp1.getValueType();
2938 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002939 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002940
Chris Lattner456a93a2006-01-28 07:39:30 +00002941 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2942 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2943 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2944 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2945 break;
2946 }
2947 case TargetLowering::Expand:
2948 Result = ExpandBSWAP(Tmp1);
2949 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002950 }
2951 break;
2952
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002953 case ISD::CTPOP:
2954 case ISD::CTTZ:
2955 case ISD::CTLZ:
2956 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2957 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00002958 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002959 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002960 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00002961 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00002962 TargetLowering::Custom) {
2963 Tmp1 = TLI.LowerOperation(Result, DAG);
2964 if (Tmp1.Val) {
2965 Result = Tmp1;
2966 }
Scott Michel910b66d2007-07-30 21:00:31 +00002967 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002968 break;
2969 case TargetLowering::Promote: {
2970 MVT::ValueType OVT = Tmp1.getValueType();
2971 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002972
2973 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002974 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2975 // Perform the larger operation, then subtract if needed.
2976 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002977 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002978 case ISD::CTPOP:
2979 Result = Tmp1;
2980 break;
2981 case ISD::CTTZ:
2982 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002983 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002984 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002985 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002986 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00002987 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002988 break;
2989 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002990 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002991 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002992 DAG.getConstant(MVT::getSizeInBits(NVT) -
2993 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002994 break;
2995 }
2996 break;
2997 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002998 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002999 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003000 break;
3001 }
3002 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003003
Chris Lattner2c8086f2005-04-02 05:00:07 +00003004 // Unary operators
3005 case ISD::FABS:
3006 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003007 case ISD::FSQRT:
3008 case ISD::FSIN:
3009 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003010 Tmp1 = LegalizeOp(Node->getOperand(0));
3011 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003012 case TargetLowering::Promote:
3013 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003014 isCustom = true;
3015 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003016 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003017 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003018 if (isCustom) {
3019 Tmp1 = TLI.LowerOperation(Result, DAG);
3020 if (Tmp1.Val) Result = Tmp1;
3021 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003022 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003023 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003024 switch (Node->getOpcode()) {
3025 default: assert(0 && "Unreachable!");
3026 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003027 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3028 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003029 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003030 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003031 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003032 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3033 MVT::ValueType VT = Node->getValueType(0);
3034 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003035 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003036 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3037 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003038 break;
3039 }
3040 case ISD::FSQRT:
3041 case ISD::FSIN:
3042 case ISD::FCOS: {
3043 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00003044 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003045 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003046 case ISD::FSQRT:
3047 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
3048 break;
3049 case ISD::FSIN:
3050 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3051 break;
3052 case ISD::FCOS:
3053 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3054 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003055 default: assert(0 && "Unreachable!");
3056 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003057 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003058 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3059 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003060 break;
3061 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003062 }
3063 break;
3064 }
3065 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003066 case ISD::FPOWI: {
3067 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00003068 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
3069 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003070 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003071 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3072 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003073 break;
3074 }
Chris Lattner35481892005-12-23 00:16:34 +00003075 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003076 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003077 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003078 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3079 // The input has to be a vector type, we have to either scalarize it, pack
3080 // it, or convert it based on whether the input vector type is legal.
3081 SDNode *InVal = Node->getOperand(0).Val;
3082 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3083 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3084
3085 // Figure out if there is a simple type corresponding to this Vector
3086 // type. If so, convert to the vector type.
3087 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3088 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003089 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003090 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3091 LegalizeOp(Node->getOperand(0)));
3092 break;
3093 } else if (NumElems == 1) {
3094 // Turn this into a bit convert of the scalar input.
3095 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3096 ScalarizeVectorOp(Node->getOperand(0)));
3097 break;
3098 } else {
3099 // FIXME: UNIMP! Store then reload
3100 assert(0 && "Cast from unsupported vector type not implemented yet!");
3101 }
Chris Lattner67993f72006-01-23 07:30:46 +00003102 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003103 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3104 Node->getOperand(0).getValueType())) {
3105 default: assert(0 && "Unknown operation action!");
3106 case TargetLowering::Expand:
3107 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3108 break;
3109 case TargetLowering::Legal:
3110 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003111 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003112 break;
3113 }
3114 }
3115 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003116
Chris Lattner2c8086f2005-04-02 05:00:07 +00003117 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003118 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003119 case ISD::UINT_TO_FP: {
3120 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003121 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3122 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003123 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003124 Node->getOperand(0).getValueType())) {
3125 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003126 case TargetLowering::Custom:
3127 isCustom = true;
3128 // FALLTHROUGH
3129 case TargetLowering::Legal:
3130 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003131 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003132 if (isCustom) {
3133 Tmp1 = TLI.LowerOperation(Result, DAG);
3134 if (Tmp1.Val) Result = Tmp1;
3135 }
3136 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003137 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003138 Result = ExpandLegalINT_TO_FP(isSigned,
3139 LegalizeOp(Node->getOperand(0)),
3140 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003141 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003142 case TargetLowering::Promote:
3143 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3144 Node->getValueType(0),
3145 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003146 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003147 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003148 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003149 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003150 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3151 Node->getValueType(0), Node->getOperand(0));
3152 break;
3153 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003154 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003155 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003156 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3157 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003158 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003159 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3160 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003161 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003162 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3163 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003164 break;
3165 }
3166 break;
3167 }
3168 case ISD::TRUNCATE:
3169 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3170 case Legal:
3171 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003172 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003173 break;
3174 case Expand:
3175 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3176
3177 // Since the result is legal, we should just be able to truncate the low
3178 // part of the source.
3179 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3180 break;
3181 case Promote:
3182 Result = PromoteOp(Node->getOperand(0));
3183 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3184 break;
3185 }
3186 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003187
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003188 case ISD::FP_TO_SINT:
3189 case ISD::FP_TO_UINT:
3190 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3191 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003192 Tmp1 = LegalizeOp(Node->getOperand(0));
3193
Chris Lattner1618beb2005-07-29 00:11:56 +00003194 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3195 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003196 case TargetLowering::Custom:
3197 isCustom = true;
3198 // FALLTHROUGH
3199 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003200 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003201 if (isCustom) {
3202 Tmp1 = TLI.LowerOperation(Result, DAG);
3203 if (Tmp1.Val) Result = Tmp1;
3204 }
3205 break;
3206 case TargetLowering::Promote:
3207 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3208 Node->getOpcode() == ISD::FP_TO_SINT);
3209 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003210 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003211 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3212 SDOperand True, False;
3213 MVT::ValueType VT = Node->getOperand(0).getValueType();
3214 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003215 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003216 const uint64_t zero[] = {0, 0};
3217 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3218 uint64_t x = 1ULL << ShiftAmt;
Dale Johannesen910993e2007-09-21 22:09:37 +00003219 (void)apf.convertFromInteger(&x, MVT::getSizeInBits(NVT), false,
3220 APFloat::rmTowardZero);
Dale Johannesen73328d12007-09-19 23:55:34 +00003221 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003222 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3223 Node->getOperand(0), Tmp2, ISD::SETLT);
3224 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3225 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003226 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003227 Tmp2));
3228 False = DAG.getNode(ISD::XOR, NVT, False,
3229 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003230 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3231 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003232 } else {
3233 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3234 }
3235 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003236 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003237 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003238 case Expand: {
3239 // Convert f32 / f64 to i32 / i64.
3240 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00003241 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003242 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003243 case ISD::FP_TO_SINT: {
3244 MVT::ValueType OVT = Node->getOperand(0).getValueType();
3245 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003246 LC = (VT == MVT::i32)
3247 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003248 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003249 LC = (VT == MVT::i32)
3250 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003251 else if (OVT == MVT::f80 || OVT == MVT::f128 || OVT == MVT::ppcf128) {
3252 assert(VT == MVT::i64);
3253 LC = RTLIB::FPTOSINT_LD_I64;
3254 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003255 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003256 }
3257 case ISD::FP_TO_UINT: {
3258 MVT::ValueType OVT = Node->getOperand(0).getValueType();
3259 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003260 LC = (VT == MVT::i32)
3261 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003262 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003263 LC = (VT == MVT::i32)
3264 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003265 else if (OVT == MVT::f80 || OVT == MVT::f128 || OVT == MVT::ppcf128) {
3266 LC = (VT == MVT::i32)
3267 ? RTLIB::FPTOUINT_LD_I32 : RTLIB::FPTOUINT_LD_I64;
3268 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003269 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003270 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003271 default: assert(0 && "Unreachable!");
3272 }
3273 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003274 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3275 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003276 break;
3277 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003278 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003279 Tmp1 = PromoteOp(Node->getOperand(0));
3280 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3281 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003282 break;
3283 }
3284 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003285
Dale Johannesenab081c72007-08-09 17:27:48 +00003286 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003287 case ISD::FP_ROUND: {
3288 MVT::ValueType newVT = Op.getValueType();
3289 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3290 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenab081c72007-08-09 17:27:48 +00003291 // The only way we can lower this is to turn it into a STORE,
Dale Johannesen5411a392007-08-09 01:04:01 +00003292 // LOAD pair, targetting a temporary location (a stack slot).
3293
3294 // NOTE: there is a choice here between constantly creating new stack
3295 // slots and always reusing the same one. We currently always create
3296 // new ones, as reuse may inhibit scheduling.
Dale Johannesenab081c72007-08-09 17:27:48 +00003297 MVT::ValueType slotVT =
3298 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3299 const Type *Ty = MVT::getTypeForValueType(slotVT);
Dale Johannesen5411a392007-08-09 01:04:01 +00003300 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3301 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3302 MachineFunction &MF = DAG.getMachineFunction();
3303 int SSFI =
3304 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3305 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesenab081c72007-08-09 17:27:48 +00003306 if (Node->getOpcode() == ISD::FP_EXTEND) {
3307 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3308 StackSlot, NULL, 0);
3309 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3310 Result, StackSlot, NULL, 0, oldVT);
3311 } else {
3312 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3313 StackSlot, NULL, 0, newVT);
3314 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3315 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003316 break;
3317 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003318 }
3319 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003320 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003321 case ISD::ZERO_EXTEND:
3322 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003323 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003324 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003325 case Legal:
3326 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003327 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003328 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003329 case Promote:
3330 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003331 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003332 Tmp1 = PromoteOp(Node->getOperand(0));
3333 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003334 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003335 case ISD::ZERO_EXTEND:
3336 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003337 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003338 Result = DAG.getZeroExtendInReg(Result,
3339 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003340 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003341 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003342 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003343 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003344 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003345 Result,
3346 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003347 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003348 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003349 Result = PromoteOp(Node->getOperand(0));
3350 if (Result.getValueType() != Op.getValueType())
3351 // Dynamically dead while we have only 2 FP types.
3352 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3353 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003354 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003355 Result = PromoteOp(Node->getOperand(0));
3356 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3357 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003358 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003359 }
3360 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003361 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003362 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003363 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003364 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003365
3366 // If this operation is not supported, convert it to a shl/shr or load/store
3367 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003368 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3369 default: assert(0 && "This action not supported for this op yet!");
3370 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003371 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003372 break;
3373 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003374 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003375 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003376 // NOTE: we could fall back on load/store here too for targets without
3377 // SAR. However, it is doubtful that any exist.
3378 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3379 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003380 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003381 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3382 Node->getOperand(0), ShiftCst);
3383 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3384 Result, ShiftCst);
3385 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003386 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003387 // EXTLOAD pair, targetting a temporary location (a stack slot).
3388
3389 // NOTE: there is a choice here between constantly creating new stack
3390 // slots and always reusing the same one. We currently always create
3391 // new ones, as reuse may inhibit scheduling.
3392 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003393 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003394 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003395 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003396 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003397 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003398 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003399 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3400 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003401 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003402 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003403 } else {
3404 assert(0 && "Unknown op");
3405 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003406 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003407 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003408 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003409 }
Duncan Sands36397f52007-07-27 12:58:54 +00003410 case ISD::TRAMPOLINE: {
3411 SDOperand Ops[6];
3412 for (unsigned i = 0; i != 6; ++i)
3413 Ops[i] = LegalizeOp(Node->getOperand(i));
3414 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3415 // The only option for this node is to custom lower it.
3416 Result = TLI.LowerOperation(Result, DAG);
3417 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003418
3419 // Since trampoline produces two values, make sure to remember that we
3420 // legalized both of them.
3421 Tmp1 = LegalizeOp(Result.getValue(1));
3422 Result = LegalizeOp(Result);
3423 AddLegalizedOperand(SDOperand(Node, 0), Result);
3424 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3425 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003426 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003427 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003428
Chris Lattner4ddd2832006-04-08 04:13:17 +00003429 assert(Result.getValueType() == Op.getValueType() &&
3430 "Bad legalization!");
3431
Chris Lattner456a93a2006-01-28 07:39:30 +00003432 // Make sure that the generated code is itself legal.
3433 if (Result != Op)
3434 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003435
Chris Lattner45982da2005-05-12 16:53:42 +00003436 // Note that LegalizeOp may be reentered even from single-use nodes, which
3437 // means that we always must cache transformed nodes.
3438 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003439 return Result;
3440}
3441
Chris Lattner8b6fa222005-01-15 22:16:26 +00003442/// PromoteOp - Given an operation that produces a value in an invalid type,
3443/// promote it to compute the value into a larger type. The produced value will
3444/// have the correct bits for the low portion of the register, but no guarantee
3445/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003446SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3447 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003448 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003449 assert(getTypeAction(VT) == Promote &&
3450 "Caller should expand or legalize operands that are not promotable!");
3451 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3452 "Cannot promote to smaller type!");
3453
Chris Lattner03c85462005-01-15 05:21:40 +00003454 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003455 SDOperand Result;
3456 SDNode *Node = Op.Val;
3457
Chris Lattner40030bf2007-02-04 01:17:38 +00003458 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003459 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003460
Chris Lattner03c85462005-01-15 05:21:40 +00003461 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003462 case ISD::CopyFromReg:
3463 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003464 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003465#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003466 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003467#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003468 assert(0 && "Do not know how to promote this operator!");
3469 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003470 case ISD::UNDEF:
3471 Result = DAG.getNode(ISD::UNDEF, NVT);
3472 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003473 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003474 if (VT != MVT::i1)
3475 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3476 else
3477 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003478 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3479 break;
3480 case ISD::ConstantFP:
3481 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3482 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3483 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003484
Chris Lattner82fbfb62005-01-18 02:59:52 +00003485 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003486 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003487 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3488 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003489 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003490
Chris Lattner03c85462005-01-15 05:21:40 +00003491 case ISD::TRUNCATE:
3492 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3493 case Legal:
3494 Result = LegalizeOp(Node->getOperand(0));
3495 assert(Result.getValueType() >= NVT &&
3496 "This truncation doesn't make sense!");
3497 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3498 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3499 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003500 case Promote:
3501 // The truncation is not required, because we don't guarantee anything
3502 // about high bits anyway.
3503 Result = PromoteOp(Node->getOperand(0));
3504 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003505 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003506 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3507 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003508 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003509 }
3510 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003511 case ISD::SIGN_EXTEND:
3512 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003513 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003514 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3515 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3516 case Legal:
3517 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003518 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003519 break;
3520 case Promote:
3521 // Promote the reg if it's smaller.
3522 Result = PromoteOp(Node->getOperand(0));
3523 // The high bits are not guaranteed to be anything. Insert an extend.
3524 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003525 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003526 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003527 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003528 Result = DAG.getZeroExtendInReg(Result,
3529 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003530 break;
3531 }
3532 break;
Chris Lattner35481892005-12-23 00:16:34 +00003533 case ISD::BIT_CONVERT:
3534 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3535 Result = PromoteOp(Result);
3536 break;
3537
Chris Lattner8b6fa222005-01-15 22:16:26 +00003538 case ISD::FP_EXTEND:
3539 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3540 case ISD::FP_ROUND:
3541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3542 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3543 case Promote: assert(0 && "Unreachable with 2 FP types!");
3544 case Legal:
3545 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003546 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003547 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003548 break;
3549 }
3550 break;
3551
3552 case ISD::SINT_TO_FP:
3553 case ISD::UINT_TO_FP:
3554 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3555 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003556 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003557 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003558 break;
3559
3560 case Promote:
3561 Result = PromoteOp(Node->getOperand(0));
3562 if (Node->getOpcode() == ISD::SINT_TO_FP)
3563 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003564 Result,
3565 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003566 else
Chris Lattner23993562005-04-13 02:38:47 +00003567 Result = DAG.getZeroExtendInReg(Result,
3568 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003569 // No extra round required here.
3570 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003571 break;
3572 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003573 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3574 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003575 // Round if we cannot tolerate excess precision.
3576 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003577 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3578 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003579 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003580 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003581 break;
3582
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003583 case ISD::SIGN_EXTEND_INREG:
3584 Result = PromoteOp(Node->getOperand(0));
3585 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3586 Node->getOperand(1));
3587 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003588 case ISD::FP_TO_SINT:
3589 case ISD::FP_TO_UINT:
3590 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3591 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003592 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003593 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003594 break;
3595 case Promote:
3596 // The input result is prerounded, so we don't have to do anything
3597 // special.
3598 Tmp1 = PromoteOp(Node->getOperand(0));
3599 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003600 }
Nate Begemand2558e32005-08-14 01:20:53 +00003601 // If we're promoting a UINT to a larger size, check to see if the new node
3602 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3603 // we can use that instead. This allows us to generate better code for
3604 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3605 // legal, such as PowerPC.
3606 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003607 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003608 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3609 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003610 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3611 } else {
3612 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3613 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003614 break;
3615
Chris Lattner2c8086f2005-04-02 05:00:07 +00003616 case ISD::FABS:
3617 case ISD::FNEG:
3618 Tmp1 = PromoteOp(Node->getOperand(0));
3619 assert(Tmp1.getValueType() == NVT);
3620 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3621 // NOTE: we do not have to do any extra rounding here for
3622 // NoExcessFPPrecision, because we know the input will have the appropriate
3623 // precision, and these operations don't modify precision at all.
3624 break;
3625
Chris Lattnerda6ba872005-04-28 21:44:33 +00003626 case ISD::FSQRT:
3627 case ISD::FSIN:
3628 case ISD::FCOS:
3629 Tmp1 = PromoteOp(Node->getOperand(0));
3630 assert(Tmp1.getValueType() == NVT);
3631 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003632 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003633 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3634 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003635 break;
3636
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003637 case ISD::FPOWI: {
3638 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3639 // directly as well, which may be better.
3640 Tmp1 = PromoteOp(Node->getOperand(0));
3641 assert(Tmp1.getValueType() == NVT);
3642 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3643 if (NoExcessFPPrecision)
3644 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3645 DAG.getValueType(VT));
3646 break;
3647 }
3648
Chris Lattner03c85462005-01-15 05:21:40 +00003649 case ISD::AND:
3650 case ISD::OR:
3651 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003652 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003653 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003654 case ISD::MUL:
3655 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003656 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003657 // that too is okay if they are integer operations.
3658 Tmp1 = PromoteOp(Node->getOperand(0));
3659 Tmp2 = PromoteOp(Node->getOperand(1));
3660 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3661 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003662 break;
3663 case ISD::FADD:
3664 case ISD::FSUB:
3665 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003666 Tmp1 = PromoteOp(Node->getOperand(0));
3667 Tmp2 = PromoteOp(Node->getOperand(1));
3668 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3669 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3670
3671 // Floating point operations will give excess precision that we may not be
3672 // able to tolerate. If we DO allow excess precision, just leave it,
3673 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003674 // FIXME: Why would we need to round FP ops more than integer ones?
3675 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003676 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003677 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3678 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003679 break;
3680
Chris Lattner8b6fa222005-01-15 22:16:26 +00003681 case ISD::SDIV:
3682 case ISD::SREM:
3683 // These operators require that their input be sign extended.
3684 Tmp1 = PromoteOp(Node->getOperand(0));
3685 Tmp2 = PromoteOp(Node->getOperand(1));
3686 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003687 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3688 DAG.getValueType(VT));
3689 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3690 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003691 }
3692 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3693
3694 // Perform FP_ROUND: this is probably overly pessimistic.
3695 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003696 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3697 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003698 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003699 case ISD::FDIV:
3700 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003701 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003702 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003703 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3704 case Legal:
3705 Tmp1 = LegalizeOp(Node->getOperand(0));
3706 break;
3707 case Promote:
3708 Tmp1 = PromoteOp(Node->getOperand(0));
3709 break;
3710 case Expand:
3711 assert(0 && "not implemented");
3712 }
3713 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3714 case Legal:
3715 Tmp2 = LegalizeOp(Node->getOperand(1));
3716 break;
3717 case Promote:
3718 Tmp2 = PromoteOp(Node->getOperand(1));
3719 break;
3720 case Expand:
3721 assert(0 && "not implemented");
3722 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003723 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3724
3725 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003726 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003727 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3728 DAG.getValueType(VT));
3729 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003730
3731 case ISD::UDIV:
3732 case ISD::UREM:
3733 // These operators require that their input be zero extended.
3734 Tmp1 = PromoteOp(Node->getOperand(0));
3735 Tmp2 = PromoteOp(Node->getOperand(1));
3736 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003737 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3738 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003739 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3740 break;
3741
3742 case ISD::SHL:
3743 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003744 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003745 break;
3746 case ISD::SRA:
3747 // The input value must be properly sign extended.
3748 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003749 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3750 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003751 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003752 break;
3753 case ISD::SRL:
3754 // The input value must be properly zero extended.
3755 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003756 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003757 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003758 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003759
3760 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003761 Tmp1 = Node->getOperand(0); // Get the chain.
3762 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003763 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3764 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3765 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3766 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003767 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003768 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003769 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003770 // Increment the pointer, VAList, to the next vaarg
3771 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3772 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3773 TLI.getPointerTy()));
3774 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003775 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3776 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003777 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003778 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003779 }
3780 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003781 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003782 break;
3783
Evan Cheng466685d2006-10-09 20:57:25 +00003784 case ISD::LOAD: {
3785 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003786 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3787 ? ISD::EXTLOAD : LD->getExtensionType();
3788 Result = DAG.getExtLoad(ExtType, NVT,
3789 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003790 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003791 LD->getLoadedVT(),
3792 LD->isVolatile(),
3793 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003794 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003795 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003796 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003797 }
Chris Lattner03c85462005-01-15 05:21:40 +00003798 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003799 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3800 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003801 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003802 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003803 case ISD::SELECT_CC:
3804 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3805 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3806 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003807 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003808 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003809 case ISD::BSWAP:
3810 Tmp1 = Node->getOperand(0);
3811 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3812 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3813 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003814 DAG.getConstant(MVT::getSizeInBits(NVT) -
3815 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003816 TLI.getShiftAmountTy()));
3817 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003818 case ISD::CTPOP:
3819 case ISD::CTTZ:
3820 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003821 // Zero extend the argument
3822 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003823 // Perform the larger operation, then subtract if needed.
3824 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003825 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003826 case ISD::CTPOP:
3827 Result = Tmp1;
3828 break;
3829 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003830 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003831 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003832 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3833 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003834 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003835 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003836 break;
3837 case ISD::CTLZ:
3838 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003839 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003840 DAG.getConstant(MVT::getSizeInBits(NVT) -
3841 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003842 break;
3843 }
3844 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003845 case ISD::EXTRACT_SUBVECTOR:
3846 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003847 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003848 case ISD::EXTRACT_VECTOR_ELT:
3849 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3850 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003851 }
3852
3853 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003854
3855 // Make sure the result is itself legal.
3856 Result = LegalizeOp(Result);
3857
3858 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003859 AddPromotedOperand(Op, Result);
3860 return Result;
3861}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003862
Dan Gohman7f321562007-06-25 16:23:39 +00003863/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3864/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3865/// based on the vector type. The return type of this matches the element type
3866/// of the vector, which may not be legal for the target.
3867SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003868 // We know that operand #0 is the Vec vector. If the index is a constant
3869 // or if the invec is a supported hardware type, we can use it. Otherwise,
3870 // lower to a store then an indexed load.
3871 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003872 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003873
Dan Gohmane40c7b02007-09-24 15:54:53 +00003874 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00003875 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003876
Dan Gohman7f321562007-06-25 16:23:39 +00003877 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3878 default: assert(0 && "This action is not supported yet!");
3879 case TargetLowering::Custom: {
3880 Vec = LegalizeOp(Vec);
3881 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3882 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3883 if (Tmp3.Val)
3884 return Tmp3;
3885 break;
3886 }
3887 case TargetLowering::Legal:
3888 if (isTypeLegal(TVT)) {
3889 Vec = LegalizeOp(Vec);
3890 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00003891 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00003892 }
3893 break;
3894 case TargetLowering::Expand:
3895 break;
3896 }
3897
3898 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003899 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003900 Op = ScalarizeVectorOp(Vec);
3901 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3902 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003903 SDOperand Lo, Hi;
3904 SplitVectorOp(Vec, Lo, Hi);
3905 if (CIdx->getValue() < NumElems/2) {
3906 Vec = Lo;
3907 } else {
3908 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003909 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3910 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003911 }
Dan Gohman7f321562007-06-25 16:23:39 +00003912
Chris Lattner15972212006-03-31 17:55:51 +00003913 // It's now an extract from the appropriate high or low part. Recurse.
3914 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003915 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003916 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003917 // Store the value to a temporary stack slot, then LOAD the scalar
3918 // element back out.
3919 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3920 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3921
3922 // Add the offset to the index.
3923 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3924 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3925 DAG.getConstant(EltSize, Idx.getValueType()));
3926 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3927
3928 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003929 }
Dan Gohman7f321562007-06-25 16:23:39 +00003930 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003931}
3932
Dan Gohman7f321562007-06-25 16:23:39 +00003933/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003934/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003935SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003936 // We know that operand #0 is the Vec vector. For now we assume the index
3937 // is a constant and that the extracted result is a supported hardware type.
3938 SDOperand Vec = Op.getOperand(0);
3939 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3940
Dan Gohman7f321562007-06-25 16:23:39 +00003941 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003942
3943 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3944 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003945 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003946 }
3947
3948 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3949 SDOperand Lo, Hi;
3950 SplitVectorOp(Vec, Lo, Hi);
3951 if (CIdx->getValue() < NumElems/2) {
3952 Vec = Lo;
3953 } else {
3954 Vec = Hi;
3955 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3956 }
3957
3958 // It's now an extract from the appropriate high or low part. Recurse.
3959 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003960 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003961}
3962
Nate Begeman750ac1b2006-02-01 07:19:44 +00003963/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3964/// with condition CC on the current target. This usually involves legalizing
3965/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3966/// there may be no choice but to create a new SetCC node to represent the
3967/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3968/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3969void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3970 SDOperand &RHS,
3971 SDOperand &CC) {
3972 SDOperand Tmp1, Tmp2, Result;
3973
3974 switch (getTypeAction(LHS.getValueType())) {
3975 case Legal:
3976 Tmp1 = LegalizeOp(LHS); // LHS
3977 Tmp2 = LegalizeOp(RHS); // RHS
3978 break;
3979 case Promote:
3980 Tmp1 = PromoteOp(LHS); // LHS
3981 Tmp2 = PromoteOp(RHS); // RHS
3982
3983 // If this is an FP compare, the operands have already been extended.
3984 if (MVT::isInteger(LHS.getValueType())) {
3985 MVT::ValueType VT = LHS.getValueType();
3986 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3987
3988 // Otherwise, we have to insert explicit sign or zero extends. Note
3989 // that we could insert sign extends for ALL conditions, but zero extend
3990 // is cheaper on many machines (an AND instead of two shifts), so prefer
3991 // it.
3992 switch (cast<CondCodeSDNode>(CC)->get()) {
3993 default: assert(0 && "Unknown integer comparison!");
3994 case ISD::SETEQ:
3995 case ISD::SETNE:
3996 case ISD::SETUGE:
3997 case ISD::SETUGT:
3998 case ISD::SETULE:
3999 case ISD::SETULT:
4000 // ALL of these operations will work if we either sign or zero extend
4001 // the operands (including the unsigned comparisons!). Zero extend is
4002 // usually a simpler/cheaper operation, so prefer it.
4003 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4004 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4005 break;
4006 case ISD::SETGE:
4007 case ISD::SETGT:
4008 case ISD::SETLT:
4009 case ISD::SETLE:
4010 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4011 DAG.getValueType(VT));
4012 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4013 DAG.getValueType(VT));
4014 break;
4015 }
4016 }
4017 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004018 case Expand: {
4019 MVT::ValueType VT = LHS.getValueType();
4020 if (VT == MVT::f32 || VT == MVT::f64) {
4021 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004022 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004023 switch (cast<CondCodeSDNode>(CC)->get()) {
4024 case ISD::SETEQ:
4025 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004026 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004027 break;
4028 case ISD::SETNE:
4029 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004030 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004031 break;
4032 case ISD::SETGE:
4033 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004034 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004035 break;
4036 case ISD::SETLT:
4037 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004038 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004039 break;
4040 case ISD::SETLE:
4041 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004042 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004043 break;
4044 case ISD::SETGT:
4045 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004046 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004047 break;
4048 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004049 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4050 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004051 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004052 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004053 break;
4054 default:
Evan Cheng56966222007-01-12 02:11:51 +00004055 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004056 switch (cast<CondCodeSDNode>(CC)->get()) {
4057 case ISD::SETONE:
4058 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004059 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004060 // Fallthrough
4061 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004062 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004063 break;
4064 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004065 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004066 break;
4067 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004068 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004069 break;
4070 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004071 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004072 break;
Evan Cheng56966222007-01-12 02:11:51 +00004073 case ISD::SETUEQ:
4074 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004075 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004076 default: assert(0 && "Unsupported FP setcc!");
4077 }
4078 }
4079
4080 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004081 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004082 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004083 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004084 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004085 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004086 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004087 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004088 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004089 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004090 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004091 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004092 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004093 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4094 Tmp2 = SDOperand();
4095 }
4096 LHS = Tmp1;
4097 RHS = Tmp2;
4098 return;
4099 }
4100
Nate Begeman750ac1b2006-02-01 07:19:44 +00004101 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4102 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00004103 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004104 switch (cast<CondCodeSDNode>(CC)->get()) {
4105 case ISD::SETEQ:
4106 case ISD::SETNE:
4107 if (RHSLo == RHSHi)
4108 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4109 if (RHSCST->isAllOnesValue()) {
4110 // Comparison to -1.
4111 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4112 Tmp2 = RHSLo;
4113 break;
4114 }
4115
4116 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4117 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4118 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4119 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4120 break;
4121 default:
4122 // If this is a comparison of the sign bit, just look at the top part.
4123 // X > -1, x < 0
4124 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4125 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4126 CST->getValue() == 0) || // X < 0
4127 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4128 CST->isAllOnesValue())) { // X > -1
4129 Tmp1 = LHSHi;
4130 Tmp2 = RHSHi;
4131 break;
4132 }
4133
4134 // FIXME: This generated code sucks.
4135 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004136 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4137 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004138 default: assert(0 && "Unknown integer setcc!");
4139 case ISD::SETLT:
4140 case ISD::SETULT: LowCC = ISD::SETULT; break;
4141 case ISD::SETGT:
4142 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4143 case ISD::SETLE:
4144 case ISD::SETULE: LowCC = ISD::SETULE; break;
4145 case ISD::SETGE:
4146 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4147 }
4148
4149 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4150 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4151 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4152
4153 // NOTE: on targets without efficient SELECT of bools, we can always use
4154 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004155 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4156 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4157 false, DagCombineInfo);
4158 if (!Tmp1.Val)
4159 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4160 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4161 CCCode, false, DagCombineInfo);
4162 if (!Tmp2.Val)
4163 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4164
4165 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4166 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4167 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4168 (Tmp2C && Tmp2C->getValue() == 0 &&
4169 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4170 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4171 (Tmp2C && Tmp2C->getValue() == 1 &&
4172 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4173 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4174 // low part is known false, returns high part.
4175 // For LE / GE, if high part is known false, ignore the low part.
4176 // For LT / GT, if high part is known true, ignore the low part.
4177 Tmp1 = Tmp2;
4178 Tmp2 = SDOperand();
4179 } else {
4180 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4181 ISD::SETEQ, false, DagCombineInfo);
4182 if (!Result.Val)
4183 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4184 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4185 Result, Tmp1, Tmp2));
4186 Tmp1 = Result;
4187 Tmp2 = SDOperand();
4188 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004189 }
4190 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004191 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004192 LHS = Tmp1;
4193 RHS = Tmp2;
4194}
4195
Chris Lattner35481892005-12-23 00:16:34 +00004196/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004197/// The resultant code need not be legal. Note that SrcOp is the input operand
4198/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004199SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4200 SDOperand SrcOp) {
4201 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00004202 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004203
4204 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004205 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004206 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004207 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004208}
4209
Chris Lattner4352cc92006-04-04 17:23:26 +00004210SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4211 // Create a vector sized/aligned stack slot, store the value to element #0,
4212 // then load the whole vector back out.
4213 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004214 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004215 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004216 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004217}
4218
4219
Chris Lattnerce872152006-03-19 06:31:19 +00004220/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004221/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004222SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4223
4224 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004225 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004226 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004227 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004228 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004229 std::map<SDOperand, std::vector<unsigned> > Values;
4230 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004231 bool isConstant = true;
4232 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4233 SplatValue.getOpcode() != ISD::UNDEF)
4234 isConstant = false;
4235
Evan Cheng033e6812006-03-24 01:17:21 +00004236 for (unsigned i = 1; i < NumElems; ++i) {
4237 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004238 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004239 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004240 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004241 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004242 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004243
4244 // If this isn't a constant element or an undef, we can't use a constant
4245 // pool load.
4246 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4247 V.getOpcode() != ISD::UNDEF)
4248 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004249 }
4250
4251 if (isOnlyLowElement) {
4252 // If the low element is an undef too, then this whole things is an undef.
4253 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4254 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4255 // Otherwise, turn this into a scalar_to_vector node.
4256 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4257 Node->getOperand(0));
4258 }
4259
Chris Lattner2eb86532006-03-24 07:29:17 +00004260 // If all elements are constants, create a load from the constant pool.
4261 if (isConstant) {
4262 MVT::ValueType VT = Node->getValueType(0);
4263 const Type *OpNTy =
4264 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4265 std::vector<Constant*> CV;
4266 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4267 if (ConstantFPSDNode *V =
4268 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004269 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004270 } else if (ConstantSDNode *V =
4271 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004272 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004273 } else {
4274 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4275 CV.push_back(UndefValue::get(OpNTy));
4276 }
4277 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004278 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004279 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004280 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004281 }
4282
Chris Lattner87100e02006-03-20 01:52:29 +00004283 if (SplatValue.Val) { // Splat of one value?
4284 // Build the shuffle constant vector: <0, 0, 0, 0>
4285 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004286 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004287 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004288 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004289 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4290 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004291
4292 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004293 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004294 // Get the splatted value into the low element of a vector register.
4295 SDOperand LowValVec =
4296 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4297
4298 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4299 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4300 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4301 SplatMask);
4302 }
4303 }
4304
Evan Cheng033e6812006-03-24 01:17:21 +00004305 // If there are only two unique elements, we may be able to turn this into a
4306 // vector shuffle.
4307 if (Values.size() == 2) {
4308 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4309 MVT::ValueType MaskVT =
4310 MVT::getIntVectorWithNumElements(NumElems);
4311 std::vector<SDOperand> MaskVec(NumElems);
4312 unsigned i = 0;
4313 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4314 E = Values.end(); I != E; ++I) {
4315 for (std::vector<unsigned>::iterator II = I->second.begin(),
4316 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004317 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004318 i += NumElems;
4319 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004320 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4321 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004322
4323 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004324 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4325 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004326 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004327 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4328 E = Values.end(); I != E; ++I) {
4329 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4330 I->first);
4331 Ops.push_back(Op);
4332 }
4333 Ops.push_back(ShuffleMask);
4334
4335 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004336 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4337 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004338 }
4339 }
Chris Lattnerce872152006-03-19 06:31:19 +00004340
4341 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4342 // aligned object on the stack, store each element into it, then load
4343 // the result as a vector.
4344 MVT::ValueType VT = Node->getValueType(0);
4345 // Create the stack frame object.
4346 SDOperand FIPtr = CreateStackTemporary(VT);
4347
4348 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004349 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004350 unsigned TypeByteSize =
4351 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004352 // Store (in the right endianness) the elements to memory.
4353 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4354 // Ignore undef elements.
4355 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4356
Chris Lattner841c8822006-03-22 01:46:54 +00004357 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004358
4359 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4360 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4361
Evan Cheng786225a2006-10-05 23:01:46 +00004362 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004363 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004364 }
4365
4366 SDOperand StoreChain;
4367 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004368 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4369 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004370 else
4371 StoreChain = DAG.getEntryNode();
4372
4373 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004374 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004375}
4376
4377/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4378/// specified value type.
4379SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4380 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4381 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004382 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004383 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004384 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004385 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4386}
4387
Chris Lattner5b359c62005-04-02 04:00:59 +00004388void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4389 SDOperand Op, SDOperand Amt,
4390 SDOperand &Lo, SDOperand &Hi) {
4391 // Expand the subcomponents.
4392 SDOperand LHSL, LHSH;
4393 ExpandOp(Op, LHSL, LHSH);
4394
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004395 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004396 MVT::ValueType VT = LHSL.getValueType();
4397 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004398 Hi = Lo.getValue(1);
4399}
4400
4401
Chris Lattnere34b3962005-01-19 04:19:40 +00004402/// ExpandShift - Try to find a clever way to expand this shift operation out to
4403/// smaller elements. If we can't find a way that is more efficient than a
4404/// libcall on this target, return false. Otherwise, return true with the
4405/// low-parts expanded into Lo and Hi.
4406bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4407 SDOperand &Lo, SDOperand &Hi) {
4408 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4409 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004410
Chris Lattnere34b3962005-01-19 04:19:40 +00004411 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004412 SDOperand ShAmt = LegalizeOp(Amt);
4413 MVT::ValueType ShTy = ShAmt.getValueType();
4414 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4415 unsigned NVTBits = MVT::getSizeInBits(NVT);
4416
4417 // Handle the case when Amt is an immediate. Other cases are currently broken
4418 // and are disabled.
4419 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4420 unsigned Cst = CN->getValue();
4421 // Expand the incoming operand to be shifted, so that we have its parts
4422 SDOperand InL, InH;
4423 ExpandOp(Op, InL, InH);
4424 switch(Opc) {
4425 case ISD::SHL:
4426 if (Cst > VTBits) {
4427 Lo = DAG.getConstant(0, NVT);
4428 Hi = DAG.getConstant(0, NVT);
4429 } else if (Cst > NVTBits) {
4430 Lo = DAG.getConstant(0, NVT);
4431 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004432 } else if (Cst == NVTBits) {
4433 Lo = DAG.getConstant(0, NVT);
4434 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004435 } else {
4436 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4437 Hi = DAG.getNode(ISD::OR, NVT,
4438 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4439 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4440 }
4441 return true;
4442 case ISD::SRL:
4443 if (Cst > VTBits) {
4444 Lo = DAG.getConstant(0, NVT);
4445 Hi = DAG.getConstant(0, NVT);
4446 } else if (Cst > NVTBits) {
4447 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4448 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004449 } else if (Cst == NVTBits) {
4450 Lo = InH;
4451 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004452 } else {
4453 Lo = DAG.getNode(ISD::OR, NVT,
4454 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4455 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4456 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4457 }
4458 return true;
4459 case ISD::SRA:
4460 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004461 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004462 DAG.getConstant(NVTBits-1, ShTy));
4463 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004464 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004465 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004466 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004467 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004468 } else if (Cst == NVTBits) {
4469 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004470 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004471 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004472 } else {
4473 Lo = DAG.getNode(ISD::OR, NVT,
4474 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4475 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4476 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4477 }
4478 return true;
4479 }
4480 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004481
4482 // Okay, the shift amount isn't constant. However, if we can tell that it is
4483 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4484 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004485 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004486
4487 // If we know that the high bit of the shift amount is one, then we can do
4488 // this as a couple of simple shifts.
4489 if (KnownOne & Mask) {
4490 // Mask out the high bit, which we know is set.
4491 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4492 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4493
4494 // Expand the incoming operand to be shifted, so that we have its parts
4495 SDOperand InL, InH;
4496 ExpandOp(Op, InL, InH);
4497 switch(Opc) {
4498 case ISD::SHL:
4499 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4500 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4501 return true;
4502 case ISD::SRL:
4503 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4504 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4505 return true;
4506 case ISD::SRA:
4507 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4508 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4509 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4510 return true;
4511 }
4512 }
4513
4514 // If we know that the high bit of the shift amount is zero, then we can do
4515 // this as a couple of simple shifts.
4516 if (KnownZero & Mask) {
4517 // Compute 32-amt.
4518 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4519 DAG.getConstant(NVTBits, Amt.getValueType()),
4520 Amt);
4521
4522 // Expand the incoming operand to be shifted, so that we have its parts
4523 SDOperand InL, InH;
4524 ExpandOp(Op, InL, InH);
4525 switch(Opc) {
4526 case ISD::SHL:
4527 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4528 Hi = DAG.getNode(ISD::OR, NVT,
4529 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4530 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4531 return true;
4532 case ISD::SRL:
4533 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4534 Lo = DAG.getNode(ISD::OR, NVT,
4535 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4536 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4537 return true;
4538 case ISD::SRA:
4539 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4540 Lo = DAG.getNode(ISD::OR, NVT,
4541 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4542 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4543 return true;
4544 }
4545 }
4546
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004547 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004548}
Chris Lattner77e77a62005-01-21 06:05:23 +00004549
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004550
Chris Lattner77e77a62005-01-21 06:05:23 +00004551// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4552// does not fit into a register, return the lo part and set the hi part to the
4553// by-reg argument. If it does fit into a single register, return the result
4554// and leave the Hi part unset.
4555SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004556 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004557 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4558 // The input chain to this libcall is the entry node of the function.
4559 // Legalizing the call will automatically add the previous call to the
4560 // dependence.
4561 SDOperand InChain = DAG.getEntryNode();
4562
Chris Lattner77e77a62005-01-21 06:05:23 +00004563 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004564 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004565 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4566 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4567 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004568 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004569 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004570 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004571 }
4572 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004573
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004574 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004575 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004576 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004577 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004578 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004579
Chris Lattner6831a812006-02-13 09:18:02 +00004580 // Legalize the call sequence, starting with the chain. This will advance
4581 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4582 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4583 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004584 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004585 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004586 default: assert(0 && "Unknown thing");
4587 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004588 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004589 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004590 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004591 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004592 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004593 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004594 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004595}
4596
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004597
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004598/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4599///
Chris Lattner77e77a62005-01-21 06:05:23 +00004600SDOperand SelectionDAGLegalize::
4601ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004602 assert(getTypeAction(Source.getValueType()) == Expand &&
4603 "This is not an expansion!");
4604 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4605
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004606 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004607 assert(Source.getValueType() == MVT::i64 &&
4608 "This only works for 64-bit -> FP");
4609 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4610 // incoming integer is set. To handle this, we dynamically test to see if
4611 // it is set, and, if so, add a fudge factor.
4612 SDOperand Lo, Hi;
4613 ExpandOp(Source, Lo, Hi);
4614
Chris Lattner66de05b2005-05-13 04:45:13 +00004615 // If this is unsigned, and not supported, first perform the conversion to
4616 // signed, then adjust the result if the sign bit is set.
4617 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4618 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4619
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004620 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4621 DAG.getConstant(0, Hi.getValueType()),
4622 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004623 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4624 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4625 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004626 uint64_t FF = 0x5f800000ULL;
4627 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004628 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004629
Chris Lattner5839bf22005-08-26 17:15:30 +00004630 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004631 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4632 SDOperand FudgeInReg;
4633 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004634 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004635 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004636 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004637 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004638 CPIdx, NULL, 0, MVT::f32);
4639 else
4640 assert(0 && "Unexpected conversion");
4641
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004642 MVT::ValueType SCVT = SignedConv.getValueType();
4643 if (SCVT != DestTy) {
4644 // Destination type needs to be expanded as well. The FADD now we are
4645 // constructing will be expanded into a libcall.
4646 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4647 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4648 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4649 SignedConv, SignedConv.getValue(1));
4650 }
4651 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4652 }
Chris Lattner473a9902005-09-29 06:44:39 +00004653 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004654 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004655
Chris Lattnera88a2602005-05-14 05:33:54 +00004656 // Check to see if the target has a custom way to lower this. If so, use it.
4657 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4658 default: assert(0 && "This action not implemented for this operation!");
4659 case TargetLowering::Legal:
4660 case TargetLowering::Expand:
4661 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004662 case TargetLowering::Custom: {
4663 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4664 Source), DAG);
4665 if (NV.Val)
4666 return LegalizeOp(NV);
4667 break; // The target decided this was legal after all
4668 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004669 }
4670
Chris Lattner13689e22005-05-12 07:00:44 +00004671 // Expand the source, then glue it back together for the call. We must expand
4672 // the source in case it is shared (this pass of legalize must traverse it).
4673 SDOperand SrcLo, SrcHi;
4674 ExpandOp(Source, SrcLo, SrcHi);
4675 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4676
Evan Cheng56966222007-01-12 02:11:51 +00004677 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004678 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004679 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004680 else {
4681 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004682 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004683 }
Chris Lattner6831a812006-02-13 09:18:02 +00004684
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004685 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004686 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4687 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004688 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4689 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004690}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004691
Chris Lattner22cde6a2006-01-28 08:25:58 +00004692/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4693/// INT_TO_FP operation of the specified operand when the target requests that
4694/// we expand it. At this point, we know that the result and operand types are
4695/// legal for the target.
4696SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4697 SDOperand Op0,
4698 MVT::ValueType DestVT) {
4699 if (Op0.getValueType() == MVT::i32) {
4700 // simple 32-bit [signed|unsigned] integer to float/double expansion
4701
Chris Lattner58092e32007-01-20 22:35:55 +00004702 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004703 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004704 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4705 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004706 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004707 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004708 // get address of 8 byte buffer
4709 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4710 // word offset constant for Hi/Lo address computation
4711 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4712 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004713 SDOperand Hi = StackSlot;
4714 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4715 if (TLI.isLittleEndian())
4716 std::swap(Hi, Lo);
4717
Chris Lattner22cde6a2006-01-28 08:25:58 +00004718 // if signed map to unsigned space
4719 SDOperand Op0Mapped;
4720 if (isSigned) {
4721 // constant used to invert sign bit (signed to unsigned mapping)
4722 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4723 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4724 } else {
4725 Op0Mapped = Op0;
4726 }
4727 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004728 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004729 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004730 // initial hi portion of constructed double
4731 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4732 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004733 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004734 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004735 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004736 // FP constant to bias correct the final result
4737 SDOperand Bias = DAG.getConstantFP(isSigned ?
4738 BitsToDouble(0x4330000080000000ULL)
4739 : BitsToDouble(0x4330000000000000ULL),
4740 MVT::f64);
4741 // subtract the bias
4742 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4743 // final result
4744 SDOperand Result;
4745 // handle final rounding
4746 if (DestVT == MVT::f64) {
4747 // do nothing
4748 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004749 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
4750 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
4751 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
4752 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004753 }
4754 return Result;
4755 }
4756 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4757 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4758
4759 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4760 DAG.getConstant(0, Op0.getValueType()),
4761 ISD::SETLT);
4762 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4763 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4764 SignSet, Four, Zero);
4765
4766 // If the sign bit of the integer is set, the large number will be treated
4767 // as a negative number. To counteract this, the dynamic code adds an
4768 // offset depending on the data type.
4769 uint64_t FF;
4770 switch (Op0.getValueType()) {
4771 default: assert(0 && "Unsupported integer type!");
4772 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4773 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4774 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4775 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4776 }
4777 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004778 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004779
4780 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4781 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4782 SDOperand FudgeInReg;
4783 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004784 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004785 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00004786 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00004787 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004788 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004789 }
4790
4791 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4792}
4793
4794/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4795/// *INT_TO_FP operation of the specified operand when the target requests that
4796/// we promote it. At this point, we know that the result and operand types are
4797/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4798/// operation that takes a larger input.
4799SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4800 MVT::ValueType DestVT,
4801 bool isSigned) {
4802 // First step, figure out the appropriate *INT_TO_FP operation to use.
4803 MVT::ValueType NewInTy = LegalOp.getValueType();
4804
4805 unsigned OpToUse = 0;
4806
4807 // Scan for the appropriate larger type to use.
4808 while (1) {
4809 NewInTy = (MVT::ValueType)(NewInTy+1);
4810 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4811
4812 // If the target supports SINT_TO_FP of this type, use it.
4813 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4814 default: break;
4815 case TargetLowering::Legal:
4816 if (!TLI.isTypeLegal(NewInTy))
4817 break; // Can't use this datatype.
4818 // FALL THROUGH.
4819 case TargetLowering::Custom:
4820 OpToUse = ISD::SINT_TO_FP;
4821 break;
4822 }
4823 if (OpToUse) break;
4824 if (isSigned) continue;
4825
4826 // If the target supports UINT_TO_FP of this type, use it.
4827 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4828 default: break;
4829 case TargetLowering::Legal:
4830 if (!TLI.isTypeLegal(NewInTy))
4831 break; // Can't use this datatype.
4832 // FALL THROUGH.
4833 case TargetLowering::Custom:
4834 OpToUse = ISD::UINT_TO_FP;
4835 break;
4836 }
4837 if (OpToUse) break;
4838
4839 // Otherwise, try a larger type.
4840 }
4841
4842 // Okay, we found the operation and type to use. Zero extend our input to the
4843 // desired type then run the operation on it.
4844 return DAG.getNode(OpToUse, DestVT,
4845 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4846 NewInTy, LegalOp));
4847}
4848
4849/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4850/// FP_TO_*INT operation of the specified operand when the target requests that
4851/// we promote it. At this point, we know that the result and operand types are
4852/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4853/// operation that returns a larger result.
4854SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4855 MVT::ValueType DestVT,
4856 bool isSigned) {
4857 // First step, figure out the appropriate FP_TO*INT operation to use.
4858 MVT::ValueType NewOutTy = DestVT;
4859
4860 unsigned OpToUse = 0;
4861
4862 // Scan for the appropriate larger type to use.
4863 while (1) {
4864 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4865 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4866
4867 // If the target supports FP_TO_SINT returning this type, use it.
4868 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4869 default: break;
4870 case TargetLowering::Legal:
4871 if (!TLI.isTypeLegal(NewOutTy))
4872 break; // Can't use this datatype.
4873 // FALL THROUGH.
4874 case TargetLowering::Custom:
4875 OpToUse = ISD::FP_TO_SINT;
4876 break;
4877 }
4878 if (OpToUse) break;
4879
4880 // If the target supports FP_TO_UINT of this type, use it.
4881 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4882 default: break;
4883 case TargetLowering::Legal:
4884 if (!TLI.isTypeLegal(NewOutTy))
4885 break; // Can't use this datatype.
4886 // FALL THROUGH.
4887 case TargetLowering::Custom:
4888 OpToUse = ISD::FP_TO_UINT;
4889 break;
4890 }
4891 if (OpToUse) break;
4892
4893 // Otherwise, try a larger type.
4894 }
4895
4896 // Okay, we found the operation and type to use. Truncate the result of the
4897 // extended FP_TO_*INT operation to the desired size.
4898 return DAG.getNode(ISD::TRUNCATE, DestVT,
4899 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4900}
4901
4902/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4903///
4904SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4905 MVT::ValueType VT = Op.getValueType();
4906 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4907 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4908 switch (VT) {
4909 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4910 case MVT::i16:
4911 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4912 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4913 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4914 case MVT::i32:
4915 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4916 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4917 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4918 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4919 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4920 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4921 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4922 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4923 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4924 case MVT::i64:
4925 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4926 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4927 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4928 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4929 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4930 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4931 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4932 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4933 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4934 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4935 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4936 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4937 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4938 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4939 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4940 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4941 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4942 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4943 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4944 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4945 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4946 }
4947}
4948
4949/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4950///
4951SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4952 switch (Opc) {
4953 default: assert(0 && "Cannot expand this yet!");
4954 case ISD::CTPOP: {
4955 static const uint64_t mask[6] = {
4956 0x5555555555555555ULL, 0x3333333333333333ULL,
4957 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4958 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4959 };
4960 MVT::ValueType VT = Op.getValueType();
4961 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004962 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004963 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4964 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4965 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4966 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4967 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4968 DAG.getNode(ISD::AND, VT,
4969 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4970 }
4971 return Op;
4972 }
4973 case ISD::CTLZ: {
4974 // for now, we do this:
4975 // x = x | (x >> 1);
4976 // x = x | (x >> 2);
4977 // ...
4978 // x = x | (x >>16);
4979 // x = x | (x >>32); // for 64-bit input
4980 // return popcount(~x);
4981 //
4982 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4983 MVT::ValueType VT = Op.getValueType();
4984 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004985 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004986 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4987 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4988 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4989 }
4990 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4991 return DAG.getNode(ISD::CTPOP, VT, Op);
4992 }
4993 case ISD::CTTZ: {
4994 // for now, we use: { return popcount(~x & (x - 1)); }
4995 // unless the target has ctlz but not ctpop, in which case we use:
4996 // { return 32 - nlz(~x & (x-1)); }
4997 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4998 MVT::ValueType VT = Op.getValueType();
4999 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5000 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5001 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5002 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5003 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5004 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5005 TLI.isOperationLegal(ISD::CTLZ, VT))
5006 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005007 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005008 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5009 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5010 }
5011 }
5012}
Chris Lattnere34b3962005-01-19 04:19:40 +00005013
Chris Lattner3e928bb2005-01-07 07:47:09 +00005014/// ExpandOp - Expand the specified SDOperand into its two component pieces
5015/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5016/// LegalizeNodes map is filled in for any results that are not expanded, the
5017/// ExpandedNodes map is filled in for any results that are expanded, and the
5018/// Lo/Hi values are returned.
5019void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5020 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005021 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005022 SDNode *Node = Op.Val;
5023 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005024 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005025 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005026 "Cannot expand to FP value or to larger int value!");
5027
Chris Lattner6fdcb252005-09-02 20:32:45 +00005028 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005029 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005030 = ExpandedNodes.find(Op);
5031 if (I != ExpandedNodes.end()) {
5032 Lo = I->second.first;
5033 Hi = I->second.second;
5034 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005035 }
5036
Chris Lattner3e928bb2005-01-07 07:47:09 +00005037 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005038 case ISD::CopyFromReg:
5039 assert(0 && "CopyFromReg must be legal!");
5040 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005041#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005042 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005043#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005044 assert(0 && "Do not know how to expand this operator!");
5045 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005046 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005047 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005048 Lo = DAG.getNode(ISD::UNDEF, NVT);
5049 Hi = DAG.getNode(ISD::UNDEF, NVT);
5050 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005051 case ISD::Constant: {
5052 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5053 Lo = DAG.getConstant(Cst, NVT);
5054 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5055 break;
5056 }
Evan Cheng00495212006-12-12 21:32:44 +00005057 case ISD::ConstantFP: {
5058 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00005059 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005060 if (getTypeAction(Lo.getValueType()) == Expand)
5061 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005062 break;
5063 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005064 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005065 // Return the operands.
5066 Lo = Node->getOperand(0);
5067 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005068 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005069
5070 case ISD::SIGN_EXTEND_INREG:
5071 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005072 // sext_inreg the low part if needed.
5073 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5074
5075 // The high part gets the sign extension from the lo-part. This handles
5076 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005077 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5078 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5079 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005080 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005081
Nate Begemand88fc032006-01-14 03:14:10 +00005082 case ISD::BSWAP: {
5083 ExpandOp(Node->getOperand(0), Lo, Hi);
5084 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5085 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5086 Lo = TempLo;
5087 break;
5088 }
5089
Chris Lattneredb1add2005-05-11 04:51:16 +00005090 case ISD::CTPOP:
5091 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005092 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5093 DAG.getNode(ISD::CTPOP, NVT, Lo),
5094 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005095 Hi = DAG.getConstant(0, NVT);
5096 break;
5097
Chris Lattner39a8f332005-05-12 19:05:01 +00005098 case ISD::CTLZ: {
5099 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+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 HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005103 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5104 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005105 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5106 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5107
5108 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5109 Hi = DAG.getConstant(0, NVT);
5110 break;
5111 }
5112
5113 case ISD::CTTZ: {
5114 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005115 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005116 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5117 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005118 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5119 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005120 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5121 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5122
5123 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5124 Hi = DAG.getConstant(0, NVT);
5125 break;
5126 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005127
Nate Begemanacc398c2006-01-25 18:21:52 +00005128 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005129 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5130 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005131 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5132 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5133
5134 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005135 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005136 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5137 if (!TLI.isLittleEndian())
5138 std::swap(Lo, Hi);
5139 break;
5140 }
5141
Chris Lattner3e928bb2005-01-07 07:47:09 +00005142 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005143 LoadSDNode *LD = cast<LoadSDNode>(Node);
5144 SDOperand Ch = LD->getChain(); // Legalize the chain.
5145 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5146 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005147 int SVOffset = LD->getSrcValueOffset();
5148 unsigned Alignment = LD->getAlignment();
5149 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005150
Evan Cheng466685d2006-10-09 20:57:25 +00005151 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005152 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5153 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005154 if (VT == MVT::f32 || VT == MVT::f64) {
5155 // f32->i32 or f64->i64 one to one expansion.
5156 // Remember that we legalized the chain.
5157 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005158 // Recursively expand the new load.
5159 if (getTypeAction(NVT) == Expand)
5160 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005161 break;
5162 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005163
Evan Cheng466685d2006-10-09 20:57:25 +00005164 // Increment the pointer to the other half.
5165 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5166 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5167 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005168 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005169 if (Alignment > IncrementSize)
5170 Alignment = IncrementSize;
5171 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5172 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005173
Evan Cheng466685d2006-10-09 20:57:25 +00005174 // Build a factor node to remember that this load is independent of the
5175 // other one.
5176 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5177 Hi.getValue(1));
5178
5179 // Remember that we legalized the chain.
5180 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5181 if (!TLI.isLittleEndian())
5182 std::swap(Lo, Hi);
5183 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005184 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005185
5186 if (VT == MVT::f64 && EVT == MVT::f32) {
5187 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5188 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005189 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005190 // Remember that we legalized the chain.
5191 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5192 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5193 break;
5194 }
Evan Cheng466685d2006-10-09 20:57:25 +00005195
5196 if (EVT == NVT)
5197 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005198 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005199 else
5200 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005201 SVOffset, EVT, isVolatile,
5202 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005203
5204 // Remember that we legalized the chain.
5205 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5206
5207 if (ExtType == ISD::SEXTLOAD) {
5208 // The high part is obtained by SRA'ing all but one of the bits of the
5209 // lo part.
5210 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5211 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5212 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5213 } else if (ExtType == ISD::ZEXTLOAD) {
5214 // The high part is just a zero.
5215 Hi = DAG.getConstant(0, NVT);
5216 } else /* if (ExtType == ISD::EXTLOAD) */ {
5217 // The high part is undefined.
5218 Hi = DAG.getNode(ISD::UNDEF, NVT);
5219 }
5220 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005221 break;
5222 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005223 case ISD::AND:
5224 case ISD::OR:
5225 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5226 SDOperand LL, LH, RL, RH;
5227 ExpandOp(Node->getOperand(0), LL, LH);
5228 ExpandOp(Node->getOperand(1), RL, RH);
5229 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5230 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5231 break;
5232 }
5233 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005234 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005235 ExpandOp(Node->getOperand(1), LL, LH);
5236 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005237 if (getTypeAction(NVT) == Expand)
5238 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005239 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005240 if (VT != MVT::f32)
5241 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005242 break;
5243 }
Nate Begeman9373a812005-08-10 20:51:12 +00005244 case ISD::SELECT_CC: {
5245 SDOperand TL, TH, FL, FH;
5246 ExpandOp(Node->getOperand(2), TL, TH);
5247 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005248 if (getTypeAction(NVT) == Expand)
5249 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005250 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5251 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005252 if (VT != MVT::f32)
5253 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5254 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005255 break;
5256 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005257 case ISD::ANY_EXTEND:
5258 // The low part is any extension of the input (which degenerates to a copy).
5259 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5260 // The high part is undefined.
5261 Hi = DAG.getNode(ISD::UNDEF, NVT);
5262 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005263 case ISD::SIGN_EXTEND: {
5264 // The low part is just a sign extension of the input (which degenerates to
5265 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005266 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005267
Chris Lattner3e928bb2005-01-07 07:47:09 +00005268 // The high part is obtained by SRA'ing all but one of the bits of the lo
5269 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005270 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005271 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5272 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005273 break;
5274 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005275 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005276 // The low part is just a zero extension of the input (which degenerates to
5277 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005278 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005279
Chris Lattner3e928bb2005-01-07 07:47:09 +00005280 // The high part is just a zero.
5281 Hi = DAG.getConstant(0, NVT);
5282 break;
Chris Lattner35481892005-12-23 00:16:34 +00005283
Chris Lattner4c948eb2007-02-13 23:55:16 +00005284 case ISD::TRUNCATE: {
5285 // The input value must be larger than this value. Expand *it*.
5286 SDOperand NewLo;
5287 ExpandOp(Node->getOperand(0), NewLo, Hi);
5288
5289 // The low part is now either the right size, or it is closer. If not the
5290 // right size, make an illegal truncate so we recursively expand it.
5291 if (NewLo.getValueType() != Node->getValueType(0))
5292 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5293 ExpandOp(NewLo, Lo, Hi);
5294 break;
5295 }
5296
Chris Lattner35481892005-12-23 00:16:34 +00005297 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005298 SDOperand Tmp;
5299 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5300 // If the target wants to, allow it to lower this itself.
5301 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5302 case Expand: assert(0 && "cannot expand FP!");
5303 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5304 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5305 }
5306 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5307 }
5308
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005309 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005310 if (VT == MVT::f32 || VT == MVT::f64) {
5311 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005312 if (getTypeAction(NVT) == Expand)
5313 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005314 break;
5315 }
5316
5317 // If source operand will be expanded to the same type as VT, i.e.
5318 // i64 <- f64, i32 <- f32, expand the source operand instead.
5319 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5320 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5321 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005322 break;
5323 }
5324
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005325 // Turn this into a load/store pair by default.
5326 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005327 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005328
Chris Lattner35481892005-12-23 00:16:34 +00005329 ExpandOp(Tmp, Lo, Hi);
5330 break;
5331 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005332
Chris Lattner8137c9e2006-01-28 05:07:51 +00005333 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005334 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5335 TargetLowering::Custom &&
5336 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005337 Lo = TLI.LowerOperation(Op, DAG);
5338 assert(Lo.Val && "Node must be custom expanded!");
5339 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005340 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005341 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005342 break;
5343
Chris Lattner4e6c7462005-01-08 19:27:05 +00005344 // These operators cannot be expanded directly, emit them as calls to
5345 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005346 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005347 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005348 SDOperand Op;
5349 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5350 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005351 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5352 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005353 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005354
Chris Lattnerf20d1832005-07-30 01:40:57 +00005355 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5356
Chris Lattner80a3e942005-07-29 00:33:32 +00005357 // Now that the custom expander is done, expand the result, which is still
5358 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005359 if (Op.Val) {
5360 ExpandOp(Op, Lo, Hi);
5361 break;
5362 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005363 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005364
Evan Cheng56966222007-01-12 02:11:51 +00005365 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005366 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005367 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005368 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005369 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005370 else
5371 LC = RTLIB::FPTOSINT_LD_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005372 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5373 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005374 break;
Evan Cheng56966222007-01-12 02:11:51 +00005375 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005376
Evan Cheng56966222007-01-12 02:11:51 +00005377 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005378 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005379 SDOperand Op;
5380 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5381 case Expand: assert(0 && "cannot expand FP!");
5382 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5383 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5384 }
5385
5386 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5387
5388 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005389 if (Op.Val) {
5390 ExpandOp(Op, Lo, Hi);
5391 break;
5392 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005393 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005394
Evan Cheng56966222007-01-12 02:11:51 +00005395 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005396 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005397 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005398 else
Evan Cheng56966222007-01-12 02:11:51 +00005399 LC = RTLIB::FPTOUINT_F64_I64;
5400 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5401 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005402 break;
Evan Cheng56966222007-01-12 02:11:51 +00005403 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005404
Evan Cheng05a2d562006-01-09 18:31:59 +00005405 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005406 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005407 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005408 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005409 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005410 Op = TLI.LowerOperation(Op, DAG);
5411 if (Op.Val) {
5412 // Now that the custom expander is done, expand the result, which is
5413 // still VT.
5414 ExpandOp(Op, Lo, Hi);
5415 break;
5416 }
5417 }
5418
Chris Lattner79980b02006-09-13 03:50:39 +00005419 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5420 // this X << 1 as X+X.
5421 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5422 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5423 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5424 SDOperand LoOps[2], HiOps[3];
5425 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5426 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5427 LoOps[1] = LoOps[0];
5428 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5429
5430 HiOps[1] = HiOps[0];
5431 HiOps[2] = Lo.getValue(1);
5432 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5433 break;
5434 }
5435 }
5436
Chris Lattnere34b3962005-01-19 04:19:40 +00005437 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005438 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005439 break;
Chris Lattner47599822005-04-02 03:38:53 +00005440
5441 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005442 TargetLowering::LegalizeAction Action =
5443 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5444 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5445 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005446 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005447 break;
5448 }
5449
Chris Lattnere34b3962005-01-19 04:19:40 +00005450 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005451 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5452 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005453 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005454 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005455
Evan Cheng05a2d562006-01-09 18:31:59 +00005456 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005457 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005458 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005459 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005460 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005461 Op = TLI.LowerOperation(Op, DAG);
5462 if (Op.Val) {
5463 // Now that the custom expander is done, expand the result, which is
5464 // still VT.
5465 ExpandOp(Op, Lo, Hi);
5466 break;
5467 }
5468 }
5469
Chris Lattnere34b3962005-01-19 04:19:40 +00005470 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005471 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005472 break;
Chris Lattner47599822005-04-02 03:38:53 +00005473
5474 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005475 TargetLowering::LegalizeAction Action =
5476 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5477 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5478 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005479 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005480 break;
5481 }
5482
Chris Lattnere34b3962005-01-19 04:19:40 +00005483 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005484 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5485 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005486 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005487 }
5488
5489 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005490 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005491 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005492 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005493 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005494 Op = TLI.LowerOperation(Op, DAG);
5495 if (Op.Val) {
5496 // Now that the custom expander is done, expand the result, which is
5497 // still VT.
5498 ExpandOp(Op, Lo, Hi);
5499 break;
5500 }
5501 }
5502
Chris Lattnere34b3962005-01-19 04:19:40 +00005503 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005504 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005505 break;
Chris Lattner47599822005-04-02 03:38:53 +00005506
5507 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005508 TargetLowering::LegalizeAction Action =
5509 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5510 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5511 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005512 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005513 break;
5514 }
5515
Chris Lattnere34b3962005-01-19 04:19:40 +00005516 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005517 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5518 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005519 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005520 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005521
Misha Brukmanedf128a2005-04-21 22:36:52 +00005522 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005523 case ISD::SUB: {
5524 // If the target wants to custom expand this, let them.
5525 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5526 TargetLowering::Custom) {
5527 Op = TLI.LowerOperation(Op, DAG);
5528 if (Op.Val) {
5529 ExpandOp(Op, Lo, Hi);
5530 break;
5531 }
5532 }
5533
5534 // Expand the subcomponents.
5535 SDOperand LHSL, LHSH, RHSL, RHSH;
5536 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5537 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005538 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5539 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005540 LoOps[0] = LHSL;
5541 LoOps[1] = RHSL;
5542 HiOps[0] = LHSH;
5543 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005544 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005545 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005546 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005547 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005548 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005549 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005550 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005551 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005552 }
Chris Lattner84f67882005-01-20 18:52:28 +00005553 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005554 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005555
5556 case ISD::ADDC:
5557 case ISD::SUBC: {
5558 // Expand the subcomponents.
5559 SDOperand LHSL, LHSH, RHSL, RHSH;
5560 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5561 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5562 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5563 SDOperand LoOps[2] = { LHSL, RHSL };
5564 SDOperand HiOps[3] = { LHSH, RHSH };
5565
5566 if (Node->getOpcode() == ISD::ADDC) {
5567 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5568 HiOps[2] = Lo.getValue(1);
5569 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5570 } else {
5571 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5572 HiOps[2] = Lo.getValue(1);
5573 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5574 }
5575 // Remember that we legalized the flag.
5576 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5577 break;
5578 }
5579 case ISD::ADDE:
5580 case ISD::SUBE: {
5581 // Expand the subcomponents.
5582 SDOperand LHSL, LHSH, RHSL, RHSH;
5583 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5584 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5585 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5586 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5587 SDOperand HiOps[3] = { LHSH, RHSH };
5588
5589 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5590 HiOps[2] = Lo.getValue(1);
5591 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5592
5593 // Remember that we legalized the flag.
5594 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5595 break;
5596 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005597 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005598 // If the target wants to custom expand this, let them.
5599 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005600 SDOperand New = TLI.LowerOperation(Op, DAG);
5601 if (New.Val) {
5602 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005603 break;
5604 }
5605 }
5606
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005607 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5608 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005609 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005610 SDOperand LL, LH, RL, RH;
5611 ExpandOp(Node->getOperand(0), LL, LH);
5612 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005613 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005614 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005615 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5616 // extended the sign bit of the low half through the upper half, and if so
5617 // emit a MULHS instead of the alternate sequence that is valid for any
5618 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005619 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005620 // is RH an extension of the sign bit of RL?
5621 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5622 RH.getOperand(1).getOpcode() == ISD::Constant &&
5623 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5624 // is LH an extension of the sign bit of LL?
5625 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5626 LH.getOperand(1).getOpcode() == ISD::Constant &&
5627 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005628 // Low part:
5629 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5630 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005631 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005632 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005633 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005634 // Low part:
5635 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5636
5637 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005638 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5639 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5640 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5641 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5642 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005643 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005644 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005645 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005646
Evan Cheng56966222007-01-12 02:11:51 +00005647 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5648 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005649 break;
5650 }
Evan Cheng56966222007-01-12 02:11:51 +00005651 case ISD::SDIV:
5652 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5653 break;
5654 case ISD::UDIV:
5655 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5656 break;
5657 case ISD::SREM:
5658 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5659 break;
5660 case ISD::UREM:
5661 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5662 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005663
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005664 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005665 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5666 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5667 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005668 break;
5669 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005670 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5671 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5672 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005673 break;
5674 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005675 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5676 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5677 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005678 break;
5679 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005680 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5681 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5682 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005683 break;
5684 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005685 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005686 break;
5687 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005688 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005689 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005690 case ISD::FPOWI:
5691 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5692 ? RTLIB::POWI_F32 : RTLIB::POWI_F64),
5693 Node, false, Hi);
5694 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005695 case ISD::FSQRT:
5696 case ISD::FSIN:
5697 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005698 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005699 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005700 case ISD::FSQRT:
5701 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5702 break;
5703 case ISD::FSIN:
5704 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5705 break;
5706 case ISD::FCOS:
5707 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5708 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005709 default: assert(0 && "Unreachable!");
5710 }
Evan Cheng56966222007-01-12 02:11:51 +00005711 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005712 break;
5713 }
Evan Cheng966bf242006-12-16 00:52:40 +00005714 case ISD::FABS: {
5715 SDOperand Mask = (VT == MVT::f64)
5716 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5717 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5718 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5719 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5720 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5721 if (getTypeAction(NVT) == Expand)
5722 ExpandOp(Lo, Lo, Hi);
5723 break;
5724 }
5725 case ISD::FNEG: {
5726 SDOperand Mask = (VT == MVT::f64)
5727 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5728 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5729 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5730 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5731 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5732 if (getTypeAction(NVT) == Expand)
5733 ExpandOp(Lo, Lo, Hi);
5734 break;
5735 }
Evan Cheng912095b2007-01-04 21:56:39 +00005736 case ISD::FCOPYSIGN: {
5737 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5738 if (getTypeAction(NVT) == Expand)
5739 ExpandOp(Lo, Lo, Hi);
5740 break;
5741 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005742 case ISD::SINT_TO_FP:
5743 case ISD::UINT_TO_FP: {
5744 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5745 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005746 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005747 if (Node->getOperand(0).getValueType() == MVT::i64) {
5748 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005749 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00005750 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005751 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005752 else if (VT == MVT::f80 || VT == MVT::f128 || VT == MVT::ppcf128) {
5753 assert(isSigned);
5754 LC = RTLIB::SINTTOFP_I64_LD;
5755 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005756 } else {
5757 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005758 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005759 else
Evan Cheng56966222007-01-12 02:11:51 +00005760 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005761 }
5762
5763 // Promote the operand if needed.
5764 if (getTypeAction(SrcVT) == Promote) {
5765 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5766 Tmp = isSigned
5767 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5768 DAG.getValueType(SrcVT))
5769 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5770 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5771 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005772
5773 const char *LibCall = TLI.getLibcallName(LC);
5774 if (LibCall)
5775 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5776 else {
5777 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5778 Node->getOperand(0));
5779 if (getTypeAction(Lo.getValueType()) == Expand)
5780 ExpandOp(Lo, Lo, Hi);
5781 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005782 break;
5783 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005784 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005785
Chris Lattner83397362005-12-21 18:02:52 +00005786 // Make sure the resultant values have been legalized themselves, unless this
5787 // is a type that requires multi-step expansion.
5788 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5789 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005790 if (Hi.Val)
5791 // Don't legalize the high part if it is expanded to a single node.
5792 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005793 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005794
5795 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005796 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005797 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005798}
5799
Dan Gohman7f321562007-06-25 16:23:39 +00005800/// SplitVectorOp - Given an operand of vector type, break it down into
5801/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005802void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5803 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005804 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005805 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00005806 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00005807 assert(NumElements > 1 && "Cannot split a single element vector!");
5808 unsigned NewNumElts = NumElements/2;
Dan Gohmane40c7b02007-09-24 15:54:53 +00005809 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00005810 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005811
5812 // See if we already split it.
5813 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5814 = SplitNodes.find(Op);
5815 if (I != SplitNodes.end()) {
5816 Lo = I->second.first;
5817 Hi = I->second.second;
5818 return;
5819 }
5820
5821 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005822 default:
5823#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005824 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005825#endif
5826 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005827 case ISD::BUILD_PAIR:
5828 Lo = Node->getOperand(0);
5829 Hi = Node->getOperand(1);
5830 break;
5831 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005832 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5833 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005834 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005835
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005836 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005837 Node->op_end());
5838 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005839 break;
5840 }
Dan Gohman7f321562007-06-25 16:23:39 +00005841 case ISD::CONCAT_VECTORS: {
5842 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5843 if (NewNumSubvectors == 1) {
5844 Lo = Node->getOperand(0);
5845 Hi = Node->getOperand(1);
5846 } else {
5847 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5848 Node->op_begin()+NewNumSubvectors);
5849 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005850
Dan Gohman7f321562007-06-25 16:23:39 +00005851 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5852 Node->op_end());
5853 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5854 }
Dan Gohman65956352007-06-13 15:12:02 +00005855 break;
5856 }
Dan Gohman7f321562007-06-25 16:23:39 +00005857 case ISD::ADD:
5858 case ISD::SUB:
5859 case ISD::MUL:
5860 case ISD::FADD:
5861 case ISD::FSUB:
5862 case ISD::FMUL:
5863 case ISD::SDIV:
5864 case ISD::UDIV:
5865 case ISD::FDIV:
5866 case ISD::AND:
5867 case ISD::OR:
5868 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005869 SDOperand LL, LH, RL, RH;
5870 SplitVectorOp(Node->getOperand(0), LL, LH);
5871 SplitVectorOp(Node->getOperand(1), RL, RH);
5872
Dan Gohman7f321562007-06-25 16:23:39 +00005873 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5874 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005875 break;
5876 }
Dan Gohman7f321562007-06-25 16:23:39 +00005877 case ISD::LOAD: {
5878 LoadSDNode *LD = cast<LoadSDNode>(Node);
5879 SDOperand Ch = LD->getChain();
5880 SDOperand Ptr = LD->getBasePtr();
5881 const Value *SV = LD->getSrcValue();
5882 int SVOffset = LD->getSrcValueOffset();
5883 unsigned Alignment = LD->getAlignment();
5884 bool isVolatile = LD->isVolatile();
5885
5886 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5887 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005888 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5889 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005890 SVOffset += IncrementSize;
5891 if (Alignment > IncrementSize)
5892 Alignment = IncrementSize;
5893 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005894
5895 // Build a factor node to remember that this load is independent of the
5896 // other one.
5897 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5898 Hi.getValue(1));
5899
5900 // Remember that we legalized the chain.
5901 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005902 break;
5903 }
Dan Gohman7f321562007-06-25 16:23:39 +00005904 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005905 // We know the result is a vector. The input may be either a vector or a
5906 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005907 SDOperand InOp = Node->getOperand(0);
5908 if (!MVT::isVector(InOp.getValueType()) ||
5909 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5910 // The input is a scalar or single-element vector.
5911 // Lower to a store/load so that it can be split.
5912 // FIXME: this could be improved probably.
5913 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005914
Evan Cheng786225a2006-10-05 23:01:46 +00005915 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005916 InOp, Ptr, NULL, 0);
5917 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005918 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005919 // Split the vector and convert each of the pieces now.
5920 SplitVectorOp(InOp, Lo, Hi);
5921 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5922 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5923 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005924 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005925 }
5926
5927 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005928 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005929 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005930 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005931}
5932
5933
Dan Gohman89b20c02007-06-27 14:06:22 +00005934/// ScalarizeVectorOp - Given an operand of single-element vector type
5935/// (e.g. v1f32), convert it into the equivalent operation that returns a
5936/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005937SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5938 assert(MVT::isVector(Op.getValueType()) &&
5939 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005940 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005941 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5942 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005943
Dan Gohman7f321562007-06-25 16:23:39 +00005944 // See if we already scalarized it.
5945 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5946 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005947
5948 SDOperand Result;
5949 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005950 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005951#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005952 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005953#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005954 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5955 case ISD::ADD:
5956 case ISD::FADD:
5957 case ISD::SUB:
5958 case ISD::FSUB:
5959 case ISD::MUL:
5960 case ISD::FMUL:
5961 case ISD::SDIV:
5962 case ISD::UDIV:
5963 case ISD::FDIV:
5964 case ISD::SREM:
5965 case ISD::UREM:
5966 case ISD::FREM:
5967 case ISD::AND:
5968 case ISD::OR:
5969 case ISD::XOR:
5970 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005971 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005972 ScalarizeVectorOp(Node->getOperand(0)),
5973 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005974 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005975 case ISD::FNEG:
5976 case ISD::FABS:
5977 case ISD::FSQRT:
5978 case ISD::FSIN:
5979 case ISD::FCOS:
5980 Result = DAG.getNode(Node->getOpcode(),
5981 NewVT,
5982 ScalarizeVectorOp(Node->getOperand(0)));
5983 break;
5984 case ISD::LOAD: {
5985 LoadSDNode *LD = cast<LoadSDNode>(Node);
5986 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5987 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005988
Dan Gohman7f321562007-06-25 16:23:39 +00005989 const Value *SV = LD->getSrcValue();
5990 int SVOffset = LD->getSrcValueOffset();
5991 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5992 LD->isVolatile(), LD->getAlignment());
5993
Chris Lattnerc7029802006-03-18 01:44:44 +00005994 // Remember that we legalized the chain.
5995 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5996 break;
5997 }
Dan Gohman7f321562007-06-25 16:23:39 +00005998 case ISD::BUILD_VECTOR:
5999 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006000 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006001 case ISD::INSERT_VECTOR_ELT:
6002 // Returning the inserted scalar element.
6003 Result = Node->getOperand(1);
6004 break;
6005 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006006 assert(Node->getOperand(0).getValueType() == NewVT &&
6007 "Concat of non-legal vectors not yet supported!");
6008 Result = Node->getOperand(0);
6009 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006010 case ISD::VECTOR_SHUFFLE: {
6011 // Figure out if the scalar is the LHS or RHS and return it.
6012 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6013 if (cast<ConstantSDNode>(EltNum)->getValue())
6014 Result = ScalarizeVectorOp(Node->getOperand(1));
6015 else
6016 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006017 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006018 }
6019 case ISD::EXTRACT_SUBVECTOR:
6020 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006021 assert(Result.getValueType() == NewVT);
6022 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006023 case ISD::BIT_CONVERT:
6024 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006025 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006026 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006027 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006028 ScalarizeVectorOp(Op.getOperand(1)),
6029 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006030 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006031 }
6032
6033 if (TLI.isTypeLegal(NewVT))
6034 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006035 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6036 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006037 return Result;
6038}
6039
Chris Lattner3e928bb2005-01-07 07:47:09 +00006040
6041// SelectionDAG::Legalize - This is the entry point for the file.
6042//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006043void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006044 if (ViewLegalizeDAGs) viewGraph();
6045
Chris Lattner3e928bb2005-01-07 07:47:09 +00006046 /// run - This is the main entry point to this class.
6047 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006048 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006049}
6050