blob: 688e4f9fea07c5fca399b751c8eaeea62f496b0d [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000019#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000021#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000023#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000024#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000025#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000026#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000027#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000030#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000031#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000032#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000033#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000034using namespace llvm;
35
Chris Lattner5e46a192006-04-02 03:07:27 +000036#ifndef NDEBUG
37static cl::opt<bool>
38ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
39 cl::desc("Pop up a window to show dags before legalize"));
40#else
41static const bool ViewLegalizeDAGs = 0;
42#endif
43
Chris Lattner3e928bb2005-01-07 07:47:09 +000044//===----------------------------------------------------------------------===//
45/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
46/// hacks on it until the target machine can handle it. This involves
47/// eliminating value sizes the machine cannot handle (promoting small sizes to
48/// large sizes or splitting up large values into small values) as well as
49/// eliminating operations the machine cannot handle.
50///
51/// This code also does a small amount of optimization and recognition of idioms
52/// as part of its processing. For example, if a target does not support a
53/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
54/// will attempt merge setcc and brc instructions into brcc's.
55///
56namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000057class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000058 TargetLowering &TLI;
59 SelectionDAG &DAG;
60
Chris Lattner6831a812006-02-13 09:18:02 +000061 // Libcall insertion helpers.
62
63 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
64 /// legalized. We use this to ensure that calls are properly serialized
65 /// against each other, including inserted libcalls.
66 SDOperand LastCALLSEQ_END;
67
68 /// IsLegalizingCall - This member is used *only* for purposes of providing
69 /// helpful assertions that a libcall isn't created while another call is
70 /// being legalized (which could lead to non-serialized call sequences).
71 bool IsLegalizingCall;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000074 Legal, // The target natively supports this operation.
75 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000076 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000077 };
Chris Lattner6831a812006-02-13 09:18:02 +000078
Chris Lattner3e928bb2005-01-07 07:47:09 +000079 /// ValueTypeActions - This is a bitvector that contains two bits for each
80 /// value type, where the two bits correspond to the LegalizeAction enum.
81 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000082 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000083
Chris Lattner3e928bb2005-01-07 07:47:09 +000084 /// LegalizedNodes - For nodes that are of legal width, and that have more
85 /// than one use, this map indicates what regularized operand to use. This
86 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000087 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000088
Chris Lattner03c85462005-01-15 05:21:40 +000089 /// PromotedNodes - For nodes that are below legal width, and that have more
90 /// than one use, this map indicates what promoted value to use. This allows
91 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000092 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000093
Chris Lattnerc7029802006-03-18 01:44:44 +000094 /// ExpandedNodes - For nodes that need to be expanded this map indicates
95 /// which which operands are the expanded version of the input. This allows
96 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000097 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000098
Chris Lattnerc7029802006-03-18 01:44:44 +000099 /// SplitNodes - For vector nodes that need to be split, this map indicates
100 /// which which operands are the split version of the input. This allows us
101 /// to avoid splitting the same node more than once.
102 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
103
Dan Gohman7f321562007-06-25 16:23:39 +0000104 /// ScalarizedNodes - For nodes that need to be converted from vector types to
105 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000106 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000107 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000108
Chris Lattner8afc48e2005-01-07 22:28:47 +0000109 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000110 LegalizedNodes.insert(std::make_pair(From, To));
111 // If someone requests legalization of the new node, return itself.
112 if (From != To)
113 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000114 }
Chris Lattner03c85462005-01-15 05:21:40 +0000115 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000116 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000117 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000120 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000121
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122public:
123
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000124 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000125
Chris Lattner3e928bb2005-01-07 07:47:09 +0000126 /// getTypeAction - Return how we should legalize values of this type, either
127 /// it is already legal or we need to expand it into multiple registers of
128 /// smaller integer type, or we need to promote it to a larger type.
129 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000130 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000131 }
132
133 /// isTypeLegal - Return true if this type is legal on this target.
134 ///
135 bool isTypeLegal(MVT::ValueType VT) const {
136 return getTypeAction(VT) == Legal;
137 }
138
Chris Lattner3e928bb2005-01-07 07:47:09 +0000139 void LegalizeDAG();
140
Chris Lattner456a93a2006-01-28 07:39:30 +0000141private:
Dan Gohman7f321562007-06-25 16:23:39 +0000142 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000143 /// appropriate for its type.
144 void HandleOp(SDOperand Op);
145
146 /// LegalizeOp - We know that the specified value has a legal type.
147 /// Recursively ensure that the operands have legal types, then return the
148 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000149 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000150
Dan Gohman82669522007-10-11 23:57:53 +0000151 /// UnrollVectorOp - We know that the given vector has a legal type, however
152 /// the operation it performs is not legal and is an operation that we have
153 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
154 /// operating on each element individually.
155 SDOperand UnrollVectorOp(SDOperand O);
156
Chris Lattnerc7029802006-03-18 01:44:44 +0000157 /// PromoteOp - Given an operation that produces a value in an invalid type,
158 /// promote it to compute the value into a larger type. The produced value
159 /// will have the correct bits for the low portion of the register, but no
160 /// guarantee is made about the top bits: it may be zero, sign-extended, or
161 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000162 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000163
Chris Lattnerc7029802006-03-18 01:44:44 +0000164 /// ExpandOp - Expand the specified SDOperand into its two component pieces
165 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
166 /// the LegalizeNodes map is filled in for any results that are not expanded,
167 /// the ExpandedNodes map is filled in for any results that are expanded, and
168 /// the Lo/Hi values are returned. This applies to integer types and Vector
169 /// types.
170 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
171
Dan Gohman7f321562007-06-25 16:23:39 +0000172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000174 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
Dan Gohman89b20c02007-06-27 14:06:22 +0000176 /// ScalarizeVectorOp - Given an operand of single-element vector type
177 /// (e.g. v1f32), convert it into the equivalent operation that returns a
178 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000179 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000180
Chris Lattner4352cc92006-04-04 17:23:26 +0000181 /// isShuffleLegal - Return true if a vector shuffle is legal with the
182 /// specified mask and type. Targets can specify exactly which masks they
183 /// support and the code generator is tasked with not creating illegal masks.
184 ///
185 /// Note that this will also return true for shuffles that are promoted to a
186 /// different type.
187 ///
188 /// If this is a legal shuffle, this method returns the (possibly promoted)
189 /// build_vector Mask. If it's not a legal shuffle, it returns null.
190 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
191
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000194
Nate Begeman750ac1b2006-02-01 07:19:44 +0000195 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
196
Reid Spencer47857812006-12-31 05:55:36 +0000197 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000198 SDOperand &Hi);
199 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
200 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000201
Chris Lattner35481892005-12-23 00:16:34 +0000202 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000203 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000204 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000205 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
206 SDOperand LegalOp,
207 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000208 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
209 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000210 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000212
Chris Lattner456a93a2006-01-28 07:39:30 +0000213 SDOperand ExpandBSWAP(SDOperand Op);
214 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000215 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
216 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000217 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000219
Dan Gohman7f321562007-06-25 16:23:39 +0000220 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000221 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000222
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223 SDOperand getIntPtrConstant(uint64_t Val) {
224 return DAG.getConstant(Val, TLI.getPointerTy());
225 }
226};
227}
228
Chris Lattner4352cc92006-04-04 17:23:26 +0000229/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
230/// specified mask and type. Targets can specify exactly which masks they
231/// support and the code generator is tasked with not creating illegal masks.
232///
233/// Note that this will also return true for shuffles that are promoted to a
234/// different type.
235SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
236 SDOperand Mask) const {
237 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
238 default: return 0;
239 case TargetLowering::Legal:
240 case TargetLowering::Custom:
241 break;
242 case TargetLowering::Promote: {
243 // If this is promoted to a different type, convert the shuffle mask and
244 // ask if it is legal in the promoted type!
245 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
246
247 // If we changed # elements, change the shuffle mask.
248 unsigned NumEltsGrowth =
249 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
250 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
251 if (NumEltsGrowth > 1) {
252 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000253 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000254 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
255 SDOperand InOp = Mask.getOperand(i);
256 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
257 if (InOp.getOpcode() == ISD::UNDEF)
258 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
259 else {
260 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
261 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
262 }
263 }
264 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000265 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000266 }
267 VT = NVT;
268 break;
269 }
270 }
271 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
272}
273
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000274SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
275 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
276 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000277 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000278 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279}
280
Chris Lattnere10e6f72007-06-18 21:28:10 +0000281/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
282/// contains all of a nodes operands before it contains the node.
283static void ComputeTopDownOrdering(SelectionDAG &DAG,
284 SmallVector<SDNode*, 64> &Order) {
285
286 DenseMap<SDNode*, unsigned> Visited;
287 std::vector<SDNode*> Worklist;
288 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000289
Chris Lattnere10e6f72007-06-18 21:28:10 +0000290 // Compute ordering from all of the leaves in the graphs, those (like the
291 // entry node) that have no operands.
292 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
293 E = DAG.allnodes_end(); I != E; ++I) {
294 if (I->getNumOperands() == 0) {
295 Visited[I] = 0 - 1U;
296 Worklist.push_back(I);
297 }
Chris Lattner32fca002005-10-06 01:20:27 +0000298 }
299
Chris Lattnere10e6f72007-06-18 21:28:10 +0000300 while (!Worklist.empty()) {
301 SDNode *N = Worklist.back();
302 Worklist.pop_back();
303
304 if (++Visited[N] != N->getNumOperands())
305 continue; // Haven't visited all operands yet
306
307 Order.push_back(N);
308
309 // Now that we have N in, add anything that uses it if all of their operands
310 // are now done.
311 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
312 UI != E; ++UI)
313 Worklist.push_back(*UI);
314 }
315
316 assert(Order.size() == Visited.size() &&
317 Order.size() ==
318 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
319 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000320}
321
Chris Lattner1618beb2005-07-29 00:11:56 +0000322
Chris Lattner3e928bb2005-01-07 07:47:09 +0000323void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000324 LastCALLSEQ_END = DAG.getEntryNode();
325 IsLegalizingCall = false;
326
Chris Lattnerab510a72005-10-02 17:49:46 +0000327 // The legalize process is inherently a bottom-up recursive process (users
328 // legalize their uses before themselves). Given infinite stack space, we
329 // could just start legalizing on the root and traverse the whole graph. In
330 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000331 // blocks. To avoid this problem, compute an ordering of the nodes where each
332 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000333 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000334 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000335
Chris Lattnerc7029802006-03-18 01:44:44 +0000336 for (unsigned i = 0, e = Order.size(); i != e; ++i)
337 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000338
339 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000340 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000341 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
342 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000343
344 ExpandedNodes.clear();
345 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000346 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000347 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000348 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000349
350 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000351 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352}
353
Chris Lattner6831a812006-02-13 09:18:02 +0000354
355/// FindCallEndFromCallStart - Given a chained node that is part of a call
356/// sequence, find the CALLSEQ_END node that terminates the call sequence.
357static SDNode *FindCallEndFromCallStart(SDNode *Node) {
358 if (Node->getOpcode() == ISD::CALLSEQ_END)
359 return Node;
360 if (Node->use_empty())
361 return 0; // No CallSeqEnd
362
363 // The chain is usually at the end.
364 SDOperand TheChain(Node, Node->getNumValues()-1);
365 if (TheChain.getValueType() != MVT::Other) {
366 // Sometimes it's at the beginning.
367 TheChain = SDOperand(Node, 0);
368 if (TheChain.getValueType() != MVT::Other) {
369 // Otherwise, hunt for it.
370 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
371 if (Node->getValueType(i) == MVT::Other) {
372 TheChain = SDOperand(Node, i);
373 break;
374 }
375
376 // Otherwise, we walked into a node without a chain.
377 if (TheChain.getValueType() != MVT::Other)
378 return 0;
379 }
380 }
381
382 for (SDNode::use_iterator UI = Node->use_begin(),
383 E = Node->use_end(); UI != E; ++UI) {
384
385 // Make sure to only follow users of our token chain.
386 SDNode *User = *UI;
387 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
388 if (User->getOperand(i) == TheChain)
389 if (SDNode *Result = FindCallEndFromCallStart(User))
390 return Result;
391 }
392 return 0;
393}
394
395/// FindCallStartFromCallEnd - Given a chained node that is part of a call
396/// sequence, find the CALLSEQ_START node that initiates the call sequence.
397static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
398 assert(Node && "Didn't find callseq_start for a call??");
399 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
400
401 assert(Node->getOperand(0).getValueType() == MVT::Other &&
402 "Node doesn't have a token chain argument!");
403 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
404}
405
406/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
407/// see if any uses can reach Dest. If no dest operands can get to dest,
408/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000409///
410/// Keep track of the nodes we fine that actually do lead to Dest in
411/// NodesLeadingTo. This avoids retraversing them exponential number of times.
412///
413bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000414 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000415 if (N == Dest) return true; // N certainly leads to Dest :)
416
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000417 // If we've already processed this node and it does lead to Dest, there is no
418 // need to reprocess it.
419 if (NodesLeadingTo.count(N)) return true;
420
Chris Lattner6831a812006-02-13 09:18:02 +0000421 // If the first result of this node has been already legalized, then it cannot
422 // reach N.
423 switch (getTypeAction(N->getValueType(0))) {
424 case Legal:
425 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 case Promote:
428 if (PromotedNodes.count(SDOperand(N, 0))) return false;
429 break;
430 case Expand:
431 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 }
434
435 // Okay, this node has not already been legalized. Check and legalize all
436 // operands. If none lead to Dest, then we can legalize this node.
437 bool OperandsLeadToDest = false;
438 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
439 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000440 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000441
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000442 if (OperandsLeadToDest) {
443 NodesLeadingTo.insert(N);
444 return true;
445 }
Chris Lattner6831a812006-02-13 09:18:02 +0000446
447 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000448 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000449 return false;
450}
451
Dan Gohman7f321562007-06-25 16:23:39 +0000452/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000453/// appropriate for its type.
454void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000455 MVT::ValueType VT = Op.getValueType();
456 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000457 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000458 case Legal: (void)LegalizeOp(Op); break;
459 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000460 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000461 if (!MVT::isVector(VT)) {
462 // If this is an illegal scalar, expand it into its two component
463 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000464 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000465 if (Op.getOpcode() == ISD::TargetConstant)
466 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000467 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000468 } else if (MVT::getVectorNumElements(VT) == 1) {
469 // If this is an illegal single element vector, convert it to a
470 // scalar operation.
471 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000472 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000473 // Otherwise, this is an illegal multiple element vector.
474 // Split it in half and legalize both parts.
475 SDOperand X, Y;
476 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000477 }
478 break;
479 }
480}
Chris Lattner6831a812006-02-13 09:18:02 +0000481
Evan Cheng9f877882006-12-13 20:57:08 +0000482/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
483/// a load from the constant pool.
484static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000485 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000486 bool Extend = false;
487
488 // If a FP immediate is precise when represented as a float and if the
489 // target can do an extending load from float to double, we put it into
490 // the constant pool as a float, even if it's is statically typed as a
491 // double.
492 MVT::ValueType VT = CFP->getValueType(0);
493 bool isDouble = VT == MVT::f64;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000494 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000495 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000496 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000497 if (VT!=MVT::f64 && VT!=MVT::f32)
498 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000499 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
500 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000501 }
502
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000503 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000504 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000505 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000506 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
507 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
508 VT = MVT::f32;
509 Extend = true;
510 }
511
512 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
513 if (Extend) {
514 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
515 CPIdx, NULL, 0, MVT::f32);
516 } else {
517 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
518 }
519}
520
Chris Lattner6831a812006-02-13 09:18:02 +0000521
Evan Cheng912095b2007-01-04 21:56:39 +0000522/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
523/// operations.
524static
525SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
526 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000527 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000528 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000529 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
530 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000531 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000532
Evan Cheng912095b2007-01-04 21:56:39 +0000533 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000534 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
536 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000537 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000538 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000539 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000540 // Shift right or sign-extend it if the two operands have different types.
541 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
542 if (SizeDiff > 0) {
543 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
544 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
545 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
546 } else if (SizeDiff < 0)
547 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
569 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
572 if (MVT::isFloatingPoint(ST->getStoredVT())) {
573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
575 MVT::ValueType intVT;
576 if (VT==MVT::f64)
577 intVT = MVT::i64;
578 else if (VT==MVT::f32)
579 intVT = MVT::i32;
580 else
581 assert(0 && "Unaligned load of unsupported floating point type");
582
583 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
584 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
585 SVOffset, ST->isVolatile(), Alignment);
586 }
587 assert(MVT::isInteger(ST->getStoredVT()) &&
588 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000589 // Get the half-size VT
590 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
591 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000592 int IncrementSize = NumBits / 8;
593
594 // Divide the stored value in two parts.
595 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
596 SDOperand Lo = Val;
597 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
598
599 // Store the two parts
600 SDOperand Store1, Store2;
601 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
602 ST->getSrcValue(), SVOffset, NewStoredVT,
603 ST->isVolatile(), Alignment);
604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
605 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000606 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000607 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
608 ST->getSrcValue(), SVOffset + IncrementSize,
609 NewStoredVT, ST->isVolatile(), Alignment);
610
611 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
612}
613
614/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
615static
616SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
617 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 int SVOffset = LD->getSrcValueOffset();
619 SDOperand Chain = LD->getChain();
620 SDOperand Ptr = LD->getBasePtr();
621 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000622 MVT::ValueType LoadedVT = LD->getLoadedVT();
Chris Lattnere400af82007-11-19 21:38:03 +0000623 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000624 // Expand to a (misaligned) integer load of the same size,
625 // then bitconvert to floating point.
626 MVT::ValueType intVT;
Chris Lattnere400af82007-11-19 21:38:03 +0000627 if (LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000628 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000629 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000630 intVT = MVT::i32;
631 else
632 assert(0 && "Unaligned load of unsupported floating point type");
633
634 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
635 SVOffset, LD->isVolatile(),
636 LD->getAlignment());
637 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
638 if (LoadedVT != VT)
639 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
640
641 SDOperand Ops[] = { Result, Chain };
642 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
643 Ops, 2);
644 }
Chris Lattnere400af82007-11-19 21:38:03 +0000645 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
646 "Unaligned load of unsupported type.");
647
648 // Compute the new VT that is half the size of the old one. We either have an
649 // integer MVT or we have a vector MVT.
650 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
651 MVT::ValueType NewLoadedVT;
652 if (!MVT::isVector(LoadedVT)) {
653 NewLoadedVT = MVT::getIntegerType(NumBits/2);
654 } else {
655 // FIXME: This is not right for <1 x anything> it is also not right for
656 // non-power-of-two vectors.
657 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
658 MVT::getVectorNumElements(LoadedVT)/2);
659 }
660 NumBits >>= 1;
661
662 unsigned Alignment = LD->getAlignment();
663 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000664 ISD::LoadExtType HiExtType = LD->getExtensionType();
665
666 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
667 if (HiExtType == ISD::NON_EXTLOAD)
668 HiExtType = ISD::ZEXTLOAD;
669
670 // Load the value in two parts
671 SDOperand Lo, Hi;
672 if (TLI.isLittleEndian()) {
673 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
674 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
675 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
676 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
677 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
678 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000679 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000680 } else {
681 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
682 NewLoadedVT,LD->isVolatile(), Alignment);
683 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
684 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
685 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
686 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000687 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000688 }
689
690 // aggregate the two parts
691 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
692 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
693 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
694
695 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
696 Hi.getValue(1));
697
698 SDOperand Ops[] = { Result, TF };
699 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
700}
Evan Cheng912095b2007-01-04 21:56:39 +0000701
Dan Gohman82669522007-10-11 23:57:53 +0000702/// UnrollVectorOp - We know that the given vector has a legal type, however
703/// the operation it performs is not legal and is an operation that we have
704/// no way of lowering. "Unroll" the vector, splitting out the scalars and
705/// operating on each element individually.
706SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
707 MVT::ValueType VT = Op.getValueType();
708 assert(isTypeLegal(VT) &&
709 "Caller should expand or promote operands that are not legal!");
710 assert(Op.Val->getNumValues() == 1 &&
711 "Can't unroll a vector with multiple results!");
712 unsigned NE = MVT::getVectorNumElements(VT);
713 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
714
715 SmallVector<SDOperand, 8> Scalars;
716 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
717 for (unsigned i = 0; i != NE; ++i) {
718 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
719 SDOperand Operand = Op.getOperand(j);
720 MVT::ValueType OperandVT = Operand.getValueType();
721 if (MVT::isVector(OperandVT)) {
722 // A vector operand; extract a single element.
723 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
724 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
725 OperandEltVT,
726 Operand,
727 DAG.getConstant(i, MVT::i32));
728 } else {
729 // A scalar operand; just use it as is.
730 Operands[j] = Operand;
731 }
732 }
733 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
734 &Operands[0], Operands.size()));
735 }
736
737 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
738}
739
Dan Gohmana3466152007-07-13 20:14:11 +0000740/// LegalizeOp - We know that the specified value has a legal type, and
741/// that its operands are legal. Now ensure that the operation itself
742/// is legal, recursively ensuring that the operands' operations remain
743/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000744SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000745 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
746 return Op;
747
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000748 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000749 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000750 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000751
Chris Lattner3e928bb2005-01-07 07:47:09 +0000752 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000753 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000754 if (Node->getNumValues() > 1) {
755 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000756 if (getTypeAction(Node->getValueType(i)) != Legal) {
757 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000758 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000759 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000760 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000761 }
762 }
763
Chris Lattner45982da2005-05-12 16:53:42 +0000764 // Note that LegalizeOp may be reentered even from single-use nodes, which
765 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000766 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000767 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000768
Nate Begeman9373a812005-08-10 20:51:12 +0000769 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000770 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000771 bool isCustom = false;
772
Chris Lattner3e928bb2005-01-07 07:47:09 +0000773 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000774 case ISD::FrameIndex:
775 case ISD::EntryToken:
776 case ISD::Register:
777 case ISD::BasicBlock:
778 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000779 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000780 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000781 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000782 case ISD::TargetConstantPool:
783 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000784 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000785 case ISD::TargetExternalSymbol:
786 case ISD::VALUETYPE:
787 case ISD::SRCVALUE:
788 case ISD::STRING:
789 case ISD::CONDCODE:
790 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000791 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000792 "This must be legal!");
793 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000794 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000795 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
796 // If this is a target node, legalize it by legalizing the operands then
797 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000798 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000799 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000800 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000801
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000802 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000803
804 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
805 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
806 return Result.getValue(Op.ResNo);
807 }
808 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000809#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000810 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000811#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000812 assert(0 && "Do not know how to legalize this operator!");
813 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000814 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000815 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000816 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000817 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000818 case ISD::ConstantPool:
819 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000820 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
821 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000822 case TargetLowering::Custom:
823 Tmp1 = TLI.LowerOperation(Op, DAG);
824 if (Tmp1.Val) Result = Tmp1;
825 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000826 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000827 break;
828 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000830 case ISD::FRAMEADDR:
831 case ISD::RETURNADDR:
832 // The only option for these nodes is to custom lower them. If the target
833 // does not custom lower them, then return zero.
834 Tmp1 = TLI.LowerOperation(Op, DAG);
835 if (Tmp1.Val)
836 Result = Tmp1;
837 else
838 Result = DAG.getConstant(0, TLI.getPointerTy());
839 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000840 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000841 MVT::ValueType VT = Node->getValueType(0);
842 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
843 default: assert(0 && "This action is not supported yet!");
844 case TargetLowering::Custom:
845 Result = TLI.LowerOperation(Op, DAG);
846 if (Result.Val) break;
847 // Fall Thru
848 case TargetLowering::Legal:
849 Result = DAG.getConstant(0, VT);
850 break;
851 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000852 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000853 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000854 case ISD::EXCEPTIONADDR: {
855 Tmp1 = LegalizeOp(Node->getOperand(0));
856 MVT::ValueType VT = Node->getValueType(0);
857 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000860 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000861 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000862 }
863 break;
864 case TargetLowering::Custom:
865 Result = TLI.LowerOperation(Op, DAG);
866 if (Result.Val) break;
867 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000868 case TargetLowering::Legal: {
869 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
870 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000871 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000872 break;
873 }
874 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000875 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000876 if (Result.Val->getNumValues() == 1) break;
877
878 assert(Result.Val->getNumValues() == 2 &&
879 "Cannot return more than two values!");
880
881 // Since we produced two values, make sure to remember that we
882 // legalized both of them.
883 Tmp1 = LegalizeOp(Result);
884 Tmp2 = LegalizeOp(Result.getValue(1));
885 AddLegalizedOperand(Op.getValue(0), Tmp1);
886 AddLegalizedOperand(Op.getValue(1), Tmp2);
887 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000888 case ISD::EHSELECTION: {
889 Tmp1 = LegalizeOp(Node->getOperand(0));
890 Tmp2 = LegalizeOp(Node->getOperand(1));
891 MVT::ValueType VT = Node->getValueType(0);
892 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
893 default: assert(0 && "This action is not supported yet!");
894 case TargetLowering::Expand: {
895 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000896 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000897 }
898 break;
899 case TargetLowering::Custom:
900 Result = TLI.LowerOperation(Op, DAG);
901 if (Result.Val) break;
902 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000903 case TargetLowering::Legal: {
904 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
905 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000906 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000907 break;
908 }
909 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000910 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000911 if (Result.Val->getNumValues() == 1) break;
912
913 assert(Result.Val->getNumValues() == 2 &&
914 "Cannot return more than two values!");
915
916 // Since we produced two values, make sure to remember that we
917 // legalized both of them.
918 Tmp1 = LegalizeOp(Result);
919 Tmp2 = LegalizeOp(Result.getValue(1));
920 AddLegalizedOperand(Op.getValue(0), Tmp1);
921 AddLegalizedOperand(Op.getValue(1), Tmp2);
922 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000923 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000924 MVT::ValueType VT = Node->getValueType(0);
925 // The only "good" option for this node is to custom lower it.
926 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
927 default: assert(0 && "This action is not supported at all!");
928 case TargetLowering::Custom:
929 Result = TLI.LowerOperation(Op, DAG);
930 if (Result.Val) break;
931 // Fall Thru
932 case TargetLowering::Legal:
933 // Target does not know, how to lower this, lower to noop
934 Result = LegalizeOp(Node->getOperand(0));
935 break;
936 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000937 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000938 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000939 case ISD::AssertSext:
940 case ISD::AssertZext:
941 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000942 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000943 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000944 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000945 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000946 Result = Node->getOperand(Op.ResNo);
947 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000948 case ISD::CopyFromReg:
949 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000950 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000951 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000952 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000953 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000954 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000955 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000956 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
958 } else {
959 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
960 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000961 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
962 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000963 // Since CopyFromReg produces two values, make sure to remember that we
964 // legalized both of them.
965 AddLegalizedOperand(Op.getValue(0), Result);
966 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
967 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000968 case ISD::UNDEF: {
969 MVT::ValueType VT = Op.getValueType();
970 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000971 default: assert(0 && "This action is not supported yet!");
972 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000973 if (MVT::isInteger(VT))
974 Result = DAG.getConstant(0, VT);
975 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000976 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
977 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000978 else
979 assert(0 && "Unknown value type!");
980 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000981 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000982 break;
983 }
984 break;
985 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000986
Chris Lattner48b61a72006-03-28 00:40:33 +0000987 case ISD::INTRINSIC_W_CHAIN:
988 case ISD::INTRINSIC_WO_CHAIN:
989 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000990 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000991 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
992 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000993 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000994
995 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000996 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000997 TargetLowering::Custom) {
998 Tmp3 = TLI.LowerOperation(Result, DAG);
999 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001000 }
1001
1002 if (Result.Val->getNumValues() == 1) break;
1003
1004 // Must have return value and chain result.
1005 assert(Result.Val->getNumValues() == 2 &&
1006 "Cannot return more than two values!");
1007
1008 // Since loads produce two values, make sure to remember that we
1009 // legalized both of them.
1010 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1011 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1012 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001013 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001014
1015 case ISD::LOCATION:
1016 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1017 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1018
1019 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1020 case TargetLowering::Promote:
1021 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001022 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001023 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001024 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001025 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001026
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001027 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001028 const std::string &FName =
1029 cast<StringSDNode>(Node->getOperand(3))->getValue();
1030 const std::string &DirName =
1031 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001032 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001033
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001034 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001035 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001036 SDOperand LineOp = Node->getOperand(1);
1037 SDOperand ColOp = Node->getOperand(2);
1038
1039 if (useDEBUG_LOC) {
1040 Ops.push_back(LineOp); // line #
1041 Ops.push_back(ColOp); // col #
1042 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001043 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001044 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001045 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1046 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001047 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001048 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001049 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001050 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001051 } else {
1052 Result = Tmp1; // chain
1053 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001054 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001055 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001056 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001057 if (Tmp1 != Node->getOperand(0) ||
1058 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001059 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001060 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001061 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1062 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1063 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1064 } else {
1065 // Otherwise promote them.
1066 Ops.push_back(PromoteOp(Node->getOperand(1)));
1067 Ops.push_back(PromoteOp(Node->getOperand(2)));
1068 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001069 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1070 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001071 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001072 }
1073 break;
1074 }
1075 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001076
1077 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001078 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001079 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001080 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001081 case TargetLowering::Legal:
1082 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1083 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1084 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1085 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001086 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001087 break;
1088 }
1089 break;
1090
Jim Laskey1ee29252007-01-26 14:34:52 +00001091 case ISD::LABEL:
1092 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1093 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001094 default: assert(0 && "This action is not supported yet!");
1095 case TargetLowering::Legal:
1096 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1097 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001098 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001099 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001100 case TargetLowering::Expand:
1101 Result = LegalizeOp(Node->getOperand(0));
1102 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001103 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001104 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001105
Scott Michelc1513d22007-08-08 23:23:31 +00001106 case ISD::Constant: {
1107 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1108 unsigned opAction =
1109 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1110
Chris Lattner3e928bb2005-01-07 07:47:09 +00001111 // We know we don't need to expand constants here, constants only have one
1112 // value and we check that it is fine above.
1113
Scott Michelc1513d22007-08-08 23:23:31 +00001114 if (opAction == TargetLowering::Custom) {
1115 Tmp1 = TLI.LowerOperation(Result, DAG);
1116 if (Tmp1.Val)
1117 Result = Tmp1;
1118 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001119 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001120 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001121 case ISD::ConstantFP: {
1122 // Spill FP immediates to the constant pool if the target cannot directly
1123 // codegen them. Targets often have some immediate values that can be
1124 // efficiently generated into an FP register without a load. We explicitly
1125 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001126 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1127
1128 // Check to see if this FP immediate is already legal.
1129 bool isLegal = false;
1130 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1131 E = TLI.legal_fpimm_end(); I != E; ++I)
1132 if (CFP->isExactlyValue(*I)) {
1133 isLegal = true;
1134 break;
1135 }
1136
Chris Lattner3181a772006-01-29 06:26:56 +00001137 // If this is a legal constant, turn it into a TargetConstantFP node.
1138 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001139 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1140 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001141 break;
1142 }
1143
1144 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1145 default: assert(0 && "This action is not supported yet!");
1146 case TargetLowering::Custom:
1147 Tmp3 = TLI.LowerOperation(Result, DAG);
1148 if (Tmp3.Val) {
1149 Result = Tmp3;
1150 break;
1151 }
1152 // FALLTHROUGH
1153 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001154 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001155 }
1156 break;
1157 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001158 case ISD::TokenFactor:
1159 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001160 Tmp1 = LegalizeOp(Node->getOperand(0));
1161 Tmp2 = LegalizeOp(Node->getOperand(1));
1162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1163 } else if (Node->getNumOperands() == 3) {
1164 Tmp1 = LegalizeOp(Node->getOperand(0));
1165 Tmp2 = LegalizeOp(Node->getOperand(1));
1166 Tmp3 = LegalizeOp(Node->getOperand(2));
1167 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001168 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001169 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001170 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001171 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1172 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001173 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001174 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001175 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001176
1177 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001178 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001179 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001180 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1181 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001182
1183 // The number of incoming and outgoing values should match; unless the final
1184 // outgoing value is a flag.
1185 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1186 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1187 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1188 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001189 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001190
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001191 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001192 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001193 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001194 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1195 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001196 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001197 if (Op.ResNo == i)
1198 Tmp2 = Tmp1;
1199 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1200 }
1201 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001202 case ISD::EXTRACT_SUBREG: {
1203 Tmp1 = LegalizeOp(Node->getOperand(0));
1204 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1205 assert(idx && "Operand must be a constant");
1206 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1207 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1208 }
1209 break;
1210 case ISD::INSERT_SUBREG: {
1211 Tmp1 = LegalizeOp(Node->getOperand(0));
1212 Tmp2 = LegalizeOp(Node->getOperand(1));
1213 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1214 assert(idx && "Operand must be a constant");
1215 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1216 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1217 }
1218 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001219 case ISD::BUILD_VECTOR:
1220 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001221 default: assert(0 && "This action is not supported yet!");
1222 case TargetLowering::Custom:
1223 Tmp3 = TLI.LowerOperation(Result, DAG);
1224 if (Tmp3.Val) {
1225 Result = Tmp3;
1226 break;
1227 }
1228 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001229 case TargetLowering::Expand:
1230 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001231 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001232 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001233 break;
1234 case ISD::INSERT_VECTOR_ELT:
1235 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1236 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1237 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1238 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1239
1240 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1241 Node->getValueType(0))) {
1242 default: assert(0 && "This action is not supported yet!");
1243 case TargetLowering::Legal:
1244 break;
1245 case TargetLowering::Custom:
1246 Tmp3 = TLI.LowerOperation(Result, DAG);
1247 if (Tmp3.Val) {
1248 Result = Tmp3;
1249 break;
1250 }
1251 // FALLTHROUGH
1252 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001253 // If the insert index is a constant, codegen this as a scalar_to_vector,
1254 // then a shuffle that inserts it into the right position in the vector.
1255 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1256 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1257 Tmp1.getValueType(), Tmp2);
1258
1259 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1260 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001261 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001262
1263 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1264 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1265 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001266 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001267 for (unsigned i = 0; i != NumElts; ++i) {
1268 if (i != InsertPos->getValue())
1269 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1270 else
1271 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1272 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001273 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1274 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001275
1276 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1277 Tmp1, ScVec, ShufMask);
1278 Result = LegalizeOp(Result);
1279 break;
1280 }
1281
Chris Lattner2332b9f2006-03-19 01:17:20 +00001282 // If the target doesn't support this, we have to spill the input vector
1283 // to a temporary stack slot, update the element, then reload it. This is
1284 // badness. We could also load the value into a vector register (either
1285 // with a "move to register" or "extload into register" instruction, then
1286 // permute it into place, if the idx is a constant and if the idx is
1287 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001288 MVT::ValueType VT = Tmp1.getValueType();
1289 MVT::ValueType EltVT = Tmp2.getValueType();
1290 MVT::ValueType IdxVT = Tmp3.getValueType();
1291 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001292 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001293 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001294 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001295
1296 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001297 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1298 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001299 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001300 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1301 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1302 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001303 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001304 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001305 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001306 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001307 break;
1308 }
1309 }
1310 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001311 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001312 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1313 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1314 break;
1315 }
1316
Chris Lattnerce872152006-03-19 06:31:19 +00001317 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1318 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1319 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1320 Node->getValueType(0))) {
1321 default: assert(0 && "This action is not supported yet!");
1322 case TargetLowering::Legal:
1323 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001324 case TargetLowering::Custom:
1325 Tmp3 = TLI.LowerOperation(Result, DAG);
1326 if (Tmp3.Val) {
1327 Result = Tmp3;
1328 break;
1329 }
1330 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001331 case TargetLowering::Expand:
1332 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001333 break;
1334 }
Chris Lattnerce872152006-03-19 06:31:19 +00001335 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001336 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001337 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1338 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1339 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1340
1341 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001342 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1343 default: assert(0 && "Unknown operation action!");
1344 case TargetLowering::Legal:
1345 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1346 "vector shuffle should not be created if not legal!");
1347 break;
1348 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001349 Tmp3 = TLI.LowerOperation(Result, DAG);
1350 if (Tmp3.Val) {
1351 Result = Tmp3;
1352 break;
1353 }
1354 // FALLTHROUGH
1355 case TargetLowering::Expand: {
1356 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001357 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001358 MVT::ValueType PtrVT = TLI.getPointerTy();
1359 SDOperand Mask = Node->getOperand(2);
1360 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001361 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001362 for (unsigned i = 0; i != NumElems; ++i) {
1363 SDOperand Arg = Mask.getOperand(i);
1364 if (Arg.getOpcode() == ISD::UNDEF) {
1365 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1366 } else {
1367 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1368 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1369 if (Idx < NumElems)
1370 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1371 DAG.getConstant(Idx, PtrVT)));
1372 else
1373 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1374 DAG.getConstant(Idx - NumElems, PtrVT)));
1375 }
1376 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001377 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001378 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001379 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001380 case TargetLowering::Promote: {
1381 // Change base type to a different vector type.
1382 MVT::ValueType OVT = Node->getValueType(0);
1383 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1384
1385 // Cast the two input vectors.
1386 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1387 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1388
1389 // Convert the shuffle mask to the right # elements.
1390 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1391 assert(Tmp3.Val && "Shuffle not legal?");
1392 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1393 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1394 break;
1395 }
Chris Lattner87100e02006-03-20 01:52:29 +00001396 }
1397 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001398
1399 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001400 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001401 Tmp2 = LegalizeOp(Node->getOperand(1));
1402 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001403 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001404 break;
1405
Dan Gohman7f321562007-06-25 16:23:39 +00001406 case ISD::EXTRACT_SUBVECTOR:
1407 Tmp1 = Node->getOperand(0);
1408 Tmp2 = LegalizeOp(Node->getOperand(1));
1409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1410 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001411 break;
1412
Chris Lattner6831a812006-02-13 09:18:02 +00001413 case ISD::CALLSEQ_START: {
1414 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1415
1416 // Recursively Legalize all of the inputs of the call end that do not lead
1417 // to this call start. This ensures that any libcalls that need be inserted
1418 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001419 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001420 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001421 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1422 NodesLeadingTo);
1423 }
Chris Lattner6831a812006-02-13 09:18:02 +00001424
1425 // Now that we legalized all of the inputs (which may have inserted
1426 // libcalls) create the new CALLSEQ_START node.
1427 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1428
1429 // Merge in the last call, to ensure that this call start after the last
1430 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001431 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001432 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1433 Tmp1 = LegalizeOp(Tmp1);
1434 }
Chris Lattner6831a812006-02-13 09:18:02 +00001435
1436 // Do not try to legalize the target-specific arguments (#1+).
1437 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001438 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001439 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001440 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001441 }
1442
1443 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001444 AddLegalizedOperand(Op.getValue(0), Result);
1445 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1446 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1447
Chris Lattner6831a812006-02-13 09:18:02 +00001448 // Now that the callseq_start and all of the non-call nodes above this call
1449 // sequence have been legalized, legalize the call itself. During this
1450 // process, no libcalls can/will be inserted, guaranteeing that no calls
1451 // can overlap.
1452 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1453 SDOperand InCallSEQ = LastCALLSEQ_END;
1454 // Note that we are selecting this call!
1455 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1456 IsLegalizingCall = true;
1457
1458 // Legalize the call, starting from the CALLSEQ_END.
1459 LegalizeOp(LastCALLSEQ_END);
1460 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1461 return Result;
1462 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001463 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001464 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1465 // will cause this node to be legalized as well as handling libcalls right.
1466 if (LastCALLSEQ_END.Val != Node) {
1467 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001468 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001469 assert(I != LegalizedNodes.end() &&
1470 "Legalizing the call start should have legalized this node!");
1471 return I->second;
1472 }
1473
1474 // Otherwise, the call start has been legalized and everything is going
1475 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001476 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001477 // Do not try to legalize the target-specific arguments (#1+), except for
1478 // an optional flag input.
1479 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1480 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001481 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001482 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001483 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001484 }
1485 } else {
1486 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1487 if (Tmp1 != Node->getOperand(0) ||
1488 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001489 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001490 Ops[0] = Tmp1;
1491 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001492 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001493 }
Chris Lattner6a542892006-01-24 05:48:21 +00001494 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001495 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001496 // This finishes up call legalization.
1497 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001498
1499 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1500 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1501 if (Node->getNumValues() == 2)
1502 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1503 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001504 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001505 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1507 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1508 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001510
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001511 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001512 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001513 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001514 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001515 case TargetLowering::Expand: {
1516 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1517 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1518 " not tell us which reg is the stack pointer!");
1519 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001520
1521 // Chain the dynamic stack allocation so that it doesn't modify the stack
1522 // pointer when other instructions are using the stack.
1523 Chain = DAG.getCALLSEQ_START(Chain,
1524 DAG.getConstant(0, TLI.getPointerTy()));
1525
Chris Lattner903d2782006-01-15 08:54:32 +00001526 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001527 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1528 Chain = SP.getValue(1);
1529 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1530 unsigned StackAlign =
1531 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1532 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001533 SP = DAG.getNode(ISD::AND, VT, SP,
1534 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001535 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001536 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1537
1538 Tmp2 =
1539 DAG.getCALLSEQ_END(Chain,
1540 DAG.getConstant(0, TLI.getPointerTy()),
1541 DAG.getConstant(0, TLI.getPointerTy()),
1542 SDOperand());
1543
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001544 Tmp1 = LegalizeOp(Tmp1);
1545 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001546 break;
1547 }
1548 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001549 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1550 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001551 Tmp1 = LegalizeOp(Tmp3);
1552 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001553 }
Chris Lattner903d2782006-01-15 08:54:32 +00001554 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001555 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001556 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001557 }
Chris Lattner903d2782006-01-15 08:54:32 +00001558 // Since this op produce two values, make sure to remember that we
1559 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001560 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1561 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001562 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001563 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001564 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001565 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001566 bool Changed = false;
1567 // Legalize all of the operands of the inline asm, in case they are nodes
1568 // that need to be expanded or something. Note we skip the asm string and
1569 // all of the TargetConstant flags.
1570 SDOperand Op = LegalizeOp(Ops[0]);
1571 Changed = Op != Ops[0];
1572 Ops[0] = Op;
1573
1574 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1575 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1576 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1577 for (++i; NumVals; ++i, --NumVals) {
1578 SDOperand Op = LegalizeOp(Ops[i]);
1579 if (Op != Ops[i]) {
1580 Changed = true;
1581 Ops[i] = Op;
1582 }
1583 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001584 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001585
1586 if (HasInFlag) {
1587 Op = LegalizeOp(Ops.back());
1588 Changed |= Op != Ops.back();
1589 Ops.back() = Op;
1590 }
1591
1592 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001593 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001594
1595 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001596 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001597 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1598 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001599 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001600 case ISD::BR:
1601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001602 // Ensure that libcalls are emitted before a branch.
1603 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1604 Tmp1 = LegalizeOp(Tmp1);
1605 LastCALLSEQ_END = DAG.getEntryNode();
1606
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001608 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001609 case ISD::BRIND:
1610 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1611 // Ensure that libcalls are emitted before a branch.
1612 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1613 Tmp1 = LegalizeOp(Tmp1);
1614 LastCALLSEQ_END = DAG.getEntryNode();
1615
1616 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1617 default: assert(0 && "Indirect target must be legal type (pointer)!");
1618 case Legal:
1619 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1620 break;
1621 }
1622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1623 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001624 case ISD::BR_JT:
1625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1626 // Ensure that libcalls are emitted before a branch.
1627 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1628 Tmp1 = LegalizeOp(Tmp1);
1629 LastCALLSEQ_END = DAG.getEntryNode();
1630
1631 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1632 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1633
1634 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1635 default: assert(0 && "This action is not supported yet!");
1636 case TargetLowering::Legal: break;
1637 case TargetLowering::Custom:
1638 Tmp1 = TLI.LowerOperation(Result, DAG);
1639 if (Tmp1.Val) Result = Tmp1;
1640 break;
1641 case TargetLowering::Expand: {
1642 SDOperand Chain = Result.getOperand(0);
1643 SDOperand Table = Result.getOperand(1);
1644 SDOperand Index = Result.getOperand(2);
1645
1646 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001647 MachineFunction &MF = DAG.getMachineFunction();
1648 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001649 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1650 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001651
1652 SDOperand LD;
1653 switch (EntrySize) {
1654 default: assert(0 && "Size of jump table not supported yet."); break;
1655 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1656 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1657 }
1658
Evan Chengcc415862007-11-09 01:32:10 +00001659 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001660 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001661 // For PIC, the sequence is:
1662 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001663 // RelocBase can be JumpTable, GOT or some sort of global base.
1664 if (PTy != MVT::i32)
1665 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1666 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1667 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001668 }
Evan Chengcc415862007-11-09 01:32:10 +00001669 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001670 }
1671 }
1672 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001673 case ISD::BRCOND:
1674 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001675 // Ensure that libcalls are emitted before a return.
1676 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1677 Tmp1 = LegalizeOp(Tmp1);
1678 LastCALLSEQ_END = DAG.getEntryNode();
1679
Chris Lattner47e92232005-01-18 19:27:06 +00001680 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1681 case Expand: assert(0 && "It's impossible to expand bools");
1682 case Legal:
1683 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1684 break;
1685 case Promote:
1686 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001687
1688 // The top bits of the promoted condition are not necessarily zero, ensure
1689 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001690 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001691 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1692 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001693 break;
1694 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001695
1696 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001698
1699 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1700 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001701 case TargetLowering::Legal: break;
1702 case TargetLowering::Custom:
1703 Tmp1 = TLI.LowerOperation(Result, DAG);
1704 if (Tmp1.Val) Result = Tmp1;
1705 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001706 case TargetLowering::Expand:
1707 // Expand brcond's setcc into its constituent parts and create a BR_CC
1708 // Node.
1709 if (Tmp2.getOpcode() == ISD::SETCC) {
1710 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1711 Tmp2.getOperand(0), Tmp2.getOperand(1),
1712 Node->getOperand(2));
1713 } else {
1714 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1715 DAG.getCondCode(ISD::SETNE), Tmp2,
1716 DAG.getConstant(0, Tmp2.getValueType()),
1717 Node->getOperand(2));
1718 }
1719 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001720 }
1721 break;
1722 case ISD::BR_CC:
1723 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001724 // Ensure that libcalls are emitted before a branch.
1725 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1726 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001727 Tmp2 = Node->getOperand(2); // LHS
1728 Tmp3 = Node->getOperand(3); // RHS
1729 Tmp4 = Node->getOperand(1); // CC
1730
1731 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001732 LastCALLSEQ_END = DAG.getEntryNode();
1733
Nate Begeman750ac1b2006-02-01 07:19:44 +00001734 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1735 // the LHS is a legal SETCC itself. In this case, we need to compare
1736 // the result against zero to select between true and false values.
1737 if (Tmp3.Val == 0) {
1738 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1739 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001740 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001741
1742 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1743 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001744
Chris Lattner181b7a32005-12-17 23:46:46 +00001745 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1746 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001747 case TargetLowering::Legal: break;
1748 case TargetLowering::Custom:
1749 Tmp4 = TLI.LowerOperation(Result, DAG);
1750 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001751 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001752 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001753 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001754 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001755 LoadSDNode *LD = cast<LoadSDNode>(Node);
1756 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1757 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001758
Evan Cheng466685d2006-10-09 20:57:25 +00001759 ISD::LoadExtType ExtType = LD->getExtensionType();
1760 if (ExtType == ISD::NON_EXTLOAD) {
1761 MVT::ValueType VT = Node->getValueType(0);
1762 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1763 Tmp3 = Result.getValue(0);
1764 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001765
Evan Cheng466685d2006-10-09 20:57:25 +00001766 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1767 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001768 case TargetLowering::Legal:
1769 // If this is an unaligned load and the target doesn't support it,
1770 // expand it.
1771 if (!TLI.allowsUnalignedMemoryAccesses()) {
1772 unsigned ABIAlignment = TLI.getTargetData()->
1773 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1774 if (LD->getAlignment() < ABIAlignment){
1775 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1776 TLI);
1777 Tmp3 = Result.getOperand(0);
1778 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001779 Tmp3 = LegalizeOp(Tmp3);
1780 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001781 }
1782 }
1783 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001784 case TargetLowering::Custom:
1785 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1786 if (Tmp1.Val) {
1787 Tmp3 = LegalizeOp(Tmp1);
1788 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001789 }
Evan Cheng466685d2006-10-09 20:57:25 +00001790 break;
1791 case TargetLowering::Promote: {
1792 // Only promote a load of vector type to another.
1793 assert(MVT::isVector(VT) && "Cannot promote this load!");
1794 // Change base type to a different vector type.
1795 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1796
1797 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001798 LD->getSrcValueOffset(),
1799 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001800 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1801 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001802 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001803 }
Evan Cheng466685d2006-10-09 20:57:25 +00001804 }
1805 // Since loads produce two values, make sure to remember that we
1806 // legalized both of them.
1807 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1808 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1809 return Op.ResNo ? Tmp4 : Tmp3;
1810 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001811 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001812 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1813 default: assert(0 && "This action is not supported yet!");
1814 case TargetLowering::Promote:
1815 assert(SrcVT == MVT::i1 &&
1816 "Can only promote extending LOAD from i1 -> i8!");
1817 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1818 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001819 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsf411b832007-10-17 13:49:58 +00001820 Tmp1 = Result.getValue(0);
1821 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001822 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001823 case TargetLowering::Custom:
1824 isCustom = true;
1825 // FALLTHROUGH
1826 case TargetLowering::Legal:
1827 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1828 Tmp1 = Result.getValue(0);
1829 Tmp2 = Result.getValue(1);
1830
1831 if (isCustom) {
1832 Tmp3 = TLI.LowerOperation(Result, DAG);
1833 if (Tmp3.Val) {
1834 Tmp1 = LegalizeOp(Tmp3);
1835 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1836 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001837 } else {
1838 // If this is an unaligned load and the target doesn't support it,
1839 // expand it.
1840 if (!TLI.allowsUnalignedMemoryAccesses()) {
1841 unsigned ABIAlignment = TLI.getTargetData()->
1842 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1843 if (LD->getAlignment() < ABIAlignment){
1844 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1845 TLI);
1846 Tmp1 = Result.getOperand(0);
1847 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001848 Tmp1 = LegalizeOp(Tmp1);
1849 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001850 }
1851 }
Evan Cheng466685d2006-10-09 20:57:25 +00001852 }
1853 break;
1854 case TargetLowering::Expand:
1855 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1856 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1857 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001858 LD->getSrcValueOffset(),
1859 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001860 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1861 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1862 Tmp2 = LegalizeOp(Load.getValue(1));
1863 break;
1864 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001865 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001866 // Turn the unsupported load into an EXTLOAD followed by an explicit
1867 // zero/sign extend inreg.
1868 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1869 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001870 LD->getSrcValueOffset(), SrcVT,
1871 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001872 SDOperand ValRes;
1873 if (ExtType == ISD::SEXTLOAD)
1874 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1875 Result, DAG.getValueType(SrcVT));
1876 else
1877 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1878 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1879 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1880 break;
1881 }
1882 // Since loads produce two values, make sure to remember that we legalized
1883 // both of them.
1884 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1885 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1886 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001887 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001888 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001889 case ISD::EXTRACT_ELEMENT: {
1890 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1891 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001892 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001893 case Legal:
1894 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1895 // 1 -> Hi
1896 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1897 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1898 TLI.getShiftAmountTy()));
1899 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1900 } else {
1901 // 0 -> Lo
1902 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1903 Node->getOperand(0));
1904 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001905 break;
1906 case Expand:
1907 // Get both the low and high parts.
1908 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1909 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1910 Result = Tmp2; // 1 -> Hi
1911 else
1912 Result = Tmp1; // 0 -> Lo
1913 break;
1914 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001915 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001916 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001917
1918 case ISD::CopyToReg:
1919 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001920
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001921 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001922 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001923 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001924 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001925 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001926 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001927 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001928 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001929 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001930 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001931 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1932 Tmp3);
1933 } else {
1934 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001935 }
1936
1937 // Since this produces two values, make sure to remember that we legalized
1938 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001939 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001940 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001941 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001942 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001943 break;
1944
1945 case ISD::RET:
1946 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001947
1948 // Ensure that libcalls are emitted before a return.
1949 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1950 Tmp1 = LegalizeOp(Tmp1);
1951 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001952
Chris Lattner3e928bb2005-01-07 07:47:09 +00001953 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001954 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001955 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001956 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001957 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001958 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001959 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001960 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001961 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001962 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001963 SDOperand Lo, Hi;
1964 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001965
1966 // Big endian systems want the hi reg first.
1967 if (!TLI.isLittleEndian())
1968 std::swap(Lo, Hi);
1969
Evan Cheng13acce32006-12-11 19:27:14 +00001970 if (Hi.Val)
1971 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1972 else
1973 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001974 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001975 } else {
1976 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00001977 int InIx = Tmp2.ResNo;
1978 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1979 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001980
Dan Gohman7f321562007-06-25 16:23:39 +00001981 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001982 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001983 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001984 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001985 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001986 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001987 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001988 } else if (NumElems == 1) {
1989 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001990 Tmp2 = ScalarizeVectorOp(Tmp2);
1991 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001992 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001993
1994 // FIXME: Returns of gcc generic vectors smaller than a legal type
1995 // should be returned in integer registers!
1996
Chris Lattnerf87324e2006-04-11 01:31:51 +00001997 // The scalarized value type may not be legal, e.g. it might require
1998 // promotion or expansion. Relegalize the return.
1999 Result = LegalizeOp(Result);
2000 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002001 // FIXME: Returns of gcc generic vectors larger than a legal vector
2002 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002003 SDOperand Lo, Hi;
2004 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002005 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002006 Result = LegalizeOp(Result);
2007 }
2008 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002009 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002010 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002011 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002012 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002013 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002014 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002015 }
2016 break;
2017 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002018 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002019 break;
2020 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002021 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002022 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002023 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002024 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2025 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002026 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002027 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002028 break;
2029 case Expand: {
2030 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002031 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002032 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002033 ExpandOp(Node->getOperand(i), Lo, Hi);
2034 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002035 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002036 if (Hi.Val) {
2037 NewValues.push_back(Hi);
2038 NewValues.push_back(Node->getOperand(i+1));
2039 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002040 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002041 }
2042 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002043 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002044 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002045
2046 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002047 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002048 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002049 Result = DAG.getNode(ISD::RET, MVT::Other,
2050 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002051 break;
2052 }
2053 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002054
Chris Lattner6862dbc2006-01-29 21:02:23 +00002055 if (Result.getOpcode() == ISD::RET) {
2056 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2057 default: assert(0 && "This action is not supported yet!");
2058 case TargetLowering::Legal: break;
2059 case TargetLowering::Custom:
2060 Tmp1 = TLI.LowerOperation(Result, DAG);
2061 if (Tmp1.Val) Result = Tmp1;
2062 break;
2063 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002064 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002065 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002066 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002067 StoreSDNode *ST = cast<StoreSDNode>(Node);
2068 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2069 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002070 int SVOffset = ST->getSrcValueOffset();
2071 unsigned Alignment = ST->getAlignment();
2072 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002073
Evan Cheng8b2794a2006-10-13 21:14:26 +00002074 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002075 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2076 // FIXME: We shouldn't do this for TargetConstantFP's.
2077 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2078 // to phase ordering between legalized code and the dag combiner. This
2079 // probably means that we need to integrate dag combiner and legalizer
2080 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002081 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002082 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002083 if (CFP->getValueType(0) == MVT::f32 &&
2084 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002085 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2086 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002087 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002088 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2089 SVOffset, isVolatile, Alignment);
2090 break;
2091 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002092 // If this target supports 64-bit registers, do a single 64-bit store.
2093 if (getTypeAction(MVT::i64) == Legal) {
2094 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2095 getZExtValue(), MVT::i64);
2096 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2097 SVOffset, isVolatile, Alignment);
2098 break;
2099 } else if (getTypeAction(MVT::i32) == Legal) {
2100 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2101 // stores. If the target supports neither 32- nor 64-bits, this
2102 // xform is certainly not worth it.
2103 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2104 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2105 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2106 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2107
2108 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2109 SVOffset, isVolatile, Alignment);
2110 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2111 getIntPtrConstant(4));
2112 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002113 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002114
2115 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2116 break;
2117 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002118 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002119 }
2120
Evan Cheng8b2794a2006-10-13 21:14:26 +00002121 switch (getTypeAction(ST->getStoredVT())) {
2122 case Legal: {
2123 Tmp3 = LegalizeOp(ST->getValue());
2124 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2125 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002126
Evan Cheng8b2794a2006-10-13 21:14:26 +00002127 MVT::ValueType VT = Tmp3.getValueType();
2128 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2129 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002130 case TargetLowering::Legal:
2131 // If this is an unaligned store and the target doesn't support it,
2132 // expand it.
2133 if (!TLI.allowsUnalignedMemoryAccesses()) {
2134 unsigned ABIAlignment = TLI.getTargetData()->
2135 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2136 if (ST->getAlignment() < ABIAlignment)
2137 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2138 TLI);
2139 }
2140 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002141 case TargetLowering::Custom:
2142 Tmp1 = TLI.LowerOperation(Result, DAG);
2143 if (Tmp1.Val) Result = Tmp1;
2144 break;
2145 case TargetLowering::Promote:
2146 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2147 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2148 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2149 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002150 ST->getSrcValue(), SVOffset, isVolatile,
2151 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002152 break;
2153 }
2154 break;
2155 }
2156 case Promote:
2157 // Truncate the value and store the result.
2158 Tmp3 = PromoteOp(ST->getValue());
2159 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002160 SVOffset, ST->getStoredVT(),
2161 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002162 break;
2163
2164 case Expand:
2165 unsigned IncrementSize = 0;
2166 SDOperand Lo, Hi;
2167
2168 // If this is a vector type, then we have to calculate the increment as
2169 // the product of the element size in bytes, and the number of elements
2170 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002171 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002172 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002173 int InIx = ST->getValue().ResNo;
2174 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2175 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002176
Dan Gohman7f321562007-06-25 16:23:39 +00002177 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002178 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002179 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002180 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002181 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002182 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002183 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002184 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002185 Result = LegalizeOp(Result);
2186 break;
2187 } else if (NumElems == 1) {
2188 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002189 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002190 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002191 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002192 // The scalarized value type may not be legal, e.g. it might require
2193 // promotion or expansion. Relegalize the scalar store.
2194 Result = LegalizeOp(Result);
2195 break;
2196 } else {
2197 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002198 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2199 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002200 }
2201 } else {
2202 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002203 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002204
2205 if (!TLI.isLittleEndian())
2206 std::swap(Lo, Hi);
2207 }
2208
2209 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002210 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002211
2212 if (Hi.Val == NULL) {
2213 // Must be int <-> float one-to-one expansion.
2214 Result = Lo;
2215 break;
2216 }
2217
Evan Cheng8b2794a2006-10-13 21:14:26 +00002218 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2219 getIntPtrConstant(IncrementSize));
2220 assert(isTypeLegal(Tmp2.getValueType()) &&
2221 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002222 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002223 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002224 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002225 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002226 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2227 break;
2228 }
2229 } else {
2230 // Truncating store
2231 assert(isTypeLegal(ST->getValue().getValueType()) &&
2232 "Cannot handle illegal TRUNCSTORE yet!");
2233 Tmp3 = LegalizeOp(ST->getValue());
2234
2235 // The only promote case we handle is TRUNCSTORE:i1 X into
2236 // -> TRUNCSTORE:i8 (and X, 1)
2237 if (ST->getStoredVT() == MVT::i1 &&
2238 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2239 // Promote the bool to a mask then store.
2240 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2241 DAG.getConstant(1, Tmp3.getValueType()));
2242 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002243 SVOffset, MVT::i8,
2244 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002245 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2246 Tmp2 != ST->getBasePtr()) {
2247 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2248 ST->getOffset());
2249 }
2250
2251 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2252 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002253 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002254 case TargetLowering::Legal:
2255 // If this is an unaligned store and the target doesn't support it,
2256 // expand it.
2257 if (!TLI.allowsUnalignedMemoryAccesses()) {
2258 unsigned ABIAlignment = TLI.getTargetData()->
2259 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2260 if (ST->getAlignment() < ABIAlignment)
2261 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2262 TLI);
2263 }
2264 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002265 case TargetLowering::Custom:
2266 Tmp1 = TLI.LowerOperation(Result, DAG);
2267 if (Tmp1.Val) Result = Tmp1;
2268 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002269 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002270 }
2271 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002272 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002273 case ISD::PCMARKER:
2274 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002275 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002276 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002277 case ISD::STACKSAVE:
2278 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002279 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2280 Tmp1 = Result.getValue(0);
2281 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002282
Chris Lattner140d53c2006-01-13 02:50:02 +00002283 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2284 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002285 case TargetLowering::Legal: break;
2286 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002287 Tmp3 = TLI.LowerOperation(Result, DAG);
2288 if (Tmp3.Val) {
2289 Tmp1 = LegalizeOp(Tmp3);
2290 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002291 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002292 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002293 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002294 // Expand to CopyFromReg if the target set
2295 // StackPointerRegisterToSaveRestore.
2296 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002297 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002298 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002299 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002300 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002301 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2302 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002303 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002304 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002305 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002306
2307 // Since stacksave produce two values, make sure to remember that we
2308 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002309 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2310 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2311 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002312
Chris Lattner140d53c2006-01-13 02:50:02 +00002313 case ISD::STACKRESTORE:
2314 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2315 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002316 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002317
2318 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2319 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002320 case TargetLowering::Legal: break;
2321 case TargetLowering::Custom:
2322 Tmp1 = TLI.LowerOperation(Result, DAG);
2323 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002324 break;
2325 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002326 // Expand to CopyToReg if the target set
2327 // StackPointerRegisterToSaveRestore.
2328 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2329 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2330 } else {
2331 Result = Tmp1;
2332 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002333 break;
2334 }
2335 break;
2336
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002337 case ISD::READCYCLECOUNTER:
2338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002339 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002340 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2341 Node->getValueType(0))) {
2342 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002343 case TargetLowering::Legal:
2344 Tmp1 = Result.getValue(0);
2345 Tmp2 = Result.getValue(1);
2346 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002347 case TargetLowering::Custom:
2348 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002349 Tmp1 = LegalizeOp(Result.getValue(0));
2350 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002351 break;
2352 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002353
2354 // Since rdcc produce two values, make sure to remember that we legalized
2355 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002356 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2357 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002358 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002359
Chris Lattner2ee743f2005-01-14 22:08:15 +00002360 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002361 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2362 case Expand: assert(0 && "It's impossible to expand bools");
2363 case Legal:
2364 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2365 break;
2366 case Promote:
2367 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002368 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002369 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002370 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2371 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002372 break;
2373 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002374 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002375 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002376
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002377 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002378
Nate Begemanb942a3d2005-08-23 04:29:48 +00002379 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002380 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002381 case TargetLowering::Legal: break;
2382 case TargetLowering::Custom: {
2383 Tmp1 = TLI.LowerOperation(Result, DAG);
2384 if (Tmp1.Val) Result = Tmp1;
2385 break;
2386 }
Nate Begeman9373a812005-08-10 20:51:12 +00002387 case TargetLowering::Expand:
2388 if (Tmp1.getOpcode() == ISD::SETCC) {
2389 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2390 Tmp2, Tmp3,
2391 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2392 } else {
2393 Result = DAG.getSelectCC(Tmp1,
2394 DAG.getConstant(0, Tmp1.getValueType()),
2395 Tmp2, Tmp3, ISD::SETNE);
2396 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002397 break;
2398 case TargetLowering::Promote: {
2399 MVT::ValueType NVT =
2400 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2401 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002402 if (MVT::isVector(Tmp2.getValueType())) {
2403 ExtOp = ISD::BIT_CONVERT;
2404 TruncOp = ISD::BIT_CONVERT;
2405 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002406 ExtOp = ISD::ANY_EXTEND;
2407 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002408 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002409 ExtOp = ISD::FP_EXTEND;
2410 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002411 }
2412 // Promote each of the values to the new type.
2413 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2414 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2415 // Perform the larger operation, then round down.
2416 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2417 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2418 break;
2419 }
2420 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002421 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002422 case ISD::SELECT_CC: {
2423 Tmp1 = Node->getOperand(0); // LHS
2424 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002425 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2426 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002427 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002428
Nate Begeman750ac1b2006-02-01 07:19:44 +00002429 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2430
2431 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2432 // the LHS is a legal SETCC itself. In this case, we need to compare
2433 // the result against zero to select between true and false values.
2434 if (Tmp2.Val == 0) {
2435 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2436 CC = DAG.getCondCode(ISD::SETNE);
2437 }
2438 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2439
2440 // Everything is legal, see if we should expand this op or something.
2441 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2442 default: assert(0 && "This action is not supported yet!");
2443 case TargetLowering::Legal: break;
2444 case TargetLowering::Custom:
2445 Tmp1 = TLI.LowerOperation(Result, DAG);
2446 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002447 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002448 }
2449 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002450 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002451 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002452 Tmp1 = Node->getOperand(0);
2453 Tmp2 = Node->getOperand(1);
2454 Tmp3 = Node->getOperand(2);
2455 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2456
2457 // If we had to Expand the SetCC operands into a SELECT node, then it may
2458 // not always be possible to return a true LHS & RHS. In this case, just
2459 // return the value we legalized, returned in the LHS
2460 if (Tmp2.Val == 0) {
2461 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002462 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002463 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002464
Chris Lattner73e142f2006-01-30 22:43:50 +00002465 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002466 default: assert(0 && "Cannot handle this action for SETCC yet!");
2467 case TargetLowering::Custom:
2468 isCustom = true;
2469 // FALLTHROUGH.
2470 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002471 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002472 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002473 Tmp4 = TLI.LowerOperation(Result, DAG);
2474 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002475 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002476 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002477 case TargetLowering::Promote: {
2478 // First step, figure out the appropriate operation to use.
2479 // Allow SETCC to not be supported for all legal data types
2480 // Mostly this targets FP
2481 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002482 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002483
2484 // Scan for the appropriate larger type to use.
2485 while (1) {
2486 NewInTy = (MVT::ValueType)(NewInTy+1);
2487
2488 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2489 "Fell off of the edge of the integer world");
2490 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2491 "Fell off of the edge of the floating point world");
2492
2493 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002494 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002495 break;
2496 }
2497 if (MVT::isInteger(NewInTy))
2498 assert(0 && "Cannot promote Legal Integer SETCC yet");
2499 else {
2500 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2501 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2502 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002503 Tmp1 = LegalizeOp(Tmp1);
2504 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002505 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002506 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002507 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002508 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002509 case TargetLowering::Expand:
2510 // Expand a setcc node into a select_cc of the same condition, lhs, and
2511 // rhs that selects between const 1 (true) and const 0 (false).
2512 MVT::ValueType VT = Node->getValueType(0);
2513 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2514 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002515 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002516 break;
2517 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002518 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002519 case ISD::MEMSET:
2520 case ISD::MEMCPY:
2521 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002522 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002523 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2524
2525 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2526 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2527 case Expand: assert(0 && "Cannot expand a byte!");
2528 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002529 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002530 break;
2531 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002532 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002533 break;
2534 }
2535 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002536 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002537 }
Chris Lattner272455b2005-02-02 03:44:41 +00002538
2539 SDOperand Tmp4;
2540 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002541 case Expand: {
2542 // Length is too big, just take the lo-part of the length.
2543 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002544 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002545 break;
2546 }
Chris Lattnere5605212005-01-28 22:29:18 +00002547 case Legal:
2548 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002549 break;
2550 case Promote:
2551 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002552 break;
2553 }
2554
2555 SDOperand Tmp5;
2556 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2557 case Expand: assert(0 && "Cannot expand this yet!");
2558 case Legal:
2559 Tmp5 = LegalizeOp(Node->getOperand(4));
2560 break;
2561 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002562 Tmp5 = PromoteOp(Node->getOperand(4));
2563 break;
2564 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002565
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002566 SDOperand Tmp6;
2567 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2568 case Expand: assert(0 && "Cannot expand this yet!");
2569 case Legal:
2570 Tmp6 = LegalizeOp(Node->getOperand(5));
2571 break;
2572 case Promote:
2573 Tmp6 = PromoteOp(Node->getOperand(5));
2574 break;
2575 }
2576
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002577 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2578 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002579 case TargetLowering::Custom:
2580 isCustom = true;
2581 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002582 case TargetLowering::Legal: {
2583 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2584 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002585 if (isCustom) {
2586 Tmp1 = TLI.LowerOperation(Result, DAG);
2587 if (Tmp1.Val) Result = Tmp1;
2588 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002589 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002590 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002591 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002592 // Otherwise, the target does not support this operation. Lower the
2593 // operation to an explicit libcall as appropriate.
2594 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002595 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002596 TargetLowering::ArgListTy Args;
2597 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002598
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002599 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002600 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002601 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002602 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002603 // Extend the (previously legalized) ubyte argument to be an int value
2604 // for the call.
2605 if (Tmp3.getValueType() > MVT::i32)
2606 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2607 else
2608 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002609 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002610 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002611 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002612 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002613
2614 FnName = "memset";
2615 } else if (Node->getOpcode() == ISD::MEMCPY ||
2616 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002617 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002618 Entry.Node = Tmp2; Args.push_back(Entry);
2619 Entry.Node = Tmp3; Args.push_back(Entry);
2620 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002621 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2622 } else {
2623 assert(0 && "Unknown op!");
2624 }
Chris Lattner45982da2005-05-12 16:53:42 +00002625
Chris Lattnere1bd8222005-01-11 05:57:22 +00002626 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002627 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002628 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002629 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002630 break;
2631 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002632 }
2633 break;
2634 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002635
Chris Lattner5b359c62005-04-02 04:00:59 +00002636 case ISD::SHL_PARTS:
2637 case ISD::SRA_PARTS:
2638 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002639 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002640 bool Changed = false;
2641 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2642 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2643 Changed |= Ops.back() != Node->getOperand(i);
2644 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002645 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002646 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002647
Evan Cheng05a2d562006-01-09 18:31:59 +00002648 switch (TLI.getOperationAction(Node->getOpcode(),
2649 Node->getValueType(0))) {
2650 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002651 case TargetLowering::Legal: break;
2652 case TargetLowering::Custom:
2653 Tmp1 = TLI.LowerOperation(Result, DAG);
2654 if (Tmp1.Val) {
2655 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002656 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002657 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002658 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2659 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002660 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002661 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002662 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002663 return RetVal;
2664 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002665 break;
2666 }
2667
Chris Lattner2c8086f2005-04-02 05:00:07 +00002668 // Since these produce multiple values, make sure to remember that we
2669 // legalized all of them.
2670 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2671 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2672 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002673 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002674
2675 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002676 case ISD::ADD:
2677 case ISD::SUB:
2678 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002679 case ISD::MULHS:
2680 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002681 case ISD::UDIV:
2682 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002683 case ISD::AND:
2684 case ISD::OR:
2685 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002686 case ISD::SHL:
2687 case ISD::SRL:
2688 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002689 case ISD::FADD:
2690 case ISD::FSUB:
2691 case ISD::FMUL:
2692 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002693 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002694 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002695 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2696 case Expand: assert(0 && "Not possible");
2697 case Legal:
2698 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2699 break;
2700 case Promote:
2701 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2702 break;
2703 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002704
2705 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002706
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002707 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002708 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002709 case TargetLowering::Legal: break;
2710 case TargetLowering::Custom:
2711 Tmp1 = TLI.LowerOperation(Result, DAG);
2712 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002713 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002714 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002715 MVT::ValueType VT = Op.getValueType();
2716
2717 // See if multiply or divide can be lowered using two-result operations.
2718 SDVTList VTs = DAG.getVTList(VT, VT);
2719 if (Node->getOpcode() == ISD::MUL) {
2720 // We just need the low half of the multiply; try both the signed
2721 // and unsigned forms. If the target supports both SMUL_LOHI and
2722 // UMUL_LOHI, form a preference by checking which forms of plain
2723 // MULH it supports.
2724 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2725 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2726 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2727 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2728 unsigned OpToUse = 0;
2729 if (HasSMUL_LOHI && !HasMULHS) {
2730 OpToUse = ISD::SMUL_LOHI;
2731 } else if (HasUMUL_LOHI && !HasMULHU) {
2732 OpToUse = ISD::UMUL_LOHI;
2733 } else if (HasSMUL_LOHI) {
2734 OpToUse = ISD::SMUL_LOHI;
2735 } else if (HasUMUL_LOHI) {
2736 OpToUse = ISD::UMUL_LOHI;
2737 }
2738 if (OpToUse) {
2739 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2740 break;
2741 }
2742 }
2743 if (Node->getOpcode() == ISD::MULHS &&
2744 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2745 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2746 break;
2747 }
2748 if (Node->getOpcode() == ISD::MULHU &&
2749 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2750 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2751 break;
2752 }
2753 if (Node->getOpcode() == ISD::SDIV &&
2754 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2755 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2756 break;
2757 }
2758 if (Node->getOpcode() == ISD::UDIV &&
2759 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2760 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2761 break;
2762 }
2763
Dan Gohman82669522007-10-11 23:57:53 +00002764 // Check to see if we have a libcall for this operator.
2765 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2766 bool isSigned = false;
2767 switch (Node->getOpcode()) {
2768 case ISD::UDIV:
2769 case ISD::SDIV:
2770 if (VT == MVT::i32) {
2771 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002772 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002773 isSigned = Node->getOpcode() == ISD::SDIV;
2774 }
2775 break;
2776 case ISD::FPOW:
2777 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2778 VT == MVT::f64 ? RTLIB::POW_F64 :
2779 VT == MVT::f80 ? RTLIB::POW_F80 :
2780 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2781 RTLIB::UNKNOWN_LIBCALL;
2782 break;
2783 default: break;
2784 }
2785 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2786 SDOperand Dummy;
2787 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002788 break;
2789 }
2790
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002791 assert(MVT::isVector(Node->getValueType(0)) &&
2792 "Cannot expand this binary operator!");
2793 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002794 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002795 break;
2796 }
Evan Chengcc987612006-04-12 21:20:24 +00002797 case TargetLowering::Promote: {
2798 switch (Node->getOpcode()) {
2799 default: assert(0 && "Do not know how to promote this BinOp!");
2800 case ISD::AND:
2801 case ISD::OR:
2802 case ISD::XOR: {
2803 MVT::ValueType OVT = Node->getValueType(0);
2804 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2805 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2806 // Bit convert each of the values to the new type.
2807 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2808 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2809 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2810 // Bit convert the result back the original type.
2811 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2812 break;
2813 }
2814 }
2815 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002816 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002817 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002818
Dan Gohmane14ea862007-10-05 14:17:22 +00002819 case ISD::SMUL_LOHI:
2820 case ISD::UMUL_LOHI:
2821 case ISD::SDIVREM:
2822 case ISD::UDIVREM:
2823 // These nodes will only be produced by target-specific lowering, so
2824 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00002825 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00002826 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002827
2828 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2829 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2830 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002831 break;
2832
Chris Lattnera09f8482006-03-05 05:09:38 +00002833 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2834 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2835 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2836 case Expand: assert(0 && "Not possible");
2837 case Legal:
2838 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2839 break;
2840 case Promote:
2841 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2842 break;
2843 }
2844
2845 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2846
2847 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2848 default: assert(0 && "Operation not supported");
2849 case TargetLowering::Custom:
2850 Tmp1 = TLI.LowerOperation(Result, DAG);
2851 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002852 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002853 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002854 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002855 // If this target supports fabs/fneg natively and select is cheap,
2856 // do this efficiently.
2857 if (!TLI.isSelectExpensive() &&
2858 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2859 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002860 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002861 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002862 // Get the sign bit of the RHS.
2863 MVT::ValueType IVT =
2864 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2865 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2866 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2867 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2868 // Get the absolute value of the result.
2869 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2870 // Select between the nabs and abs value based on the sign bit of
2871 // the input.
2872 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2873 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2874 AbsVal),
2875 AbsVal);
2876 Result = LegalizeOp(Result);
2877 break;
2878 }
2879
2880 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002881 MVT::ValueType NVT =
2882 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2883 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2884 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2885 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002886 break;
2887 }
Evan Cheng912095b2007-01-04 21:56:39 +00002888 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002889 break;
2890
Nate Begeman551bf3f2006-02-17 05:43:56 +00002891 case ISD::ADDC:
2892 case ISD::SUBC:
2893 Tmp1 = LegalizeOp(Node->getOperand(0));
2894 Tmp2 = LegalizeOp(Node->getOperand(1));
2895 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2896 // Since this produces two values, make sure to remember that we legalized
2897 // both of them.
2898 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2899 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2900 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002901
Nate Begeman551bf3f2006-02-17 05:43:56 +00002902 case ISD::ADDE:
2903 case ISD::SUBE:
2904 Tmp1 = LegalizeOp(Node->getOperand(0));
2905 Tmp2 = LegalizeOp(Node->getOperand(1));
2906 Tmp3 = LegalizeOp(Node->getOperand(2));
2907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2908 // Since this produces two values, make sure to remember that we legalized
2909 // both of them.
2910 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2911 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2912 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002913
Nate Begeman419f8b62005-10-18 00:27:41 +00002914 case ISD::BUILD_PAIR: {
2915 MVT::ValueType PairTy = Node->getValueType(0);
2916 // TODO: handle the case where the Lo and Hi operands are not of legal type
2917 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2918 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2919 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002920 case TargetLowering::Promote:
2921 case TargetLowering::Custom:
2922 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002923 case TargetLowering::Legal:
2924 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2925 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2926 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002927 case TargetLowering::Expand:
2928 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2929 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2930 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2931 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2932 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002933 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002934 break;
2935 }
2936 break;
2937 }
2938
Nate Begemanc105e192005-04-06 00:23:54 +00002939 case ISD::UREM:
2940 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002941 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002942 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2943 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002944
Nate Begemanc105e192005-04-06 00:23:54 +00002945 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002946 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2947 case TargetLowering::Custom:
2948 isCustom = true;
2949 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002950 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002951 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002952 if (isCustom) {
2953 Tmp1 = TLI.LowerOperation(Result, DAG);
2954 if (Tmp1.Val) Result = Tmp1;
2955 }
Nate Begemanc105e192005-04-06 00:23:54 +00002956 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002957 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002958 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002959 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00002960 MVT::ValueType VT = Node->getValueType(0);
2961
2962 // See if remainder can be lowered using two-result operations.
2963 SDVTList VTs = DAG.getVTList(VT, VT);
2964 if (Node->getOpcode() == ISD::SREM &&
2965 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2966 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2967 break;
2968 }
2969 if (Node->getOpcode() == ISD::UREM &&
2970 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2971 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2972 break;
2973 }
2974
2975 if (MVT::isInteger(VT)) {
2976 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002977 TargetLowering::Legal) {
2978 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00002979 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002980 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2981 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00002982 } else if (MVT::isVector(VT)) {
2983 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002984 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00002985 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002986 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002987 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2988 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002989 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002990 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002991 }
Dan Gohman0d974262007-11-06 22:11:54 +00002992 } else {
2993 assert(MVT::isFloatingPoint(VT) &&
2994 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00002995 if (MVT::isVector(VT)) {
2996 Result = LegalizeOp(UnrollVectorOp(Op));
2997 } else {
2998 // Floating point mod -> fmod libcall.
2999 RTLIB::Libcall LC = VT == MVT::f32
3000 ? RTLIB::REM_F32 : RTLIB::REM_F64;
3001 SDOperand Dummy;
3002 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3003 false/*sign irrelevant*/, Dummy);
3004 }
Nate Begemanc105e192005-04-06 00:23:54 +00003005 }
3006 break;
3007 }
Dan Gohman525178c2007-10-08 18:33:35 +00003008 }
Nate Begemanc105e192005-04-06 00:23:54 +00003009 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003010 case ISD::VAARG: {
3011 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3012 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3013
Chris Lattner5c62f332006-01-28 07:42:08 +00003014 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003015 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3016 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003017 case TargetLowering::Custom:
3018 isCustom = true;
3019 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003020 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003021 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3022 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003023 Tmp1 = Result.getValue(1);
3024
3025 if (isCustom) {
3026 Tmp2 = TLI.LowerOperation(Result, DAG);
3027 if (Tmp2.Val) {
3028 Result = LegalizeOp(Tmp2);
3029 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3030 }
3031 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003032 break;
3033 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00003034 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00003035 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003036 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003037 // Increment the pointer, VAList, to the next vaarg
3038 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3039 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3040 TLI.getPointerTy()));
3041 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003042 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3043 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003044 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003045 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003046 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003047 Result = LegalizeOp(Result);
3048 break;
3049 }
3050 }
3051 // Since VAARG produces two values, make sure to remember that we
3052 // legalized both of them.
3053 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003054 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3055 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003056 }
3057
3058 case ISD::VACOPY:
3059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3060 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3061 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3062
3063 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3064 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003065 case TargetLowering::Custom:
3066 isCustom = true;
3067 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003068 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3070 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003071 if (isCustom) {
3072 Tmp1 = TLI.LowerOperation(Result, DAG);
3073 if (Tmp1.Val) Result = Tmp1;
3074 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003075 break;
3076 case TargetLowering::Expand:
3077 // This defaults to loading a pointer from the input and storing it to the
3078 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00003079 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003080 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00003081 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3082 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003083 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3084 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003085 break;
3086 }
3087 break;
3088
3089 case ISD::VAEND:
3090 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3091 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3092
3093 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3094 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003095 case TargetLowering::Custom:
3096 isCustom = true;
3097 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003098 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003099 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003100 if (isCustom) {
3101 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3102 if (Tmp1.Val) Result = Tmp1;
3103 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003104 break;
3105 case TargetLowering::Expand:
3106 Result = Tmp1; // Default to a no-op, return the chain
3107 break;
3108 }
3109 break;
3110
3111 case ISD::VASTART:
3112 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3113 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3114
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3116
Nate Begemanacc398c2006-01-25 18:21:52 +00003117 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3118 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003119 case TargetLowering::Legal: break;
3120 case TargetLowering::Custom:
3121 Tmp1 = TLI.LowerOperation(Result, DAG);
3122 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003123 break;
3124 }
3125 break;
3126
Nate Begeman35ef9132006-01-11 21:21:00 +00003127 case ISD::ROTL:
3128 case ISD::ROTR:
3129 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3130 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003132 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3133 default:
3134 assert(0 && "ROTL/ROTR legalize operation not supported");
3135 break;
3136 case TargetLowering::Legal:
3137 break;
3138 case TargetLowering::Custom:
3139 Tmp1 = TLI.LowerOperation(Result, DAG);
3140 if (Tmp1.Val) Result = Tmp1;
3141 break;
3142 case TargetLowering::Promote:
3143 assert(0 && "Do not know how to promote ROTL/ROTR");
3144 break;
3145 case TargetLowering::Expand:
3146 assert(0 && "Do not know how to expand ROTL/ROTR");
3147 break;
3148 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003149 break;
3150
Nate Begemand88fc032006-01-14 03:14:10 +00003151 case ISD::BSWAP:
3152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3153 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003154 case TargetLowering::Custom:
3155 assert(0 && "Cannot custom legalize this yet!");
3156 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003157 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003158 break;
3159 case TargetLowering::Promote: {
3160 MVT::ValueType OVT = Tmp1.getValueType();
3161 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003162 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003163
Chris Lattner456a93a2006-01-28 07:39:30 +00003164 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3165 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3166 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3167 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3168 break;
3169 }
3170 case TargetLowering::Expand:
3171 Result = ExpandBSWAP(Tmp1);
3172 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003173 }
3174 break;
3175
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003176 case ISD::CTPOP:
3177 case ISD::CTTZ:
3178 case ISD::CTLZ:
3179 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3180 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003181 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003182 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003183 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003184 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003185 TargetLowering::Custom) {
3186 Tmp1 = TLI.LowerOperation(Result, DAG);
3187 if (Tmp1.Val) {
3188 Result = Tmp1;
3189 }
Scott Michel910b66d2007-07-30 21:00:31 +00003190 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003191 break;
3192 case TargetLowering::Promote: {
3193 MVT::ValueType OVT = Tmp1.getValueType();
3194 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003195
3196 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003197 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3198 // Perform the larger operation, then subtract if needed.
3199 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003200 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003201 case ISD::CTPOP:
3202 Result = Tmp1;
3203 break;
3204 case ISD::CTTZ:
3205 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003206 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003207 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003208 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003209 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003210 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003211 break;
3212 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003213 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003214 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003215 DAG.getConstant(MVT::getSizeInBits(NVT) -
3216 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003217 break;
3218 }
3219 break;
3220 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003221 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003222 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003223 break;
3224 }
3225 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003226
Chris Lattner2c8086f2005-04-02 05:00:07 +00003227 // Unary operators
3228 case ISD::FABS:
3229 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003230 case ISD::FSQRT:
3231 case ISD::FSIN:
3232 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003233 Tmp1 = LegalizeOp(Node->getOperand(0));
3234 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003235 case TargetLowering::Promote:
3236 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003237 isCustom = true;
3238 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003239 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003240 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003241 if (isCustom) {
3242 Tmp1 = TLI.LowerOperation(Result, DAG);
3243 if (Tmp1.Val) Result = Tmp1;
3244 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003245 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003246 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003247 switch (Node->getOpcode()) {
3248 default: assert(0 && "Unreachable!");
3249 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003250 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3251 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003252 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003253 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003254 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003255 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3256 MVT::ValueType VT = Node->getValueType(0);
3257 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003258 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003259 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3260 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003261 break;
3262 }
3263 case ISD::FSQRT:
3264 case ISD::FSIN:
3265 case ISD::FCOS: {
3266 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003267
3268 // Expand unsupported unary vector operators by unrolling them.
3269 if (MVT::isVector(VT)) {
3270 Result = LegalizeOp(UnrollVectorOp(Op));
3271 break;
3272 }
3273
Evan Cheng56966222007-01-12 02:11:51 +00003274 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003275 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003276 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00003277 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003278 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3279 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3280 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3281 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00003282 break;
3283 case ISD::FSIN:
3284 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3285 break;
3286 case ISD::FCOS:
3287 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3288 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003289 default: assert(0 && "Unreachable!");
3290 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003291 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003292 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3293 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003294 break;
3295 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003296 }
3297 break;
3298 }
3299 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003300 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003301 MVT::ValueType VT = Node->getValueType(0);
3302
3303 // Expand unsupported unary vector operators by unrolling them.
3304 if (MVT::isVector(VT)) {
3305 Result = LegalizeOp(UnrollVectorOp(Op));
3306 break;
3307 }
3308
3309 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen317096a2007-09-28 01:08:20 +00003310 RTLIB::Libcall LC =
Dan Gohman82669522007-10-11 23:57:53 +00003311 VT == MVT::f32 ? RTLIB::POWI_F32 :
3312 VT == MVT::f64 ? RTLIB::POWI_F64 :
3313 VT == MVT::f80 ? RTLIB::POWI_F80 :
3314 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesen161e8972007-10-05 20:04:43 +00003315 RTLIB::UNKNOWN_LIBCALL;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003316 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003317 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3318 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003319 break;
3320 }
Chris Lattner35481892005-12-23 00:16:34 +00003321 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003322 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003323 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003324 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3325 // The input has to be a vector type, we have to either scalarize it, pack
3326 // it, or convert it based on whether the input vector type is legal.
3327 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003328 int InIx = Node->getOperand(0).ResNo;
3329 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3330 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003331
3332 // Figure out if there is a simple type corresponding to this Vector
3333 // type. If so, convert to the vector type.
3334 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3335 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003336 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003337 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3338 LegalizeOp(Node->getOperand(0)));
3339 break;
3340 } else if (NumElems == 1) {
3341 // Turn this into a bit convert of the scalar input.
3342 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3343 ScalarizeVectorOp(Node->getOperand(0)));
3344 break;
3345 } else {
3346 // FIXME: UNIMP! Store then reload
3347 assert(0 && "Cast from unsupported vector type not implemented yet!");
3348 }
Chris Lattner67993f72006-01-23 07:30:46 +00003349 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003350 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3351 Node->getOperand(0).getValueType())) {
3352 default: assert(0 && "Unknown operation action!");
3353 case TargetLowering::Expand:
3354 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3355 break;
3356 case TargetLowering::Legal:
3357 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003358 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003359 break;
3360 }
3361 }
3362 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003363
Chris Lattner2c8086f2005-04-02 05:00:07 +00003364 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003365 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003366 case ISD::UINT_TO_FP: {
3367 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003368 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3369 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003370 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003371 Node->getOperand(0).getValueType())) {
3372 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003373 case TargetLowering::Custom:
3374 isCustom = true;
3375 // FALLTHROUGH
3376 case TargetLowering::Legal:
3377 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003378 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003379 if (isCustom) {
3380 Tmp1 = TLI.LowerOperation(Result, DAG);
3381 if (Tmp1.Val) Result = Tmp1;
3382 }
3383 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003384 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003385 Result = ExpandLegalINT_TO_FP(isSigned,
3386 LegalizeOp(Node->getOperand(0)),
3387 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003388 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003389 case TargetLowering::Promote:
3390 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3391 Node->getValueType(0),
3392 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003393 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003394 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003395 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003396 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003397 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3398 Node->getValueType(0), Node->getOperand(0));
3399 break;
3400 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003401 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003402 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003403 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3404 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003405 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003406 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3407 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003408 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003409 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3410 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003411 break;
3412 }
3413 break;
3414 }
3415 case ISD::TRUNCATE:
3416 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3417 case Legal:
3418 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003419 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003420 break;
3421 case Expand:
3422 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3423
3424 // Since the result is legal, we should just be able to truncate the low
3425 // part of the source.
3426 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3427 break;
3428 case Promote:
3429 Result = PromoteOp(Node->getOperand(0));
3430 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3431 break;
3432 }
3433 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003434
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003435 case ISD::FP_TO_SINT:
3436 case ISD::FP_TO_UINT:
3437 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3438 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003439 Tmp1 = LegalizeOp(Node->getOperand(0));
3440
Chris Lattner1618beb2005-07-29 00:11:56 +00003441 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3442 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003443 case TargetLowering::Custom:
3444 isCustom = true;
3445 // FALLTHROUGH
3446 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003447 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003448 if (isCustom) {
3449 Tmp1 = TLI.LowerOperation(Result, DAG);
3450 if (Tmp1.Val) Result = Tmp1;
3451 }
3452 break;
3453 case TargetLowering::Promote:
3454 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3455 Node->getOpcode() == ISD::FP_TO_SINT);
3456 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003457 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003458 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3459 SDOperand True, False;
3460 MVT::ValueType VT = Node->getOperand(0).getValueType();
3461 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003462 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003463 const uint64_t zero[] = {0, 0};
3464 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3465 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003466 (void)apf.convertFromZeroExtendedInteger
3467 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003468 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003469 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3470 Node->getOperand(0), Tmp2, ISD::SETLT);
3471 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3472 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003473 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003474 Tmp2));
3475 False = DAG.getNode(ISD::XOR, NVT, False,
3476 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003477 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3478 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003479 } else {
3480 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3481 }
3482 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003483 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003484 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003485 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003486 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003487 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003488 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003489 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003490 if (Node->getOpcode()==ISD::FP_TO_SINT)
3491 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003492 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3493 (DAG.getNode(ISD::FP_ROUND_INREG,
3494 MVT::ppcf128, Node->getOperand(0),
3495 DAG.getValueType(MVT::f64)))));
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003496 else {
3497 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3498 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3499 Tmp2 = DAG.getConstantFP(apf, OVT);
3500 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3501 // FIXME: generated code sucks.
3502 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3503 DAG.getNode(ISD::ADD, MVT::i32,
3504 DAG.getNode(ISD::FP_TO_SINT, VT,
3505 DAG.getNode(ISD::FSUB, OVT,
3506 Node->getOperand(0), Tmp2)),
3507 DAG.getConstant(0x80000000, MVT::i32)),
3508 DAG.getNode(ISD::FP_TO_SINT, VT,
3509 Node->getOperand(0)),
3510 DAG.getCondCode(ISD::SETGE));
3511 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003512 break;
3513 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003514 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003515 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003516 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003517 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003518 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003519 LC = (VT == MVT::i32)
3520 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003521 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003522 LC = (VT == MVT::i32)
3523 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003524 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003525 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003526 LC = RTLIB::FPTOSINT_F80_I64;
3527 }
3528 else if (OVT == MVT::ppcf128) {
3529 assert(VT == MVT::i64);
3530 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003531 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003532 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003533 }
3534 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003535 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003536 LC = (VT == MVT::i32)
3537 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003538 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003539 LC = (VT == MVT::i32)
3540 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003541 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003542 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003543 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3544 }
3545 else if (OVT == MVT::ppcf128) {
3546 assert(VT == MVT::i64);
3547 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003548 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003549 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003550 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003551 default: assert(0 && "Unreachable!");
3552 }
3553 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003554 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3555 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003556 break;
3557 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003558 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003559 Tmp1 = PromoteOp(Node->getOperand(0));
3560 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3561 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003562 break;
3563 }
3564 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003565
Dale Johannesenab081c72007-08-09 17:27:48 +00003566 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003567 case ISD::FP_ROUND: {
3568 MVT::ValueType newVT = Op.getValueType();
3569 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3570 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003571 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3572 SDOperand Lo, Hi;
3573 ExpandOp(Node->getOperand(0), Lo, Hi);
3574 if (newVT == MVT::f64)
3575 Result = Hi;
3576 else
3577 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3578 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003579 } else {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003580 // The only other way we can lower this is to turn it into a STORE,
3581 // LOAD pair, targetting a temporary location (a stack slot).
3582
3583 // NOTE: there is a choice here between constantly creating new stack
3584 // slots and always reusing the same one. We currently always create
3585 // new ones, as reuse may inhibit scheduling.
3586 MVT::ValueType slotVT =
3587 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3588 const Type *Ty = MVT::getTypeForValueType(slotVT);
Duncan Sands514ab342007-11-01 20:53:16 +00003589 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003590 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3591 MachineFunction &MF = DAG.getMachineFunction();
3592 int SSFI =
3593 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3594 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3595 if (Node->getOpcode() == ISD::FP_EXTEND) {
3596 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3597 StackSlot, NULL, 0);
3598 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3599 Result, StackSlot, NULL, 0, oldVT);
3600 } else {
3601 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3602 StackSlot, NULL, 0, newVT);
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003603 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003604 }
3605 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003606 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003607 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003608 }
3609 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003610 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003611 case ISD::ZERO_EXTEND:
3612 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003613 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003614 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003615 case Legal:
3616 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003617 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003618 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003619 case Promote:
3620 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003621 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003622 Tmp1 = PromoteOp(Node->getOperand(0));
3623 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003624 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003625 case ISD::ZERO_EXTEND:
3626 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003627 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003628 Result = DAG.getZeroExtendInReg(Result,
3629 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003630 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003631 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003632 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003633 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003634 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003635 Result,
3636 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003637 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003638 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003639 Result = PromoteOp(Node->getOperand(0));
3640 if (Result.getValueType() != Op.getValueType())
3641 // Dynamically dead while we have only 2 FP types.
3642 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3643 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003644 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003645 Result = PromoteOp(Node->getOperand(0));
3646 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3647 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003648 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003649 }
3650 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003651 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003652 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003653 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003654 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003655
3656 // If this operation is not supported, convert it to a shl/shr or load/store
3657 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003658 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3659 default: assert(0 && "This action not supported for this op yet!");
3660 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003661 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003662 break;
3663 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003664 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003665 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003666 // NOTE: we could fall back on load/store here too for targets without
3667 // SAR. However, it is doubtful that any exist.
3668 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3669 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003670 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003671 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3672 Node->getOperand(0), ShiftCst);
3673 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3674 Result, ShiftCst);
3675 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003676 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003677 // EXTLOAD pair, targetting a temporary location (a stack slot).
3678
3679 // NOTE: there is a choice here between constantly creating new stack
3680 // slots and always reusing the same one. We currently always create
3681 // new ones, as reuse may inhibit scheduling.
3682 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Duncan Sands514ab342007-11-01 20:53:16 +00003683 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003684 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003685 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003686 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003687 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003688 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003689 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3690 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003691 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003692 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003693 } else {
3694 assert(0 && "Unknown op");
3695 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003696 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003697 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003698 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003699 }
Duncan Sands36397f52007-07-27 12:58:54 +00003700 case ISD::TRAMPOLINE: {
3701 SDOperand Ops[6];
3702 for (unsigned i = 0; i != 6; ++i)
3703 Ops[i] = LegalizeOp(Node->getOperand(i));
3704 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3705 // The only option for this node is to custom lower it.
3706 Result = TLI.LowerOperation(Result, DAG);
3707 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003708
3709 // Since trampoline produces two values, make sure to remember that we
3710 // legalized both of them.
3711 Tmp1 = LegalizeOp(Result.getValue(1));
3712 Result = LegalizeOp(Result);
3713 AddLegalizedOperand(SDOperand(Node, 0), Result);
3714 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3715 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003716 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003717 case ISD::FLT_ROUNDS: {
3718 MVT::ValueType VT = Node->getValueType(0);
3719 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3720 default: assert(0 && "This action not supported for this op yet!");
3721 case TargetLowering::Custom:
3722 Result = TLI.LowerOperation(Op, DAG);
3723 if (Result.Val) break;
3724 // Fall Thru
3725 case TargetLowering::Legal:
3726 // If this operation is not supported, lower it to constant 1
3727 Result = DAG.getConstant(1, VT);
3728 break;
3729 }
3730 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003731 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003732
Chris Lattner4ddd2832006-04-08 04:13:17 +00003733 assert(Result.getValueType() == Op.getValueType() &&
3734 "Bad legalization!");
3735
Chris Lattner456a93a2006-01-28 07:39:30 +00003736 // Make sure that the generated code is itself legal.
3737 if (Result != Op)
3738 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003739
Chris Lattner45982da2005-05-12 16:53:42 +00003740 // Note that LegalizeOp may be reentered even from single-use nodes, which
3741 // means that we always must cache transformed nodes.
3742 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003743 return Result;
3744}
3745
Chris Lattner8b6fa222005-01-15 22:16:26 +00003746/// PromoteOp - Given an operation that produces a value in an invalid type,
3747/// promote it to compute the value into a larger type. The produced value will
3748/// have the correct bits for the low portion of the register, but no guarantee
3749/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003750SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3751 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003752 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003753 assert(getTypeAction(VT) == Promote &&
3754 "Caller should expand or legalize operands that are not promotable!");
3755 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3756 "Cannot promote to smaller type!");
3757
Chris Lattner03c85462005-01-15 05:21:40 +00003758 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003759 SDOperand Result;
3760 SDNode *Node = Op.Val;
3761
Chris Lattner40030bf2007-02-04 01:17:38 +00003762 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003763 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003764
Chris Lattner03c85462005-01-15 05:21:40 +00003765 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003766 case ISD::CopyFromReg:
3767 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003768 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003769#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003770 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003771#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003772 assert(0 && "Do not know how to promote this operator!");
3773 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003774 case ISD::UNDEF:
3775 Result = DAG.getNode(ISD::UNDEF, NVT);
3776 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003777 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003778 if (VT != MVT::i1)
3779 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3780 else
3781 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003782 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3783 break;
3784 case ISD::ConstantFP:
3785 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3786 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3787 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003788
Chris Lattner82fbfb62005-01-18 02:59:52 +00003789 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003790 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003791 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3792 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003793 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003794
Chris Lattner03c85462005-01-15 05:21:40 +00003795 case ISD::TRUNCATE:
3796 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3797 case Legal:
3798 Result = LegalizeOp(Node->getOperand(0));
3799 assert(Result.getValueType() >= NVT &&
3800 "This truncation doesn't make sense!");
3801 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3802 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3803 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003804 case Promote:
3805 // The truncation is not required, because we don't guarantee anything
3806 // about high bits anyway.
3807 Result = PromoteOp(Node->getOperand(0));
3808 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003809 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003810 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3811 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003812 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003813 }
3814 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003815 case ISD::SIGN_EXTEND:
3816 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003817 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003818 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3819 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3820 case Legal:
3821 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003822 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003823 break;
3824 case Promote:
3825 // Promote the reg if it's smaller.
3826 Result = PromoteOp(Node->getOperand(0));
3827 // The high bits are not guaranteed to be anything. Insert an extend.
3828 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003829 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003830 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003831 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003832 Result = DAG.getZeroExtendInReg(Result,
3833 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003834 break;
3835 }
3836 break;
Chris Lattner35481892005-12-23 00:16:34 +00003837 case ISD::BIT_CONVERT:
3838 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3839 Result = PromoteOp(Result);
3840 break;
3841
Chris Lattner8b6fa222005-01-15 22:16:26 +00003842 case ISD::FP_EXTEND:
3843 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3844 case ISD::FP_ROUND:
3845 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3846 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3847 case Promote: assert(0 && "Unreachable with 2 FP types!");
3848 case Legal:
3849 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003850 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003851 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003852 break;
3853 }
3854 break;
3855
3856 case ISD::SINT_TO_FP:
3857 case ISD::UINT_TO_FP:
3858 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3859 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003860 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003861 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003862 break;
3863
3864 case Promote:
3865 Result = PromoteOp(Node->getOperand(0));
3866 if (Node->getOpcode() == ISD::SINT_TO_FP)
3867 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003868 Result,
3869 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003870 else
Chris Lattner23993562005-04-13 02:38:47 +00003871 Result = DAG.getZeroExtendInReg(Result,
3872 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003873 // No extra round required here.
3874 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003875 break;
3876 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003877 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3878 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003879 // Round if we cannot tolerate excess precision.
3880 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003881 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3882 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003883 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003884 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003885 break;
3886
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003887 case ISD::SIGN_EXTEND_INREG:
3888 Result = PromoteOp(Node->getOperand(0));
3889 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3890 Node->getOperand(1));
3891 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003892 case ISD::FP_TO_SINT:
3893 case ISD::FP_TO_UINT:
3894 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3895 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003896 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003897 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003898 break;
3899 case Promote:
3900 // The input result is prerounded, so we don't have to do anything
3901 // special.
3902 Tmp1 = PromoteOp(Node->getOperand(0));
3903 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003904 }
Nate Begemand2558e32005-08-14 01:20:53 +00003905 // If we're promoting a UINT to a larger size, check to see if the new node
3906 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3907 // we can use that instead. This allows us to generate better code for
3908 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3909 // legal, such as PowerPC.
3910 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003911 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003912 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3913 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003914 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3915 } else {
3916 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3917 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003918 break;
3919
Chris Lattner2c8086f2005-04-02 05:00:07 +00003920 case ISD::FABS:
3921 case ISD::FNEG:
3922 Tmp1 = PromoteOp(Node->getOperand(0));
3923 assert(Tmp1.getValueType() == NVT);
3924 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3925 // NOTE: we do not have to do any extra rounding here for
3926 // NoExcessFPPrecision, because we know the input will have the appropriate
3927 // precision, and these operations don't modify precision at all.
3928 break;
3929
Chris Lattnerda6ba872005-04-28 21:44:33 +00003930 case ISD::FSQRT:
3931 case ISD::FSIN:
3932 case ISD::FCOS:
3933 Tmp1 = PromoteOp(Node->getOperand(0));
3934 assert(Tmp1.getValueType() == NVT);
3935 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003936 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003937 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3938 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003939 break;
3940
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003941 case ISD::FPOWI: {
3942 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3943 // directly as well, which may be better.
3944 Tmp1 = PromoteOp(Node->getOperand(0));
3945 assert(Tmp1.getValueType() == NVT);
3946 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3947 if (NoExcessFPPrecision)
3948 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3949 DAG.getValueType(VT));
3950 break;
3951 }
3952
Chris Lattner03c85462005-01-15 05:21:40 +00003953 case ISD::AND:
3954 case ISD::OR:
3955 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003956 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003957 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003958 case ISD::MUL:
3959 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003960 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003961 // that too is okay if they are integer operations.
3962 Tmp1 = PromoteOp(Node->getOperand(0));
3963 Tmp2 = PromoteOp(Node->getOperand(1));
3964 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3965 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003966 break;
3967 case ISD::FADD:
3968 case ISD::FSUB:
3969 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003970 Tmp1 = PromoteOp(Node->getOperand(0));
3971 Tmp2 = PromoteOp(Node->getOperand(1));
3972 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3973 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3974
3975 // Floating point operations will give excess precision that we may not be
3976 // able to tolerate. If we DO allow excess precision, just leave it,
3977 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003978 // FIXME: Why would we need to round FP ops more than integer ones?
3979 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003980 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003981 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3982 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003983 break;
3984
Chris Lattner8b6fa222005-01-15 22:16:26 +00003985 case ISD::SDIV:
3986 case ISD::SREM:
3987 // These operators require that their input be sign extended.
3988 Tmp1 = PromoteOp(Node->getOperand(0));
3989 Tmp2 = PromoteOp(Node->getOperand(1));
3990 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003991 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3992 DAG.getValueType(VT));
3993 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3994 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003995 }
3996 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3997
3998 // Perform FP_ROUND: this is probably overly pessimistic.
3999 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004000 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4001 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004002 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004003 case ISD::FDIV:
4004 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004005 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004006 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004007 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4008 case Legal:
4009 Tmp1 = LegalizeOp(Node->getOperand(0));
4010 break;
4011 case Promote:
4012 Tmp1 = PromoteOp(Node->getOperand(0));
4013 break;
4014 case Expand:
4015 assert(0 && "not implemented");
4016 }
4017 switch (getTypeAction(Node->getOperand(1).getValueType())) {
4018 case Legal:
4019 Tmp2 = LegalizeOp(Node->getOperand(1));
4020 break;
4021 case Promote:
4022 Tmp2 = PromoteOp(Node->getOperand(1));
4023 break;
4024 case Expand:
4025 assert(0 && "not implemented");
4026 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004027 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4028
4029 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004030 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004031 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4032 DAG.getValueType(VT));
4033 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004034
4035 case ISD::UDIV:
4036 case ISD::UREM:
4037 // These operators require that their input be zero extended.
4038 Tmp1 = PromoteOp(Node->getOperand(0));
4039 Tmp2 = PromoteOp(Node->getOperand(1));
4040 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004041 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4042 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004043 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4044 break;
4045
4046 case ISD::SHL:
4047 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004048 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004049 break;
4050 case ISD::SRA:
4051 // The input value must be properly sign extended.
4052 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004053 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4054 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004055 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004056 break;
4057 case ISD::SRL:
4058 // The input value must be properly zero extended.
4059 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004060 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004061 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004062 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004063
4064 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004065 Tmp1 = Node->getOperand(0); // Get the chain.
4066 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004067 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4068 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4069 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4070 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00004071 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00004072 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00004073 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004074 // Increment the pointer, VAList, to the next vaarg
4075 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4076 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4077 TLI.getPointerTy()));
4078 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00004079 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4080 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004081 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004082 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004083 }
4084 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004085 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004086 break;
4087
Evan Cheng466685d2006-10-09 20:57:25 +00004088 case ISD::LOAD: {
4089 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004090 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4091 ? ISD::EXTLOAD : LD->getExtensionType();
4092 Result = DAG.getExtLoad(ExtType, NVT,
4093 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004094 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004095 LD->getLoadedVT(),
4096 LD->isVolatile(),
4097 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004098 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004099 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004100 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004101 }
Chris Lattner03c85462005-01-15 05:21:40 +00004102 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004103 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4104 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004105 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004106 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004107 case ISD::SELECT_CC:
4108 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4109 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4110 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004111 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004112 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004113 case ISD::BSWAP:
4114 Tmp1 = Node->getOperand(0);
4115 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4116 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4117 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004118 DAG.getConstant(MVT::getSizeInBits(NVT) -
4119 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004120 TLI.getShiftAmountTy()));
4121 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004122 case ISD::CTPOP:
4123 case ISD::CTTZ:
4124 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004125 // Zero extend the argument
4126 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004127 // Perform the larger operation, then subtract if needed.
4128 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004129 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004130 case ISD::CTPOP:
4131 Result = Tmp1;
4132 break;
4133 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004134 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004135 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004136 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4137 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004138 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004139 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004140 break;
4141 case ISD::CTLZ:
4142 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004143 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004144 DAG.getConstant(MVT::getSizeInBits(NVT) -
4145 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004146 break;
4147 }
4148 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004149 case ISD::EXTRACT_SUBVECTOR:
4150 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004151 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004152 case ISD::EXTRACT_VECTOR_ELT:
4153 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4154 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004155 }
4156
4157 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004158
4159 // Make sure the result is itself legal.
4160 Result = LegalizeOp(Result);
4161
4162 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004163 AddPromotedOperand(Op, Result);
4164 return Result;
4165}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004166
Dan Gohman7f321562007-06-25 16:23:39 +00004167/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4168/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4169/// based on the vector type. The return type of this matches the element type
4170/// of the vector, which may not be legal for the target.
4171SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004172 // We know that operand #0 is the Vec vector. If the index is a constant
4173 // or if the invec is a supported hardware type, we can use it. Otherwise,
4174 // lower to a store then an indexed load.
4175 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004176 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004177
Dan Gohmane40c7b02007-09-24 15:54:53 +00004178 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004179 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004180
Dan Gohman7f321562007-06-25 16:23:39 +00004181 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4182 default: assert(0 && "This action is not supported yet!");
4183 case TargetLowering::Custom: {
4184 Vec = LegalizeOp(Vec);
4185 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4186 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4187 if (Tmp3.Val)
4188 return Tmp3;
4189 break;
4190 }
4191 case TargetLowering::Legal:
4192 if (isTypeLegal(TVT)) {
4193 Vec = LegalizeOp(Vec);
4194 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004195 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004196 }
4197 break;
4198 case TargetLowering::Expand:
4199 break;
4200 }
4201
4202 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004203 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004204 Op = ScalarizeVectorOp(Vec);
4205 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4206 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004207 SDOperand Lo, Hi;
4208 SplitVectorOp(Vec, Lo, Hi);
4209 if (CIdx->getValue() < NumElems/2) {
4210 Vec = Lo;
4211 } else {
4212 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004213 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4214 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004215 }
Dan Gohman7f321562007-06-25 16:23:39 +00004216
Chris Lattner15972212006-03-31 17:55:51 +00004217 // It's now an extract from the appropriate high or low part. Recurse.
4218 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004219 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004220 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004221 // Store the value to a temporary stack slot, then LOAD the scalar
4222 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004223 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004224 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4225
4226 // Add the offset to the index.
4227 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4228 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4229 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004230
4231 if (MVT::getSizeInBits(Idx.getValueType()) >
4232 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004233 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004234 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004235 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004236
Dan Gohman7f321562007-06-25 16:23:39 +00004237 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4238
4239 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004240 }
Dan Gohman7f321562007-06-25 16:23:39 +00004241 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004242}
4243
Dan Gohman7f321562007-06-25 16:23:39 +00004244/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004245/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004246SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004247 // We know that operand #0 is the Vec vector. For now we assume the index
4248 // is a constant and that the extracted result is a supported hardware type.
4249 SDOperand Vec = Op.getOperand(0);
4250 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4251
Dan Gohman7f321562007-06-25 16:23:39 +00004252 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004253
4254 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4255 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004256 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004257 }
4258
4259 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4260 SDOperand Lo, Hi;
4261 SplitVectorOp(Vec, Lo, Hi);
4262 if (CIdx->getValue() < NumElems/2) {
4263 Vec = Lo;
4264 } else {
4265 Vec = Hi;
4266 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4267 }
4268
4269 // It's now an extract from the appropriate high or low part. Recurse.
4270 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004271 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004272}
4273
Nate Begeman750ac1b2006-02-01 07:19:44 +00004274/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4275/// with condition CC on the current target. This usually involves legalizing
4276/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4277/// there may be no choice but to create a new SetCC node to represent the
4278/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4279/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4280void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4281 SDOperand &RHS,
4282 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004283 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004284
4285 switch (getTypeAction(LHS.getValueType())) {
4286 case Legal:
4287 Tmp1 = LegalizeOp(LHS); // LHS
4288 Tmp2 = LegalizeOp(RHS); // RHS
4289 break;
4290 case Promote:
4291 Tmp1 = PromoteOp(LHS); // LHS
4292 Tmp2 = PromoteOp(RHS); // RHS
4293
4294 // If this is an FP compare, the operands have already been extended.
4295 if (MVT::isInteger(LHS.getValueType())) {
4296 MVT::ValueType VT = LHS.getValueType();
4297 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4298
4299 // Otherwise, we have to insert explicit sign or zero extends. Note
4300 // that we could insert sign extends for ALL conditions, but zero extend
4301 // is cheaper on many machines (an AND instead of two shifts), so prefer
4302 // it.
4303 switch (cast<CondCodeSDNode>(CC)->get()) {
4304 default: assert(0 && "Unknown integer comparison!");
4305 case ISD::SETEQ:
4306 case ISD::SETNE:
4307 case ISD::SETUGE:
4308 case ISD::SETUGT:
4309 case ISD::SETULE:
4310 case ISD::SETULT:
4311 // ALL of these operations will work if we either sign or zero extend
4312 // the operands (including the unsigned comparisons!). Zero extend is
4313 // usually a simpler/cheaper operation, so prefer it.
4314 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4315 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4316 break;
4317 case ISD::SETGE:
4318 case ISD::SETGT:
4319 case ISD::SETLT:
4320 case ISD::SETLE:
4321 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4322 DAG.getValueType(VT));
4323 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4324 DAG.getValueType(VT));
4325 break;
4326 }
4327 }
4328 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004329 case Expand: {
4330 MVT::ValueType VT = LHS.getValueType();
4331 if (VT == MVT::f32 || VT == MVT::f64) {
4332 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004333 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004334 switch (cast<CondCodeSDNode>(CC)->get()) {
4335 case ISD::SETEQ:
4336 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004337 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004338 break;
4339 case ISD::SETNE:
4340 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004341 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004342 break;
4343 case ISD::SETGE:
4344 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004345 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004346 break;
4347 case ISD::SETLT:
4348 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004349 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004350 break;
4351 case ISD::SETLE:
4352 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004353 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004354 break;
4355 case ISD::SETGT:
4356 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004357 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004358 break;
4359 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004360 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4361 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004362 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004363 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004364 break;
4365 default:
Evan Cheng56966222007-01-12 02:11:51 +00004366 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004367 switch (cast<CondCodeSDNode>(CC)->get()) {
4368 case ISD::SETONE:
4369 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004370 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004371 // Fallthrough
4372 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004373 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004374 break;
4375 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004376 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004377 break;
4378 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004379 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004380 break;
4381 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004382 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004383 break;
Evan Cheng56966222007-01-12 02:11:51 +00004384 case ISD::SETUEQ:
4385 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004386 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004387 default: assert(0 && "Unsupported FP setcc!");
4388 }
4389 }
4390
4391 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004392 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004393 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004394 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004395 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004396 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004397 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004398 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004399 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004400 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004401 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004402 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004403 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004404 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4405 Tmp2 = SDOperand();
4406 }
4407 LHS = Tmp1;
4408 RHS = Tmp2;
4409 return;
4410 }
4411
Nate Begeman750ac1b2006-02-01 07:19:44 +00004412 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4413 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004414 ExpandOp(RHS, RHSLo, RHSHi);
4415 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4416
4417 if (VT==MVT::ppcf128) {
4418 // FIXME: This generated code sucks. We want to generate
4419 // FCMP crN, hi1, hi2
4420 // BNE crN, L:
4421 // FCMP crN, lo1, lo2
4422 // The following can be improved, but not that much.
4423 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4424 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4425 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4426 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4427 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4428 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4429 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4430 Tmp2 = SDOperand();
4431 break;
4432 }
4433
4434 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004435 case ISD::SETEQ:
4436 case ISD::SETNE:
4437 if (RHSLo == RHSHi)
4438 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4439 if (RHSCST->isAllOnesValue()) {
4440 // Comparison to -1.
4441 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4442 Tmp2 = RHSLo;
4443 break;
4444 }
4445
4446 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4447 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4448 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4449 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4450 break;
4451 default:
4452 // If this is a comparison of the sign bit, just look at the top part.
4453 // X > -1, x < 0
4454 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4455 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4456 CST->getValue() == 0) || // X < 0
4457 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4458 CST->isAllOnesValue())) { // X > -1
4459 Tmp1 = LHSHi;
4460 Tmp2 = RHSHi;
4461 break;
4462 }
4463
4464 // FIXME: This generated code sucks.
4465 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004466 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004467 default: assert(0 && "Unknown integer setcc!");
4468 case ISD::SETLT:
4469 case ISD::SETULT: LowCC = ISD::SETULT; break;
4470 case ISD::SETGT:
4471 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4472 case ISD::SETLE:
4473 case ISD::SETULE: LowCC = ISD::SETULE; break;
4474 case ISD::SETGE:
4475 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4476 }
4477
4478 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4479 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4480 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4481
4482 // NOTE: on targets without efficient SELECT of bools, we can always use
4483 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004484 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4485 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4486 false, DagCombineInfo);
4487 if (!Tmp1.Val)
4488 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4489 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4490 CCCode, false, DagCombineInfo);
4491 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004492 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004493
4494 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4495 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4496 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4497 (Tmp2C && Tmp2C->getValue() == 0 &&
4498 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4499 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4500 (Tmp2C && Tmp2C->getValue() == 1 &&
4501 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4502 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4503 // low part is known false, returns high part.
4504 // For LE / GE, if high part is known false, ignore the low part.
4505 // For LT / GT, if high part is known true, ignore the low part.
4506 Tmp1 = Tmp2;
4507 Tmp2 = SDOperand();
4508 } else {
4509 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4510 ISD::SETEQ, false, DagCombineInfo);
4511 if (!Result.Val)
4512 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4513 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4514 Result, Tmp1, Tmp2));
4515 Tmp1 = Result;
4516 Tmp2 = SDOperand();
4517 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004518 }
4519 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004520 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004521 LHS = Tmp1;
4522 RHS = Tmp2;
4523}
4524
Chris Lattner35481892005-12-23 00:16:34 +00004525/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004526/// The resultant code need not be legal. Note that SrcOp is the input operand
4527/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004528SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4529 SDOperand SrcOp) {
4530 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004531 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004532
4533 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004534 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004535 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004536 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004537}
4538
Chris Lattner4352cc92006-04-04 17:23:26 +00004539SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4540 // Create a vector sized/aligned stack slot, store the value to element #0,
4541 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004542 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004543 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004544 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004545 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004546}
4547
4548
Chris Lattnerce872152006-03-19 06:31:19 +00004549/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004550/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004551SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4552
4553 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004554 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004555 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004556 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004557 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004558 std::map<SDOperand, std::vector<unsigned> > Values;
4559 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004560 bool isConstant = true;
4561 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4562 SplatValue.getOpcode() != ISD::UNDEF)
4563 isConstant = false;
4564
Evan Cheng033e6812006-03-24 01:17:21 +00004565 for (unsigned i = 1; i < NumElems; ++i) {
4566 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004567 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004568 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004569 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004570 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004571 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004572
4573 // If this isn't a constant element or an undef, we can't use a constant
4574 // pool load.
4575 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4576 V.getOpcode() != ISD::UNDEF)
4577 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004578 }
4579
4580 if (isOnlyLowElement) {
4581 // If the low element is an undef too, then this whole things is an undef.
4582 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4583 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4584 // Otherwise, turn this into a scalar_to_vector node.
4585 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4586 Node->getOperand(0));
4587 }
4588
Chris Lattner2eb86532006-03-24 07:29:17 +00004589 // If all elements are constants, create a load from the constant pool.
4590 if (isConstant) {
4591 MVT::ValueType VT = Node->getValueType(0);
4592 const Type *OpNTy =
4593 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4594 std::vector<Constant*> CV;
4595 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4596 if (ConstantFPSDNode *V =
4597 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004598 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004599 } else if (ConstantSDNode *V =
4600 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004601 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004602 } else {
4603 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4604 CV.push_back(UndefValue::get(OpNTy));
4605 }
4606 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004607 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004608 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004609 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004610 }
4611
Chris Lattner87100e02006-03-20 01:52:29 +00004612 if (SplatValue.Val) { // Splat of one value?
4613 // Build the shuffle constant vector: <0, 0, 0, 0>
4614 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004615 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004616 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004617 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004618 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4619 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004620
4621 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004622 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004623 // Get the splatted value into the low element of a vector register.
4624 SDOperand LowValVec =
4625 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4626
4627 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4628 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4629 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4630 SplatMask);
4631 }
4632 }
4633
Evan Cheng033e6812006-03-24 01:17:21 +00004634 // If there are only two unique elements, we may be able to turn this into a
4635 // vector shuffle.
4636 if (Values.size() == 2) {
4637 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4638 MVT::ValueType MaskVT =
4639 MVT::getIntVectorWithNumElements(NumElems);
4640 std::vector<SDOperand> MaskVec(NumElems);
4641 unsigned i = 0;
4642 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4643 E = Values.end(); I != E; ++I) {
4644 for (std::vector<unsigned>::iterator II = I->second.begin(),
4645 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004646 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004647 i += NumElems;
4648 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004649 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4650 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004651
4652 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004653 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4654 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004655 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004656 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4657 E = Values.end(); I != E; ++I) {
4658 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4659 I->first);
4660 Ops.push_back(Op);
4661 }
4662 Ops.push_back(ShuffleMask);
4663
4664 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004665 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4666 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004667 }
4668 }
Chris Lattnerce872152006-03-19 06:31:19 +00004669
4670 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4671 // aligned object on the stack, store each element into it, then load
4672 // the result as a vector.
4673 MVT::ValueType VT = Node->getValueType(0);
4674 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004675 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004676
4677 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004678 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004679 unsigned TypeByteSize =
4680 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004681 // Store (in the right endianness) the elements to memory.
4682 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4683 // Ignore undef elements.
4684 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4685
Chris Lattner841c8822006-03-22 01:46:54 +00004686 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004687
4688 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4689 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4690
Evan Cheng786225a2006-10-05 23:01:46 +00004691 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004692 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004693 }
4694
4695 SDOperand StoreChain;
4696 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004697 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4698 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004699 else
4700 StoreChain = DAG.getEntryNode();
4701
4702 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004703 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004704}
4705
Chris Lattner5b359c62005-04-02 04:00:59 +00004706void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4707 SDOperand Op, SDOperand Amt,
4708 SDOperand &Lo, SDOperand &Hi) {
4709 // Expand the subcomponents.
4710 SDOperand LHSL, LHSH;
4711 ExpandOp(Op, LHSL, LHSH);
4712
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004713 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004714 MVT::ValueType VT = LHSL.getValueType();
4715 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004716 Hi = Lo.getValue(1);
4717}
4718
4719
Chris Lattnere34b3962005-01-19 04:19:40 +00004720/// ExpandShift - Try to find a clever way to expand this shift operation out to
4721/// smaller elements. If we can't find a way that is more efficient than a
4722/// libcall on this target, return false. Otherwise, return true with the
4723/// low-parts expanded into Lo and Hi.
4724bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4725 SDOperand &Lo, SDOperand &Hi) {
4726 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4727 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004728
Chris Lattnere34b3962005-01-19 04:19:40 +00004729 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004730 SDOperand ShAmt = LegalizeOp(Amt);
4731 MVT::ValueType ShTy = ShAmt.getValueType();
4732 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4733 unsigned NVTBits = MVT::getSizeInBits(NVT);
4734
Chris Lattner7a3c8552007-10-14 20:35:12 +00004735 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004736 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4737 unsigned Cst = CN->getValue();
4738 // Expand the incoming operand to be shifted, so that we have its parts
4739 SDOperand InL, InH;
4740 ExpandOp(Op, InL, InH);
4741 switch(Opc) {
4742 case ISD::SHL:
4743 if (Cst > VTBits) {
4744 Lo = DAG.getConstant(0, NVT);
4745 Hi = DAG.getConstant(0, NVT);
4746 } else if (Cst > NVTBits) {
4747 Lo = DAG.getConstant(0, NVT);
4748 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004749 } else if (Cst == NVTBits) {
4750 Lo = DAG.getConstant(0, NVT);
4751 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004752 } else {
4753 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4754 Hi = DAG.getNode(ISD::OR, NVT,
4755 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4756 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4757 }
4758 return true;
4759 case ISD::SRL:
4760 if (Cst > VTBits) {
4761 Lo = DAG.getConstant(0, NVT);
4762 Hi = DAG.getConstant(0, NVT);
4763 } else if (Cst > NVTBits) {
4764 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4765 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004766 } else if (Cst == NVTBits) {
4767 Lo = InH;
4768 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004769 } else {
4770 Lo = DAG.getNode(ISD::OR, NVT,
4771 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4772 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4773 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4774 }
4775 return true;
4776 case ISD::SRA:
4777 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004778 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004779 DAG.getConstant(NVTBits-1, ShTy));
4780 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004781 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004782 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004783 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004784 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004785 } else if (Cst == NVTBits) {
4786 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004787 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004788 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004789 } else {
4790 Lo = DAG.getNode(ISD::OR, NVT,
4791 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4792 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4793 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4794 }
4795 return true;
4796 }
4797 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004798
4799 // Okay, the shift amount isn't constant. However, if we can tell that it is
4800 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4801 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004802 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004803
4804 // If we know that the high bit of the shift amount is one, then we can do
4805 // this as a couple of simple shifts.
4806 if (KnownOne & Mask) {
4807 // Mask out the high bit, which we know is set.
4808 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4809 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4810
4811 // Expand the incoming operand to be shifted, so that we have its parts
4812 SDOperand InL, InH;
4813 ExpandOp(Op, InL, InH);
4814 switch(Opc) {
4815 case ISD::SHL:
4816 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4817 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4818 return true;
4819 case ISD::SRL:
4820 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4821 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4822 return true;
4823 case ISD::SRA:
4824 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4825 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4826 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4827 return true;
4828 }
4829 }
4830
4831 // If we know that the high bit of the shift amount is zero, then we can do
4832 // this as a couple of simple shifts.
4833 if (KnownZero & Mask) {
4834 // Compute 32-amt.
4835 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4836 DAG.getConstant(NVTBits, Amt.getValueType()),
4837 Amt);
4838
4839 // Expand the incoming operand to be shifted, so that we have its parts
4840 SDOperand InL, InH;
4841 ExpandOp(Op, InL, InH);
4842 switch(Opc) {
4843 case ISD::SHL:
4844 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4845 Hi = DAG.getNode(ISD::OR, NVT,
4846 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4847 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4848 return true;
4849 case ISD::SRL:
4850 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4851 Lo = DAG.getNode(ISD::OR, NVT,
4852 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4853 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4854 return true;
4855 case ISD::SRA:
4856 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4857 Lo = DAG.getNode(ISD::OR, NVT,
4858 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4859 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4860 return true;
4861 }
4862 }
4863
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004864 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004865}
Chris Lattner77e77a62005-01-21 06:05:23 +00004866
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004867
Chris Lattner77e77a62005-01-21 06:05:23 +00004868// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4869// does not fit into a register, return the lo part and set the hi part to the
4870// by-reg argument. If it does fit into a single register, return the result
4871// and leave the Hi part unset.
4872SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004873 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004874 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4875 // The input chain to this libcall is the entry node of the function.
4876 // Legalizing the call will automatically add the previous call to the
4877 // dependence.
4878 SDOperand InChain = DAG.getEntryNode();
4879
Chris Lattner77e77a62005-01-21 06:05:23 +00004880 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004881 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004882 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4883 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4884 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004885 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004886 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004887 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004888 }
4889 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004890
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004891 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004892 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004893 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004894 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004895 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004896
Chris Lattner6831a812006-02-13 09:18:02 +00004897 // Legalize the call sequence, starting with the chain. This will advance
4898 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4899 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4900 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004901 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004902 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004903 default: assert(0 && "Unknown thing");
4904 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004905 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004906 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004907 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004908 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004909 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004910 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004911 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004912}
4913
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004914
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004915/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4916///
Chris Lattner77e77a62005-01-21 06:05:23 +00004917SDOperand SelectionDAGLegalize::
4918ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004919 assert(getTypeAction(Source.getValueType()) == Expand &&
4920 "This is not an expansion!");
4921 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4922
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004923 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004924 assert(Source.getValueType() == MVT::i64 &&
4925 "This only works for 64-bit -> FP");
4926 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4927 // incoming integer is set. To handle this, we dynamically test to see if
4928 // it is set, and, if so, add a fudge factor.
4929 SDOperand Lo, Hi;
4930 ExpandOp(Source, Lo, Hi);
4931
Chris Lattner66de05b2005-05-13 04:45:13 +00004932 // If this is unsigned, and not supported, first perform the conversion to
4933 // signed, then adjust the result if the sign bit is set.
4934 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4935 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4936
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004937 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4938 DAG.getConstant(0, Hi.getValueType()),
4939 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004940 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4941 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4942 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004943 uint64_t FF = 0x5f800000ULL;
4944 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004945 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004946
Chris Lattner5839bf22005-08-26 17:15:30 +00004947 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004948 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4949 SDOperand FudgeInReg;
4950 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004951 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004952 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004953 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00004954 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004955 CPIdx, NULL, 0, MVT::f32);
4956 else
4957 assert(0 && "Unexpected conversion");
4958
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004959 MVT::ValueType SCVT = SignedConv.getValueType();
4960 if (SCVT != DestTy) {
4961 // Destination type needs to be expanded as well. The FADD now we are
4962 // constructing will be expanded into a libcall.
4963 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4964 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4965 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4966 SignedConv, SignedConv.getValue(1));
4967 }
4968 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4969 }
Chris Lattner473a9902005-09-29 06:44:39 +00004970 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004971 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004972
Chris Lattnera88a2602005-05-14 05:33:54 +00004973 // Check to see if the target has a custom way to lower this. If so, use it.
4974 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4975 default: assert(0 && "This action not implemented for this operation!");
4976 case TargetLowering::Legal:
4977 case TargetLowering::Expand:
4978 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004979 case TargetLowering::Custom: {
4980 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4981 Source), DAG);
4982 if (NV.Val)
4983 return LegalizeOp(NV);
4984 break; // The target decided this was legal after all
4985 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004986 }
4987
Chris Lattner13689e22005-05-12 07:00:44 +00004988 // Expand the source, then glue it back together for the call. We must expand
4989 // the source in case it is shared (this pass of legalize must traverse it).
4990 SDOperand SrcLo, SrcHi;
4991 ExpandOp(Source, SrcLo, SrcHi);
4992 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4993
Evan Cheng56966222007-01-12 02:11:51 +00004994 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004995 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004996 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004997 else {
4998 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004999 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005000 }
Chris Lattner6831a812006-02-13 09:18:02 +00005001
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005002 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005003 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5004 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00005005 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5006 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00005007}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005008
Chris Lattner22cde6a2006-01-28 08:25:58 +00005009/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5010/// INT_TO_FP operation of the specified operand when the target requests that
5011/// we expand it. At this point, we know that the result and operand types are
5012/// legal for the target.
5013SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5014 SDOperand Op0,
5015 MVT::ValueType DestVT) {
5016 if (Op0.getValueType() == MVT::i32) {
5017 // simple 32-bit [signed|unsigned] integer to float/double expansion
5018
Chris Lattner58092e32007-01-20 22:35:55 +00005019 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00005020 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00005021 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
5022 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00005023 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00005024 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005025 // get address of 8 byte buffer
5026 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
5027 // word offset constant for Hi/Lo address computation
5028 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5029 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005030 SDOperand Hi = StackSlot;
5031 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5032 if (TLI.isLittleEndian())
5033 std::swap(Hi, Lo);
5034
Chris Lattner22cde6a2006-01-28 08:25:58 +00005035 // if signed map to unsigned space
5036 SDOperand Op0Mapped;
5037 if (isSigned) {
5038 // constant used to invert sign bit (signed to unsigned mapping)
5039 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5040 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5041 } else {
5042 Op0Mapped = Op0;
5043 }
5044 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005045 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005046 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005047 // initial hi portion of constructed double
5048 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5049 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005050 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005051 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005052 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005053 // FP constant to bias correct the final result
5054 SDOperand Bias = DAG.getConstantFP(isSigned ?
5055 BitsToDouble(0x4330000080000000ULL)
5056 : BitsToDouble(0x4330000000000000ULL),
5057 MVT::f64);
5058 // subtract the bias
5059 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5060 // final result
5061 SDOperand Result;
5062 // handle final rounding
5063 if (DestVT == MVT::f64) {
5064 // do nothing
5065 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005066 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5067 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5068 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5069 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005070 }
5071 return Result;
5072 }
5073 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5074 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5075
5076 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5077 DAG.getConstant(0, Op0.getValueType()),
5078 ISD::SETLT);
5079 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5080 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5081 SignSet, Four, Zero);
5082
5083 // If the sign bit of the integer is set, the large number will be treated
5084 // as a negative number. To counteract this, the dynamic code adds an
5085 // offset depending on the data type.
5086 uint64_t FF;
5087 switch (Op0.getValueType()) {
5088 default: assert(0 && "Unsupported integer type!");
5089 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5090 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5091 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5092 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5093 }
5094 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005095 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005096
5097 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5098 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5099 SDOperand FudgeInReg;
5100 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005101 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005102 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005103 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005104 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00005105 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005106 }
5107
5108 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5109}
5110
5111/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5112/// *INT_TO_FP operation of the specified operand when the target requests that
5113/// we promote it. At this point, we know that the result and operand types are
5114/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5115/// operation that takes a larger input.
5116SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5117 MVT::ValueType DestVT,
5118 bool isSigned) {
5119 // First step, figure out the appropriate *INT_TO_FP operation to use.
5120 MVT::ValueType NewInTy = LegalOp.getValueType();
5121
5122 unsigned OpToUse = 0;
5123
5124 // Scan for the appropriate larger type to use.
5125 while (1) {
5126 NewInTy = (MVT::ValueType)(NewInTy+1);
5127 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5128
5129 // If the target supports SINT_TO_FP of this type, use it.
5130 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5131 default: break;
5132 case TargetLowering::Legal:
5133 if (!TLI.isTypeLegal(NewInTy))
5134 break; // Can't use this datatype.
5135 // FALL THROUGH.
5136 case TargetLowering::Custom:
5137 OpToUse = ISD::SINT_TO_FP;
5138 break;
5139 }
5140 if (OpToUse) break;
5141 if (isSigned) continue;
5142
5143 // If the target supports UINT_TO_FP of this type, use it.
5144 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5145 default: break;
5146 case TargetLowering::Legal:
5147 if (!TLI.isTypeLegal(NewInTy))
5148 break; // Can't use this datatype.
5149 // FALL THROUGH.
5150 case TargetLowering::Custom:
5151 OpToUse = ISD::UINT_TO_FP;
5152 break;
5153 }
5154 if (OpToUse) break;
5155
5156 // Otherwise, try a larger type.
5157 }
5158
5159 // Okay, we found the operation and type to use. Zero extend our input to the
5160 // desired type then run the operation on it.
5161 return DAG.getNode(OpToUse, DestVT,
5162 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5163 NewInTy, LegalOp));
5164}
5165
5166/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5167/// FP_TO_*INT operation of the specified operand when the target requests that
5168/// we promote it. At this point, we know that the result and operand types are
5169/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5170/// operation that returns a larger result.
5171SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5172 MVT::ValueType DestVT,
5173 bool isSigned) {
5174 // First step, figure out the appropriate FP_TO*INT operation to use.
5175 MVT::ValueType NewOutTy = DestVT;
5176
5177 unsigned OpToUse = 0;
5178
5179 // Scan for the appropriate larger type to use.
5180 while (1) {
5181 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5182 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5183
5184 // If the target supports FP_TO_SINT returning this type, use it.
5185 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5186 default: break;
5187 case TargetLowering::Legal:
5188 if (!TLI.isTypeLegal(NewOutTy))
5189 break; // Can't use this datatype.
5190 // FALL THROUGH.
5191 case TargetLowering::Custom:
5192 OpToUse = ISD::FP_TO_SINT;
5193 break;
5194 }
5195 if (OpToUse) break;
5196
5197 // If the target supports FP_TO_UINT of this type, use it.
5198 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5199 default: break;
5200 case TargetLowering::Legal:
5201 if (!TLI.isTypeLegal(NewOutTy))
5202 break; // Can't use this datatype.
5203 // FALL THROUGH.
5204 case TargetLowering::Custom:
5205 OpToUse = ISD::FP_TO_UINT;
5206 break;
5207 }
5208 if (OpToUse) break;
5209
5210 // Otherwise, try a larger type.
5211 }
5212
Chris Lattner27a6c732007-11-24 07:07:01 +00005213
5214 // Okay, we found the operation and type to use.
5215 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5216
5217 // If the operation produces an invalid type, it must be custom lowered. Use
5218 // the target lowering hooks to expand it. Just keep the low part of the
5219 // expanded operation, we know that we're truncating anyway.
5220 if (getTypeAction(NewOutTy) == Expand) {
5221 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5222 assert(Operation.Val && "Didn't return anything");
5223 }
5224
5225 // Truncate the result of the extended FP_TO_*INT operation to the desired
5226 // size.
5227 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005228}
5229
5230/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5231///
5232SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5233 MVT::ValueType VT = Op.getValueType();
5234 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5235 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5236 switch (VT) {
5237 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5238 case MVT::i16:
5239 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5240 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5241 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5242 case MVT::i32:
5243 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5244 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5245 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5246 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5247 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5248 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5249 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5250 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5251 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5252 case MVT::i64:
5253 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5254 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5255 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5256 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5257 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5258 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5259 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5260 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5261 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5262 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5263 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5264 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5265 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5266 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5267 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5268 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5269 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5270 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5271 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5272 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5273 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5274 }
5275}
5276
5277/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5278///
5279SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5280 switch (Opc) {
5281 default: assert(0 && "Cannot expand this yet!");
5282 case ISD::CTPOP: {
5283 static const uint64_t mask[6] = {
5284 0x5555555555555555ULL, 0x3333333333333333ULL,
5285 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5286 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5287 };
5288 MVT::ValueType VT = Op.getValueType();
5289 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005290 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005291 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5292 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5293 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5294 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5295 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5296 DAG.getNode(ISD::AND, VT,
5297 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5298 }
5299 return Op;
5300 }
5301 case ISD::CTLZ: {
5302 // for now, we do this:
5303 // x = x | (x >> 1);
5304 // x = x | (x >> 2);
5305 // ...
5306 // x = x | (x >>16);
5307 // x = x | (x >>32); // for 64-bit input
5308 // return popcount(~x);
5309 //
5310 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5311 MVT::ValueType VT = Op.getValueType();
5312 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005313 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005314 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5315 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5316 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5317 }
5318 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5319 return DAG.getNode(ISD::CTPOP, VT, Op);
5320 }
5321 case ISD::CTTZ: {
5322 // for now, we use: { return popcount(~x & (x - 1)); }
5323 // unless the target has ctlz but not ctpop, in which case we use:
5324 // { return 32 - nlz(~x & (x-1)); }
5325 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5326 MVT::ValueType VT = Op.getValueType();
5327 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5328 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5329 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5330 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5331 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5332 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5333 TLI.isOperationLegal(ISD::CTLZ, VT))
5334 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005335 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005336 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5337 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5338 }
5339 }
5340}
Chris Lattnere34b3962005-01-19 04:19:40 +00005341
Chris Lattner3e928bb2005-01-07 07:47:09 +00005342/// ExpandOp - Expand the specified SDOperand into its two component pieces
5343/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5344/// LegalizeNodes map is filled in for any results that are not expanded, the
5345/// ExpandedNodes map is filled in for any results that are expanded, and the
5346/// Lo/Hi values are returned.
5347void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5348 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005349 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005350 SDNode *Node = Op.Val;
5351 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005352 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005353 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005354 "Cannot expand to FP value or to larger int value!");
5355
Chris Lattner6fdcb252005-09-02 20:32:45 +00005356 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005357 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005358 = ExpandedNodes.find(Op);
5359 if (I != ExpandedNodes.end()) {
5360 Lo = I->second.first;
5361 Hi = I->second.second;
5362 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005363 }
5364
Chris Lattner3e928bb2005-01-07 07:47:09 +00005365 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005366 case ISD::CopyFromReg:
5367 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005368 case ISD::FP_ROUND_INREG:
5369 if (VT == MVT::ppcf128 &&
5370 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5371 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005372 SDOperand SrcLo, SrcHi, Src;
5373 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5374 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5375 SDOperand Result = TLI.LowerOperation(
5376 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005377 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5378 Lo = Result.Val->getOperand(0);
5379 Hi = Result.Val->getOperand(1);
5380 break;
5381 }
5382 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005383 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005384#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005385 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005386#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005387 assert(0 && "Do not know how to expand this operator!");
5388 abort();
Dale Johannesen25f1d082007-10-31 00:32:36 +00005389 case ISD::EXTRACT_VECTOR_ELT:
5390 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5391 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5392 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5393 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005394 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005395 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005396 Lo = DAG.getNode(ISD::UNDEF, NVT);
5397 Hi = DAG.getNode(ISD::UNDEF, NVT);
5398 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005399 case ISD::Constant: {
5400 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5401 Lo = DAG.getConstant(Cst, NVT);
5402 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5403 break;
5404 }
Evan Cheng00495212006-12-12 21:32:44 +00005405 case ISD::ConstantFP: {
5406 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005407 if (CFP->getValueType(0) == MVT::ppcf128) {
5408 APInt api = CFP->getValueAPF().convertToAPInt();
5409 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5410 MVT::f64);
5411 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5412 MVT::f64);
5413 break;
5414 }
Evan Cheng279101e2006-12-12 22:19:28 +00005415 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005416 if (getTypeAction(Lo.getValueType()) == Expand)
5417 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005418 break;
5419 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005420 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005421 // Return the operands.
5422 Lo = Node->getOperand(0);
5423 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005424 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005425
5426 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005427 if (Node->getNumValues() == 1) {
5428 ExpandOp(Op.getOperand(0), Lo, Hi);
5429 break;
5430 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005431 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5432 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5433 Op.getValue(1).getValueType() == MVT::Other &&
5434 "unhandled MERGE_VALUES");
5435 ExpandOp(Op.getOperand(0), Lo, Hi);
5436 // Remember that we legalized the chain.
5437 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5438 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005439
5440 case ISD::SIGN_EXTEND_INREG:
5441 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005442 // sext_inreg the low part if needed.
5443 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5444
5445 // The high part gets the sign extension from the lo-part. This handles
5446 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005447 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5448 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5449 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005450 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005451
Nate Begemand88fc032006-01-14 03:14:10 +00005452 case ISD::BSWAP: {
5453 ExpandOp(Node->getOperand(0), Lo, Hi);
5454 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5455 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5456 Lo = TempLo;
5457 break;
5458 }
5459
Chris Lattneredb1add2005-05-11 04:51:16 +00005460 case ISD::CTPOP:
5461 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005462 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5463 DAG.getNode(ISD::CTPOP, NVT, Lo),
5464 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005465 Hi = DAG.getConstant(0, NVT);
5466 break;
5467
Chris Lattner39a8f332005-05-12 19:05:01 +00005468 case ISD::CTLZ: {
5469 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005470 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005471 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5472 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005473 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5474 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005475 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5476 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5477
5478 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5479 Hi = DAG.getConstant(0, NVT);
5480 break;
5481 }
5482
5483 case ISD::CTTZ: {
5484 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005485 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005486 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5487 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005488 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5489 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005490 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5491 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5492
5493 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5494 Hi = DAG.getConstant(0, NVT);
5495 break;
5496 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005497
Nate Begemanacc398c2006-01-25 18:21:52 +00005498 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005499 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5500 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005501 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5502 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5503
5504 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005505 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005506 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5507 if (!TLI.isLittleEndian())
5508 std::swap(Lo, Hi);
5509 break;
5510 }
5511
Chris Lattner3e928bb2005-01-07 07:47:09 +00005512 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005513 LoadSDNode *LD = cast<LoadSDNode>(Node);
5514 SDOperand Ch = LD->getChain(); // Legalize the chain.
5515 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5516 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005517 int SVOffset = LD->getSrcValueOffset();
5518 unsigned Alignment = LD->getAlignment();
5519 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005520
Evan Cheng466685d2006-10-09 20:57:25 +00005521 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005522 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5523 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005524 if (VT == MVT::f32 || VT == MVT::f64) {
5525 // f32->i32 or f64->i64 one to one expansion.
5526 // Remember that we legalized the chain.
5527 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005528 // Recursively expand the new load.
5529 if (getTypeAction(NVT) == Expand)
5530 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005531 break;
5532 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005533
Evan Cheng466685d2006-10-09 20:57:25 +00005534 // Increment the pointer to the other half.
5535 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5536 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5537 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005538 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005539 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005540 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5541 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005542
Evan Cheng466685d2006-10-09 20:57:25 +00005543 // Build a factor node to remember that this load is independent of the
5544 // other one.
5545 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5546 Hi.getValue(1));
5547
5548 // Remember that we legalized the chain.
5549 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5550 if (!TLI.isLittleEndian())
5551 std::swap(Lo, Hi);
5552 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005553 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005554
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005555 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5556 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005557 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5558 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005559 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005560 // Remember that we legalized the chain.
5561 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5562 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5563 break;
5564 }
Evan Cheng466685d2006-10-09 20:57:25 +00005565
5566 if (EVT == NVT)
5567 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005568 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005569 else
5570 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005571 SVOffset, EVT, isVolatile,
5572 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005573
5574 // Remember that we legalized the chain.
5575 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5576
5577 if (ExtType == ISD::SEXTLOAD) {
5578 // The high part is obtained by SRA'ing all but one of the bits of the
5579 // lo part.
5580 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5581 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5582 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5583 } else if (ExtType == ISD::ZEXTLOAD) {
5584 // The high part is just a zero.
5585 Hi = DAG.getConstant(0, NVT);
5586 } else /* if (ExtType == ISD::EXTLOAD) */ {
5587 // The high part is undefined.
5588 Hi = DAG.getNode(ISD::UNDEF, NVT);
5589 }
5590 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005591 break;
5592 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005593 case ISD::AND:
5594 case ISD::OR:
5595 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5596 SDOperand LL, LH, RL, RH;
5597 ExpandOp(Node->getOperand(0), LL, LH);
5598 ExpandOp(Node->getOperand(1), RL, RH);
5599 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5600 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5601 break;
5602 }
5603 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005604 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005605 ExpandOp(Node->getOperand(1), LL, LH);
5606 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005607 if (getTypeAction(NVT) == Expand)
5608 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005609 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005610 if (VT != MVT::f32)
5611 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005612 break;
5613 }
Nate Begeman9373a812005-08-10 20:51:12 +00005614 case ISD::SELECT_CC: {
5615 SDOperand TL, TH, FL, FH;
5616 ExpandOp(Node->getOperand(2), TL, TH);
5617 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005618 if (getTypeAction(NVT) == Expand)
5619 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005620 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5621 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005622 if (VT != MVT::f32)
5623 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5624 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005625 break;
5626 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005627 case ISD::ANY_EXTEND:
5628 // The low part is any extension of the input (which degenerates to a copy).
5629 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5630 // The high part is undefined.
5631 Hi = DAG.getNode(ISD::UNDEF, NVT);
5632 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005633 case ISD::SIGN_EXTEND: {
5634 // The low part is just a sign extension of the input (which degenerates to
5635 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005636 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005637
Chris Lattner3e928bb2005-01-07 07:47:09 +00005638 // The high part is obtained by SRA'ing all but one of the bits of the lo
5639 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005640 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005641 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5642 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005643 break;
5644 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005645 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005646 // The low part is just a zero extension of the input (which degenerates to
5647 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005648 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005649
Chris Lattner3e928bb2005-01-07 07:47:09 +00005650 // The high part is just a zero.
5651 Hi = DAG.getConstant(0, NVT);
5652 break;
Chris Lattner35481892005-12-23 00:16:34 +00005653
Chris Lattner4c948eb2007-02-13 23:55:16 +00005654 case ISD::TRUNCATE: {
5655 // The input value must be larger than this value. Expand *it*.
5656 SDOperand NewLo;
5657 ExpandOp(Node->getOperand(0), NewLo, Hi);
5658
5659 // The low part is now either the right size, or it is closer. If not the
5660 // right size, make an illegal truncate so we recursively expand it.
5661 if (NewLo.getValueType() != Node->getValueType(0))
5662 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5663 ExpandOp(NewLo, Lo, Hi);
5664 break;
5665 }
5666
Chris Lattner35481892005-12-23 00:16:34 +00005667 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005668 SDOperand Tmp;
5669 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5670 // If the target wants to, allow it to lower this itself.
5671 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5672 case Expand: assert(0 && "cannot expand FP!");
5673 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5674 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5675 }
5676 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5677 }
5678
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005679 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005680 if (VT == MVT::f32 || VT == MVT::f64) {
5681 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005682 if (getTypeAction(NVT) == Expand)
5683 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005684 break;
5685 }
5686
5687 // If source operand will be expanded to the same type as VT, i.e.
5688 // i64 <- f64, i32 <- f32, expand the source operand instead.
5689 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5690 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5691 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005692 break;
5693 }
5694
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005695 // Turn this into a load/store pair by default.
5696 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005697 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005698
Chris Lattner35481892005-12-23 00:16:34 +00005699 ExpandOp(Tmp, Lo, Hi);
5700 break;
5701 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005702
Chris Lattner27a6c732007-11-24 07:07:01 +00005703 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00005704 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5705 TargetLowering::Custom &&
5706 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00005707 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5708 assert(Tmp.Val && "Node must be custom expanded!");
5709 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00005710 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00005711 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005712 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005713 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005714
Chris Lattner4e6c7462005-01-08 19:27:05 +00005715 // These operators cannot be expanded directly, emit them as calls to
5716 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005717 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005718 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005719 SDOperand Op;
5720 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5721 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005722 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5723 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005724 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005725
Chris Lattnerf20d1832005-07-30 01:40:57 +00005726 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5727
Chris Lattner80a3e942005-07-29 00:33:32 +00005728 // Now that the custom expander is done, expand the result, which is still
5729 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005730 if (Op.Val) {
5731 ExpandOp(Op, Lo, Hi);
5732 break;
5733 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005734 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005735
Dale Johannesen161e8972007-10-05 20:04:43 +00005736 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005737 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005738 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005739 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005740 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005741 else if (Node->getOperand(0).getValueType() == MVT::f80)
5742 LC = RTLIB::FPTOSINT_F80_I64;
5743 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5744 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005745 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5746 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005747 break;
Evan Cheng56966222007-01-12 02:11:51 +00005748 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005749
Evan Cheng56966222007-01-12 02:11:51 +00005750 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005751 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005752 SDOperand Op;
5753 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5754 case Expand: assert(0 && "cannot expand FP!");
5755 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5756 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5757 }
5758
5759 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5760
5761 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005762 if (Op.Val) {
5763 ExpandOp(Op, Lo, Hi);
5764 break;
5765 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005766 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005767
Evan Chengdaccea182007-10-05 01:09:32 +00005768 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005769 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005770 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005771 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005772 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005773 else if (Node->getOperand(0).getValueType() == MVT::f80)
5774 LC = RTLIB::FPTOUINT_F80_I64;
5775 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5776 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005777 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5778 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005779 break;
Evan Cheng56966222007-01-12 02:11:51 +00005780 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005781
Evan Cheng05a2d562006-01-09 18:31:59 +00005782 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005783 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005784 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005785 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005786 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005787 Op = TLI.LowerOperation(Op, DAG);
5788 if (Op.Val) {
5789 // Now that the custom expander is done, expand the result, which is
5790 // still VT.
5791 ExpandOp(Op, Lo, Hi);
5792 break;
5793 }
5794 }
5795
Chris Lattner79980b02006-09-13 03:50:39 +00005796 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5797 // this X << 1 as X+X.
5798 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5799 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5800 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5801 SDOperand LoOps[2], HiOps[3];
5802 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5803 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5804 LoOps[1] = LoOps[0];
5805 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5806
5807 HiOps[1] = HiOps[0];
5808 HiOps[2] = Lo.getValue(1);
5809 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5810 break;
5811 }
5812 }
5813
Chris Lattnere34b3962005-01-19 04:19:40 +00005814 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005815 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005816 break;
Chris Lattner47599822005-04-02 03:38:53 +00005817
5818 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005819 TargetLowering::LegalizeAction Action =
5820 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5821 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5822 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005823 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005824 break;
5825 }
5826
Chris Lattnere34b3962005-01-19 04:19:40 +00005827 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005828 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5829 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005830 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005831 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005832
Evan Cheng05a2d562006-01-09 18:31:59 +00005833 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005834 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005835 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005836 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005837 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005838 Op = TLI.LowerOperation(Op, DAG);
5839 if (Op.Val) {
5840 // Now that the custom expander is done, expand the result, which is
5841 // still VT.
5842 ExpandOp(Op, Lo, Hi);
5843 break;
5844 }
5845 }
5846
Chris Lattnere34b3962005-01-19 04:19:40 +00005847 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005848 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005849 break;
Chris Lattner47599822005-04-02 03:38:53 +00005850
5851 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005852 TargetLowering::LegalizeAction Action =
5853 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5854 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5855 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005856 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005857 break;
5858 }
5859
Chris Lattnere34b3962005-01-19 04:19:40 +00005860 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005861 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5862 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005863 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005864 }
5865
5866 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005867 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005868 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005869 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005870 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005871 Op = TLI.LowerOperation(Op, DAG);
5872 if (Op.Val) {
5873 // Now that the custom expander is done, expand the result, which is
5874 // still VT.
5875 ExpandOp(Op, Lo, Hi);
5876 break;
5877 }
5878 }
5879
Chris Lattnere34b3962005-01-19 04:19:40 +00005880 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005881 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005882 break;
Chris Lattner47599822005-04-02 03:38:53 +00005883
5884 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005885 TargetLowering::LegalizeAction Action =
5886 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5887 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5888 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005889 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005890 break;
5891 }
5892
Chris Lattnere34b3962005-01-19 04:19:40 +00005893 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005894 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5895 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005896 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005897 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005898
Misha Brukmanedf128a2005-04-21 22:36:52 +00005899 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005900 case ISD::SUB: {
5901 // If the target wants to custom expand this, let them.
5902 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5903 TargetLowering::Custom) {
5904 Op = TLI.LowerOperation(Op, DAG);
5905 if (Op.Val) {
5906 ExpandOp(Op, Lo, Hi);
5907 break;
5908 }
5909 }
5910
5911 // Expand the subcomponents.
5912 SDOperand LHSL, LHSH, RHSL, RHSH;
5913 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5914 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005915 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5916 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005917 LoOps[0] = LHSL;
5918 LoOps[1] = RHSL;
5919 HiOps[0] = LHSH;
5920 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005921 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005922 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005923 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005924 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005925 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005926 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005927 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005928 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005929 }
Chris Lattner84f67882005-01-20 18:52:28 +00005930 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005931 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005932
5933 case ISD::ADDC:
5934 case ISD::SUBC: {
5935 // Expand the subcomponents.
5936 SDOperand LHSL, LHSH, RHSL, RHSH;
5937 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5938 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5939 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5940 SDOperand LoOps[2] = { LHSL, RHSL };
5941 SDOperand HiOps[3] = { LHSH, RHSH };
5942
5943 if (Node->getOpcode() == ISD::ADDC) {
5944 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5945 HiOps[2] = Lo.getValue(1);
5946 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5947 } else {
5948 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5949 HiOps[2] = Lo.getValue(1);
5950 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5951 }
5952 // Remember that we legalized the flag.
5953 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5954 break;
5955 }
5956 case ISD::ADDE:
5957 case ISD::SUBE: {
5958 // Expand the subcomponents.
5959 SDOperand LHSL, LHSH, RHSL, RHSH;
5960 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5961 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5962 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5963 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5964 SDOperand HiOps[3] = { LHSH, RHSH };
5965
5966 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5967 HiOps[2] = Lo.getValue(1);
5968 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5969
5970 // Remember that we legalized the flag.
5971 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5972 break;
5973 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005974 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005975 // If the target wants to custom expand this, let them.
5976 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005977 SDOperand New = TLI.LowerOperation(Op, DAG);
5978 if (New.Val) {
5979 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005980 break;
5981 }
5982 }
5983
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005984 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5985 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00005986 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5987 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5988 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005989 SDOperand LL, LH, RL, RH;
5990 ExpandOp(Node->getOperand(0), LL, LH);
5991 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00005992 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5993 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5994 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5995 // FIXME: generalize this to handle other bit sizes
5996 if (LHSSB == 32 && RHSSB == 32 &&
5997 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5998 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5999 // The inputs are both zero-extended.
6000 if (HasUMUL_LOHI) {
6001 // We can emit a umul_lohi.
6002 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6003 Hi = SDOperand(Lo.Val, 1);
6004 break;
6005 }
6006 if (HasMULHU) {
6007 // We can emit a mulhu+mul.
6008 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6009 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6010 break;
6011 }
Dan Gohman525178c2007-10-08 18:33:35 +00006012 }
6013 if (LHSSB > BitSize && RHSSB > BitSize) {
6014 // The input values are both sign-extended.
6015 if (HasSMUL_LOHI) {
6016 // We can emit a smul_lohi.
6017 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6018 Hi = SDOperand(Lo.Val, 1);
6019 break;
6020 }
6021 if (HasMULHS) {
6022 // We can emit a mulhs+mul.
6023 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6024 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6025 break;
6026 }
6027 }
6028 if (HasUMUL_LOHI) {
6029 // Lo,Hi = umul LHS, RHS.
6030 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6031 DAG.getVTList(NVT, NVT), LL, RL);
6032 Lo = UMulLOHI;
6033 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006034 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6035 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6036 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6037 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006038 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006039 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006040 if (HasMULHU) {
6041 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6042 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6043 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6044 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6045 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6046 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6047 break;
6048 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006049 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006050
Dan Gohman525178c2007-10-08 18:33:35 +00006051 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006052 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6053 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006054 break;
6055 }
Evan Cheng56966222007-01-12 02:11:51 +00006056 case ISD::SDIV:
6057 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6058 break;
6059 case ISD::UDIV:
6060 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6061 break;
6062 case ISD::SREM:
6063 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6064 break;
6065 case ISD::UREM:
6066 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6067 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006068
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006069 case ISD::FADD:
Dale Johannesen161e8972007-10-05 20:04:43 +00006070 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
6071 VT == MVT::f64 ? RTLIB::ADD_F64 :
6072 VT == MVT::ppcf128 ?
6073 RTLIB::ADD_PPCF128 :
6074 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006075 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006076 break;
6077 case ISD::FSUB:
Dale Johannesen161e8972007-10-05 20:04:43 +00006078 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
6079 VT == MVT::f64 ? RTLIB::SUB_F64 :
6080 VT == MVT::ppcf128 ?
6081 RTLIB::SUB_PPCF128 :
6082 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006083 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006084 break;
6085 case ISD::FMUL:
Dale Johannesen161e8972007-10-05 20:04:43 +00006086 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
6087 VT == MVT::f64 ? RTLIB::MUL_F64 :
6088 VT == MVT::ppcf128 ?
6089 RTLIB::MUL_PPCF128 :
6090 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006091 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006092 break;
6093 case ISD::FDIV:
Dale Johannesen161e8972007-10-05 20:04:43 +00006094 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
6095 VT == MVT::f64 ? RTLIB::DIV_F64 :
6096 VT == MVT::ppcf128 ?
6097 RTLIB::DIV_PPCF128 :
6098 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng56966222007-01-12 02:11:51 +00006099 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006100 break;
6101 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006102 if (VT == MVT::ppcf128) {
6103 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6104 Node->getOperand(0).getValueType()==MVT::f64);
6105 const uint64_t zero = 0;
6106 if (Node->getOperand(0).getValueType()==MVT::f32)
6107 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6108 else
6109 Hi = Node->getOperand(0);
6110 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6111 break;
6112 }
Evan Cheng56966222007-01-12 02:11:51 +00006113 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006114 break;
6115 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006116 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006117 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006118 case ISD::FPOWI:
Dale Johannesen317096a2007-09-28 01:08:20 +00006119 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
6120 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesen161e8972007-10-05 20:04:43 +00006121 (VT == MVT::f80) ? RTLIB::POWI_F80 :
6122 (VT == MVT::ppcf128) ?
6123 RTLIB::POWI_PPCF128 :
6124 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006125 Node, false, Hi);
6126 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006127 case ISD::FSQRT:
6128 case ISD::FSIN:
6129 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006130 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006131 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006132 case ISD::FSQRT:
Dale Johannesen317096a2007-09-28 01:08:20 +00006133 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesen161e8972007-10-05 20:04:43 +00006134 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
6135 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
6136 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
6137 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng56966222007-01-12 02:11:51 +00006138 break;
6139 case ISD::FSIN:
6140 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6141 break;
6142 case ISD::FCOS:
6143 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6144 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006145 default: assert(0 && "Unreachable!");
6146 }
Evan Cheng56966222007-01-12 02:11:51 +00006147 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006148 break;
6149 }
Evan Cheng966bf242006-12-16 00:52:40 +00006150 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006151 if (VT == MVT::ppcf128) {
6152 SDOperand Tmp;
6153 ExpandOp(Node->getOperand(0), Lo, Tmp);
6154 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6155 // lo = hi==fabs(hi) ? lo : -lo;
6156 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6157 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6158 DAG.getCondCode(ISD::SETEQ));
6159 break;
6160 }
Evan Cheng966bf242006-12-16 00:52:40 +00006161 SDOperand Mask = (VT == MVT::f64)
6162 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6163 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6164 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6165 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6166 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6167 if (getTypeAction(NVT) == Expand)
6168 ExpandOp(Lo, Lo, Hi);
6169 break;
6170 }
6171 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006172 if (VT == MVT::ppcf128) {
6173 ExpandOp(Node->getOperand(0), Lo, Hi);
6174 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6175 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6176 break;
6177 }
Evan Cheng966bf242006-12-16 00:52:40 +00006178 SDOperand Mask = (VT == MVT::f64)
6179 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6180 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6181 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6182 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6183 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6184 if (getTypeAction(NVT) == Expand)
6185 ExpandOp(Lo, Lo, Hi);
6186 break;
6187 }
Evan Cheng912095b2007-01-04 21:56:39 +00006188 case ISD::FCOPYSIGN: {
6189 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6190 if (getTypeAction(NVT) == Expand)
6191 ExpandOp(Lo, Lo, Hi);
6192 break;
6193 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006194 case ISD::SINT_TO_FP:
6195 case ISD::UINT_TO_FP: {
6196 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6197 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006198 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006199 static uint64_t zero = 0;
6200 if (isSigned) {
6201 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6202 Node->getOperand(0)));
6203 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6204 } else {
6205 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6206 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6207 Node->getOperand(0)));
6208 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6209 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006210 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006211 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6212 DAG.getConstant(0, MVT::i32),
6213 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6214 DAG.getConstantFP(
6215 APFloat(APInt(128, 2, TwoE32)),
6216 MVT::ppcf128)),
6217 Hi,
6218 DAG.getCondCode(ISD::SETLT)),
6219 Lo, Hi);
6220 }
6221 break;
6222 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006223 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6224 // si64->ppcf128 done by libcall, below
6225 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6226 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6227 Lo, Hi);
6228 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6229 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6230 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6231 DAG.getConstant(0, MVT::i64),
6232 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6233 DAG.getConstantFP(
6234 APFloat(APInt(128, 2, TwoE64)),
6235 MVT::ppcf128)),
6236 Hi,
6237 DAG.getCondCode(ISD::SETLT)),
6238 Lo, Hi);
6239 break;
6240 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006241 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006242 if (Node->getOperand(0).getValueType() == MVT::i64) {
6243 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006244 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006245 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006246 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006247 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006248 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006249 LC = RTLIB::SINTTOFP_I64_F80;
6250 }
6251 else if (VT == MVT::ppcf128) {
6252 assert(isSigned);
6253 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006254 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006255 } else {
6256 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006257 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006258 else
Evan Cheng56966222007-01-12 02:11:51 +00006259 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006260 }
6261
6262 // Promote the operand if needed.
6263 if (getTypeAction(SrcVT) == Promote) {
6264 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6265 Tmp = isSigned
6266 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6267 DAG.getValueType(SrcVT))
6268 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6269 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6270 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006271
6272 const char *LibCall = TLI.getLibcallName(LC);
6273 if (LibCall)
6274 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6275 else {
6276 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6277 Node->getOperand(0));
6278 if (getTypeAction(Lo.getValueType()) == Expand)
6279 ExpandOp(Lo, Lo, Hi);
6280 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006281 break;
6282 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006283 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006284
Chris Lattner83397362005-12-21 18:02:52 +00006285 // Make sure the resultant values have been legalized themselves, unless this
6286 // is a type that requires multi-step expansion.
6287 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6288 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006289 if (Hi.Val)
6290 // Don't legalize the high part if it is expanded to a single node.
6291 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006292 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006293
6294 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006295 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006296 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006297}
6298
Dan Gohman7f321562007-06-25 16:23:39 +00006299/// SplitVectorOp - Given an operand of vector type, break it down into
6300/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006301void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6302 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006303 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006304 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006305 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006306 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006307
Dan Gohmane40c7b02007-09-24 15:54:53 +00006308 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006309
6310 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6311 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6312
6313 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6314 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6315
Chris Lattnerc7029802006-03-18 01:44:44 +00006316 // See if we already split it.
6317 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6318 = SplitNodes.find(Op);
6319 if (I != SplitNodes.end()) {
6320 Lo = I->second.first;
6321 Hi = I->second.second;
6322 return;
6323 }
6324
6325 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006326 default:
6327#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006328 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006329#endif
6330 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006331 case ISD::UNDEF:
6332 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6333 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6334 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006335 case ISD::BUILD_PAIR:
6336 Lo = Node->getOperand(0);
6337 Hi = Node->getOperand(1);
6338 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006339 case ISD::INSERT_VECTOR_ELT: {
6340 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6341 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6342 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006343 if (Index < NewNumElts_Lo)
6344 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006345 DAG.getConstant(Index, TLI.getPointerTy()));
6346 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006347 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6348 DAG.getConstant(Index - NewNumElts_Lo,
6349 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006350 break;
6351 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006352 case ISD::VECTOR_SHUFFLE: {
6353 // Build the low part.
6354 SDOperand Mask = Node->getOperand(2);
6355 SmallVector<SDOperand, 8> Ops;
6356 MVT::ValueType PtrVT = TLI.getPointerTy();
6357
6358 // Insert all of the elements from the input that are needed. We use
6359 // buildvector of extractelement here because the input vectors will have
6360 // to be legalized, so this makes the code simpler.
6361 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6362 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6363 SDOperand InVec = Node->getOperand(0);
6364 if (Idx >= NumElements) {
6365 InVec = Node->getOperand(1);
6366 Idx -= NumElements;
6367 }
6368 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6369 DAG.getConstant(Idx, PtrVT)));
6370 }
6371 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6372 Ops.clear();
6373
6374 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6375 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6376 SDOperand InVec = Node->getOperand(0);
6377 if (Idx >= NumElements) {
6378 InVec = Node->getOperand(1);
6379 Idx -= NumElements;
6380 }
6381 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6382 DAG.getConstant(Idx, PtrVT)));
6383 }
6384 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6385 break;
6386 }
Dan Gohman7f321562007-06-25 16:23:39 +00006387 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006388 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006389 Node->op_begin()+NewNumElts_Lo);
6390 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006391
Nate Begeman5db1afb2007-11-15 21:15:26 +00006392 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006393 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006394 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006395 break;
6396 }
Dan Gohman7f321562007-06-25 16:23:39 +00006397 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006398 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006399 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6400 if (NewNumSubvectors == 1) {
6401 Lo = Node->getOperand(0);
6402 Hi = Node->getOperand(1);
6403 } else {
6404 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6405 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006406 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006407
Dan Gohman7f321562007-06-25 16:23:39 +00006408 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6409 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006410 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006411 }
Dan Gohman65956352007-06-13 15:12:02 +00006412 break;
6413 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006414 case ISD::SELECT: {
6415 SDOperand Cond = Node->getOperand(0);
6416
6417 SDOperand LL, LH, RL, RH;
6418 SplitVectorOp(Node->getOperand(1), LL, LH);
6419 SplitVectorOp(Node->getOperand(2), RL, RH);
6420
6421 if (MVT::isVector(Cond.getValueType())) {
6422 // Handle a vector merge.
6423 SDOperand CL, CH;
6424 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006425 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6426 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006427 } else {
6428 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006429 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6430 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006431 }
6432 break;
6433 }
Dan Gohman7f321562007-06-25 16:23:39 +00006434 case ISD::ADD:
6435 case ISD::SUB:
6436 case ISD::MUL:
6437 case ISD::FADD:
6438 case ISD::FSUB:
6439 case ISD::FMUL:
6440 case ISD::SDIV:
6441 case ISD::UDIV:
6442 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006443 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006444 case ISD::AND:
6445 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006446 case ISD::XOR:
6447 case ISD::UREM:
6448 case ISD::SREM:
6449 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006450 SDOperand LL, LH, RL, RH;
6451 SplitVectorOp(Node->getOperand(0), LL, LH);
6452 SplitVectorOp(Node->getOperand(1), RL, RH);
6453
Nate Begeman5db1afb2007-11-15 21:15:26 +00006454 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6455 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006456 break;
6457 }
Dan Gohman82669522007-10-11 23:57:53 +00006458 case ISD::FPOWI: {
6459 SDOperand L, H;
6460 SplitVectorOp(Node->getOperand(0), L, H);
6461
Nate Begeman5db1afb2007-11-15 21:15:26 +00006462 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6463 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006464 break;
6465 }
6466 case ISD::CTTZ:
6467 case ISD::CTLZ:
6468 case ISD::CTPOP:
6469 case ISD::FNEG:
6470 case ISD::FABS:
6471 case ISD::FSQRT:
6472 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006473 case ISD::FCOS:
6474 case ISD::FP_TO_SINT:
6475 case ISD::FP_TO_UINT:
6476 case ISD::SINT_TO_FP:
6477 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006478 SDOperand L, H;
6479 SplitVectorOp(Node->getOperand(0), L, H);
6480
Nate Begeman5db1afb2007-11-15 21:15:26 +00006481 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6482 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006483 break;
6484 }
Dan Gohman7f321562007-06-25 16:23:39 +00006485 case ISD::LOAD: {
6486 LoadSDNode *LD = cast<LoadSDNode>(Node);
6487 SDOperand Ch = LD->getChain();
6488 SDOperand Ptr = LD->getBasePtr();
6489 const Value *SV = LD->getSrcValue();
6490 int SVOffset = LD->getSrcValueOffset();
6491 unsigned Alignment = LD->getAlignment();
6492 bool isVolatile = LD->isVolatile();
6493
Nate Begeman5db1afb2007-11-15 21:15:26 +00006494 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6495 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006496 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6497 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006498 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006499 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006500 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006501
6502 // Build a factor node to remember that this load is independent of the
6503 // other one.
6504 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6505 Hi.getValue(1));
6506
6507 // Remember that we legalized the chain.
6508 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006509 break;
6510 }
Dan Gohman7f321562007-06-25 16:23:39 +00006511 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006512 // We know the result is a vector. The input may be either a vector or a
6513 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006514 SDOperand InOp = Node->getOperand(0);
6515 if (!MVT::isVector(InOp.getValueType()) ||
6516 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6517 // The input is a scalar or single-element vector.
6518 // Lower to a store/load so that it can be split.
6519 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006520 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006521
Evan Cheng786225a2006-10-05 23:01:46 +00006522 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006523 InOp, Ptr, NULL, 0);
6524 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006525 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006526 // Split the vector and convert each of the pieces now.
6527 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006528 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6529 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006530 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006531 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006532 }
6533
6534 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006535 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006536 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006537 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006538}
6539
6540
Dan Gohman89b20c02007-06-27 14:06:22 +00006541/// ScalarizeVectorOp - Given an operand of single-element vector type
6542/// (e.g. v1f32), convert it into the equivalent operation that returns a
6543/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006544SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6545 assert(MVT::isVector(Op.getValueType()) &&
6546 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006547 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006548 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6549 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006550
Dan Gohman7f321562007-06-25 16:23:39 +00006551 // See if we already scalarized it.
6552 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6553 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006554
6555 SDOperand Result;
6556 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006557 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006558#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006559 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006560#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006561 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6562 case ISD::ADD:
6563 case ISD::FADD:
6564 case ISD::SUB:
6565 case ISD::FSUB:
6566 case ISD::MUL:
6567 case ISD::FMUL:
6568 case ISD::SDIV:
6569 case ISD::UDIV:
6570 case ISD::FDIV:
6571 case ISD::SREM:
6572 case ISD::UREM:
6573 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006574 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006575 case ISD::AND:
6576 case ISD::OR:
6577 case ISD::XOR:
6578 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006579 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006580 ScalarizeVectorOp(Node->getOperand(0)),
6581 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006582 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006583 case ISD::FNEG:
6584 case ISD::FABS:
6585 case ISD::FSQRT:
6586 case ISD::FSIN:
6587 case ISD::FCOS:
6588 Result = DAG.getNode(Node->getOpcode(),
6589 NewVT,
6590 ScalarizeVectorOp(Node->getOperand(0)));
6591 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006592 case ISD::FPOWI:
6593 Result = DAG.getNode(Node->getOpcode(),
6594 NewVT,
6595 ScalarizeVectorOp(Node->getOperand(0)),
6596 Node->getOperand(1));
6597 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006598 case ISD::LOAD: {
6599 LoadSDNode *LD = cast<LoadSDNode>(Node);
6600 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6601 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006602
Dan Gohman7f321562007-06-25 16:23:39 +00006603 const Value *SV = LD->getSrcValue();
6604 int SVOffset = LD->getSrcValueOffset();
6605 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6606 LD->isVolatile(), LD->getAlignment());
6607
Chris Lattnerc7029802006-03-18 01:44:44 +00006608 // Remember that we legalized the chain.
6609 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6610 break;
6611 }
Dan Gohman7f321562007-06-25 16:23:39 +00006612 case ISD::BUILD_VECTOR:
6613 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006614 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006615 case ISD::INSERT_VECTOR_ELT:
6616 // Returning the inserted scalar element.
6617 Result = Node->getOperand(1);
6618 break;
6619 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006620 assert(Node->getOperand(0).getValueType() == NewVT &&
6621 "Concat of non-legal vectors not yet supported!");
6622 Result = Node->getOperand(0);
6623 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006624 case ISD::VECTOR_SHUFFLE: {
6625 // Figure out if the scalar is the LHS or RHS and return it.
6626 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6627 if (cast<ConstantSDNode>(EltNum)->getValue())
6628 Result = ScalarizeVectorOp(Node->getOperand(1));
6629 else
6630 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006631 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006632 }
6633 case ISD::EXTRACT_SUBVECTOR:
6634 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006635 assert(Result.getValueType() == NewVT);
6636 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006637 case ISD::BIT_CONVERT:
6638 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006639 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006640 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006641 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006642 ScalarizeVectorOp(Op.getOperand(1)),
6643 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006644 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006645 }
6646
6647 if (TLI.isTypeLegal(NewVT))
6648 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006649 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6650 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006651 return Result;
6652}
6653
Chris Lattner3e928bb2005-01-07 07:47:09 +00006654
6655// SelectionDAG::Legalize - This is the entry point for the file.
6656//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006657void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006658 if (ViewLegalizeDAGs) viewGraph();
6659
Chris Lattner3e928bb2005-01-07 07:47:09 +00006660 /// run - This is the main entry point to this class.
6661 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006662 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006663}
6664