blob: 09f937b921b5b7da60ea60b621f586b0f0ea34ba [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 Lattner1401d152008-01-16 07:45:30 +0000202 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
203 MVT::ValueType DestVT);
Chris Lattnerce872152006-03-19 06:31:19 +0000204 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000205 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000206 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
207 SDOperand LegalOp,
208 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000209 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
210 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000211 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
212 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000213
Chris Lattner456a93a2006-01-28 07:39:30 +0000214 SDOperand ExpandBSWAP(SDOperand Op);
215 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000216 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
217 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000218 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
219 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000220
Dan Gohman7f321562007-06-25 16:23:39 +0000221 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000222 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000223
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224 SDOperand getIntPtrConstant(uint64_t Val) {
225 return DAG.getConstant(Val, TLI.getPointerTy());
226 }
227};
228}
229
Chris Lattner4352cc92006-04-04 17:23:26 +0000230/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
231/// specified mask and type. Targets can specify exactly which masks they
232/// support and the code generator is tasked with not creating illegal masks.
233///
234/// Note that this will also return true for shuffles that are promoted to a
235/// different type.
236SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
237 SDOperand Mask) const {
238 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
239 default: return 0;
240 case TargetLowering::Legal:
241 case TargetLowering::Custom:
242 break;
243 case TargetLowering::Promote: {
244 // If this is promoted to a different type, convert the shuffle mask and
245 // ask if it is legal in the promoted type!
246 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
247
248 // If we changed # elements, change the shuffle mask.
249 unsigned NumEltsGrowth =
250 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
251 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
252 if (NumEltsGrowth > 1) {
253 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000254 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000255 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
256 SDOperand InOp = Mask.getOperand(i);
257 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
258 if (InOp.getOpcode() == ISD::UNDEF)
259 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
260 else {
261 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
262 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
263 }
264 }
265 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000266 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000267 }
268 VT = NVT;
269 break;
270 }
271 }
272 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
273}
274
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000275SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
276 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
277 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000278 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000279 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000280}
281
Chris Lattnere10e6f72007-06-18 21:28:10 +0000282/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
283/// contains all of a nodes operands before it contains the node.
284static void ComputeTopDownOrdering(SelectionDAG &DAG,
285 SmallVector<SDNode*, 64> &Order) {
286
287 DenseMap<SDNode*, unsigned> Visited;
288 std::vector<SDNode*> Worklist;
289 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000290
Chris Lattnere10e6f72007-06-18 21:28:10 +0000291 // Compute ordering from all of the leaves in the graphs, those (like the
292 // entry node) that have no operands.
293 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
294 E = DAG.allnodes_end(); I != E; ++I) {
295 if (I->getNumOperands() == 0) {
296 Visited[I] = 0 - 1U;
297 Worklist.push_back(I);
298 }
Chris Lattner32fca002005-10-06 01:20:27 +0000299 }
300
Chris Lattnere10e6f72007-06-18 21:28:10 +0000301 while (!Worklist.empty()) {
302 SDNode *N = Worklist.back();
303 Worklist.pop_back();
304
305 if (++Visited[N] != N->getNumOperands())
306 continue; // Haven't visited all operands yet
307
308 Order.push_back(N);
309
310 // Now that we have N in, add anything that uses it if all of their operands
311 // are now done.
312 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
313 UI != E; ++UI)
314 Worklist.push_back(*UI);
315 }
316
317 assert(Order.size() == Visited.size() &&
318 Order.size() ==
319 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
320 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000321}
322
Chris Lattner1618beb2005-07-29 00:11:56 +0000323
Chris Lattner3e928bb2005-01-07 07:47:09 +0000324void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000325 LastCALLSEQ_END = DAG.getEntryNode();
326 IsLegalizingCall = false;
327
Chris Lattnerab510a72005-10-02 17:49:46 +0000328 // The legalize process is inherently a bottom-up recursive process (users
329 // legalize their uses before themselves). Given infinite stack space, we
330 // could just start legalizing on the root and traverse the whole graph. In
331 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000332 // blocks. To avoid this problem, compute an ordering of the nodes where each
333 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000334 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000335 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000336
Chris Lattnerc7029802006-03-18 01:44:44 +0000337 for (unsigned i = 0, e = Order.size(); i != e; ++i)
338 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000339
340 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000341 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000342 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
343 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000344
345 ExpandedNodes.clear();
346 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000347 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000348 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000349 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000350
351 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000352 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000353}
354
Chris Lattner6831a812006-02-13 09:18:02 +0000355
356/// FindCallEndFromCallStart - Given a chained node that is part of a call
357/// sequence, find the CALLSEQ_END node that terminates the call sequence.
358static SDNode *FindCallEndFromCallStart(SDNode *Node) {
359 if (Node->getOpcode() == ISD::CALLSEQ_END)
360 return Node;
361 if (Node->use_empty())
362 return 0; // No CallSeqEnd
363
364 // The chain is usually at the end.
365 SDOperand TheChain(Node, Node->getNumValues()-1);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Sometimes it's at the beginning.
368 TheChain = SDOperand(Node, 0);
369 if (TheChain.getValueType() != MVT::Other) {
370 // Otherwise, hunt for it.
371 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
372 if (Node->getValueType(i) == MVT::Other) {
373 TheChain = SDOperand(Node, i);
374 break;
375 }
376
377 // Otherwise, we walked into a node without a chain.
378 if (TheChain.getValueType() != MVT::Other)
379 return 0;
380 }
381 }
382
383 for (SDNode::use_iterator UI = Node->use_begin(),
384 E = Node->use_end(); UI != E; ++UI) {
385
386 // Make sure to only follow users of our token chain.
387 SDNode *User = *UI;
388 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
389 if (User->getOperand(i) == TheChain)
390 if (SDNode *Result = FindCallEndFromCallStart(User))
391 return Result;
392 }
393 return 0;
394}
395
396/// FindCallStartFromCallEnd - Given a chained node that is part of a call
397/// sequence, find the CALLSEQ_START node that initiates the call sequence.
398static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
399 assert(Node && "Didn't find callseq_start for a call??");
400 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
401
402 assert(Node->getOperand(0).getValueType() == MVT::Other &&
403 "Node doesn't have a token chain argument!");
404 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
405}
406
407/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
408/// see if any uses can reach Dest. If no dest operands can get to dest,
409/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000410///
411/// Keep track of the nodes we fine that actually do lead to Dest in
412/// NodesLeadingTo. This avoids retraversing them exponential number of times.
413///
414bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000415 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000416 if (N == Dest) return true; // N certainly leads to Dest :)
417
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000418 // If we've already processed this node and it does lead to Dest, there is no
419 // need to reprocess it.
420 if (NodesLeadingTo.count(N)) return true;
421
Chris Lattner6831a812006-02-13 09:18:02 +0000422 // If the first result of this node has been already legalized, then it cannot
423 // reach N.
424 switch (getTypeAction(N->getValueType(0))) {
425 case Legal:
426 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Promote:
429 if (PromotedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 case Expand:
432 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
433 break;
434 }
435
436 // Okay, this node has not already been legalized. Check and legalize all
437 // operands. If none lead to Dest, then we can legalize this node.
438 bool OperandsLeadToDest = false;
439 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
440 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000441 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000442
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000443 if (OperandsLeadToDest) {
444 NodesLeadingTo.insert(N);
445 return true;
446 }
Chris Lattner6831a812006-02-13 09:18:02 +0000447
448 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000449 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000450 return false;
451}
452
Dan Gohman7f321562007-06-25 16:23:39 +0000453/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000454/// appropriate for its type.
455void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000456 MVT::ValueType VT = Op.getValueType();
457 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000458 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000459 case Legal: (void)LegalizeOp(Op); break;
460 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000461 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000462 if (!MVT::isVector(VT)) {
463 // If this is an illegal scalar, expand it into its two component
464 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000465 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000466 if (Op.getOpcode() == ISD::TargetConstant)
467 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000468 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000469 } else if (MVT::getVectorNumElements(VT) == 1) {
470 // If this is an illegal single element vector, convert it to a
471 // scalar operation.
472 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000473 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000474 // Otherwise, this is an illegal multiple element vector.
475 // Split it in half and legalize both parts.
476 SDOperand X, Y;
477 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000478 }
479 break;
480 }
481}
Chris Lattner6831a812006-02-13 09:18:02 +0000482
Evan Cheng9f877882006-12-13 20:57:08 +0000483/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
484/// a load from the constant pool.
485static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000486 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000487 bool Extend = false;
488
489 // If a FP immediate is precise when represented as a float and if the
490 // target can do an extending load from float to double, we put it into
491 // the constant pool as a float, even if it's is statically typed as a
492 // double.
493 MVT::ValueType VT = CFP->getValueType(0);
494 bool isDouble = VT == MVT::f64;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000495 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000496 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000497 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000498 if (VT!=MVT::f64 && VT!=MVT::f32)
499 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000500 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
501 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000502 }
503
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000504 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000505 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000506 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000507 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
508 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
509 VT = MVT::f32;
510 Extend = true;
511 }
512
513 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
514 if (Extend) {
515 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
516 CPIdx, NULL, 0, MVT::f32);
517 } else {
518 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
519 }
520}
521
Chris Lattner6831a812006-02-13 09:18:02 +0000522
Evan Cheng912095b2007-01-04 21:56:39 +0000523/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
524/// operations.
525static
526SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
527 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000528 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000529 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000530 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
531 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000532 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000533
Evan Cheng912095b2007-01-04 21:56:39 +0000534 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000535 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000536 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
537 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000538 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000539 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000540 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000541 // Shift right or sign-extend it if the two operands have different types.
542 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
543 if (SizeDiff > 0) {
544 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
545 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
546 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
547 } else if (SizeDiff < 0)
548 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000549
550 // Clear the sign bit of first operand.
551 SDOperand Mask2 = (VT == MVT::f64)
552 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
553 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
554 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000555 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000556 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
557
558 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000559 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
560 return Result;
561}
562
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000563/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
564static
565SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
566 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000567 SDOperand Chain = ST->getChain();
568 SDOperand Ptr = ST->getBasePtr();
569 SDOperand Val = ST->getValue();
570 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000571 int Alignment = ST->getAlignment();
572 int SVOffset = ST->getSrcValueOffset();
573 if (MVT::isFloatingPoint(ST->getStoredVT())) {
574 // Expand to a bitconvert of the value to the integer type of the
575 // same size, then a (misaligned) int store.
576 MVT::ValueType intVT;
577 if (VT==MVT::f64)
578 intVT = MVT::i64;
579 else if (VT==MVT::f32)
580 intVT = MVT::i32;
581 else
582 assert(0 && "Unaligned load of unsupported floating point type");
583
584 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
585 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
586 SVOffset, ST->isVolatile(), Alignment);
587 }
588 assert(MVT::isInteger(ST->getStoredVT()) &&
589 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000590 // Get the half-size VT
591 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
592 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000593 int IncrementSize = NumBits / 8;
594
595 // Divide the stored value in two parts.
596 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
597 SDOperand Lo = Val;
598 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
599
600 // Store the two parts
601 SDOperand Store1, Store2;
602 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
603 ST->getSrcValue(), SVOffset, NewStoredVT,
604 ST->isVolatile(), Alignment);
605 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
606 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000607 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000608 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
609 ST->getSrcValue(), SVOffset + IncrementSize,
610 NewStoredVT, ST->isVolatile(), Alignment);
611
612 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
613}
614
615/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
616static
617SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
618 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000619 int SVOffset = LD->getSrcValueOffset();
620 SDOperand Chain = LD->getChain();
621 SDOperand Ptr = LD->getBasePtr();
622 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000623 MVT::ValueType LoadedVT = LD->getLoadedVT();
Chris Lattnere400af82007-11-19 21:38:03 +0000624 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000625 // Expand to a (misaligned) integer load of the same size,
626 // then bitconvert to floating point.
627 MVT::ValueType intVT;
Chris Lattnere400af82007-11-19 21:38:03 +0000628 if (LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000629 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000630 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000631 intVT = MVT::i32;
632 else
633 assert(0 && "Unaligned load of unsupported floating point type");
634
635 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
636 SVOffset, LD->isVolatile(),
637 LD->getAlignment());
638 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
639 if (LoadedVT != VT)
640 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
641
642 SDOperand Ops[] = { Result, Chain };
643 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
644 Ops, 2);
645 }
Chris Lattnere400af82007-11-19 21:38:03 +0000646 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
647 "Unaligned load of unsupported type.");
648
649 // Compute the new VT that is half the size of the old one. We either have an
650 // integer MVT or we have a vector MVT.
651 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
652 MVT::ValueType NewLoadedVT;
653 if (!MVT::isVector(LoadedVT)) {
654 NewLoadedVT = MVT::getIntegerType(NumBits/2);
655 } else {
656 // FIXME: This is not right for <1 x anything> it is also not right for
657 // non-power-of-two vectors.
658 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
659 MVT::getVectorNumElements(LoadedVT)/2);
660 }
661 NumBits >>= 1;
662
663 unsigned Alignment = LD->getAlignment();
664 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000665 ISD::LoadExtType HiExtType = LD->getExtensionType();
666
667 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
668 if (HiExtType == ISD::NON_EXTLOAD)
669 HiExtType = ISD::ZEXTLOAD;
670
671 // Load the value in two parts
672 SDOperand Lo, Hi;
673 if (TLI.isLittleEndian()) {
674 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
675 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
676 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
677 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
678 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
679 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000680 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000681 } else {
682 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
683 NewLoadedVT,LD->isVolatile(), Alignment);
684 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
685 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
686 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
687 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000688 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000689 }
690
691 // aggregate the two parts
692 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
693 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
694 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
695
696 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
697 Hi.getValue(1));
698
699 SDOperand Ops[] = { Result, TF };
700 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
701}
Evan Cheng912095b2007-01-04 21:56:39 +0000702
Dan Gohman82669522007-10-11 23:57:53 +0000703/// UnrollVectorOp - We know that the given vector has a legal type, however
704/// the operation it performs is not legal and is an operation that we have
705/// no way of lowering. "Unroll" the vector, splitting out the scalars and
706/// operating on each element individually.
707SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
708 MVT::ValueType VT = Op.getValueType();
709 assert(isTypeLegal(VT) &&
710 "Caller should expand or promote operands that are not legal!");
711 assert(Op.Val->getNumValues() == 1 &&
712 "Can't unroll a vector with multiple results!");
713 unsigned NE = MVT::getVectorNumElements(VT);
714 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
715
716 SmallVector<SDOperand, 8> Scalars;
717 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
718 for (unsigned i = 0; i != NE; ++i) {
719 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
720 SDOperand Operand = Op.getOperand(j);
721 MVT::ValueType OperandVT = Operand.getValueType();
722 if (MVT::isVector(OperandVT)) {
723 // A vector operand; extract a single element.
724 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
725 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
726 OperandEltVT,
727 Operand,
728 DAG.getConstant(i, MVT::i32));
729 } else {
730 // A scalar operand; just use it as is.
731 Operands[j] = Operand;
732 }
733 }
734 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
735 &Operands[0], Operands.size()));
736 }
737
738 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
739}
740
Duncan Sands007f9842008-01-10 10:28:30 +0000741/// GetFPLibCall - Return the right libcall for the given floating point type.
742static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
743 RTLIB::Libcall Call_F32,
744 RTLIB::Libcall Call_F64,
745 RTLIB::Libcall Call_F80,
746 RTLIB::Libcall Call_PPCF128) {
747 return
748 VT == MVT::f32 ? Call_F32 :
749 VT == MVT::f64 ? Call_F64 :
750 VT == MVT::f80 ? Call_F80 :
751 VT == MVT::ppcf128 ? Call_PPCF128 :
752 RTLIB::UNKNOWN_LIBCALL;
753}
754
Dan Gohmana3466152007-07-13 20:14:11 +0000755/// LegalizeOp - We know that the specified value has a legal type, and
756/// that its operands are legal. Now ensure that the operation itself
757/// is legal, recursively ensuring that the operands' operations remain
758/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000759SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000760 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
761 return Op;
762
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000763 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000764 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000765 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000766
Chris Lattner3e928bb2005-01-07 07:47:09 +0000767 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000768 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000769 if (Node->getNumValues() > 1) {
770 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000771 if (getTypeAction(Node->getValueType(i)) != Legal) {
772 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000773 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000774 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000775 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000776 }
777 }
778
Chris Lattner45982da2005-05-12 16:53:42 +0000779 // Note that LegalizeOp may be reentered even from single-use nodes, which
780 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000781 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000782 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000783
Nate Begeman9373a812005-08-10 20:51:12 +0000784 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000785 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000786 bool isCustom = false;
787
Chris Lattner3e928bb2005-01-07 07:47:09 +0000788 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000789 case ISD::FrameIndex:
790 case ISD::EntryToken:
791 case ISD::Register:
792 case ISD::BasicBlock:
793 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000794 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000795 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000796 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000797 case ISD::TargetConstantPool:
798 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000799 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000800 case ISD::TargetExternalSymbol:
801 case ISD::VALUETYPE:
802 case ISD::SRCVALUE:
803 case ISD::STRING:
804 case ISD::CONDCODE:
805 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000806 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000807 "This must be legal!");
808 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000809 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000810 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
811 // If this is a target node, legalize it by legalizing the operands then
812 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000813 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000815 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000816
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000817 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000818
819 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
820 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
821 return Result.getValue(Op.ResNo);
822 }
823 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000824#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000825 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000826#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000827 assert(0 && "Do not know how to legalize this operator!");
828 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000829 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000830 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000831 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000832 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000833 case ISD::ConstantPool:
834 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000835 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
836 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000837 case TargetLowering::Custom:
838 Tmp1 = TLI.LowerOperation(Op, DAG);
839 if (Tmp1.Val) Result = Tmp1;
840 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000841 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000842 break;
843 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000844 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000845 case ISD::FRAMEADDR:
846 case ISD::RETURNADDR:
847 // The only option for these nodes is to custom lower them. If the target
848 // does not custom lower them, then return zero.
849 Tmp1 = TLI.LowerOperation(Op, DAG);
850 if (Tmp1.Val)
851 Result = Tmp1;
852 else
853 Result = DAG.getConstant(0, TLI.getPointerTy());
854 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000855 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000856 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::Custom:
860 Result = TLI.LowerOperation(Op, DAG);
861 if (Result.Val) break;
862 // Fall Thru
863 case TargetLowering::Legal:
864 Result = DAG.getConstant(0, VT);
865 break;
866 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000867 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000868 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000869 case ISD::EXCEPTIONADDR: {
870 Tmp1 = LegalizeOp(Node->getOperand(0));
871 MVT::ValueType VT = Node->getValueType(0);
872 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
873 default: assert(0 && "This action is not supported yet!");
874 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000875 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000876 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000877 }
878 break;
879 case TargetLowering::Custom:
880 Result = TLI.LowerOperation(Op, DAG);
881 if (Result.Val) break;
882 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000883 case TargetLowering::Legal: {
884 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
885 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000886 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000887 break;
888 }
889 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000890 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000891 if (Result.Val->getNumValues() == 1) break;
892
893 assert(Result.Val->getNumValues() == 2 &&
894 "Cannot return more than two values!");
895
896 // Since we produced two values, make sure to remember that we
897 // legalized both of them.
898 Tmp1 = LegalizeOp(Result);
899 Tmp2 = LegalizeOp(Result.getValue(1));
900 AddLegalizedOperand(Op.getValue(0), Tmp1);
901 AddLegalizedOperand(Op.getValue(1), Tmp2);
902 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000903 case ISD::EHSELECTION: {
904 Tmp1 = LegalizeOp(Node->getOperand(0));
905 Tmp2 = LegalizeOp(Node->getOperand(1));
906 MVT::ValueType VT = Node->getValueType(0);
907 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
908 default: assert(0 && "This action is not supported yet!");
909 case TargetLowering::Expand: {
910 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000911 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000912 }
913 break;
914 case TargetLowering::Custom:
915 Result = TLI.LowerOperation(Op, DAG);
916 if (Result.Val) break;
917 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000918 case TargetLowering::Legal: {
919 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
920 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000921 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000922 break;
923 }
924 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000925 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000926 if (Result.Val->getNumValues() == 1) break;
927
928 assert(Result.Val->getNumValues() == 2 &&
929 "Cannot return more than two values!");
930
931 // Since we produced two values, make sure to remember that we
932 // legalized both of them.
933 Tmp1 = LegalizeOp(Result);
934 Tmp2 = LegalizeOp(Result.getValue(1));
935 AddLegalizedOperand(Op.getValue(0), Tmp1);
936 AddLegalizedOperand(Op.getValue(1), Tmp2);
937 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000938 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000939 MVT::ValueType VT = Node->getValueType(0);
940 // The only "good" option for this node is to custom lower it.
941 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
942 default: assert(0 && "This action is not supported at all!");
943 case TargetLowering::Custom:
944 Result = TLI.LowerOperation(Op, DAG);
945 if (Result.Val) break;
946 // Fall Thru
947 case TargetLowering::Legal:
948 // Target does not know, how to lower this, lower to noop
949 Result = LegalizeOp(Node->getOperand(0));
950 break;
951 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000952 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000953 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000954 case ISD::AssertSext:
955 case ISD::AssertZext:
956 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000958 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000959 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000960 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000961 Result = Node->getOperand(Op.ResNo);
962 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000963 case ISD::CopyFromReg:
964 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000965 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000966 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000967 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000968 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000969 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000970 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000971 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
973 } else {
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
975 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000976 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
977 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000978 // Since CopyFromReg produces two values, make sure to remember that we
979 // legalized both of them.
980 AddLegalizedOperand(Op.getValue(0), Result);
981 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
982 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000983 case ISD::UNDEF: {
984 MVT::ValueType VT = Op.getValueType();
985 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000986 default: assert(0 && "This action is not supported yet!");
987 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000988 if (MVT::isInteger(VT))
989 Result = DAG.getConstant(0, VT);
990 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000991 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
992 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000993 else
994 assert(0 && "Unknown value type!");
995 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000996 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000997 break;
998 }
999 break;
1000 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001001
Chris Lattner48b61a72006-03-28 00:40:33 +00001002 case ISD::INTRINSIC_W_CHAIN:
1003 case ISD::INTRINSIC_WO_CHAIN:
1004 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001005 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001006 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1007 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001008 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001009
1010 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001011 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001012 TargetLowering::Custom) {
1013 Tmp3 = TLI.LowerOperation(Result, DAG);
1014 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001015 }
1016
1017 if (Result.Val->getNumValues() == 1) break;
1018
1019 // Must have return value and chain result.
1020 assert(Result.Val->getNumValues() == 2 &&
1021 "Cannot return more than two values!");
1022
1023 // Since loads produce two values, make sure to remember that we
1024 // legalized both of them.
1025 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1026 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1027 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001028 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001029
1030 case ISD::LOCATION:
1031 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1033
1034 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1035 case TargetLowering::Promote:
1036 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001037 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001038 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001039 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001040 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001041
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001042 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001043 const std::string &FName =
1044 cast<StringSDNode>(Node->getOperand(3))->getValue();
1045 const std::string &DirName =
1046 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001047 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001048
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001049 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001050 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001051 SDOperand LineOp = Node->getOperand(1);
1052 SDOperand ColOp = Node->getOperand(2);
1053
1054 if (useDEBUG_LOC) {
1055 Ops.push_back(LineOp); // line #
1056 Ops.push_back(ColOp); // col #
1057 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001058 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001059 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001060 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1061 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001062 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001063 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001064 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001065 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001066 } else {
1067 Result = Tmp1; // chain
1068 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001069 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001070 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001071 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001072 if (Tmp1 != Node->getOperand(0) ||
1073 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001074 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001075 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001076 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1077 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1078 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1079 } else {
1080 // Otherwise promote them.
1081 Ops.push_back(PromoteOp(Node->getOperand(1)));
1082 Ops.push_back(PromoteOp(Node->getOperand(2)));
1083 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001084 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1085 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001086 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001087 }
1088 break;
1089 }
1090 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001091
1092 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001093 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001094 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001095 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001096 case TargetLowering::Legal:
1097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1098 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1099 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1100 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001102 break;
1103 }
1104 break;
1105
Jim Laskey1ee29252007-01-26 14:34:52 +00001106 case ISD::LABEL:
1107 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1108 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Legal:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001113 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001114 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001115 case TargetLowering::Expand:
1116 Result = LegalizeOp(Node->getOperand(0));
1117 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001118 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001119 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001120
Scott Michelc1513d22007-08-08 23:23:31 +00001121 case ISD::Constant: {
1122 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1123 unsigned opAction =
1124 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1125
Chris Lattner3e928bb2005-01-07 07:47:09 +00001126 // We know we don't need to expand constants here, constants only have one
1127 // value and we check that it is fine above.
1128
Scott Michelc1513d22007-08-08 23:23:31 +00001129 if (opAction == TargetLowering::Custom) {
1130 Tmp1 = TLI.LowerOperation(Result, DAG);
1131 if (Tmp1.Val)
1132 Result = Tmp1;
1133 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001134 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001135 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001136 case ISD::ConstantFP: {
1137 // Spill FP immediates to the constant pool if the target cannot directly
1138 // codegen them. Targets often have some immediate values that can be
1139 // efficiently generated into an FP register without a load. We explicitly
1140 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001141 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1142
1143 // Check to see if this FP immediate is already legal.
1144 bool isLegal = false;
1145 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1146 E = TLI.legal_fpimm_end(); I != E; ++I)
1147 if (CFP->isExactlyValue(*I)) {
1148 isLegal = true;
1149 break;
1150 }
1151
Chris Lattner3181a772006-01-29 06:26:56 +00001152 // If this is a legal constant, turn it into a TargetConstantFP node.
1153 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001154 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1155 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001156 break;
1157 }
1158
1159 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1160 default: assert(0 && "This action is not supported yet!");
1161 case TargetLowering::Custom:
1162 Tmp3 = TLI.LowerOperation(Result, DAG);
1163 if (Tmp3.Val) {
1164 Result = Tmp3;
1165 break;
1166 }
1167 // FALLTHROUGH
1168 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001169 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001170 }
1171 break;
1172 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001173 case ISD::TokenFactor:
1174 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001175 Tmp1 = LegalizeOp(Node->getOperand(0));
1176 Tmp2 = LegalizeOp(Node->getOperand(1));
1177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1178 } else if (Node->getNumOperands() == 3) {
1179 Tmp1 = LegalizeOp(Node->getOperand(0));
1180 Tmp2 = LegalizeOp(Node->getOperand(1));
1181 Tmp3 = LegalizeOp(Node->getOperand(2));
1182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001183 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001184 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001185 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001186 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1187 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001188 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001189 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001190 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001191
1192 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001193 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001194 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001195 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1196 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001197
1198 // The number of incoming and outgoing values should match; unless the final
1199 // outgoing value is a flag.
1200 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1201 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1202 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1203 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001204 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001205
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001206 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001207 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001208 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001209 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1210 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001211 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001212 if (Op.ResNo == i)
1213 Tmp2 = Tmp1;
1214 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1215 }
1216 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001217 case ISD::EXTRACT_SUBREG: {
1218 Tmp1 = LegalizeOp(Node->getOperand(0));
1219 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1220 assert(idx && "Operand must be a constant");
1221 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1222 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1223 }
1224 break;
1225 case ISD::INSERT_SUBREG: {
1226 Tmp1 = LegalizeOp(Node->getOperand(0));
1227 Tmp2 = LegalizeOp(Node->getOperand(1));
1228 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1229 assert(idx && "Operand must be a constant");
1230 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1231 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1232 }
1233 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001234 case ISD::BUILD_VECTOR:
1235 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001236 default: assert(0 && "This action is not supported yet!");
1237 case TargetLowering::Custom:
1238 Tmp3 = TLI.LowerOperation(Result, DAG);
1239 if (Tmp3.Val) {
1240 Result = Tmp3;
1241 break;
1242 }
1243 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001244 case TargetLowering::Expand:
1245 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001246 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001247 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001248 break;
1249 case ISD::INSERT_VECTOR_ELT:
1250 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1251 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1252 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1253 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1254
1255 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1256 Node->getValueType(0))) {
1257 default: assert(0 && "This action is not supported yet!");
1258 case TargetLowering::Legal:
1259 break;
1260 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001261 Tmp4 = TLI.LowerOperation(Result, DAG);
1262 if (Tmp4.Val) {
1263 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001264 break;
1265 }
1266 // FALLTHROUGH
1267 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001268 // If the insert index is a constant, codegen this as a scalar_to_vector,
1269 // then a shuffle that inserts it into the right position in the vector.
1270 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1271 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1272 Tmp1.getValueType(), Tmp2);
1273
1274 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1275 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001276 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001277
1278 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1279 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1280 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001281 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001282 for (unsigned i = 0; i != NumElts; ++i) {
1283 if (i != InsertPos->getValue())
1284 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1285 else
1286 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1287 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001288 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1289 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001290
1291 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1292 Tmp1, ScVec, ShufMask);
1293 Result = LegalizeOp(Result);
1294 break;
1295 }
1296
Chris Lattner2332b9f2006-03-19 01:17:20 +00001297 // If the target doesn't support this, we have to spill the input vector
1298 // to a temporary stack slot, update the element, then reload it. This is
1299 // badness. We could also load the value into a vector register (either
1300 // with a "move to register" or "extload into register" instruction, then
1301 // permute it into place, if the idx is a constant and if the idx is
1302 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001303 MVT::ValueType VT = Tmp1.getValueType();
1304 MVT::ValueType EltVT = Tmp2.getValueType();
1305 MVT::ValueType IdxVT = Tmp3.getValueType();
1306 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001307 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001308 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001309 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001310
1311 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001312 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1313 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001314 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001315 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1316 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1317 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001318 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001319 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001320 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001321 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001322 break;
1323 }
1324 }
1325 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001326 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001327 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1328 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1329 break;
1330 }
1331
Chris Lattnerce872152006-03-19 06:31:19 +00001332 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1333 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1334 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1335 Node->getValueType(0))) {
1336 default: assert(0 && "This action is not supported yet!");
1337 case TargetLowering::Legal:
1338 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001339 case TargetLowering::Custom:
1340 Tmp3 = TLI.LowerOperation(Result, DAG);
1341 if (Tmp3.Val) {
1342 Result = Tmp3;
1343 break;
1344 }
1345 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001346 case TargetLowering::Expand:
1347 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001348 break;
1349 }
Chris Lattnerce872152006-03-19 06:31:19 +00001350 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001351 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1353 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1355
1356 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001357 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1358 default: assert(0 && "Unknown operation action!");
1359 case TargetLowering::Legal:
1360 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1361 "vector shuffle should not be created if not legal!");
1362 break;
1363 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001364 Tmp3 = TLI.LowerOperation(Result, DAG);
1365 if (Tmp3.Val) {
1366 Result = Tmp3;
1367 break;
1368 }
1369 // FALLTHROUGH
1370 case TargetLowering::Expand: {
1371 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001372 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001373 MVT::ValueType PtrVT = TLI.getPointerTy();
1374 SDOperand Mask = Node->getOperand(2);
1375 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001376 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001377 for (unsigned i = 0; i != NumElems; ++i) {
1378 SDOperand Arg = Mask.getOperand(i);
1379 if (Arg.getOpcode() == ISD::UNDEF) {
1380 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1381 } else {
1382 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1383 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1384 if (Idx < NumElems)
1385 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1386 DAG.getConstant(Idx, PtrVT)));
1387 else
1388 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1389 DAG.getConstant(Idx - NumElems, PtrVT)));
1390 }
1391 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001392 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001393 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001394 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001395 case TargetLowering::Promote: {
1396 // Change base type to a different vector type.
1397 MVT::ValueType OVT = Node->getValueType(0);
1398 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1399
1400 // Cast the two input vectors.
1401 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1402 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1403
1404 // Convert the shuffle mask to the right # elements.
1405 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1406 assert(Tmp3.Val && "Shuffle not legal?");
1407 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1408 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1409 break;
1410 }
Chris Lattner87100e02006-03-20 01:52:29 +00001411 }
1412 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001413
1414 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001415 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001416 Tmp2 = LegalizeOp(Node->getOperand(1));
1417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001418 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001419 break;
1420
Dan Gohman7f321562007-06-25 16:23:39 +00001421 case ISD::EXTRACT_SUBVECTOR:
1422 Tmp1 = Node->getOperand(0);
1423 Tmp2 = LegalizeOp(Node->getOperand(1));
1424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1425 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001426 break;
1427
Chris Lattner6831a812006-02-13 09:18:02 +00001428 case ISD::CALLSEQ_START: {
1429 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1430
1431 // Recursively Legalize all of the inputs of the call end that do not lead
1432 // to this call start. This ensures that any libcalls that need be inserted
1433 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001434 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001435 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001436 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1437 NodesLeadingTo);
1438 }
Chris Lattner6831a812006-02-13 09:18:02 +00001439
1440 // Now that we legalized all of the inputs (which may have inserted
1441 // libcalls) create the new CALLSEQ_START node.
1442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1443
1444 // Merge in the last call, to ensure that this call start after the last
1445 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001446 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001447 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1448 Tmp1 = LegalizeOp(Tmp1);
1449 }
Chris Lattner6831a812006-02-13 09:18:02 +00001450
1451 // Do not try to legalize the target-specific arguments (#1+).
1452 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001453 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001454 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001455 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001456 }
1457
1458 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001459 AddLegalizedOperand(Op.getValue(0), Result);
1460 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1461 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1462
Chris Lattner6831a812006-02-13 09:18:02 +00001463 // Now that the callseq_start and all of the non-call nodes above this call
1464 // sequence have been legalized, legalize the call itself. During this
1465 // process, no libcalls can/will be inserted, guaranteeing that no calls
1466 // can overlap.
1467 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1468 SDOperand InCallSEQ = LastCALLSEQ_END;
1469 // Note that we are selecting this call!
1470 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1471 IsLegalizingCall = true;
1472
1473 // Legalize the call, starting from the CALLSEQ_END.
1474 LegalizeOp(LastCALLSEQ_END);
1475 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1476 return Result;
1477 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001478 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001479 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1480 // will cause this node to be legalized as well as handling libcalls right.
1481 if (LastCALLSEQ_END.Val != Node) {
1482 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001483 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001484 assert(I != LegalizedNodes.end() &&
1485 "Legalizing the call start should have legalized this node!");
1486 return I->second;
1487 }
1488
1489 // Otherwise, the call start has been legalized and everything is going
1490 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001492 // Do not try to legalize the target-specific arguments (#1+), except for
1493 // an optional flag input.
1494 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1495 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001496 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001497 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001498 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001499 }
1500 } else {
1501 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1502 if (Tmp1 != Node->getOperand(0) ||
1503 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001504 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001505 Ops[0] = Tmp1;
1506 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001507 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001508 }
Chris Lattner6a542892006-01-24 05:48:21 +00001509 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001510 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001511 // This finishes up call legalization.
1512 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001513
1514 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1515 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1516 if (Node->getNumValues() == 2)
1517 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1518 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001519 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001520 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001521 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1522 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1523 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001525
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001526 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001527 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001528 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001529 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001530 case TargetLowering::Expand: {
1531 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1532 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1533 " not tell us which reg is the stack pointer!");
1534 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001535
1536 // Chain the dynamic stack allocation so that it doesn't modify the stack
1537 // pointer when other instructions are using the stack.
1538 Chain = DAG.getCALLSEQ_START(Chain,
1539 DAG.getConstant(0, TLI.getPointerTy()));
1540
Chris Lattner903d2782006-01-15 08:54:32 +00001541 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001542 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1543 Chain = SP.getValue(1);
1544 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1545 unsigned StackAlign =
1546 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1547 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001548 SP = DAG.getNode(ISD::AND, VT, SP,
1549 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001550 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001551 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1552
1553 Tmp2 =
1554 DAG.getCALLSEQ_END(Chain,
1555 DAG.getConstant(0, TLI.getPointerTy()),
1556 DAG.getConstant(0, TLI.getPointerTy()),
1557 SDOperand());
1558
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001559 Tmp1 = LegalizeOp(Tmp1);
1560 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001561 break;
1562 }
1563 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001564 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1565 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001566 Tmp1 = LegalizeOp(Tmp3);
1567 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001568 }
Chris Lattner903d2782006-01-15 08:54:32 +00001569 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001570 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001571 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001572 }
Chris Lattner903d2782006-01-15 08:54:32 +00001573 // Since this op produce two values, make sure to remember that we
1574 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001575 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1576 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001577 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001578 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001579 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001580 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001581 bool Changed = false;
1582 // Legalize all of the operands of the inline asm, in case they are nodes
1583 // that need to be expanded or something. Note we skip the asm string and
1584 // all of the TargetConstant flags.
1585 SDOperand Op = LegalizeOp(Ops[0]);
1586 Changed = Op != Ops[0];
1587 Ops[0] = Op;
1588
1589 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1590 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1591 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1592 for (++i; NumVals; ++i, --NumVals) {
1593 SDOperand Op = LegalizeOp(Ops[i]);
1594 if (Op != Ops[i]) {
1595 Changed = true;
1596 Ops[i] = Op;
1597 }
1598 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001599 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001600
1601 if (HasInFlag) {
1602 Op = LegalizeOp(Ops.back());
1603 Changed |= Op != Ops.back();
1604 Ops.back() = Op;
1605 }
1606
1607 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001608 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001609
1610 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001611 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001612 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1613 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001614 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001615 case ISD::BR:
1616 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001617 // Ensure that libcalls are emitted before a branch.
1618 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1619 Tmp1 = LegalizeOp(Tmp1);
1620 LastCALLSEQ_END = DAG.getEntryNode();
1621
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001623 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001624 case ISD::BRIND:
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 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1632 default: assert(0 && "Indirect target must be legal type (pointer)!");
1633 case Legal:
1634 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1635 break;
1636 }
1637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1638 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001639 case ISD::BR_JT:
1640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1641 // Ensure that libcalls are emitted before a branch.
1642 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1643 Tmp1 = LegalizeOp(Tmp1);
1644 LastCALLSEQ_END = DAG.getEntryNode();
1645
1646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1648
1649 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1650 default: assert(0 && "This action is not supported yet!");
1651 case TargetLowering::Legal: break;
1652 case TargetLowering::Custom:
1653 Tmp1 = TLI.LowerOperation(Result, DAG);
1654 if (Tmp1.Val) Result = Tmp1;
1655 break;
1656 case TargetLowering::Expand: {
1657 SDOperand Chain = Result.getOperand(0);
1658 SDOperand Table = Result.getOperand(1);
1659 SDOperand Index = Result.getOperand(2);
1660
1661 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001662 MachineFunction &MF = DAG.getMachineFunction();
1663 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001664 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1665 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001666
1667 SDOperand LD;
1668 switch (EntrySize) {
1669 default: assert(0 && "Size of jump table not supported yet."); break;
1670 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1671 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1672 }
1673
Evan Chengcc415862007-11-09 01:32:10 +00001674 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001675 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001676 // For PIC, the sequence is:
1677 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001678 // RelocBase can be JumpTable, GOT or some sort of global base.
1679 if (PTy != MVT::i32)
1680 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1681 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1682 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001683 }
Evan Chengcc415862007-11-09 01:32:10 +00001684 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001685 }
1686 }
1687 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001688 case ISD::BRCOND:
1689 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001690 // Ensure that libcalls are emitted before a return.
1691 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1692 Tmp1 = LegalizeOp(Tmp1);
1693 LastCALLSEQ_END = DAG.getEntryNode();
1694
Chris Lattner47e92232005-01-18 19:27:06 +00001695 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1696 case Expand: assert(0 && "It's impossible to expand bools");
1697 case Legal:
1698 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1699 break;
1700 case Promote:
1701 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001702
1703 // The top bits of the promoted condition are not necessarily zero, ensure
1704 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001705 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001706 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1707 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001708 break;
1709 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001710
1711 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001712 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001713
1714 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1715 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001716 case TargetLowering::Legal: break;
1717 case TargetLowering::Custom:
1718 Tmp1 = TLI.LowerOperation(Result, DAG);
1719 if (Tmp1.Val) Result = Tmp1;
1720 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001721 case TargetLowering::Expand:
1722 // Expand brcond's setcc into its constituent parts and create a BR_CC
1723 // Node.
1724 if (Tmp2.getOpcode() == ISD::SETCC) {
1725 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1726 Tmp2.getOperand(0), Tmp2.getOperand(1),
1727 Node->getOperand(2));
1728 } else {
1729 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1730 DAG.getCondCode(ISD::SETNE), Tmp2,
1731 DAG.getConstant(0, Tmp2.getValueType()),
1732 Node->getOperand(2));
1733 }
1734 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001735 }
1736 break;
1737 case ISD::BR_CC:
1738 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001739 // Ensure that libcalls are emitted before a branch.
1740 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1741 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001742 Tmp2 = Node->getOperand(2); // LHS
1743 Tmp3 = Node->getOperand(3); // RHS
1744 Tmp4 = Node->getOperand(1); // CC
1745
1746 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001747 LastCALLSEQ_END = DAG.getEntryNode();
1748
Nate Begeman750ac1b2006-02-01 07:19:44 +00001749 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1750 // the LHS is a legal SETCC itself. In this case, we need to compare
1751 // the result against zero to select between true and false values.
1752 if (Tmp3.Val == 0) {
1753 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1754 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001755 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001756
1757 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1758 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001759
Chris Lattner181b7a32005-12-17 23:46:46 +00001760 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1761 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001762 case TargetLowering::Legal: break;
1763 case TargetLowering::Custom:
1764 Tmp4 = TLI.LowerOperation(Result, DAG);
1765 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001766 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001767 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001768 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001769 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001770 LoadSDNode *LD = cast<LoadSDNode>(Node);
1771 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1772 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001773
Evan Cheng466685d2006-10-09 20:57:25 +00001774 ISD::LoadExtType ExtType = LD->getExtensionType();
1775 if (ExtType == ISD::NON_EXTLOAD) {
1776 MVT::ValueType VT = Node->getValueType(0);
1777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1778 Tmp3 = Result.getValue(0);
1779 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001780
Evan Cheng466685d2006-10-09 20:57:25 +00001781 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1782 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001783 case TargetLowering::Legal:
1784 // If this is an unaligned load and the target doesn't support it,
1785 // expand it.
1786 if (!TLI.allowsUnalignedMemoryAccesses()) {
1787 unsigned ABIAlignment = TLI.getTargetData()->
1788 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1789 if (LD->getAlignment() < ABIAlignment){
1790 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1791 TLI);
1792 Tmp3 = Result.getOperand(0);
1793 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001794 Tmp3 = LegalizeOp(Tmp3);
1795 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001796 }
1797 }
1798 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001799 case TargetLowering::Custom:
1800 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1801 if (Tmp1.Val) {
1802 Tmp3 = LegalizeOp(Tmp1);
1803 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001804 }
Evan Cheng466685d2006-10-09 20:57:25 +00001805 break;
1806 case TargetLowering::Promote: {
1807 // Only promote a load of vector type to another.
1808 assert(MVT::isVector(VT) && "Cannot promote this load!");
1809 // Change base type to a different vector type.
1810 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1811
1812 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001813 LD->getSrcValueOffset(),
1814 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001815 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1816 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001817 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001818 }
Evan Cheng466685d2006-10-09 20:57:25 +00001819 }
1820 // Since loads produce two values, make sure to remember that we
1821 // legalized both of them.
1822 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1823 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1824 return Op.ResNo ? Tmp4 : Tmp3;
1825 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001826 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001827 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1828 default: assert(0 && "This action is not supported yet!");
1829 case TargetLowering::Promote:
1830 assert(SrcVT == MVT::i1 &&
1831 "Can only promote extending LOAD from i1 -> i8!");
1832 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1833 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001834 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsf411b832007-10-17 13:49:58 +00001835 Tmp1 = Result.getValue(0);
1836 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001837 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001838 case TargetLowering::Custom:
1839 isCustom = true;
1840 // FALLTHROUGH
1841 case TargetLowering::Legal:
1842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1843 Tmp1 = Result.getValue(0);
1844 Tmp2 = Result.getValue(1);
1845
1846 if (isCustom) {
1847 Tmp3 = TLI.LowerOperation(Result, DAG);
1848 if (Tmp3.Val) {
1849 Tmp1 = LegalizeOp(Tmp3);
1850 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1851 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001852 } else {
1853 // If this is an unaligned load and the target doesn't support it,
1854 // expand it.
1855 if (!TLI.allowsUnalignedMemoryAccesses()) {
1856 unsigned ABIAlignment = TLI.getTargetData()->
1857 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1858 if (LD->getAlignment() < ABIAlignment){
1859 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1860 TLI);
1861 Tmp1 = Result.getOperand(0);
1862 Tmp2 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001863 Tmp1 = LegalizeOp(Tmp1);
1864 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001865 }
1866 }
Evan Cheng466685d2006-10-09 20:57:25 +00001867 }
1868 break;
1869 case TargetLowering::Expand:
1870 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1871 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1872 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001873 LD->getSrcValueOffset(),
1874 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001875 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1876 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1877 Tmp2 = LegalizeOp(Load.getValue(1));
1878 break;
1879 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001880 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001881 // Turn the unsupported load into an EXTLOAD followed by an explicit
1882 // zero/sign extend inreg.
1883 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1884 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001885 LD->getSrcValueOffset(), SrcVT,
1886 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001887 SDOperand ValRes;
1888 if (ExtType == ISD::SEXTLOAD)
1889 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1890 Result, DAG.getValueType(SrcVT));
1891 else
1892 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1893 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1894 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1895 break;
1896 }
1897 // Since loads produce two values, make sure to remember that we legalized
1898 // both of them.
1899 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1900 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1901 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001902 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001903 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001904 case ISD::EXTRACT_ELEMENT: {
1905 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1906 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001907 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001908 case Legal:
1909 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1910 // 1 -> Hi
1911 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1912 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1913 TLI.getShiftAmountTy()));
1914 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1915 } else {
1916 // 0 -> Lo
1917 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1918 Node->getOperand(0));
1919 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001920 break;
1921 case Expand:
1922 // Get both the low and high parts.
1923 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1924 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1925 Result = Tmp2; // 1 -> Hi
1926 else
1927 Result = Tmp1; // 0 -> Lo
1928 break;
1929 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001930 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001931 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001932
1933 case ISD::CopyToReg:
1934 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001935
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001936 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001937 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001938 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001939 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001940 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001942 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001943 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001944 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001945 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001946 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1947 Tmp3);
1948 } else {
1949 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001950 }
1951
1952 // Since this produces two values, make sure to remember that we legalized
1953 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001954 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001955 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001956 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001957 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001958 break;
1959
1960 case ISD::RET:
1961 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001962
1963 // Ensure that libcalls are emitted before a return.
1964 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1965 Tmp1 = LegalizeOp(Tmp1);
1966 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001967
Chris Lattner3e928bb2005-01-07 07:47:09 +00001968 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001969 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001970 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001971 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001972 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001973 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001974 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001975 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001976 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001977 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001978 SDOperand Lo, Hi;
1979 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001980
1981 // Big endian systems want the hi reg first.
1982 if (!TLI.isLittleEndian())
1983 std::swap(Lo, Hi);
1984
Evan Cheng13acce32006-12-11 19:27:14 +00001985 if (Hi.Val)
1986 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1987 else
1988 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001989 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001990 } else {
1991 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00001992 int InIx = Tmp2.ResNo;
1993 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1994 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001995
Dan Gohman7f321562007-06-25 16:23:39 +00001996 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001997 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001998 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001999 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002000 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002001 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002002 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002003 } else if (NumElems == 1) {
2004 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002005 Tmp2 = ScalarizeVectorOp(Tmp2);
2006 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002008
2009 // FIXME: Returns of gcc generic vectors smaller than a legal type
2010 // should be returned in integer registers!
2011
Chris Lattnerf87324e2006-04-11 01:31:51 +00002012 // The scalarized value type may not be legal, e.g. it might require
2013 // promotion or expansion. Relegalize the return.
2014 Result = LegalizeOp(Result);
2015 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002016 // FIXME: Returns of gcc generic vectors larger than a legal vector
2017 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002018 SDOperand Lo, Hi;
2019 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002020 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002021 Result = LegalizeOp(Result);
2022 }
2023 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002024 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002025 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002026 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002027 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002028 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002029 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002030 }
2031 break;
2032 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002033 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002034 break;
2035 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002036 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002037 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002038 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002039 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2040 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002041 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002042 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002043 break;
2044 case Expand: {
2045 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002046 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002047 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002048 ExpandOp(Node->getOperand(i), Lo, Hi);
2049 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002050 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002051 if (Hi.Val) {
2052 NewValues.push_back(Hi);
2053 NewValues.push_back(Node->getOperand(i+1));
2054 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002055 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002056 }
2057 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002058 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002059 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002060
2061 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002062 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002063 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002064 Result = DAG.getNode(ISD::RET, MVT::Other,
2065 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002066 break;
2067 }
2068 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002069
Chris Lattner6862dbc2006-01-29 21:02:23 +00002070 if (Result.getOpcode() == ISD::RET) {
2071 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2072 default: assert(0 && "This action is not supported yet!");
2073 case TargetLowering::Legal: break;
2074 case TargetLowering::Custom:
2075 Tmp1 = TLI.LowerOperation(Result, DAG);
2076 if (Tmp1.Val) Result = Tmp1;
2077 break;
2078 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002079 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002080 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002081 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002082 StoreSDNode *ST = cast<StoreSDNode>(Node);
2083 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2084 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002085 int SVOffset = ST->getSrcValueOffset();
2086 unsigned Alignment = ST->getAlignment();
2087 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002088
Evan Cheng8b2794a2006-10-13 21:14:26 +00002089 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002090 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2091 // FIXME: We shouldn't do this for TargetConstantFP's.
2092 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2093 // to phase ordering between legalized code and the dag combiner. This
2094 // probably means that we need to integrate dag combiner and legalizer
2095 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002096 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002097 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002098 if (CFP->getValueType(0) == MVT::f32 &&
2099 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002100 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2101 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002102 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002103 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2104 SVOffset, isVolatile, Alignment);
2105 break;
2106 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002107 // If this target supports 64-bit registers, do a single 64-bit store.
2108 if (getTypeAction(MVT::i64) == Legal) {
2109 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2110 getZExtValue(), MVT::i64);
2111 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2112 SVOffset, isVolatile, Alignment);
2113 break;
2114 } else if (getTypeAction(MVT::i32) == Legal) {
2115 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2116 // stores. If the target supports neither 32- nor 64-bits, this
2117 // xform is certainly not worth it.
2118 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2119 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2120 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2121 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2122
2123 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2124 SVOffset, isVolatile, Alignment);
2125 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2126 getIntPtrConstant(4));
2127 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002128 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002129
2130 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2131 break;
2132 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002133 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002134 }
2135
Evan Cheng8b2794a2006-10-13 21:14:26 +00002136 switch (getTypeAction(ST->getStoredVT())) {
2137 case Legal: {
2138 Tmp3 = LegalizeOp(ST->getValue());
2139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2140 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002141
Evan Cheng8b2794a2006-10-13 21:14:26 +00002142 MVT::ValueType VT = Tmp3.getValueType();
2143 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2144 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002145 case TargetLowering::Legal:
2146 // If this is an unaligned store and the target doesn't support it,
2147 // expand it.
2148 if (!TLI.allowsUnalignedMemoryAccesses()) {
2149 unsigned ABIAlignment = TLI.getTargetData()->
2150 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2151 if (ST->getAlignment() < ABIAlignment)
2152 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2153 TLI);
2154 }
2155 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002156 case TargetLowering::Custom:
2157 Tmp1 = TLI.LowerOperation(Result, DAG);
2158 if (Tmp1.Val) Result = Tmp1;
2159 break;
2160 case TargetLowering::Promote:
2161 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2162 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2163 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2164 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002165 ST->getSrcValue(), SVOffset, isVolatile,
2166 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002167 break;
2168 }
2169 break;
2170 }
2171 case Promote:
2172 // Truncate the value and store the result.
2173 Tmp3 = PromoteOp(ST->getValue());
2174 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002175 SVOffset, ST->getStoredVT(),
2176 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002177 break;
2178
2179 case Expand:
2180 unsigned IncrementSize = 0;
2181 SDOperand Lo, Hi;
2182
2183 // If this is a vector type, then we have to calculate the increment as
2184 // the product of the element size in bytes, and the number of elements
2185 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002186 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002187 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002188 int InIx = ST->getValue().ResNo;
2189 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2190 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002191
Dan Gohman7f321562007-06-25 16:23:39 +00002192 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002193 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002194 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002195 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002196 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002197 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002198 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002199 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002200 Result = LegalizeOp(Result);
2201 break;
2202 } else if (NumElems == 1) {
2203 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002204 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002205 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002206 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002207 // The scalarized value type may not be legal, e.g. it might require
2208 // promotion or expansion. Relegalize the scalar store.
2209 Result = LegalizeOp(Result);
2210 break;
2211 } else {
2212 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002213 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2214 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002215 }
2216 } else {
2217 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002218 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002219
2220 if (!TLI.isLittleEndian())
2221 std::swap(Lo, Hi);
2222 }
2223
2224 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002225 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002226
2227 if (Hi.Val == NULL) {
2228 // Must be int <-> float one-to-one expansion.
2229 Result = Lo;
2230 break;
2231 }
2232
Evan Cheng8b2794a2006-10-13 21:14:26 +00002233 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2234 getIntPtrConstant(IncrementSize));
2235 assert(isTypeLegal(Tmp2.getValueType()) &&
2236 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002237 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002238 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002239 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002240 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002241 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2242 break;
2243 }
2244 } else {
2245 // Truncating store
2246 assert(isTypeLegal(ST->getValue().getValueType()) &&
2247 "Cannot handle illegal TRUNCSTORE yet!");
2248 Tmp3 = LegalizeOp(ST->getValue());
2249
2250 // The only promote case we handle is TRUNCSTORE:i1 X into
2251 // -> TRUNCSTORE:i8 (and X, 1)
2252 if (ST->getStoredVT() == MVT::i1 &&
2253 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2254 // Promote the bool to a mask then store.
2255 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2256 DAG.getConstant(1, Tmp3.getValueType()));
2257 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002258 SVOffset, MVT::i8,
2259 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002260 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2261 Tmp2 != ST->getBasePtr()) {
2262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2263 ST->getOffset());
2264 }
2265
2266 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2267 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002268 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002269 case TargetLowering::Legal:
2270 // If this is an unaligned store and the target doesn't support it,
2271 // expand it.
2272 if (!TLI.allowsUnalignedMemoryAccesses()) {
2273 unsigned ABIAlignment = TLI.getTargetData()->
2274 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2275 if (ST->getAlignment() < ABIAlignment)
2276 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2277 TLI);
2278 }
2279 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002280 case TargetLowering::Custom:
2281 Tmp1 = TLI.LowerOperation(Result, DAG);
2282 if (Tmp1.Val) Result = Tmp1;
2283 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002284 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002285 }
2286 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002287 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002288 case ISD::PCMARKER:
2289 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002290 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002291 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002292 case ISD::STACKSAVE:
2293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002294 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2295 Tmp1 = Result.getValue(0);
2296 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002297
Chris Lattner140d53c2006-01-13 02:50:02 +00002298 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2299 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002300 case TargetLowering::Legal: break;
2301 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002302 Tmp3 = TLI.LowerOperation(Result, DAG);
2303 if (Tmp3.Val) {
2304 Tmp1 = LegalizeOp(Tmp3);
2305 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002306 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002307 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002308 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002309 // Expand to CopyFromReg if the target set
2310 // StackPointerRegisterToSaveRestore.
2311 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002312 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002313 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002314 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002315 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002316 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2317 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002318 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002319 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002320 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002321
2322 // Since stacksave produce two values, make sure to remember that we
2323 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002324 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2325 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2326 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002327
Chris Lattner140d53c2006-01-13 02:50:02 +00002328 case ISD::STACKRESTORE:
2329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002331 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002332
2333 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2334 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002335 case TargetLowering::Legal: break;
2336 case TargetLowering::Custom:
2337 Tmp1 = TLI.LowerOperation(Result, DAG);
2338 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002339 break;
2340 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002341 // Expand to CopyToReg if the target set
2342 // StackPointerRegisterToSaveRestore.
2343 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2344 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2345 } else {
2346 Result = Tmp1;
2347 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002348 break;
2349 }
2350 break;
2351
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002352 case ISD::READCYCLECOUNTER:
2353 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002354 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002355 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2356 Node->getValueType(0))) {
2357 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002358 case TargetLowering::Legal:
2359 Tmp1 = Result.getValue(0);
2360 Tmp2 = Result.getValue(1);
2361 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002362 case TargetLowering::Custom:
2363 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002364 Tmp1 = LegalizeOp(Result.getValue(0));
2365 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002366 break;
2367 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002368
2369 // Since rdcc produce two values, make sure to remember that we legalized
2370 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002371 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2372 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002373 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002374
Chris Lattner2ee743f2005-01-14 22:08:15 +00002375 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002376 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2377 case Expand: assert(0 && "It's impossible to expand bools");
2378 case Legal:
2379 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2380 break;
2381 case Promote:
2382 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002383 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002384 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002385 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2386 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002387 break;
2388 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002389 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002390 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002391
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002392 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002393
Nate Begemanb942a3d2005-08-23 04:29:48 +00002394 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002395 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002396 case TargetLowering::Legal: break;
2397 case TargetLowering::Custom: {
2398 Tmp1 = TLI.LowerOperation(Result, DAG);
2399 if (Tmp1.Val) Result = Tmp1;
2400 break;
2401 }
Nate Begeman9373a812005-08-10 20:51:12 +00002402 case TargetLowering::Expand:
2403 if (Tmp1.getOpcode() == ISD::SETCC) {
2404 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2405 Tmp2, Tmp3,
2406 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2407 } else {
2408 Result = DAG.getSelectCC(Tmp1,
2409 DAG.getConstant(0, Tmp1.getValueType()),
2410 Tmp2, Tmp3, ISD::SETNE);
2411 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002412 break;
2413 case TargetLowering::Promote: {
2414 MVT::ValueType NVT =
2415 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2416 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002417 if (MVT::isVector(Tmp2.getValueType())) {
2418 ExtOp = ISD::BIT_CONVERT;
2419 TruncOp = ISD::BIT_CONVERT;
2420 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002421 ExtOp = ISD::ANY_EXTEND;
2422 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002423 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002424 ExtOp = ISD::FP_EXTEND;
2425 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002426 }
2427 // Promote each of the values to the new type.
2428 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2429 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2430 // Perform the larger operation, then round down.
2431 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2432 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2433 break;
2434 }
2435 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002436 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002437 case ISD::SELECT_CC: {
2438 Tmp1 = Node->getOperand(0); // LHS
2439 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002440 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2441 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002442 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002443
Nate Begeman750ac1b2006-02-01 07:19:44 +00002444 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2445
2446 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2447 // the LHS is a legal SETCC itself. In this case, we need to compare
2448 // the result against zero to select between true and false values.
2449 if (Tmp2.Val == 0) {
2450 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2451 CC = DAG.getCondCode(ISD::SETNE);
2452 }
2453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2454
2455 // Everything is legal, see if we should expand this op or something.
2456 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2457 default: assert(0 && "This action is not supported yet!");
2458 case TargetLowering::Legal: break;
2459 case TargetLowering::Custom:
2460 Tmp1 = TLI.LowerOperation(Result, DAG);
2461 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002462 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002463 }
2464 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002465 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002466 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002467 Tmp1 = Node->getOperand(0);
2468 Tmp2 = Node->getOperand(1);
2469 Tmp3 = Node->getOperand(2);
2470 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2471
2472 // If we had to Expand the SetCC operands into a SELECT node, then it may
2473 // not always be possible to return a true LHS & RHS. In this case, just
2474 // return the value we legalized, returned in the LHS
2475 if (Tmp2.Val == 0) {
2476 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002477 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002478 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002479
Chris Lattner73e142f2006-01-30 22:43:50 +00002480 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002481 default: assert(0 && "Cannot handle this action for SETCC yet!");
2482 case TargetLowering::Custom:
2483 isCustom = true;
2484 // FALLTHROUGH.
2485 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002486 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002487 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002488 Tmp4 = TLI.LowerOperation(Result, DAG);
2489 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002490 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002491 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002492 case TargetLowering::Promote: {
2493 // First step, figure out the appropriate operation to use.
2494 // Allow SETCC to not be supported for all legal data types
2495 // Mostly this targets FP
2496 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002497 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002498
2499 // Scan for the appropriate larger type to use.
2500 while (1) {
2501 NewInTy = (MVT::ValueType)(NewInTy+1);
2502
2503 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2504 "Fell off of the edge of the integer world");
2505 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2506 "Fell off of the edge of the floating point world");
2507
2508 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002509 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002510 break;
2511 }
2512 if (MVT::isInteger(NewInTy))
2513 assert(0 && "Cannot promote Legal Integer SETCC yet");
2514 else {
2515 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2516 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2517 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002518 Tmp1 = LegalizeOp(Tmp1);
2519 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002520 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002521 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002522 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002523 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002524 case TargetLowering::Expand:
2525 // Expand a setcc node into a select_cc of the same condition, lhs, and
2526 // rhs that selects between const 1 (true) and const 0 (false).
2527 MVT::ValueType VT = Node->getValueType(0);
2528 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2529 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002530 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002531 break;
2532 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002533 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002534 case ISD::MEMSET:
2535 case ISD::MEMCPY:
2536 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002537 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002538 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2539
2540 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2541 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2542 case Expand: assert(0 && "Cannot expand a byte!");
2543 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002544 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002545 break;
2546 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002547 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002548 break;
2549 }
2550 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002551 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002552 }
Chris Lattner272455b2005-02-02 03:44:41 +00002553
2554 SDOperand Tmp4;
2555 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002556 case Expand: {
2557 // Length is too big, just take the lo-part of the length.
2558 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002559 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002560 break;
2561 }
Chris Lattnere5605212005-01-28 22:29:18 +00002562 case Legal:
2563 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002564 break;
2565 case Promote:
2566 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002567 break;
2568 }
2569
2570 SDOperand Tmp5;
2571 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2572 case Expand: assert(0 && "Cannot expand this yet!");
2573 case Legal:
2574 Tmp5 = LegalizeOp(Node->getOperand(4));
2575 break;
2576 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002577 Tmp5 = PromoteOp(Node->getOperand(4));
2578 break;
2579 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002580
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002581 SDOperand Tmp6;
2582 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2583 case Expand: assert(0 && "Cannot expand this yet!");
2584 case Legal:
2585 Tmp6 = LegalizeOp(Node->getOperand(5));
2586 break;
2587 case Promote:
2588 Tmp6 = PromoteOp(Node->getOperand(5));
2589 break;
2590 }
2591
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002592 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2593 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002594 case TargetLowering::Custom:
2595 isCustom = true;
2596 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002597 case TargetLowering::Legal: {
2598 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2599 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002600 if (isCustom) {
2601 Tmp1 = TLI.LowerOperation(Result, DAG);
2602 if (Tmp1.Val) Result = Tmp1;
2603 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002604 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002605 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002606 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002607 // Otherwise, the target does not support this operation. Lower the
2608 // operation to an explicit libcall as appropriate.
2609 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002610 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002611 TargetLowering::ArgListTy Args;
2612 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002613
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002614 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002615 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002616 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002617 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002618 // Extend the (previously legalized) ubyte argument to be an int value
2619 // for the call.
2620 if (Tmp3.getValueType() > MVT::i32)
2621 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2622 else
2623 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002624 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002625 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002626 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002627 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002628
2629 FnName = "memset";
2630 } else if (Node->getOpcode() == ISD::MEMCPY ||
2631 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002632 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002633 Entry.Node = Tmp2; Args.push_back(Entry);
2634 Entry.Node = Tmp3; Args.push_back(Entry);
2635 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002636 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2637 } else {
2638 assert(0 && "Unknown op!");
2639 }
Chris Lattner45982da2005-05-12 16:53:42 +00002640
Chris Lattnere1bd8222005-01-11 05:57:22 +00002641 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002642 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002643 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002644 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002645 break;
2646 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002647 }
2648 break;
2649 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002650
Chris Lattner5b359c62005-04-02 04:00:59 +00002651 case ISD::SHL_PARTS:
2652 case ISD::SRA_PARTS:
2653 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002654 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002655 bool Changed = false;
2656 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2657 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2658 Changed |= Ops.back() != Node->getOperand(i);
2659 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002660 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002661 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002662
Evan Cheng05a2d562006-01-09 18:31:59 +00002663 switch (TLI.getOperationAction(Node->getOpcode(),
2664 Node->getValueType(0))) {
2665 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002666 case TargetLowering::Legal: break;
2667 case TargetLowering::Custom:
2668 Tmp1 = TLI.LowerOperation(Result, DAG);
2669 if (Tmp1.Val) {
2670 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002671 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002672 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002673 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2674 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002675 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002676 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002677 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002678 return RetVal;
2679 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002680 break;
2681 }
2682
Chris Lattner2c8086f2005-04-02 05:00:07 +00002683 // Since these produce multiple values, make sure to remember that we
2684 // legalized all of them.
2685 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2686 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2687 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002688 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002689
2690 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002691 case ISD::ADD:
2692 case ISD::SUB:
2693 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002694 case ISD::MULHS:
2695 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002696 case ISD::UDIV:
2697 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002698 case ISD::AND:
2699 case ISD::OR:
2700 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002701 case ISD::SHL:
2702 case ISD::SRL:
2703 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002704 case ISD::FADD:
2705 case ISD::FSUB:
2706 case ISD::FMUL:
2707 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002708 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002709 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002710 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2711 case Expand: assert(0 && "Not possible");
2712 case Legal:
2713 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2714 break;
2715 case Promote:
2716 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2717 break;
2718 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002719
2720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002721
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002722 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002723 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002724 case TargetLowering::Legal: break;
2725 case TargetLowering::Custom:
2726 Tmp1 = TLI.LowerOperation(Result, DAG);
2727 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002728 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002729 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002730 MVT::ValueType VT = Op.getValueType();
2731
2732 // See if multiply or divide can be lowered using two-result operations.
2733 SDVTList VTs = DAG.getVTList(VT, VT);
2734 if (Node->getOpcode() == ISD::MUL) {
2735 // We just need the low half of the multiply; try both the signed
2736 // and unsigned forms. If the target supports both SMUL_LOHI and
2737 // UMUL_LOHI, form a preference by checking which forms of plain
2738 // MULH it supports.
2739 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2740 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2741 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2742 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2743 unsigned OpToUse = 0;
2744 if (HasSMUL_LOHI && !HasMULHS) {
2745 OpToUse = ISD::SMUL_LOHI;
2746 } else if (HasUMUL_LOHI && !HasMULHU) {
2747 OpToUse = ISD::UMUL_LOHI;
2748 } else if (HasSMUL_LOHI) {
2749 OpToUse = ISD::SMUL_LOHI;
2750 } else if (HasUMUL_LOHI) {
2751 OpToUse = ISD::UMUL_LOHI;
2752 }
2753 if (OpToUse) {
2754 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2755 break;
2756 }
2757 }
2758 if (Node->getOpcode() == ISD::MULHS &&
2759 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2760 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2761 break;
2762 }
2763 if (Node->getOpcode() == ISD::MULHU &&
2764 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2765 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2766 break;
2767 }
2768 if (Node->getOpcode() == ISD::SDIV &&
2769 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2770 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2771 break;
2772 }
2773 if (Node->getOpcode() == ISD::UDIV &&
2774 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2775 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2776 break;
2777 }
2778
Dan Gohman82669522007-10-11 23:57:53 +00002779 // Check to see if we have a libcall for this operator.
2780 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2781 bool isSigned = false;
2782 switch (Node->getOpcode()) {
2783 case ISD::UDIV:
2784 case ISD::SDIV:
2785 if (VT == MVT::i32) {
2786 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002787 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002788 isSigned = Node->getOpcode() == ISD::SDIV;
2789 }
2790 break;
2791 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002792 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2793 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002794 break;
2795 default: break;
2796 }
2797 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2798 SDOperand Dummy;
2799 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002800 break;
2801 }
2802
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002803 assert(MVT::isVector(Node->getValueType(0)) &&
2804 "Cannot expand this binary operator!");
2805 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002806 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002807 break;
2808 }
Evan Chengcc987612006-04-12 21:20:24 +00002809 case TargetLowering::Promote: {
2810 switch (Node->getOpcode()) {
2811 default: assert(0 && "Do not know how to promote this BinOp!");
2812 case ISD::AND:
2813 case ISD::OR:
2814 case ISD::XOR: {
2815 MVT::ValueType OVT = Node->getValueType(0);
2816 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2817 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2818 // Bit convert each of the values to the new type.
2819 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2820 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2821 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2822 // Bit convert the result back the original type.
2823 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2824 break;
2825 }
2826 }
2827 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002828 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002829 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002830
Dan Gohmane14ea862007-10-05 14:17:22 +00002831 case ISD::SMUL_LOHI:
2832 case ISD::UMUL_LOHI:
2833 case ISD::SDIVREM:
2834 case ISD::UDIVREM:
2835 // These nodes will only be produced by target-specific lowering, so
2836 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00002837 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00002838 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00002839
2840 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2841 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00002843 break;
2844
Chris Lattnera09f8482006-03-05 05:09:38 +00002845 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2846 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2847 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2848 case Expand: assert(0 && "Not possible");
2849 case Legal:
2850 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2851 break;
2852 case Promote:
2853 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2854 break;
2855 }
2856
2857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2858
2859 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2860 default: assert(0 && "Operation not supported");
2861 case TargetLowering::Custom:
2862 Tmp1 = TLI.LowerOperation(Result, DAG);
2863 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002864 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002865 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002866 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002867 // If this target supports fabs/fneg natively and select is cheap,
2868 // do this efficiently.
2869 if (!TLI.isSelectExpensive() &&
2870 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2871 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002872 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002873 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002874 // Get the sign bit of the RHS.
2875 MVT::ValueType IVT =
2876 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2877 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2878 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2879 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2880 // Get the absolute value of the result.
2881 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2882 // Select between the nabs and abs value based on the sign bit of
2883 // the input.
2884 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2885 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2886 AbsVal),
2887 AbsVal);
2888 Result = LegalizeOp(Result);
2889 break;
2890 }
2891
2892 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002893 MVT::ValueType NVT =
2894 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2895 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2896 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2897 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002898 break;
2899 }
Evan Cheng912095b2007-01-04 21:56:39 +00002900 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002901 break;
2902
Nate Begeman551bf3f2006-02-17 05:43:56 +00002903 case ISD::ADDC:
2904 case ISD::SUBC:
2905 Tmp1 = LegalizeOp(Node->getOperand(0));
2906 Tmp2 = LegalizeOp(Node->getOperand(1));
2907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
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;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002913
Nate Begeman551bf3f2006-02-17 05:43:56 +00002914 case ISD::ADDE:
2915 case ISD::SUBE:
2916 Tmp1 = LegalizeOp(Node->getOperand(0));
2917 Tmp2 = LegalizeOp(Node->getOperand(1));
2918 Tmp3 = LegalizeOp(Node->getOperand(2));
2919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2920 // Since this produces two values, make sure to remember that we legalized
2921 // both of them.
2922 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2923 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2924 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002925
Nate Begeman419f8b62005-10-18 00:27:41 +00002926 case ISD::BUILD_PAIR: {
2927 MVT::ValueType PairTy = Node->getValueType(0);
2928 // TODO: handle the case where the Lo and Hi operands are not of legal type
2929 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2930 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2931 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002932 case TargetLowering::Promote:
2933 case TargetLowering::Custom:
2934 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002935 case TargetLowering::Legal:
2936 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2937 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2938 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002939 case TargetLowering::Expand:
2940 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2941 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2942 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2943 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2944 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002945 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002946 break;
2947 }
2948 break;
2949 }
2950
Nate Begemanc105e192005-04-06 00:23:54 +00002951 case ISD::UREM:
2952 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002953 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002954 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2955 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002956
Nate Begemanc105e192005-04-06 00:23:54 +00002957 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002958 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2959 case TargetLowering::Custom:
2960 isCustom = true;
2961 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002962 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002964 if (isCustom) {
2965 Tmp1 = TLI.LowerOperation(Result, DAG);
2966 if (Tmp1.Val) Result = Tmp1;
2967 }
Nate Begemanc105e192005-04-06 00:23:54 +00002968 break;
Dan Gohman525178c2007-10-08 18:33:35 +00002969 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00002970 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002971 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00002972 MVT::ValueType VT = Node->getValueType(0);
2973
2974 // See if remainder can be lowered using two-result operations.
2975 SDVTList VTs = DAG.getVTList(VT, VT);
2976 if (Node->getOpcode() == ISD::SREM &&
2977 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2978 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2979 break;
2980 }
2981 if (Node->getOpcode() == ISD::UREM &&
2982 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2983 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2984 break;
2985 }
2986
2987 if (MVT::isInteger(VT)) {
2988 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002989 TargetLowering::Legal) {
2990 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00002991 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002992 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2993 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00002994 } else if (MVT::isVector(VT)) {
2995 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002996 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00002997 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002998 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002999 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3000 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003001 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003002 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003003 }
Dan Gohman0d974262007-11-06 22:11:54 +00003004 } else {
3005 assert(MVT::isFloatingPoint(VT) &&
3006 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003007 if (MVT::isVector(VT)) {
3008 Result = LegalizeOp(UnrollVectorOp(Op));
3009 } else {
3010 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003011 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3012 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003013 SDOperand Dummy;
3014 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3015 false/*sign irrelevant*/, Dummy);
3016 }
Nate Begemanc105e192005-04-06 00:23:54 +00003017 }
3018 break;
3019 }
Dan Gohman525178c2007-10-08 18:33:35 +00003020 }
Nate Begemanc105e192005-04-06 00:23:54 +00003021 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003022 case ISD::VAARG: {
3023 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3024 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3025
Chris Lattner5c62f332006-01-28 07:42:08 +00003026 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003027 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3028 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003029 case TargetLowering::Custom:
3030 isCustom = true;
3031 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003032 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003033 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3034 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003035 Tmp1 = Result.getValue(1);
3036
3037 if (isCustom) {
3038 Tmp2 = TLI.LowerOperation(Result, DAG);
3039 if (Tmp2.Val) {
3040 Result = LegalizeOp(Tmp2);
3041 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3042 }
3043 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003044 break;
3045 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00003046 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00003047 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003048 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003049 // Increment the pointer, VAList, to the next vaarg
3050 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3051 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3052 TLI.getPointerTy()));
3053 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003054 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3055 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003056 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003057 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003058 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003059 Result = LegalizeOp(Result);
3060 break;
3061 }
3062 }
3063 // Since VAARG produces two values, make sure to remember that we
3064 // legalized both of them.
3065 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003066 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3067 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003068 }
3069
3070 case ISD::VACOPY:
3071 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3072 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3073 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3074
3075 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3076 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003077 case TargetLowering::Custom:
3078 isCustom = true;
3079 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003080 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3082 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003083 if (isCustom) {
3084 Tmp1 = TLI.LowerOperation(Result, DAG);
3085 if (Tmp1.Val) Result = Tmp1;
3086 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003087 break;
3088 case TargetLowering::Expand:
3089 // This defaults to loading a pointer from the input and storing it to the
3090 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00003091 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003092 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00003093 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3094 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003095 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3096 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003097 break;
3098 }
3099 break;
3100
3101 case ISD::VAEND:
3102 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3103 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3104
3105 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3106 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003107 case TargetLowering::Custom:
3108 isCustom = true;
3109 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003110 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003112 if (isCustom) {
3113 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3114 if (Tmp1.Val) Result = Tmp1;
3115 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003116 break;
3117 case TargetLowering::Expand:
3118 Result = Tmp1; // Default to a no-op, return the chain
3119 break;
3120 }
3121 break;
3122
3123 case ISD::VASTART:
3124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3125 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3126
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003127 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3128
Nate Begemanacc398c2006-01-25 18:21:52 +00003129 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3130 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003131 case TargetLowering::Legal: break;
3132 case TargetLowering::Custom:
3133 Tmp1 = TLI.LowerOperation(Result, DAG);
3134 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003135 break;
3136 }
3137 break;
3138
Nate Begeman35ef9132006-01-11 21:21:00 +00003139 case ISD::ROTL:
3140 case ISD::ROTR:
3141 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3142 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003144 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3145 default:
3146 assert(0 && "ROTL/ROTR legalize operation not supported");
3147 break;
3148 case TargetLowering::Legal:
3149 break;
3150 case TargetLowering::Custom:
3151 Tmp1 = TLI.LowerOperation(Result, DAG);
3152 if (Tmp1.Val) Result = Tmp1;
3153 break;
3154 case TargetLowering::Promote:
3155 assert(0 && "Do not know how to promote ROTL/ROTR");
3156 break;
3157 case TargetLowering::Expand:
3158 assert(0 && "Do not know how to expand ROTL/ROTR");
3159 break;
3160 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003161 break;
3162
Nate Begemand88fc032006-01-14 03:14:10 +00003163 case ISD::BSWAP:
3164 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3165 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003166 case TargetLowering::Custom:
3167 assert(0 && "Cannot custom legalize this yet!");
3168 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003169 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003170 break;
3171 case TargetLowering::Promote: {
3172 MVT::ValueType OVT = Tmp1.getValueType();
3173 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003174 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003175
Chris Lattner456a93a2006-01-28 07:39:30 +00003176 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3177 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3178 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3179 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3180 break;
3181 }
3182 case TargetLowering::Expand:
3183 Result = ExpandBSWAP(Tmp1);
3184 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003185 }
3186 break;
3187
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003188 case ISD::CTPOP:
3189 case ISD::CTTZ:
3190 case ISD::CTLZ:
3191 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3192 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003193 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003194 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003195 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003196 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003197 TargetLowering::Custom) {
3198 Tmp1 = TLI.LowerOperation(Result, DAG);
3199 if (Tmp1.Val) {
3200 Result = Tmp1;
3201 }
Scott Michel910b66d2007-07-30 21:00:31 +00003202 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003203 break;
3204 case TargetLowering::Promote: {
3205 MVT::ValueType OVT = Tmp1.getValueType();
3206 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003207
3208 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003209 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3210 // Perform the larger operation, then subtract if needed.
3211 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003212 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003213 case ISD::CTPOP:
3214 Result = Tmp1;
3215 break;
3216 case ISD::CTTZ:
3217 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003218 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003219 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003220 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003221 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003222 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003223 break;
3224 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003225 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003226 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003227 DAG.getConstant(MVT::getSizeInBits(NVT) -
3228 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003229 break;
3230 }
3231 break;
3232 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003233 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003234 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003235 break;
3236 }
3237 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003238
Chris Lattner2c8086f2005-04-02 05:00:07 +00003239 // Unary operators
3240 case ISD::FABS:
3241 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003242 case ISD::FSQRT:
3243 case ISD::FSIN:
3244 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003245 Tmp1 = LegalizeOp(Node->getOperand(0));
3246 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003247 case TargetLowering::Promote:
3248 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003249 isCustom = true;
3250 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003251 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003252 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003253 if (isCustom) {
3254 Tmp1 = TLI.LowerOperation(Result, DAG);
3255 if (Tmp1.Val) Result = Tmp1;
3256 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003257 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003258 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003259 switch (Node->getOpcode()) {
3260 default: assert(0 && "Unreachable!");
3261 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003262 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3263 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003264 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003265 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003266 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003267 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3268 MVT::ValueType VT = Node->getValueType(0);
3269 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003270 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003271 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3272 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003273 break;
3274 }
3275 case ISD::FSQRT:
3276 case ISD::FSIN:
3277 case ISD::FCOS: {
3278 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003279
3280 // Expand unsupported unary vector operators by unrolling them.
3281 if (MVT::isVector(VT)) {
3282 Result = LegalizeOp(UnrollVectorOp(Op));
3283 break;
3284 }
3285
Evan Cheng56966222007-01-12 02:11:51 +00003286 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003287 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003288 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003289 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3290 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003291 break;
3292 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003293 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3294 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003295 break;
3296 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003297 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3298 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003299 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003300 default: assert(0 && "Unreachable!");
3301 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003302 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003303 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3304 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003305 break;
3306 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003307 }
3308 break;
3309 }
3310 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003311 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003312 MVT::ValueType VT = Node->getValueType(0);
3313
3314 // Expand unsupported unary vector operators by unrolling them.
3315 if (MVT::isVector(VT)) {
3316 Result = LegalizeOp(UnrollVectorOp(Op));
3317 break;
3318 }
3319
3320 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003321 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3322 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003323 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003324 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3325 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003326 break;
3327 }
Chris Lattner35481892005-12-23 00:16:34 +00003328 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003329 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003330 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3331 Node->getValueType(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003332 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3333 // The input has to be a vector type, we have to either scalarize it, pack
3334 // it, or convert it based on whether the input vector type is legal.
3335 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003336 int InIx = Node->getOperand(0).ResNo;
3337 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3338 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003339
3340 // Figure out if there is a simple type corresponding to this Vector
3341 // type. If so, convert to the vector type.
3342 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3343 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003344 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003345 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3346 LegalizeOp(Node->getOperand(0)));
3347 break;
3348 } else if (NumElems == 1) {
3349 // Turn this into a bit convert of the scalar input.
3350 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3351 ScalarizeVectorOp(Node->getOperand(0)));
3352 break;
3353 } else {
3354 // FIXME: UNIMP! Store then reload
3355 assert(0 && "Cast from unsupported vector type not implemented yet!");
3356 }
Chris Lattner67993f72006-01-23 07:30:46 +00003357 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003358 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3359 Node->getOperand(0).getValueType())) {
3360 default: assert(0 && "Unknown operation action!");
3361 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003362 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3363 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003364 break;
3365 case TargetLowering::Legal:
3366 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003367 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003368 break;
3369 }
3370 }
3371 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003372
Chris Lattner2c8086f2005-04-02 05:00:07 +00003373 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003374 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003375 case ISD::UINT_TO_FP: {
3376 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003377 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3378 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003379 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003380 Node->getOperand(0).getValueType())) {
3381 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003382 case TargetLowering::Custom:
3383 isCustom = true;
3384 // FALLTHROUGH
3385 case TargetLowering::Legal:
3386 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003387 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003388 if (isCustom) {
3389 Tmp1 = TLI.LowerOperation(Result, DAG);
3390 if (Tmp1.Val) Result = Tmp1;
3391 }
3392 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003393 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003394 Result = ExpandLegalINT_TO_FP(isSigned,
3395 LegalizeOp(Node->getOperand(0)),
3396 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003397 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003398 case TargetLowering::Promote:
3399 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3400 Node->getValueType(0),
3401 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003402 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003403 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003404 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003405 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003406 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3407 Node->getValueType(0), Node->getOperand(0));
3408 break;
3409 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003410 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003411 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003412 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3413 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003414 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003415 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3416 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003417 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003418 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3419 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003420 break;
3421 }
3422 break;
3423 }
3424 case ISD::TRUNCATE:
3425 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3426 case Legal:
3427 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003428 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003429 break;
3430 case Expand:
3431 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3432
3433 // Since the result is legal, we should just be able to truncate the low
3434 // part of the source.
3435 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3436 break;
3437 case Promote:
3438 Result = PromoteOp(Node->getOperand(0));
3439 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3440 break;
3441 }
3442 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003443
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003444 case ISD::FP_TO_SINT:
3445 case ISD::FP_TO_UINT:
3446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3447 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003448 Tmp1 = LegalizeOp(Node->getOperand(0));
3449
Chris Lattner1618beb2005-07-29 00:11:56 +00003450 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3451 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003452 case TargetLowering::Custom:
3453 isCustom = true;
3454 // FALLTHROUGH
3455 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003456 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003457 if (isCustom) {
3458 Tmp1 = TLI.LowerOperation(Result, DAG);
3459 if (Tmp1.Val) Result = Tmp1;
3460 }
3461 break;
3462 case TargetLowering::Promote:
3463 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3464 Node->getOpcode() == ISD::FP_TO_SINT);
3465 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003466 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003467 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3468 SDOperand True, False;
3469 MVT::ValueType VT = Node->getOperand(0).getValueType();
3470 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003471 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003472 const uint64_t zero[] = {0, 0};
3473 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3474 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003475 (void)apf.convertFromZeroExtendedInteger
3476 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003477 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003478 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3479 Node->getOperand(0), Tmp2, ISD::SETLT);
3480 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3481 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003482 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003483 Tmp2));
3484 False = DAG.getNode(ISD::XOR, NVT, False,
3485 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003486 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3487 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003488 } else {
3489 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3490 }
3491 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003492 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003493 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003494 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003495 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003496 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003497 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003498 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003499 if (Node->getOpcode()==ISD::FP_TO_SINT)
3500 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003501 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3502 (DAG.getNode(ISD::FP_ROUND_INREG,
3503 MVT::ppcf128, Node->getOperand(0),
3504 DAG.getValueType(MVT::f64)))));
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003505 else {
3506 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3507 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3508 Tmp2 = DAG.getConstantFP(apf, OVT);
3509 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3510 // FIXME: generated code sucks.
3511 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3512 DAG.getNode(ISD::ADD, MVT::i32,
3513 DAG.getNode(ISD::FP_TO_SINT, VT,
3514 DAG.getNode(ISD::FSUB, OVT,
3515 Node->getOperand(0), Tmp2)),
3516 DAG.getConstant(0x80000000, MVT::i32)),
3517 DAG.getNode(ISD::FP_TO_SINT, VT,
3518 Node->getOperand(0)),
3519 DAG.getCondCode(ISD::SETGE));
3520 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003521 break;
3522 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003523 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003524 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003525 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003526 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003527 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003528 LC = (VT == MVT::i32)
3529 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003530 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003531 LC = (VT == MVT::i32)
3532 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003533 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003534 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003535 LC = RTLIB::FPTOSINT_F80_I64;
3536 }
3537 else if (OVT == MVT::ppcf128) {
3538 assert(VT == MVT::i64);
3539 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003540 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003541 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003542 }
3543 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003544 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003545 LC = (VT == MVT::i32)
3546 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003547 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003548 LC = (VT == MVT::i32)
3549 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003550 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003551 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003552 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3553 }
3554 else if (OVT == MVT::ppcf128) {
3555 assert(VT == MVT::i64);
3556 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003557 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003558 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003559 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003560 default: assert(0 && "Unreachable!");
3561 }
3562 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003563 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3564 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003565 break;
3566 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003567 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003568 Tmp1 = PromoteOp(Node->getOperand(0));
3569 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3570 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003571 break;
3572 }
3573 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003574
Chris Lattnerf2670a82008-01-16 06:57:07 +00003575 case ISD::FP_EXTEND: {
3576 MVT::ValueType newVT = Op.getValueType();
3577 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3578 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
3579 // The only other way we can lower this is to turn it into a STORE,
3580 // LOAD pair, targetting a temporary location (a stack slot).
3581
3582 // NOTE: there is a choice here between constantly creating new stack
3583 // slots and always reusing the same one. We currently always create
3584 // new ones, as reuse may inhibit scheduling.
Chris Lattner23594d42008-01-16 07:03:22 +00003585 SDOperand StackSlot = DAG.CreateStackTemporary(oldVT);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003586 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3587 StackSlot, NULL, 0);
3588 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
Chris Lattner1401d152008-01-16 07:45:30 +00003589 Result, StackSlot, NULL, 0, oldVT);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003590 break;
3591 }
3592 }
3593 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3594 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3595 case Legal:
3596 Tmp1 = LegalizeOp(Node->getOperand(0));
3597 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3598 break;
3599 case Promote:
3600 Tmp1 = PromoteOp(Node->getOperand(0));
3601 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3602 break;
3603 }
3604 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003605 case ISD::FP_ROUND: {
3606 MVT::ValueType newVT = Op.getValueType();
3607 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3608 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Chris Lattnerf2670a82008-01-16 06:57:07 +00003609 if (oldVT == MVT::ppcf128) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003610 SDOperand Lo, Hi;
3611 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003612 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003613 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003614 } else {
Dale Johannesen638ccd52007-10-06 01:24:11 +00003615 // The only other way we can lower this is to turn it into a STORE,
3616 // LOAD pair, targetting a temporary location (a stack slot).
3617
3618 // NOTE: there is a choice here between constantly creating new stack
3619 // slots and always reusing the same one. We currently always create
3620 // new ones, as reuse may inhibit scheduling.
Chris Lattner23594d42008-01-16 07:03:22 +00003621 SDOperand StackSlot = DAG.CreateStackTemporary(newVT);
Chris Lattnerf2670a82008-01-16 06:57:07 +00003622 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3623 StackSlot, NULL, 0, newVT);
3624 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesen638ccd52007-10-06 01:24:11 +00003625 break;
Dale Johannesenab081c72007-08-09 17:27:48 +00003626 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003627 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003628 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003629 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3630 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3631 case Legal:
3632 Tmp1 = LegalizeOp(Node->getOperand(0));
3633 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3634 break;
3635 case Promote:
3636 Tmp1 = PromoteOp(Node->getOperand(0));
3637 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1);
3638 break;
3639 }
3640 break;
Chris Lattner13c78e22005-09-02 00:18:10 +00003641 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003642 case ISD::ZERO_EXTEND:
3643 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003644 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003645 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003646 case Legal:
3647 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003648 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003649 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003650 case Promote:
3651 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003652 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003653 Tmp1 = PromoteOp(Node->getOperand(0));
3654 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003655 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003656 case ISD::ZERO_EXTEND:
3657 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003658 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003659 Result = DAG.getZeroExtendInReg(Result,
3660 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003661 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003662 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003663 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003664 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003665 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003666 Result,
3667 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003668 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003669 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003670 }
3671 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003672 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003673 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003674 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003675 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003676
3677 // If this operation is not supported, convert it to a shl/shr or load/store
3678 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003679 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3680 default: assert(0 && "This action not supported for this op yet!");
3681 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003682 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003683 break;
3684 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003685 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003686 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003687 // NOTE: we could fall back on load/store here too for targets without
3688 // SAR. However, it is doubtful that any exist.
3689 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3690 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003691 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003692 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3693 Node->getOperand(0), ShiftCst);
3694 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3695 Result, ShiftCst);
3696 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003697 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003698 // EXTLOAD pair, targetting a temporary location (a stack slot).
3699
3700 // NOTE: there is a choice here between constantly creating new stack
3701 // slots and always reusing the same one. We currently always create
3702 // new ones, as reuse may inhibit scheduling.
Chris Lattner23594d42008-01-16 07:03:22 +00003703 SDOperand StackSlot = DAG.CreateStackTemporary(ExtraVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003704 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3705 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003706 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003707 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003708 } else {
3709 assert(0 && "Unknown op");
3710 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003711 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003712 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003713 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003714 }
Duncan Sands36397f52007-07-27 12:58:54 +00003715 case ISD::TRAMPOLINE: {
3716 SDOperand Ops[6];
3717 for (unsigned i = 0; i != 6; ++i)
3718 Ops[i] = LegalizeOp(Node->getOperand(i));
3719 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3720 // The only option for this node is to custom lower it.
3721 Result = TLI.LowerOperation(Result, DAG);
3722 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003723
3724 // Since trampoline produces two values, make sure to remember that we
3725 // legalized both of them.
3726 Tmp1 = LegalizeOp(Result.getValue(1));
3727 Result = LegalizeOp(Result);
3728 AddLegalizedOperand(SDOperand(Node, 0), Result);
3729 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3730 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003731 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003732 case ISD::FLT_ROUNDS: {
3733 MVT::ValueType VT = Node->getValueType(0);
3734 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3735 default: assert(0 && "This action not supported for this op yet!");
3736 case TargetLowering::Custom:
3737 Result = TLI.LowerOperation(Op, DAG);
3738 if (Result.Val) break;
3739 // Fall Thru
3740 case TargetLowering::Legal:
3741 // If this operation is not supported, lower it to constant 1
3742 Result = DAG.getConstant(1, VT);
3743 break;
3744 }
3745 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003746 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003747 MVT::ValueType VT = Node->getValueType(0);
3748 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3749 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003750 case TargetLowering::Legal:
3751 Tmp1 = LegalizeOp(Node->getOperand(0));
3752 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3753 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003754 case TargetLowering::Custom:
3755 Result = TLI.LowerOperation(Op, DAG);
3756 if (Result.Val) break;
3757 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003758 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003759 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003760 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003761 TargetLowering::ArgListTy Args;
3762 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner41bab0b2008-01-15 21:58:08 +00003763 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00003764 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3765 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003766 Result = CallResult.second;
3767 break;
3768 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003769 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003770 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003771 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003772
Chris Lattner4ddd2832006-04-08 04:13:17 +00003773 assert(Result.getValueType() == Op.getValueType() &&
3774 "Bad legalization!");
3775
Chris Lattner456a93a2006-01-28 07:39:30 +00003776 // Make sure that the generated code is itself legal.
3777 if (Result != Op)
3778 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003779
Chris Lattner45982da2005-05-12 16:53:42 +00003780 // Note that LegalizeOp may be reentered even from single-use nodes, which
3781 // means that we always must cache transformed nodes.
3782 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003783 return Result;
3784}
3785
Chris Lattner8b6fa222005-01-15 22:16:26 +00003786/// PromoteOp - Given an operation that produces a value in an invalid type,
3787/// promote it to compute the value into a larger type. The produced value will
3788/// have the correct bits for the low portion of the register, but no guarantee
3789/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003790SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3791 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003792 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003793 assert(getTypeAction(VT) == Promote &&
3794 "Caller should expand or legalize operands that are not promotable!");
3795 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3796 "Cannot promote to smaller type!");
3797
Chris Lattner03c85462005-01-15 05:21:40 +00003798 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003799 SDOperand Result;
3800 SDNode *Node = Op.Val;
3801
Chris Lattner40030bf2007-02-04 01:17:38 +00003802 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003803 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003804
Chris Lattner03c85462005-01-15 05:21:40 +00003805 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003806 case ISD::CopyFromReg:
3807 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003808 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003809#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003810 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003811#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003812 assert(0 && "Do not know how to promote this operator!");
3813 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003814 case ISD::UNDEF:
3815 Result = DAG.getNode(ISD::UNDEF, NVT);
3816 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003817 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003818 if (VT != MVT::i1)
3819 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3820 else
3821 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003822 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3823 break;
3824 case ISD::ConstantFP:
3825 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3826 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3827 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003828
Chris Lattner82fbfb62005-01-18 02:59:52 +00003829 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003830 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003831 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3832 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003833 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003834
Chris Lattner03c85462005-01-15 05:21:40 +00003835 case ISD::TRUNCATE:
3836 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3837 case Legal:
3838 Result = LegalizeOp(Node->getOperand(0));
3839 assert(Result.getValueType() >= NVT &&
3840 "This truncation doesn't make sense!");
3841 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3842 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3843 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003844 case Promote:
3845 // The truncation is not required, because we don't guarantee anything
3846 // about high bits anyway.
3847 Result = PromoteOp(Node->getOperand(0));
3848 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003849 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003850 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3851 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003852 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003853 }
3854 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003855 case ISD::SIGN_EXTEND:
3856 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003857 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003858 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3859 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3860 case Legal:
3861 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003862 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003863 break;
3864 case Promote:
3865 // Promote the reg if it's smaller.
3866 Result = PromoteOp(Node->getOperand(0));
3867 // The high bits are not guaranteed to be anything. Insert an extend.
3868 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003869 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003870 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003871 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003872 Result = DAG.getZeroExtendInReg(Result,
3873 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003874 break;
3875 }
3876 break;
Chris Lattner35481892005-12-23 00:16:34 +00003877 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00003878 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3879 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003880 Result = PromoteOp(Result);
3881 break;
3882
Chris Lattner8b6fa222005-01-15 22:16:26 +00003883 case ISD::FP_EXTEND:
3884 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3885 case ISD::FP_ROUND:
3886 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3887 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3888 case Promote: assert(0 && "Unreachable with 2 FP types!");
3889 case Legal:
3890 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003891 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003892 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003893 break;
3894 }
3895 break;
3896
3897 case ISD::SINT_TO_FP:
3898 case ISD::UINT_TO_FP:
3899 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3900 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003901 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003902 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003903 break;
3904
3905 case Promote:
3906 Result = PromoteOp(Node->getOperand(0));
3907 if (Node->getOpcode() == ISD::SINT_TO_FP)
3908 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003909 Result,
3910 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003911 else
Chris Lattner23993562005-04-13 02:38:47 +00003912 Result = DAG.getZeroExtendInReg(Result,
3913 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003914 // No extra round required here.
3915 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003916 break;
3917 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003918 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3919 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003920 // Round if we cannot tolerate excess precision.
3921 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003922 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3923 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003924 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003925 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003926 break;
3927
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003928 case ISD::SIGN_EXTEND_INREG:
3929 Result = PromoteOp(Node->getOperand(0));
3930 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3931 Node->getOperand(1));
3932 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003933 case ISD::FP_TO_SINT:
3934 case ISD::FP_TO_UINT:
3935 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3936 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003937 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003938 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003939 break;
3940 case Promote:
3941 // The input result is prerounded, so we don't have to do anything
3942 // special.
3943 Tmp1 = PromoteOp(Node->getOperand(0));
3944 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003945 }
Nate Begemand2558e32005-08-14 01:20:53 +00003946 // If we're promoting a UINT to a larger size, check to see if the new node
3947 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3948 // we can use that instead. This allows us to generate better code for
3949 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3950 // legal, such as PowerPC.
3951 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003952 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003953 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3954 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003955 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3956 } else {
3957 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3958 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003959 break;
3960
Chris Lattner2c8086f2005-04-02 05:00:07 +00003961 case ISD::FABS:
3962 case ISD::FNEG:
3963 Tmp1 = PromoteOp(Node->getOperand(0));
3964 assert(Tmp1.getValueType() == NVT);
3965 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3966 // NOTE: we do not have to do any extra rounding here for
3967 // NoExcessFPPrecision, because we know the input will have the appropriate
3968 // precision, and these operations don't modify precision at all.
3969 break;
3970
Chris Lattnerda6ba872005-04-28 21:44:33 +00003971 case ISD::FSQRT:
3972 case ISD::FSIN:
3973 case ISD::FCOS:
3974 Tmp1 = PromoteOp(Node->getOperand(0));
3975 assert(Tmp1.getValueType() == NVT);
3976 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003977 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003978 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3979 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003980 break;
3981
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003982 case ISD::FPOWI: {
3983 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3984 // directly as well, which may be better.
3985 Tmp1 = PromoteOp(Node->getOperand(0));
3986 assert(Tmp1.getValueType() == NVT);
3987 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3988 if (NoExcessFPPrecision)
3989 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3990 DAG.getValueType(VT));
3991 break;
3992 }
3993
Chris Lattner03c85462005-01-15 05:21:40 +00003994 case ISD::AND:
3995 case ISD::OR:
3996 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003997 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003998 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003999 case ISD::MUL:
4000 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004001 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004002 // that too is okay if they are integer operations.
4003 Tmp1 = PromoteOp(Node->getOperand(0));
4004 Tmp2 = PromoteOp(Node->getOperand(1));
4005 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4006 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004007 break;
4008 case ISD::FADD:
4009 case ISD::FSUB:
4010 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004011 Tmp1 = PromoteOp(Node->getOperand(0));
4012 Tmp2 = PromoteOp(Node->getOperand(1));
4013 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4014 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4015
4016 // Floating point operations will give excess precision that we may not be
4017 // able to tolerate. If we DO allow excess precision, just leave it,
4018 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004019 // FIXME: Why would we need to round FP ops more than integer ones?
4020 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004021 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004022 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4023 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004024 break;
4025
Chris Lattner8b6fa222005-01-15 22:16:26 +00004026 case ISD::SDIV:
4027 case ISD::SREM:
4028 // These operators require that their input be sign extended.
4029 Tmp1 = PromoteOp(Node->getOperand(0));
4030 Tmp2 = PromoteOp(Node->getOperand(1));
4031 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004032 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4033 DAG.getValueType(VT));
4034 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4035 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004036 }
4037 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4038
4039 // Perform FP_ROUND: this is probably overly pessimistic.
4040 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004041 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4042 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004043 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004044 case ISD::FDIV:
4045 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004046 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004047 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4049 case Legal:
4050 Tmp1 = LegalizeOp(Node->getOperand(0));
4051 break;
4052 case Promote:
4053 Tmp1 = PromoteOp(Node->getOperand(0));
4054 break;
4055 case Expand:
4056 assert(0 && "not implemented");
4057 }
4058 switch (getTypeAction(Node->getOperand(1).getValueType())) {
4059 case Legal:
4060 Tmp2 = LegalizeOp(Node->getOperand(1));
4061 break;
4062 case Promote:
4063 Tmp2 = PromoteOp(Node->getOperand(1));
4064 break;
4065 case Expand:
4066 assert(0 && "not implemented");
4067 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004068 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4069
4070 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004071 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004072 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4073 DAG.getValueType(VT));
4074 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004075
4076 case ISD::UDIV:
4077 case ISD::UREM:
4078 // These operators require that their input be zero extended.
4079 Tmp1 = PromoteOp(Node->getOperand(0));
4080 Tmp2 = PromoteOp(Node->getOperand(1));
4081 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004082 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4083 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004084 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4085 break;
4086
4087 case ISD::SHL:
4088 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004089 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004090 break;
4091 case ISD::SRA:
4092 // The input value must be properly sign extended.
4093 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004094 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4095 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004096 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004097 break;
4098 case ISD::SRL:
4099 // The input value must be properly zero extended.
4100 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004101 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004102 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004103 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004104
4105 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004106 Tmp1 = Node->getOperand(0); // Get the chain.
4107 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004108 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4109 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4110 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4111 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00004112 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00004113 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00004114 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004115 // Increment the pointer, VAList, to the next vaarg
4116 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4117 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4118 TLI.getPointerTy()));
4119 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00004120 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4121 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004122 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004123 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004124 }
4125 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004126 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004127 break;
4128
Evan Cheng466685d2006-10-09 20:57:25 +00004129 case ISD::LOAD: {
4130 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004131 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4132 ? ISD::EXTLOAD : LD->getExtensionType();
4133 Result = DAG.getExtLoad(ExtType, NVT,
4134 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004135 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004136 LD->getLoadedVT(),
4137 LD->isVolatile(),
4138 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004139 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004140 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004141 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004142 }
Chris Lattner03c85462005-01-15 05:21:40 +00004143 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004144 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4145 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004146 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004147 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004148 case ISD::SELECT_CC:
4149 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4150 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4151 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004152 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004153 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004154 case ISD::BSWAP:
4155 Tmp1 = Node->getOperand(0);
4156 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4157 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4158 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004159 DAG.getConstant(MVT::getSizeInBits(NVT) -
4160 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004161 TLI.getShiftAmountTy()));
4162 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004163 case ISD::CTPOP:
4164 case ISD::CTTZ:
4165 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004166 // Zero extend the argument
4167 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004168 // Perform the larger operation, then subtract if needed.
4169 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004170 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004171 case ISD::CTPOP:
4172 Result = Tmp1;
4173 break;
4174 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004175 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004176 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004177 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4178 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004179 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004180 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004181 break;
4182 case ISD::CTLZ:
4183 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004184 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004185 DAG.getConstant(MVT::getSizeInBits(NVT) -
4186 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004187 break;
4188 }
4189 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004190 case ISD::EXTRACT_SUBVECTOR:
4191 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004192 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004193 case ISD::EXTRACT_VECTOR_ELT:
4194 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4195 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004196 }
4197
4198 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004199
4200 // Make sure the result is itself legal.
4201 Result = LegalizeOp(Result);
4202
4203 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004204 AddPromotedOperand(Op, Result);
4205 return Result;
4206}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004207
Dan Gohman7f321562007-06-25 16:23:39 +00004208/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4209/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4210/// based on the vector type. The return type of this matches the element type
4211/// of the vector, which may not be legal for the target.
4212SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004213 // We know that operand #0 is the Vec vector. If the index is a constant
4214 // or if the invec is a supported hardware type, we can use it. Otherwise,
4215 // lower to a store then an indexed load.
4216 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004217 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004218
Dan Gohmane40c7b02007-09-24 15:54:53 +00004219 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004220 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004221
Dan Gohman7f321562007-06-25 16:23:39 +00004222 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4223 default: assert(0 && "This action is not supported yet!");
4224 case TargetLowering::Custom: {
4225 Vec = LegalizeOp(Vec);
4226 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4227 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4228 if (Tmp3.Val)
4229 return Tmp3;
4230 break;
4231 }
4232 case TargetLowering::Legal:
4233 if (isTypeLegal(TVT)) {
4234 Vec = LegalizeOp(Vec);
4235 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004236 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004237 }
4238 break;
4239 case TargetLowering::Expand:
4240 break;
4241 }
4242
4243 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004244 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004245 Op = ScalarizeVectorOp(Vec);
4246 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4247 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004248 SDOperand Lo, Hi;
4249 SplitVectorOp(Vec, Lo, Hi);
4250 if (CIdx->getValue() < NumElems/2) {
4251 Vec = Lo;
4252 } else {
4253 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004254 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4255 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004256 }
Dan Gohman7f321562007-06-25 16:23:39 +00004257
Chris Lattner15972212006-03-31 17:55:51 +00004258 // It's now an extract from the appropriate high or low part. Recurse.
4259 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004260 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004261 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004262 // Store the value to a temporary stack slot, then LOAD the scalar
4263 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004264 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004265 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4266
4267 // Add the offset to the index.
4268 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4269 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4270 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004271
4272 if (MVT::getSizeInBits(Idx.getValueType()) >
4273 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004274 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004275 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004276 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004277
Dan Gohman7f321562007-06-25 16:23:39 +00004278 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4279
4280 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004281 }
Dan Gohman7f321562007-06-25 16:23:39 +00004282 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004283}
4284
Dan Gohman7f321562007-06-25 16:23:39 +00004285/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004286/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004287SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004288 // We know that operand #0 is the Vec vector. For now we assume the index
4289 // is a constant and that the extracted result is a supported hardware type.
4290 SDOperand Vec = Op.getOperand(0);
4291 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4292
Dan Gohman7f321562007-06-25 16:23:39 +00004293 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004294
4295 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4296 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004297 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004298 }
4299
4300 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4301 SDOperand Lo, Hi;
4302 SplitVectorOp(Vec, Lo, Hi);
4303 if (CIdx->getValue() < NumElems/2) {
4304 Vec = Lo;
4305 } else {
4306 Vec = Hi;
4307 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4308 }
4309
4310 // It's now an extract from the appropriate high or low part. Recurse.
4311 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004312 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004313}
4314
Nate Begeman750ac1b2006-02-01 07:19:44 +00004315/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4316/// with condition CC on the current target. This usually involves legalizing
4317/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4318/// there may be no choice but to create a new SetCC node to represent the
4319/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4320/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4321void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4322 SDOperand &RHS,
4323 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004324 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004325
4326 switch (getTypeAction(LHS.getValueType())) {
4327 case Legal:
4328 Tmp1 = LegalizeOp(LHS); // LHS
4329 Tmp2 = LegalizeOp(RHS); // RHS
4330 break;
4331 case Promote:
4332 Tmp1 = PromoteOp(LHS); // LHS
4333 Tmp2 = PromoteOp(RHS); // RHS
4334
4335 // If this is an FP compare, the operands have already been extended.
4336 if (MVT::isInteger(LHS.getValueType())) {
4337 MVT::ValueType VT = LHS.getValueType();
4338 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4339
4340 // Otherwise, we have to insert explicit sign or zero extends. Note
4341 // that we could insert sign extends for ALL conditions, but zero extend
4342 // is cheaper on many machines (an AND instead of two shifts), so prefer
4343 // it.
4344 switch (cast<CondCodeSDNode>(CC)->get()) {
4345 default: assert(0 && "Unknown integer comparison!");
4346 case ISD::SETEQ:
4347 case ISD::SETNE:
4348 case ISD::SETUGE:
4349 case ISD::SETUGT:
4350 case ISD::SETULE:
4351 case ISD::SETULT:
4352 // ALL of these operations will work if we either sign or zero extend
4353 // the operands (including the unsigned comparisons!). Zero extend is
4354 // usually a simpler/cheaper operation, so prefer it.
4355 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4356 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4357 break;
4358 case ISD::SETGE:
4359 case ISD::SETGT:
4360 case ISD::SETLT:
4361 case ISD::SETLE:
4362 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4363 DAG.getValueType(VT));
4364 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4365 DAG.getValueType(VT));
4366 break;
4367 }
4368 }
4369 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004370 case Expand: {
4371 MVT::ValueType VT = LHS.getValueType();
4372 if (VT == MVT::f32 || VT == MVT::f64) {
4373 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004374 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004375 switch (cast<CondCodeSDNode>(CC)->get()) {
4376 case ISD::SETEQ:
4377 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004378 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004379 break;
4380 case ISD::SETNE:
4381 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004382 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004383 break;
4384 case ISD::SETGE:
4385 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004386 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004387 break;
4388 case ISD::SETLT:
4389 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004390 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004391 break;
4392 case ISD::SETLE:
4393 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004394 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004395 break;
4396 case ISD::SETGT:
4397 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004398 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004399 break;
4400 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004401 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4402 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004403 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004404 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004405 break;
4406 default:
Evan Cheng56966222007-01-12 02:11:51 +00004407 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004408 switch (cast<CondCodeSDNode>(CC)->get()) {
4409 case ISD::SETONE:
4410 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004411 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004412 // Fallthrough
4413 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004414 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004415 break;
4416 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004417 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004418 break;
4419 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004420 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004421 break;
4422 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004423 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004424 break;
Evan Cheng56966222007-01-12 02:11:51 +00004425 case ISD::SETUEQ:
4426 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004427 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004428 default: assert(0 && "Unsupported FP setcc!");
4429 }
4430 }
4431
4432 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004433 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004434 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004435 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004436 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004437 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004438 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004439 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004440 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004441 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004442 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004443 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004444 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004445 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4446 Tmp2 = SDOperand();
4447 }
4448 LHS = Tmp1;
4449 RHS = Tmp2;
4450 return;
4451 }
4452
Nate Begeman750ac1b2006-02-01 07:19:44 +00004453 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4454 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004455 ExpandOp(RHS, RHSLo, RHSHi);
4456 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4457
4458 if (VT==MVT::ppcf128) {
4459 // FIXME: This generated code sucks. We want to generate
4460 // FCMP crN, hi1, hi2
4461 // BNE crN, L:
4462 // FCMP crN, lo1, lo2
4463 // The following can be improved, but not that much.
4464 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4465 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4466 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4467 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4468 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4469 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4470 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4471 Tmp2 = SDOperand();
4472 break;
4473 }
4474
4475 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004476 case ISD::SETEQ:
4477 case ISD::SETNE:
4478 if (RHSLo == RHSHi)
4479 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4480 if (RHSCST->isAllOnesValue()) {
4481 // Comparison to -1.
4482 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4483 Tmp2 = RHSLo;
4484 break;
4485 }
4486
4487 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4488 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4489 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4490 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4491 break;
4492 default:
4493 // If this is a comparison of the sign bit, just look at the top part.
4494 // X > -1, x < 0
4495 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4496 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4497 CST->getValue() == 0) || // X < 0
4498 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4499 CST->isAllOnesValue())) { // X > -1
4500 Tmp1 = LHSHi;
4501 Tmp2 = RHSHi;
4502 break;
4503 }
4504
4505 // FIXME: This generated code sucks.
4506 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004507 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004508 default: assert(0 && "Unknown integer setcc!");
4509 case ISD::SETLT:
4510 case ISD::SETULT: LowCC = ISD::SETULT; break;
4511 case ISD::SETGT:
4512 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4513 case ISD::SETLE:
4514 case ISD::SETULE: LowCC = ISD::SETULE; break;
4515 case ISD::SETGE:
4516 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4517 }
4518
4519 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4520 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4521 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4522
4523 // NOTE: on targets without efficient SELECT of bools, we can always use
4524 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004525 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4526 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4527 false, DagCombineInfo);
4528 if (!Tmp1.Val)
4529 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4530 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4531 CCCode, false, DagCombineInfo);
4532 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004533 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004534
4535 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4536 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4537 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4538 (Tmp2C && Tmp2C->getValue() == 0 &&
4539 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4540 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4541 (Tmp2C && Tmp2C->getValue() == 1 &&
4542 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4543 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4544 // low part is known false, returns high part.
4545 // For LE / GE, if high part is known false, ignore the low part.
4546 // For LT / GT, if high part is known true, ignore the low part.
4547 Tmp1 = Tmp2;
4548 Tmp2 = SDOperand();
4549 } else {
4550 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4551 ISD::SETEQ, false, DagCombineInfo);
4552 if (!Result.Val)
4553 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4554 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4555 Result, Tmp1, Tmp2));
4556 Tmp1 = Result;
4557 Tmp2 = SDOperand();
4558 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004559 }
4560 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004561 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004562 LHS = Tmp1;
4563 RHS = Tmp2;
4564}
4565
Chris Lattner1401d152008-01-16 07:45:30 +00004566/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4567/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4568/// a load from the stack slot to DestVT, extending it if needed.
4569/// The resultant code need not be legal.
4570SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4571 MVT::ValueType SlotVT,
4572 MVT::ValueType DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004573 // Create the stack frame object.
Chris Lattner1401d152008-01-16 07:45:30 +00004574 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4575
4576 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4577 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4578 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004579
Chris Lattner1401d152008-01-16 07:45:30 +00004580 // Emit a store to the stack slot. Use a truncstore if the input value is
4581 // later than DestVT.
4582 SDOperand Store;
4583 if (SrcSize > SlotSize)
4584 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0,SlotVT);
4585 else {
4586 assert(SrcSize == SlotSize && "Invalid store");
4587 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
4588 }
4589
Chris Lattner35481892005-12-23 00:16:34 +00004590 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004591 if (SlotSize == DestSize)
4592 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4593
4594 assert(SlotSize < DestSize && "Unknown extension!");
4595 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner35481892005-12-23 00:16:34 +00004596}
4597
Chris Lattner4352cc92006-04-04 17:23:26 +00004598SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4599 // Create a vector sized/aligned stack slot, store the value to element #0,
4600 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004601 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004602 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004603 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004604 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004605}
4606
4607
Chris Lattnerce872152006-03-19 06:31:19 +00004608/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004609/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004610SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4611
4612 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004613 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004614 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004615 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004616 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004617 std::map<SDOperand, std::vector<unsigned> > Values;
4618 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004619 bool isConstant = true;
4620 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4621 SplatValue.getOpcode() != ISD::UNDEF)
4622 isConstant = false;
4623
Evan Cheng033e6812006-03-24 01:17:21 +00004624 for (unsigned i = 1; i < NumElems; ++i) {
4625 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004626 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004627 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004628 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004629 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004630 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004631
4632 // If this isn't a constant element or an undef, we can't use a constant
4633 // pool load.
4634 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4635 V.getOpcode() != ISD::UNDEF)
4636 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004637 }
4638
4639 if (isOnlyLowElement) {
4640 // If the low element is an undef too, then this whole things is an undef.
4641 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4642 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4643 // Otherwise, turn this into a scalar_to_vector node.
4644 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4645 Node->getOperand(0));
4646 }
4647
Chris Lattner2eb86532006-03-24 07:29:17 +00004648 // If all elements are constants, create a load from the constant pool.
4649 if (isConstant) {
4650 MVT::ValueType VT = Node->getValueType(0);
4651 const Type *OpNTy =
4652 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4653 std::vector<Constant*> CV;
4654 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4655 if (ConstantFPSDNode *V =
4656 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004657 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004658 } else if (ConstantSDNode *V =
4659 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004660 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004661 } else {
4662 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4663 CV.push_back(UndefValue::get(OpNTy));
4664 }
4665 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004666 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004667 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004668 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004669 }
4670
Chris Lattner87100e02006-03-20 01:52:29 +00004671 if (SplatValue.Val) { // Splat of one value?
4672 // Build the shuffle constant vector: <0, 0, 0, 0>
4673 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004674 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004675 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004676 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004677 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4678 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004679
4680 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004681 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004682 // Get the splatted value into the low element of a vector register.
4683 SDOperand LowValVec =
4684 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4685
4686 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4687 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4688 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4689 SplatMask);
4690 }
4691 }
4692
Evan Cheng033e6812006-03-24 01:17:21 +00004693 // If there are only two unique elements, we may be able to turn this into a
4694 // vector shuffle.
4695 if (Values.size() == 2) {
4696 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4697 MVT::ValueType MaskVT =
4698 MVT::getIntVectorWithNumElements(NumElems);
4699 std::vector<SDOperand> MaskVec(NumElems);
4700 unsigned i = 0;
4701 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4702 E = Values.end(); I != E; ++I) {
4703 for (std::vector<unsigned>::iterator II = I->second.begin(),
4704 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004705 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004706 i += NumElems;
4707 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004708 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4709 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004710
4711 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004712 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4713 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004714 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004715 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4716 E = Values.end(); I != E; ++I) {
4717 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4718 I->first);
4719 Ops.push_back(Op);
4720 }
4721 Ops.push_back(ShuffleMask);
4722
4723 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004724 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4725 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004726 }
4727 }
Chris Lattnerce872152006-03-19 06:31:19 +00004728
4729 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4730 // aligned object on the stack, store each element into it, then load
4731 // the result as a vector.
4732 MVT::ValueType VT = Node->getValueType(0);
4733 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004734 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004735
4736 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004737 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004738 unsigned TypeByteSize =
4739 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004740 // Store (in the right endianness) the elements to memory.
4741 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4742 // Ignore undef elements.
4743 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4744
Chris Lattner841c8822006-03-22 01:46:54 +00004745 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004746
4747 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4748 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4749
Evan Cheng786225a2006-10-05 23:01:46 +00004750 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004751 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004752 }
4753
4754 SDOperand StoreChain;
4755 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004756 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4757 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004758 else
4759 StoreChain = DAG.getEntryNode();
4760
4761 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004762 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004763}
4764
Chris Lattner5b359c62005-04-02 04:00:59 +00004765void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4766 SDOperand Op, SDOperand Amt,
4767 SDOperand &Lo, SDOperand &Hi) {
4768 // Expand the subcomponents.
4769 SDOperand LHSL, LHSH;
4770 ExpandOp(Op, LHSL, LHSH);
4771
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004772 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004773 MVT::ValueType VT = LHSL.getValueType();
4774 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004775 Hi = Lo.getValue(1);
4776}
4777
4778
Chris Lattnere34b3962005-01-19 04:19:40 +00004779/// ExpandShift - Try to find a clever way to expand this shift operation out to
4780/// smaller elements. If we can't find a way that is more efficient than a
4781/// libcall on this target, return false. Otherwise, return true with the
4782/// low-parts expanded into Lo and Hi.
4783bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4784 SDOperand &Lo, SDOperand &Hi) {
4785 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4786 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004787
Chris Lattnere34b3962005-01-19 04:19:40 +00004788 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004789 SDOperand ShAmt = LegalizeOp(Amt);
4790 MVT::ValueType ShTy = ShAmt.getValueType();
4791 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4792 unsigned NVTBits = MVT::getSizeInBits(NVT);
4793
Chris Lattner7a3c8552007-10-14 20:35:12 +00004794 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004795 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4796 unsigned Cst = CN->getValue();
4797 // Expand the incoming operand to be shifted, so that we have its parts
4798 SDOperand InL, InH;
4799 ExpandOp(Op, InL, InH);
4800 switch(Opc) {
4801 case ISD::SHL:
4802 if (Cst > VTBits) {
4803 Lo = DAG.getConstant(0, NVT);
4804 Hi = DAG.getConstant(0, NVT);
4805 } else if (Cst > NVTBits) {
4806 Lo = DAG.getConstant(0, NVT);
4807 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004808 } else if (Cst == NVTBits) {
4809 Lo = DAG.getConstant(0, NVT);
4810 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004811 } else {
4812 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4813 Hi = DAG.getNode(ISD::OR, NVT,
4814 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4815 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4816 }
4817 return true;
4818 case ISD::SRL:
4819 if (Cst > VTBits) {
4820 Lo = DAG.getConstant(0, NVT);
4821 Hi = DAG.getConstant(0, NVT);
4822 } else if (Cst > NVTBits) {
4823 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4824 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004825 } else if (Cst == NVTBits) {
4826 Lo = InH;
4827 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004828 } else {
4829 Lo = DAG.getNode(ISD::OR, NVT,
4830 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4831 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4832 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4833 }
4834 return true;
4835 case ISD::SRA:
4836 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004837 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004838 DAG.getConstant(NVTBits-1, ShTy));
4839 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004840 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004841 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004842 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004843 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004844 } else if (Cst == NVTBits) {
4845 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004846 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004847 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004848 } else {
4849 Lo = DAG.getNode(ISD::OR, NVT,
4850 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4851 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4852 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4853 }
4854 return true;
4855 }
4856 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004857
4858 // Okay, the shift amount isn't constant. However, if we can tell that it is
4859 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4860 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004861 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004862
4863 // If we know that the high bit of the shift amount is one, then we can do
4864 // this as a couple of simple shifts.
4865 if (KnownOne & Mask) {
4866 // Mask out the high bit, which we know is set.
4867 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4868 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4869
4870 // Expand the incoming operand to be shifted, so that we have its parts
4871 SDOperand InL, InH;
4872 ExpandOp(Op, InL, InH);
4873 switch(Opc) {
4874 case ISD::SHL:
4875 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4876 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4877 return true;
4878 case ISD::SRL:
4879 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4880 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4881 return true;
4882 case ISD::SRA:
4883 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4884 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4885 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4886 return true;
4887 }
4888 }
4889
4890 // If we know that the high bit of the shift amount is zero, then we can do
4891 // this as a couple of simple shifts.
4892 if (KnownZero & Mask) {
4893 // Compute 32-amt.
4894 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4895 DAG.getConstant(NVTBits, Amt.getValueType()),
4896 Amt);
4897
4898 // Expand the incoming operand to be shifted, so that we have its parts
4899 SDOperand InL, InH;
4900 ExpandOp(Op, InL, InH);
4901 switch(Opc) {
4902 case ISD::SHL:
4903 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4904 Hi = DAG.getNode(ISD::OR, NVT,
4905 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4906 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4907 return true;
4908 case ISD::SRL:
4909 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4910 Lo = DAG.getNode(ISD::OR, NVT,
4911 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4912 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4913 return true;
4914 case ISD::SRA:
4915 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4916 Lo = DAG.getNode(ISD::OR, NVT,
4917 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4918 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4919 return true;
4920 }
4921 }
4922
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004923 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004924}
Chris Lattner77e77a62005-01-21 06:05:23 +00004925
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004926
Chris Lattner77e77a62005-01-21 06:05:23 +00004927// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4928// does not fit into a register, return the lo part and set the hi part to the
4929// by-reg argument. If it does fit into a single register, return the result
4930// and leave the Hi part unset.
4931SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004932 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004933 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4934 // The input chain to this libcall is the entry node of the function.
4935 // Legalizing the call will automatically add the previous call to the
4936 // dependence.
4937 SDOperand InChain = DAG.getEntryNode();
4938
Chris Lattner77e77a62005-01-21 06:05:23 +00004939 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004940 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004941 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4942 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4943 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004944 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004945 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004946 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004947 }
4948 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004949
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004950 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004951 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004952 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004953 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004954 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004955
Chris Lattner6831a812006-02-13 09:18:02 +00004956 // Legalize the call sequence, starting with the chain. This will advance
4957 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4958 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4959 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004960 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004961 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004962 default: assert(0 && "Unknown thing");
4963 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004964 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004965 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004966 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004967 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004968 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004969 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004970 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004971}
4972
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004973
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004974/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4975///
Chris Lattner77e77a62005-01-21 06:05:23 +00004976SDOperand SelectionDAGLegalize::
4977ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004978 assert(getTypeAction(Source.getValueType()) == Expand &&
4979 "This is not an expansion!");
4980 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4981
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004982 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004983 assert(Source.getValueType() == MVT::i64 &&
4984 "This only works for 64-bit -> FP");
4985 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4986 // incoming integer is set. To handle this, we dynamically test to see if
4987 // it is set, and, if so, add a fudge factor.
4988 SDOperand Lo, Hi;
4989 ExpandOp(Source, Lo, Hi);
4990
Chris Lattner66de05b2005-05-13 04:45:13 +00004991 // If this is unsigned, and not supported, first perform the conversion to
4992 // signed, then adjust the result if the sign bit is set.
4993 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4994 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4995
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004996 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4997 DAG.getConstant(0, Hi.getValueType()),
4998 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004999 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5000 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5001 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005002 uint64_t FF = 0x5f800000ULL;
5003 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005004 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005005
Chris Lattner5839bf22005-08-26 17:15:30 +00005006 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005007 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5008 SDOperand FudgeInReg;
5009 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005010 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005011 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005012 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005013 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005014 CPIdx, NULL, 0, MVT::f32);
5015 else
5016 assert(0 && "Unexpected conversion");
5017
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005018 MVT::ValueType SCVT = SignedConv.getValueType();
5019 if (SCVT != DestTy) {
5020 // Destination type needs to be expanded as well. The FADD now we are
5021 // constructing will be expanded into a libcall.
5022 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5023 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5024 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5025 SignedConv, SignedConv.getValue(1));
5026 }
5027 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5028 }
Chris Lattner473a9902005-09-29 06:44:39 +00005029 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005030 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005031
Chris Lattnera88a2602005-05-14 05:33:54 +00005032 // Check to see if the target has a custom way to lower this. If so, use it.
5033 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5034 default: assert(0 && "This action not implemented for this operation!");
5035 case TargetLowering::Legal:
5036 case TargetLowering::Expand:
5037 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005038 case TargetLowering::Custom: {
5039 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5040 Source), DAG);
5041 if (NV.Val)
5042 return LegalizeOp(NV);
5043 break; // The target decided this was legal after all
5044 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005045 }
5046
Chris Lattner13689e22005-05-12 07:00:44 +00005047 // Expand the source, then glue it back together for the call. We must expand
5048 // the source in case it is shared (this pass of legalize must traverse it).
5049 SDOperand SrcLo, SrcHi;
5050 ExpandOp(Source, SrcLo, SrcHi);
5051 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5052
Evan Cheng56966222007-01-12 02:11:51 +00005053 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005054 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005055 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005056 else {
5057 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00005058 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005059 }
Chris Lattner6831a812006-02-13 09:18:02 +00005060
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005061 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005062 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5063 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00005064 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5065 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00005066}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005067
Chris Lattner22cde6a2006-01-28 08:25:58 +00005068/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5069/// INT_TO_FP operation of the specified operand when the target requests that
5070/// we expand it. At this point, we know that the result and operand types are
5071/// legal for the target.
5072SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5073 SDOperand Op0,
5074 MVT::ValueType DestVT) {
5075 if (Op0.getValueType() == MVT::i32) {
5076 // simple 32-bit [signed|unsigned] integer to float/double expansion
5077
Chris Lattner23594d42008-01-16 07:03:22 +00005078 // Get the stack frame index of a 8 byte buffer.
5079 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5080
Chris Lattner22cde6a2006-01-28 08:25:58 +00005081 // word offset constant for Hi/Lo address computation
5082 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5083 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005084 SDOperand Hi = StackSlot;
5085 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5086 if (TLI.isLittleEndian())
5087 std::swap(Hi, Lo);
5088
Chris Lattner22cde6a2006-01-28 08:25:58 +00005089 // if signed map to unsigned space
5090 SDOperand Op0Mapped;
5091 if (isSigned) {
5092 // constant used to invert sign bit (signed to unsigned mapping)
5093 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5094 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5095 } else {
5096 Op0Mapped = Op0;
5097 }
5098 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005099 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005100 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005101 // initial hi portion of constructed double
5102 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5103 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005104 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005105 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005106 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005107 // FP constant to bias correct the final result
5108 SDOperand Bias = DAG.getConstantFP(isSigned ?
5109 BitsToDouble(0x4330000080000000ULL)
5110 : BitsToDouble(0x4330000000000000ULL),
5111 MVT::f64);
5112 // subtract the bias
5113 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5114 // final result
5115 SDOperand Result;
5116 // handle final rounding
5117 if (DestVT == MVT::f64) {
5118 // do nothing
5119 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005120 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5121 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5122 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5123 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005124 }
5125 return Result;
5126 }
5127 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5128 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5129
5130 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5131 DAG.getConstant(0, Op0.getValueType()),
5132 ISD::SETLT);
5133 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5134 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5135 SignSet, Four, Zero);
5136
5137 // If the sign bit of the integer is set, the large number will be treated
5138 // as a negative number. To counteract this, the dynamic code adds an
5139 // offset depending on the data type.
5140 uint64_t FF;
5141 switch (Op0.getValueType()) {
5142 default: assert(0 && "Unsupported integer type!");
5143 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5144 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5145 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5146 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5147 }
5148 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005149 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005150
5151 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5152 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5153 SDOperand FudgeInReg;
5154 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005155 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005156 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005157 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005158 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00005159 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005160 }
5161
5162 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5163}
5164
5165/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5166/// *INT_TO_FP operation of the specified operand when the target requests that
5167/// we promote it. At this point, we know that the result and operand types are
5168/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5169/// operation that takes a larger input.
5170SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5171 MVT::ValueType DestVT,
5172 bool isSigned) {
5173 // First step, figure out the appropriate *INT_TO_FP operation to use.
5174 MVT::ValueType NewInTy = LegalOp.getValueType();
5175
5176 unsigned OpToUse = 0;
5177
5178 // Scan for the appropriate larger type to use.
5179 while (1) {
5180 NewInTy = (MVT::ValueType)(NewInTy+1);
5181 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5182
5183 // If the target supports SINT_TO_FP of this type, use it.
5184 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5185 default: break;
5186 case TargetLowering::Legal:
5187 if (!TLI.isTypeLegal(NewInTy))
5188 break; // Can't use this datatype.
5189 // FALL THROUGH.
5190 case TargetLowering::Custom:
5191 OpToUse = ISD::SINT_TO_FP;
5192 break;
5193 }
5194 if (OpToUse) break;
5195 if (isSigned) continue;
5196
5197 // If the target supports UINT_TO_FP of this type, use it.
5198 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5199 default: break;
5200 case TargetLowering::Legal:
5201 if (!TLI.isTypeLegal(NewInTy))
5202 break; // Can't use this datatype.
5203 // FALL THROUGH.
5204 case TargetLowering::Custom:
5205 OpToUse = ISD::UINT_TO_FP;
5206 break;
5207 }
5208 if (OpToUse) break;
5209
5210 // Otherwise, try a larger type.
5211 }
5212
5213 // Okay, we found the operation and type to use. Zero extend our input to the
5214 // desired type then run the operation on it.
5215 return DAG.getNode(OpToUse, DestVT,
5216 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5217 NewInTy, LegalOp));
5218}
5219
5220/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5221/// FP_TO_*INT operation of the specified operand when the target requests that
5222/// we promote it. At this point, we know that the result and operand types are
5223/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5224/// operation that returns a larger result.
5225SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5226 MVT::ValueType DestVT,
5227 bool isSigned) {
5228 // First step, figure out the appropriate FP_TO*INT operation to use.
5229 MVT::ValueType NewOutTy = DestVT;
5230
5231 unsigned OpToUse = 0;
5232
5233 // Scan for the appropriate larger type to use.
5234 while (1) {
5235 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5236 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5237
5238 // If the target supports FP_TO_SINT returning this type, use it.
5239 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5240 default: break;
5241 case TargetLowering::Legal:
5242 if (!TLI.isTypeLegal(NewOutTy))
5243 break; // Can't use this datatype.
5244 // FALL THROUGH.
5245 case TargetLowering::Custom:
5246 OpToUse = ISD::FP_TO_SINT;
5247 break;
5248 }
5249 if (OpToUse) break;
5250
5251 // If the target supports FP_TO_UINT of this type, use it.
5252 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5253 default: break;
5254 case TargetLowering::Legal:
5255 if (!TLI.isTypeLegal(NewOutTy))
5256 break; // Can't use this datatype.
5257 // FALL THROUGH.
5258 case TargetLowering::Custom:
5259 OpToUse = ISD::FP_TO_UINT;
5260 break;
5261 }
5262 if (OpToUse) break;
5263
5264 // Otherwise, try a larger type.
5265 }
5266
Chris Lattner27a6c732007-11-24 07:07:01 +00005267
5268 // Okay, we found the operation and type to use.
5269 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5270
5271 // If the operation produces an invalid type, it must be custom lowered. Use
5272 // the target lowering hooks to expand it. Just keep the low part of the
5273 // expanded operation, we know that we're truncating anyway.
5274 if (getTypeAction(NewOutTy) == Expand) {
5275 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5276 assert(Operation.Val && "Didn't return anything");
5277 }
5278
5279 // Truncate the result of the extended FP_TO_*INT operation to the desired
5280 // size.
5281 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005282}
5283
5284/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5285///
5286SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5287 MVT::ValueType VT = Op.getValueType();
5288 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5289 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5290 switch (VT) {
5291 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5292 case MVT::i16:
5293 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5294 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5295 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5296 case MVT::i32:
5297 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5298 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5299 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5300 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5301 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5302 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5303 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5304 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5305 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5306 case MVT::i64:
5307 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5308 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5309 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5310 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5311 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5312 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5313 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5314 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5315 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5316 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5317 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5318 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5319 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5320 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5321 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5322 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5323 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5324 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5325 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5326 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5327 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5328 }
5329}
5330
5331/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5332///
5333SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5334 switch (Opc) {
5335 default: assert(0 && "Cannot expand this yet!");
5336 case ISD::CTPOP: {
5337 static const uint64_t mask[6] = {
5338 0x5555555555555555ULL, 0x3333333333333333ULL,
5339 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5340 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5341 };
5342 MVT::ValueType VT = Op.getValueType();
5343 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005344 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005345 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5346 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5347 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5348 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5349 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5350 DAG.getNode(ISD::AND, VT,
5351 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5352 }
5353 return Op;
5354 }
5355 case ISD::CTLZ: {
5356 // for now, we do this:
5357 // x = x | (x >> 1);
5358 // x = x | (x >> 2);
5359 // ...
5360 // x = x | (x >>16);
5361 // x = x | (x >>32); // for 64-bit input
5362 // return popcount(~x);
5363 //
5364 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5365 MVT::ValueType VT = Op.getValueType();
5366 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005367 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005368 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5369 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5370 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5371 }
5372 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5373 return DAG.getNode(ISD::CTPOP, VT, Op);
5374 }
5375 case ISD::CTTZ: {
5376 // for now, we use: { return popcount(~x & (x - 1)); }
5377 // unless the target has ctlz but not ctpop, in which case we use:
5378 // { return 32 - nlz(~x & (x-1)); }
5379 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5380 MVT::ValueType VT = Op.getValueType();
5381 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5382 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5383 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5384 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5385 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5386 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5387 TLI.isOperationLegal(ISD::CTLZ, VT))
5388 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005389 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005390 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5391 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5392 }
5393 }
5394}
Chris Lattnere34b3962005-01-19 04:19:40 +00005395
Chris Lattner3e928bb2005-01-07 07:47:09 +00005396/// ExpandOp - Expand the specified SDOperand into its two component pieces
5397/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5398/// LegalizeNodes map is filled in for any results that are not expanded, the
5399/// ExpandedNodes map is filled in for any results that are expanded, and the
5400/// Lo/Hi values are returned.
5401void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5402 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005403 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005404 SDNode *Node = Op.Val;
5405 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005406 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005407 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005408 "Cannot expand to FP value or to larger int value!");
5409
Chris Lattner6fdcb252005-09-02 20:32:45 +00005410 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005411 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005412 = ExpandedNodes.find(Op);
5413 if (I != ExpandedNodes.end()) {
5414 Lo = I->second.first;
5415 Hi = I->second.second;
5416 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005417 }
5418
Chris Lattner3e928bb2005-01-07 07:47:09 +00005419 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005420 case ISD::CopyFromReg:
5421 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005422 case ISD::FP_ROUND_INREG:
5423 if (VT == MVT::ppcf128 &&
5424 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5425 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005426 SDOperand SrcLo, SrcHi, Src;
5427 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5428 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5429 SDOperand Result = TLI.LowerOperation(
5430 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005431 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5432 Lo = Result.Val->getOperand(0);
5433 Hi = Result.Val->getOperand(1);
5434 break;
5435 }
5436 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005437 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005438#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005439 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005440#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005441 assert(0 && "Do not know how to expand this operator!");
5442 abort();
Dale Johannesen25f1d082007-10-31 00:32:36 +00005443 case ISD::EXTRACT_VECTOR_ELT:
5444 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5445 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5446 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5447 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005448 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005449 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005450 Lo = DAG.getNode(ISD::UNDEF, NVT);
5451 Hi = DAG.getNode(ISD::UNDEF, NVT);
5452 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005453 case ISD::Constant: {
5454 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5455 Lo = DAG.getConstant(Cst, NVT);
5456 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5457 break;
5458 }
Evan Cheng00495212006-12-12 21:32:44 +00005459 case ISD::ConstantFP: {
5460 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005461 if (CFP->getValueType(0) == MVT::ppcf128) {
5462 APInt api = CFP->getValueAPF().convertToAPInt();
5463 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5464 MVT::f64);
5465 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5466 MVT::f64);
5467 break;
5468 }
Evan Cheng279101e2006-12-12 22:19:28 +00005469 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005470 if (getTypeAction(Lo.getValueType()) == Expand)
5471 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005472 break;
5473 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005474 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005475 // Return the operands.
5476 Lo = Node->getOperand(0);
5477 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005478 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005479
5480 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005481 if (Node->getNumValues() == 1) {
5482 ExpandOp(Op.getOperand(0), Lo, Hi);
5483 break;
5484 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005485 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5486 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5487 Op.getValue(1).getValueType() == MVT::Other &&
5488 "unhandled MERGE_VALUES");
5489 ExpandOp(Op.getOperand(0), Lo, Hi);
5490 // Remember that we legalized the chain.
5491 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5492 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005493
5494 case ISD::SIGN_EXTEND_INREG:
5495 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005496 // sext_inreg the low part if needed.
5497 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5498
5499 // The high part gets the sign extension from the lo-part. This handles
5500 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005501 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5502 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5503 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005504 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005505
Nate Begemand88fc032006-01-14 03:14:10 +00005506 case ISD::BSWAP: {
5507 ExpandOp(Node->getOperand(0), Lo, Hi);
5508 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5509 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5510 Lo = TempLo;
5511 break;
5512 }
5513
Chris Lattneredb1add2005-05-11 04:51:16 +00005514 case ISD::CTPOP:
5515 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005516 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5517 DAG.getNode(ISD::CTPOP, NVT, Lo),
5518 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005519 Hi = DAG.getConstant(0, NVT);
5520 break;
5521
Chris Lattner39a8f332005-05-12 19:05:01 +00005522 case ISD::CTLZ: {
5523 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005524 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005525 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5526 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005527 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5528 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005529 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5530 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5531
5532 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5533 Hi = DAG.getConstant(0, NVT);
5534 break;
5535 }
5536
5537 case ISD::CTTZ: {
5538 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005539 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005540 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5541 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005542 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5543 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005544 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5545 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5546
5547 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5548 Hi = DAG.getConstant(0, NVT);
5549 break;
5550 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005551
Nate Begemanacc398c2006-01-25 18:21:52 +00005552 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005553 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5554 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005555 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5556 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5557
5558 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005559 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005560 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5561 if (!TLI.isLittleEndian())
5562 std::swap(Lo, Hi);
5563 break;
5564 }
5565
Chris Lattner3e928bb2005-01-07 07:47:09 +00005566 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005567 LoadSDNode *LD = cast<LoadSDNode>(Node);
5568 SDOperand Ch = LD->getChain(); // Legalize the chain.
5569 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5570 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005571 int SVOffset = LD->getSrcValueOffset();
5572 unsigned Alignment = LD->getAlignment();
5573 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005574
Evan Cheng466685d2006-10-09 20:57:25 +00005575 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005576 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5577 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005578 if (VT == MVT::f32 || VT == MVT::f64) {
5579 // f32->i32 or f64->i64 one to one expansion.
5580 // Remember that we legalized the chain.
5581 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005582 // Recursively expand the new load.
5583 if (getTypeAction(NVT) == Expand)
5584 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005585 break;
5586 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005587
Evan Cheng466685d2006-10-09 20:57:25 +00005588 // Increment the pointer to the other half.
5589 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5590 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5591 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005592 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005593 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005594 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5595 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005596
Evan Cheng466685d2006-10-09 20:57:25 +00005597 // Build a factor node to remember that this load is independent of the
5598 // other one.
5599 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5600 Hi.getValue(1));
5601
5602 // Remember that we legalized the chain.
5603 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5604 if (!TLI.isLittleEndian())
5605 std::swap(Lo, Hi);
5606 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005607 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005608
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005609 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5610 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005611 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5612 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005613 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005614 // Remember that we legalized the chain.
5615 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5616 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5617 break;
5618 }
Evan Cheng466685d2006-10-09 20:57:25 +00005619
5620 if (EVT == NVT)
5621 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005622 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005623 else
5624 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005625 SVOffset, EVT, isVolatile,
5626 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005627
5628 // Remember that we legalized the chain.
5629 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5630
5631 if (ExtType == ISD::SEXTLOAD) {
5632 // The high part is obtained by SRA'ing all but one of the bits of the
5633 // lo part.
5634 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5635 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5636 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5637 } else if (ExtType == ISD::ZEXTLOAD) {
5638 // The high part is just a zero.
5639 Hi = DAG.getConstant(0, NVT);
5640 } else /* if (ExtType == ISD::EXTLOAD) */ {
5641 // The high part is undefined.
5642 Hi = DAG.getNode(ISD::UNDEF, NVT);
5643 }
5644 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005645 break;
5646 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005647 case ISD::AND:
5648 case ISD::OR:
5649 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5650 SDOperand LL, LH, RL, RH;
5651 ExpandOp(Node->getOperand(0), LL, LH);
5652 ExpandOp(Node->getOperand(1), RL, RH);
5653 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5654 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5655 break;
5656 }
5657 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005658 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005659 ExpandOp(Node->getOperand(1), LL, LH);
5660 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005661 if (getTypeAction(NVT) == Expand)
5662 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005663 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005664 if (VT != MVT::f32)
5665 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005666 break;
5667 }
Nate Begeman9373a812005-08-10 20:51:12 +00005668 case ISD::SELECT_CC: {
5669 SDOperand TL, TH, FL, FH;
5670 ExpandOp(Node->getOperand(2), TL, TH);
5671 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005672 if (getTypeAction(NVT) == Expand)
5673 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005674 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5675 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005676 if (VT != MVT::f32)
5677 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5678 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005679 break;
5680 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005681 case ISD::ANY_EXTEND:
5682 // The low part is any extension of the input (which degenerates to a copy).
5683 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5684 // The high part is undefined.
5685 Hi = DAG.getNode(ISD::UNDEF, NVT);
5686 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005687 case ISD::SIGN_EXTEND: {
5688 // The low part is just a sign extension of the input (which degenerates to
5689 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005690 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005691
Chris Lattner3e928bb2005-01-07 07:47:09 +00005692 // The high part is obtained by SRA'ing all but one of the bits of the lo
5693 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005694 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005695 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5696 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005697 break;
5698 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005699 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005700 // The low part is just a zero extension of the input (which degenerates to
5701 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005702 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005703
Chris Lattner3e928bb2005-01-07 07:47:09 +00005704 // The high part is just a zero.
5705 Hi = DAG.getConstant(0, NVT);
5706 break;
Chris Lattner35481892005-12-23 00:16:34 +00005707
Chris Lattner4c948eb2007-02-13 23:55:16 +00005708 case ISD::TRUNCATE: {
5709 // The input value must be larger than this value. Expand *it*.
5710 SDOperand NewLo;
5711 ExpandOp(Node->getOperand(0), NewLo, Hi);
5712
5713 // The low part is now either the right size, or it is closer. If not the
5714 // right size, make an illegal truncate so we recursively expand it.
5715 if (NewLo.getValueType() != Node->getValueType(0))
5716 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5717 ExpandOp(NewLo, Lo, Hi);
5718 break;
5719 }
5720
Chris Lattner35481892005-12-23 00:16:34 +00005721 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005722 SDOperand Tmp;
5723 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5724 // If the target wants to, allow it to lower this itself.
5725 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5726 case Expand: assert(0 && "cannot expand FP!");
5727 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5728 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5729 }
5730 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5731 }
5732
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005733 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005734 if (VT == MVT::f32 || VT == MVT::f64) {
5735 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005736 if (getTypeAction(NVT) == Expand)
5737 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005738 break;
5739 }
5740
5741 // If source operand will be expanded to the same type as VT, i.e.
5742 // i64 <- f64, i32 <- f32, expand the source operand instead.
5743 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5744 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5745 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005746 break;
5747 }
5748
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005749 // Turn this into a load/store pair by default.
5750 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00005751 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005752
Chris Lattner35481892005-12-23 00:16:34 +00005753 ExpandOp(Tmp, Lo, Hi);
5754 break;
5755 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005756
Chris Lattner27a6c732007-11-24 07:07:01 +00005757 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00005758 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5759 TargetLowering::Custom &&
5760 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00005761 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5762 assert(Tmp.Val && "Node must be custom expanded!");
5763 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00005764 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00005765 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005766 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005767 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005768
Chris Lattner4e6c7462005-01-08 19:27:05 +00005769 // These operators cannot be expanded directly, emit them as calls to
5770 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005771 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005772 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005773 SDOperand Op;
5774 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5775 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005776 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5777 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005778 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005779
Chris Lattnerf20d1832005-07-30 01:40:57 +00005780 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5781
Chris Lattner80a3e942005-07-29 00:33:32 +00005782 // Now that the custom expander is done, expand the result, which is still
5783 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005784 if (Op.Val) {
5785 ExpandOp(Op, Lo, Hi);
5786 break;
5787 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005788 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005789
Dale Johannesen161e8972007-10-05 20:04:43 +00005790 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005791 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005792 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005793 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005794 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005795 else if (Node->getOperand(0).getValueType() == MVT::f80)
5796 LC = RTLIB::FPTOSINT_F80_I64;
5797 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5798 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005799 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5800 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005801 break;
Evan Cheng56966222007-01-12 02:11:51 +00005802 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005803
Evan Cheng56966222007-01-12 02:11:51 +00005804 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005805 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005806 SDOperand Op;
5807 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5808 case Expand: assert(0 && "cannot expand FP!");
5809 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5810 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5811 }
5812
5813 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5814
5815 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005816 if (Op.Val) {
5817 ExpandOp(Op, Lo, Hi);
5818 break;
5819 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005820 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005821
Evan Chengdaccea182007-10-05 01:09:32 +00005822 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005823 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005824 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005825 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005826 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005827 else if (Node->getOperand(0).getValueType() == MVT::f80)
5828 LC = RTLIB::FPTOUINT_F80_I64;
5829 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5830 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005831 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5832 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005833 break;
Evan Cheng56966222007-01-12 02:11:51 +00005834 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005835
Evan Cheng05a2d562006-01-09 18:31:59 +00005836 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005837 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005838 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005839 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005840 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005841 Op = TLI.LowerOperation(Op, DAG);
5842 if (Op.Val) {
5843 // Now that the custom expander is done, expand the result, which is
5844 // still VT.
5845 ExpandOp(Op, Lo, Hi);
5846 break;
5847 }
5848 }
5849
Chris Lattner79980b02006-09-13 03:50:39 +00005850 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5851 // this X << 1 as X+X.
5852 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5853 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5854 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5855 SDOperand LoOps[2], HiOps[3];
5856 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5857 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5858 LoOps[1] = LoOps[0];
5859 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5860
5861 HiOps[1] = HiOps[0];
5862 HiOps[2] = Lo.getValue(1);
5863 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5864 break;
5865 }
5866 }
5867
Chris Lattnere34b3962005-01-19 04:19:40 +00005868 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005869 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005870 break;
Chris Lattner47599822005-04-02 03:38:53 +00005871
5872 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005873 TargetLowering::LegalizeAction Action =
5874 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5875 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5876 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005877 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005878 break;
5879 }
5880
Chris Lattnere34b3962005-01-19 04:19:40 +00005881 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005882 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5883 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005884 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005885 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005886
Evan Cheng05a2d562006-01-09 18:31:59 +00005887 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005888 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005889 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005890 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005891 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005892 Op = TLI.LowerOperation(Op, DAG);
5893 if (Op.Val) {
5894 // Now that the custom expander is done, expand the result, which is
5895 // still VT.
5896 ExpandOp(Op, Lo, Hi);
5897 break;
5898 }
5899 }
5900
Chris Lattnere34b3962005-01-19 04:19:40 +00005901 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005902 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005903 break;
Chris Lattner47599822005-04-02 03:38:53 +00005904
5905 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005906 TargetLowering::LegalizeAction Action =
5907 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5908 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5909 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005910 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005911 break;
5912 }
5913
Chris Lattnere34b3962005-01-19 04:19:40 +00005914 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005915 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5916 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005917 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005918 }
5919
5920 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005921 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005922 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005923 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005924 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005925 Op = TLI.LowerOperation(Op, DAG);
5926 if (Op.Val) {
5927 // Now that the custom expander is done, expand the result, which is
5928 // still VT.
5929 ExpandOp(Op, Lo, Hi);
5930 break;
5931 }
5932 }
5933
Chris Lattnere34b3962005-01-19 04:19:40 +00005934 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005935 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005936 break;
Chris Lattner47599822005-04-02 03:38:53 +00005937
5938 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005939 TargetLowering::LegalizeAction Action =
5940 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5941 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5942 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005943 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005944 break;
5945 }
5946
Chris Lattnere34b3962005-01-19 04:19:40 +00005947 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005948 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5949 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005950 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005951 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005952
Misha Brukmanedf128a2005-04-21 22:36:52 +00005953 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005954 case ISD::SUB: {
5955 // If the target wants to custom expand this, let them.
5956 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5957 TargetLowering::Custom) {
5958 Op = TLI.LowerOperation(Op, DAG);
5959 if (Op.Val) {
5960 ExpandOp(Op, Lo, Hi);
5961 break;
5962 }
5963 }
5964
5965 // Expand the subcomponents.
5966 SDOperand LHSL, LHSH, RHSL, RHSH;
5967 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5968 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005969 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5970 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005971 LoOps[0] = LHSL;
5972 LoOps[1] = RHSL;
5973 HiOps[0] = LHSH;
5974 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005975 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005976 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005977 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005978 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005979 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005980 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005981 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005982 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005983 }
Chris Lattner84f67882005-01-20 18:52:28 +00005984 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005985 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005986
5987 case ISD::ADDC:
5988 case ISD::SUBC: {
5989 // Expand the subcomponents.
5990 SDOperand LHSL, LHSH, RHSL, RHSH;
5991 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5992 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5993 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5994 SDOperand LoOps[2] = { LHSL, RHSL };
5995 SDOperand HiOps[3] = { LHSH, RHSH };
5996
5997 if (Node->getOpcode() == ISD::ADDC) {
5998 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5999 HiOps[2] = Lo.getValue(1);
6000 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6001 } else {
6002 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6003 HiOps[2] = Lo.getValue(1);
6004 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6005 }
6006 // Remember that we legalized the flag.
6007 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6008 break;
6009 }
6010 case ISD::ADDE:
6011 case ISD::SUBE: {
6012 // Expand the subcomponents.
6013 SDOperand LHSL, LHSH, RHSL, RHSH;
6014 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6015 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6016 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6017 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6018 SDOperand HiOps[3] = { LHSH, RHSH };
6019
6020 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6021 HiOps[2] = Lo.getValue(1);
6022 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6023
6024 // Remember that we legalized the flag.
6025 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6026 break;
6027 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006028 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006029 // If the target wants to custom expand this, let them.
6030 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006031 SDOperand New = TLI.LowerOperation(Op, DAG);
6032 if (New.Val) {
6033 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006034 break;
6035 }
6036 }
6037
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006038 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6039 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006040 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6041 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6042 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006043 SDOperand LL, LH, RL, RH;
6044 ExpandOp(Node->getOperand(0), LL, LH);
6045 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00006046 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
6047 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6048 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6049 // FIXME: generalize this to handle other bit sizes
6050 if (LHSSB == 32 && RHSSB == 32 &&
6051 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
6052 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
6053 // The inputs are both zero-extended.
6054 if (HasUMUL_LOHI) {
6055 // We can emit a umul_lohi.
6056 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6057 Hi = SDOperand(Lo.Val, 1);
6058 break;
6059 }
6060 if (HasMULHU) {
6061 // We can emit a mulhu+mul.
6062 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6063 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6064 break;
6065 }
Dan Gohman525178c2007-10-08 18:33:35 +00006066 }
6067 if (LHSSB > BitSize && RHSSB > BitSize) {
6068 // The input values are both sign-extended.
6069 if (HasSMUL_LOHI) {
6070 // We can emit a smul_lohi.
6071 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6072 Hi = SDOperand(Lo.Val, 1);
6073 break;
6074 }
6075 if (HasMULHS) {
6076 // We can emit a mulhs+mul.
6077 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6078 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6079 break;
6080 }
6081 }
6082 if (HasUMUL_LOHI) {
6083 // Lo,Hi = umul LHS, RHS.
6084 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6085 DAG.getVTList(NVT, NVT), LL, RL);
6086 Lo = UMulLOHI;
6087 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006088 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6089 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6090 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6091 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006092 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006093 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006094 if (HasMULHU) {
6095 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6096 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6097 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6098 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6099 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6100 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6101 break;
6102 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006103 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006104
Dan Gohman525178c2007-10-08 18:33:35 +00006105 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006106 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6107 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006108 break;
6109 }
Evan Cheng56966222007-01-12 02:11:51 +00006110 case ISD::SDIV:
6111 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6112 break;
6113 case ISD::UDIV:
6114 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6115 break;
6116 case ISD::SREM:
6117 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6118 break;
6119 case ISD::UREM:
6120 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6121 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006122
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006123 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006124 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6125 RTLIB::ADD_F64,
6126 RTLIB::ADD_F80,
6127 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006128 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006129 break;
6130 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006131 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6132 RTLIB::SUB_F64,
6133 RTLIB::SUB_F80,
6134 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006135 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006136 break;
6137 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006138 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6139 RTLIB::MUL_F64,
6140 RTLIB::MUL_F80,
6141 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006142 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006143 break;
6144 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006145 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6146 RTLIB::DIV_F64,
6147 RTLIB::DIV_F80,
6148 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006149 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006150 break;
6151 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006152 if (VT == MVT::ppcf128) {
6153 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6154 Node->getOperand(0).getValueType()==MVT::f64);
6155 const uint64_t zero = 0;
6156 if (Node->getOperand(0).getValueType()==MVT::f32)
6157 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6158 else
6159 Hi = Node->getOperand(0);
6160 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6161 break;
6162 }
Evan Cheng56966222007-01-12 02:11:51 +00006163 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006164 break;
6165 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006166 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006167 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006168 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006169 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6170 RTLIB::POWI_F64,
6171 RTLIB::POWI_F80,
6172 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006173 Node, false, Hi);
6174 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006175 case ISD::FSQRT:
6176 case ISD::FSIN:
6177 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006178 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006179 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006180 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006181 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6182 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006183 break;
6184 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006185 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6186 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006187 break;
6188 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006189 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6190 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006191 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006192 default: assert(0 && "Unreachable!");
6193 }
Evan Cheng56966222007-01-12 02:11:51 +00006194 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006195 break;
6196 }
Evan Cheng966bf242006-12-16 00:52:40 +00006197 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006198 if (VT == MVT::ppcf128) {
6199 SDOperand Tmp;
6200 ExpandOp(Node->getOperand(0), Lo, Tmp);
6201 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6202 // lo = hi==fabs(hi) ? lo : -lo;
6203 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6204 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6205 DAG.getCondCode(ISD::SETEQ));
6206 break;
6207 }
Evan Cheng966bf242006-12-16 00:52:40 +00006208 SDOperand Mask = (VT == MVT::f64)
6209 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6210 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6211 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6212 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6213 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6214 if (getTypeAction(NVT) == Expand)
6215 ExpandOp(Lo, Lo, Hi);
6216 break;
6217 }
6218 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006219 if (VT == MVT::ppcf128) {
6220 ExpandOp(Node->getOperand(0), Lo, Hi);
6221 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6222 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6223 break;
6224 }
Evan Cheng966bf242006-12-16 00:52:40 +00006225 SDOperand Mask = (VT == MVT::f64)
6226 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6227 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6228 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6229 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6230 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6231 if (getTypeAction(NVT) == Expand)
6232 ExpandOp(Lo, Lo, Hi);
6233 break;
6234 }
Evan Cheng912095b2007-01-04 21:56:39 +00006235 case ISD::FCOPYSIGN: {
6236 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6237 if (getTypeAction(NVT) == Expand)
6238 ExpandOp(Lo, Lo, Hi);
6239 break;
6240 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006241 case ISD::SINT_TO_FP:
6242 case ISD::UINT_TO_FP: {
6243 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6244 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006245 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006246 static uint64_t zero = 0;
6247 if (isSigned) {
6248 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6249 Node->getOperand(0)));
6250 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6251 } else {
6252 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6253 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6254 Node->getOperand(0)));
6255 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6256 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006257 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006258 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6259 DAG.getConstant(0, MVT::i32),
6260 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6261 DAG.getConstantFP(
6262 APFloat(APInt(128, 2, TwoE32)),
6263 MVT::ppcf128)),
6264 Hi,
6265 DAG.getCondCode(ISD::SETLT)),
6266 Lo, Hi);
6267 }
6268 break;
6269 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006270 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6271 // si64->ppcf128 done by libcall, below
6272 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6273 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6274 Lo, Hi);
6275 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6276 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6277 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6278 DAG.getConstant(0, MVT::i64),
6279 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6280 DAG.getConstantFP(
6281 APFloat(APInt(128, 2, TwoE64)),
6282 MVT::ppcf128)),
6283 Hi,
6284 DAG.getCondCode(ISD::SETLT)),
6285 Lo, Hi);
6286 break;
6287 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006288 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006289 if (Node->getOperand(0).getValueType() == MVT::i64) {
6290 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006291 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006292 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006293 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006294 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006295 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006296 LC = RTLIB::SINTTOFP_I64_F80;
6297 }
6298 else if (VT == MVT::ppcf128) {
6299 assert(isSigned);
6300 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006301 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006302 } else {
6303 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006304 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006305 else
Evan Cheng56966222007-01-12 02:11:51 +00006306 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006307 }
6308
6309 // Promote the operand if needed.
6310 if (getTypeAction(SrcVT) == Promote) {
6311 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6312 Tmp = isSigned
6313 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6314 DAG.getValueType(SrcVT))
6315 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6316 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6317 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006318
6319 const char *LibCall = TLI.getLibcallName(LC);
6320 if (LibCall)
6321 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6322 else {
6323 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6324 Node->getOperand(0));
6325 if (getTypeAction(Lo.getValueType()) == Expand)
6326 ExpandOp(Lo, Lo, Hi);
6327 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006328 break;
6329 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006330 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006331
Chris Lattner83397362005-12-21 18:02:52 +00006332 // Make sure the resultant values have been legalized themselves, unless this
6333 // is a type that requires multi-step expansion.
6334 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6335 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006336 if (Hi.Val)
6337 // Don't legalize the high part if it is expanded to a single node.
6338 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006339 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006340
6341 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006342 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006343 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006344}
6345
Dan Gohman7f321562007-06-25 16:23:39 +00006346/// SplitVectorOp - Given an operand of vector type, break it down into
6347/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006348void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6349 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006350 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006351 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006352 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006353 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006354
Dan Gohmane40c7b02007-09-24 15:54:53 +00006355 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006356
6357 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6358 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6359
6360 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6361 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6362
Chris Lattnerc7029802006-03-18 01:44:44 +00006363 // See if we already split it.
6364 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6365 = SplitNodes.find(Op);
6366 if (I != SplitNodes.end()) {
6367 Lo = I->second.first;
6368 Hi = I->second.second;
6369 return;
6370 }
6371
6372 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006373 default:
6374#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006375 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006376#endif
6377 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006378 case ISD::UNDEF:
6379 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6380 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6381 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006382 case ISD::BUILD_PAIR:
6383 Lo = Node->getOperand(0);
6384 Hi = Node->getOperand(1);
6385 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006386 case ISD::INSERT_VECTOR_ELT: {
6387 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6388 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6389 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006390 if (Index < NewNumElts_Lo)
6391 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006392 DAG.getConstant(Index, TLI.getPointerTy()));
6393 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006394 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6395 DAG.getConstant(Index - NewNumElts_Lo,
6396 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006397 break;
6398 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006399 case ISD::VECTOR_SHUFFLE: {
6400 // Build the low part.
6401 SDOperand Mask = Node->getOperand(2);
6402 SmallVector<SDOperand, 8> Ops;
6403 MVT::ValueType PtrVT = TLI.getPointerTy();
6404
6405 // Insert all of the elements from the input that are needed. We use
6406 // buildvector of extractelement here because the input vectors will have
6407 // to be legalized, so this makes the code simpler.
6408 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6409 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6410 SDOperand InVec = Node->getOperand(0);
6411 if (Idx >= NumElements) {
6412 InVec = Node->getOperand(1);
6413 Idx -= NumElements;
6414 }
6415 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6416 DAG.getConstant(Idx, PtrVT)));
6417 }
6418 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6419 Ops.clear();
6420
6421 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6422 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6423 SDOperand InVec = Node->getOperand(0);
6424 if (Idx >= NumElements) {
6425 InVec = Node->getOperand(1);
6426 Idx -= NumElements;
6427 }
6428 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6429 DAG.getConstant(Idx, PtrVT)));
6430 }
6431 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6432 break;
6433 }
Dan Gohman7f321562007-06-25 16:23:39 +00006434 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006435 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006436 Node->op_begin()+NewNumElts_Lo);
6437 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006438
Nate Begeman5db1afb2007-11-15 21:15:26 +00006439 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006440 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006441 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006442 break;
6443 }
Dan Gohman7f321562007-06-25 16:23:39 +00006444 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006445 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006446 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6447 if (NewNumSubvectors == 1) {
6448 Lo = Node->getOperand(0);
6449 Hi = Node->getOperand(1);
6450 } else {
6451 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6452 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006453 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006454
Dan Gohman7f321562007-06-25 16:23:39 +00006455 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6456 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006457 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006458 }
Dan Gohman65956352007-06-13 15:12:02 +00006459 break;
6460 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006461 case ISD::SELECT: {
6462 SDOperand Cond = Node->getOperand(0);
6463
6464 SDOperand LL, LH, RL, RH;
6465 SplitVectorOp(Node->getOperand(1), LL, LH);
6466 SplitVectorOp(Node->getOperand(2), RL, RH);
6467
6468 if (MVT::isVector(Cond.getValueType())) {
6469 // Handle a vector merge.
6470 SDOperand CL, CH;
6471 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006472 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6473 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006474 } else {
6475 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006476 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6477 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006478 }
6479 break;
6480 }
Dan Gohman7f321562007-06-25 16:23:39 +00006481 case ISD::ADD:
6482 case ISD::SUB:
6483 case ISD::MUL:
6484 case ISD::FADD:
6485 case ISD::FSUB:
6486 case ISD::FMUL:
6487 case ISD::SDIV:
6488 case ISD::UDIV:
6489 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006490 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006491 case ISD::AND:
6492 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006493 case ISD::XOR:
6494 case ISD::UREM:
6495 case ISD::SREM:
6496 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006497 SDOperand LL, LH, RL, RH;
6498 SplitVectorOp(Node->getOperand(0), LL, LH);
6499 SplitVectorOp(Node->getOperand(1), RL, RH);
6500
Nate Begeman5db1afb2007-11-15 21:15:26 +00006501 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6502 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006503 break;
6504 }
Dan Gohman82669522007-10-11 23:57:53 +00006505 case ISD::FPOWI: {
6506 SDOperand L, H;
6507 SplitVectorOp(Node->getOperand(0), L, H);
6508
Nate Begeman5db1afb2007-11-15 21:15:26 +00006509 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6510 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006511 break;
6512 }
6513 case ISD::CTTZ:
6514 case ISD::CTLZ:
6515 case ISD::CTPOP:
6516 case ISD::FNEG:
6517 case ISD::FABS:
6518 case ISD::FSQRT:
6519 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006520 case ISD::FCOS:
6521 case ISD::FP_TO_SINT:
6522 case ISD::FP_TO_UINT:
6523 case ISD::SINT_TO_FP:
6524 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006525 SDOperand L, H;
6526 SplitVectorOp(Node->getOperand(0), L, H);
6527
Nate Begeman5db1afb2007-11-15 21:15:26 +00006528 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6529 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006530 break;
6531 }
Dan Gohman7f321562007-06-25 16:23:39 +00006532 case ISD::LOAD: {
6533 LoadSDNode *LD = cast<LoadSDNode>(Node);
6534 SDOperand Ch = LD->getChain();
6535 SDOperand Ptr = LD->getBasePtr();
6536 const Value *SV = LD->getSrcValue();
6537 int SVOffset = LD->getSrcValueOffset();
6538 unsigned Alignment = LD->getAlignment();
6539 bool isVolatile = LD->isVolatile();
6540
Nate Begeman5db1afb2007-11-15 21:15:26 +00006541 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6542 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006543 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6544 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006545 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006546 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006547 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006548
6549 // Build a factor node to remember that this load is independent of the
6550 // other one.
6551 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6552 Hi.getValue(1));
6553
6554 // Remember that we legalized the chain.
6555 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006556 break;
6557 }
Dan Gohman7f321562007-06-25 16:23:39 +00006558 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006559 // We know the result is a vector. The input may be either a vector or a
6560 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006561 SDOperand InOp = Node->getOperand(0);
6562 if (!MVT::isVector(InOp.getValueType()) ||
6563 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6564 // The input is a scalar or single-element vector.
6565 // Lower to a store/load so that it can be split.
6566 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006567 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006568
Evan Cheng786225a2006-10-05 23:01:46 +00006569 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006570 InOp, Ptr, NULL, 0);
6571 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006572 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006573 // Split the vector and convert each of the pieces now.
6574 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006575 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6576 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006577 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006578 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006579 }
6580
6581 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006582 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006583 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006584 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006585}
6586
6587
Dan Gohman89b20c02007-06-27 14:06:22 +00006588/// ScalarizeVectorOp - Given an operand of single-element vector type
6589/// (e.g. v1f32), convert it into the equivalent operation that returns a
6590/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006591SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6592 assert(MVT::isVector(Op.getValueType()) &&
6593 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006594 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006595 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6596 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006597
Dan Gohman7f321562007-06-25 16:23:39 +00006598 // See if we already scalarized it.
6599 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6600 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006601
6602 SDOperand Result;
6603 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006604 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006605#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006606 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006607#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006608 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6609 case ISD::ADD:
6610 case ISD::FADD:
6611 case ISD::SUB:
6612 case ISD::FSUB:
6613 case ISD::MUL:
6614 case ISD::FMUL:
6615 case ISD::SDIV:
6616 case ISD::UDIV:
6617 case ISD::FDIV:
6618 case ISD::SREM:
6619 case ISD::UREM:
6620 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006621 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006622 case ISD::AND:
6623 case ISD::OR:
6624 case ISD::XOR:
6625 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006626 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006627 ScalarizeVectorOp(Node->getOperand(0)),
6628 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006629 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006630 case ISD::FNEG:
6631 case ISD::FABS:
6632 case ISD::FSQRT:
6633 case ISD::FSIN:
6634 case ISD::FCOS:
6635 Result = DAG.getNode(Node->getOpcode(),
6636 NewVT,
6637 ScalarizeVectorOp(Node->getOperand(0)));
6638 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006639 case ISD::FPOWI:
6640 Result = DAG.getNode(Node->getOpcode(),
6641 NewVT,
6642 ScalarizeVectorOp(Node->getOperand(0)),
6643 Node->getOperand(1));
6644 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006645 case ISD::LOAD: {
6646 LoadSDNode *LD = cast<LoadSDNode>(Node);
6647 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6648 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006649
Dan Gohman7f321562007-06-25 16:23:39 +00006650 const Value *SV = LD->getSrcValue();
6651 int SVOffset = LD->getSrcValueOffset();
6652 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6653 LD->isVolatile(), LD->getAlignment());
6654
Chris Lattnerc7029802006-03-18 01:44:44 +00006655 // Remember that we legalized the chain.
6656 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6657 break;
6658 }
Dan Gohman7f321562007-06-25 16:23:39 +00006659 case ISD::BUILD_VECTOR:
6660 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006661 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006662 case ISD::INSERT_VECTOR_ELT:
6663 // Returning the inserted scalar element.
6664 Result = Node->getOperand(1);
6665 break;
6666 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006667 assert(Node->getOperand(0).getValueType() == NewVT &&
6668 "Concat of non-legal vectors not yet supported!");
6669 Result = Node->getOperand(0);
6670 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006671 case ISD::VECTOR_SHUFFLE: {
6672 // Figure out if the scalar is the LHS or RHS and return it.
6673 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6674 if (cast<ConstantSDNode>(EltNum)->getValue())
6675 Result = ScalarizeVectorOp(Node->getOperand(1));
6676 else
6677 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006678 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006679 }
6680 case ISD::EXTRACT_SUBVECTOR:
6681 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006682 assert(Result.getValueType() == NewVT);
6683 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006684 case ISD::BIT_CONVERT:
6685 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006686 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006687 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006688 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006689 ScalarizeVectorOp(Op.getOperand(1)),
6690 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006691 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006692 }
6693
6694 if (TLI.isTypeLegal(NewVT))
6695 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006696 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6697 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006698 return Result;
6699}
6700
Chris Lattner3e928bb2005-01-07 07:47:09 +00006701
6702// SelectionDAG::Legalize - This is the entry point for the file.
6703//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006704void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006705 if (ViewLegalizeDAGs) viewGraph();
6706
Chris Lattner3e928bb2005-01-07 07:47:09 +00006707 /// run - This is the main entry point to this class.
6708 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006709 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006710}
6711