blob: 54cfad69309e11afd0fd2f768c844954586c4cdc [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 Lattner3e928bb2005-01-07 07:47:09 +0000223};
224}
225
Chris Lattner4352cc92006-04-04 17:23:26 +0000226/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
227/// specified mask and type. Targets can specify exactly which masks they
228/// support and the code generator is tasked with not creating illegal masks.
229///
230/// Note that this will also return true for shuffles that are promoted to a
231/// different type.
232SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
233 SDOperand Mask) const {
234 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
235 default: return 0;
236 case TargetLowering::Legal:
237 case TargetLowering::Custom:
238 break;
239 case TargetLowering::Promote: {
240 // If this is promoted to a different type, convert the shuffle mask and
241 // ask if it is legal in the promoted type!
242 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
243
244 // If we changed # elements, change the shuffle mask.
245 unsigned NumEltsGrowth =
246 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
247 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
248 if (NumEltsGrowth > 1) {
249 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000250 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000251 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
252 SDOperand InOp = Mask.getOperand(i);
253 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
254 if (InOp.getOpcode() == ISD::UNDEF)
255 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
256 else {
257 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
258 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
259 }
260 }
261 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000262 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000263 }
264 VT = NVT;
265 break;
266 }
267 }
268 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
269}
270
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000271SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
272 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
273 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000274 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000275 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000276}
277
Chris Lattnere10e6f72007-06-18 21:28:10 +0000278/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
279/// contains all of a nodes operands before it contains the node.
280static void ComputeTopDownOrdering(SelectionDAG &DAG,
281 SmallVector<SDNode*, 64> &Order) {
282
283 DenseMap<SDNode*, unsigned> Visited;
284 std::vector<SDNode*> Worklist;
285 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000286
Chris Lattnere10e6f72007-06-18 21:28:10 +0000287 // Compute ordering from all of the leaves in the graphs, those (like the
288 // entry node) that have no operands.
289 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
290 E = DAG.allnodes_end(); I != E; ++I) {
291 if (I->getNumOperands() == 0) {
292 Visited[I] = 0 - 1U;
293 Worklist.push_back(I);
294 }
Chris Lattner32fca002005-10-06 01:20:27 +0000295 }
296
Chris Lattnere10e6f72007-06-18 21:28:10 +0000297 while (!Worklist.empty()) {
298 SDNode *N = Worklist.back();
299 Worklist.pop_back();
300
301 if (++Visited[N] != N->getNumOperands())
302 continue; // Haven't visited all operands yet
303
304 Order.push_back(N);
305
306 // Now that we have N in, add anything that uses it if all of their operands
307 // are now done.
308 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
309 UI != E; ++UI)
310 Worklist.push_back(*UI);
311 }
312
313 assert(Order.size() == Visited.size() &&
314 Order.size() ==
315 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
316 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000317}
318
Chris Lattner1618beb2005-07-29 00:11:56 +0000319
Chris Lattner3e928bb2005-01-07 07:47:09 +0000320void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000321 LastCALLSEQ_END = DAG.getEntryNode();
322 IsLegalizingCall = false;
323
Chris Lattnerab510a72005-10-02 17:49:46 +0000324 // The legalize process is inherently a bottom-up recursive process (users
325 // legalize their uses before themselves). Given infinite stack space, we
326 // could just start legalizing on the root and traverse the whole graph. In
327 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000328 // blocks. To avoid this problem, compute an ordering of the nodes where each
329 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000330 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000331 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000332
Chris Lattnerc7029802006-03-18 01:44:44 +0000333 for (unsigned i = 0, e = Order.size(); i != e; ++i)
334 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000335
336 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000337 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000338 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
339 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000340
341 ExpandedNodes.clear();
342 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000343 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000344 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000345 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346
347 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000348 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000349}
350
Chris Lattner6831a812006-02-13 09:18:02 +0000351
352/// FindCallEndFromCallStart - Given a chained node that is part of a call
353/// sequence, find the CALLSEQ_END node that terminates the call sequence.
354static SDNode *FindCallEndFromCallStart(SDNode *Node) {
355 if (Node->getOpcode() == ISD::CALLSEQ_END)
356 return Node;
357 if (Node->use_empty())
358 return 0; // No CallSeqEnd
359
360 // The chain is usually at the end.
361 SDOperand TheChain(Node, Node->getNumValues()-1);
362 if (TheChain.getValueType() != MVT::Other) {
363 // Sometimes it's at the beginning.
364 TheChain = SDOperand(Node, 0);
365 if (TheChain.getValueType() != MVT::Other) {
366 // Otherwise, hunt for it.
367 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
368 if (Node->getValueType(i) == MVT::Other) {
369 TheChain = SDOperand(Node, i);
370 break;
371 }
372
373 // Otherwise, we walked into a node without a chain.
374 if (TheChain.getValueType() != MVT::Other)
375 return 0;
376 }
377 }
378
379 for (SDNode::use_iterator UI = Node->use_begin(),
380 E = Node->use_end(); UI != E; ++UI) {
381
382 // Make sure to only follow users of our token chain.
383 SDNode *User = *UI;
384 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
385 if (User->getOperand(i) == TheChain)
386 if (SDNode *Result = FindCallEndFromCallStart(User))
387 return Result;
388 }
389 return 0;
390}
391
392/// FindCallStartFromCallEnd - Given a chained node that is part of a call
393/// sequence, find the CALLSEQ_START node that initiates the call sequence.
394static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
395 assert(Node && "Didn't find callseq_start for a call??");
396 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
397
398 assert(Node->getOperand(0).getValueType() == MVT::Other &&
399 "Node doesn't have a token chain argument!");
400 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
401}
402
403/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
404/// see if any uses can reach Dest. If no dest operands can get to dest,
405/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000406///
407/// Keep track of the nodes we fine that actually do lead to Dest in
408/// NodesLeadingTo. This avoids retraversing them exponential number of times.
409///
410bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000411 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000412 if (N == Dest) return true; // N certainly leads to Dest :)
413
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000414 // If we've already processed this node and it does lead to Dest, there is no
415 // need to reprocess it.
416 if (NodesLeadingTo.count(N)) return true;
417
Chris Lattner6831a812006-02-13 09:18:02 +0000418 // If the first result of this node has been already legalized, then it cannot
419 // reach N.
420 switch (getTypeAction(N->getValueType(0))) {
421 case Legal:
422 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
423 break;
424 case Promote:
425 if (PromotedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 case Expand:
428 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
429 break;
430 }
431
432 // Okay, this node has not already been legalized. Check and legalize all
433 // operands. If none lead to Dest, then we can legalize this node.
434 bool OperandsLeadToDest = false;
435 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
436 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000437 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000438
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000439 if (OperandsLeadToDest) {
440 NodesLeadingTo.insert(N);
441 return true;
442 }
Chris Lattner6831a812006-02-13 09:18:02 +0000443
444 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000445 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000446 return false;
447}
448
Dan Gohman7f321562007-06-25 16:23:39 +0000449/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000450/// appropriate for its type.
451void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000452 MVT::ValueType VT = Op.getValueType();
453 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000454 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000455 case Legal: (void)LegalizeOp(Op); break;
456 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000457 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000458 if (!MVT::isVector(VT)) {
459 // If this is an illegal scalar, expand it into its two component
460 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000461 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000462 if (Op.getOpcode() == ISD::TargetConstant)
463 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000464 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000465 } else if (MVT::getVectorNumElements(VT) == 1) {
466 // If this is an illegal single element vector, convert it to a
467 // scalar operation.
468 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000469 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000470 // Otherwise, this is an illegal multiple element vector.
471 // Split it in half and legalize both parts.
472 SDOperand X, Y;
473 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000474 }
475 break;
476 }
477}
Chris Lattner6831a812006-02-13 09:18:02 +0000478
Evan Cheng9f877882006-12-13 20:57:08 +0000479/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
480/// a load from the constant pool.
481static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000482 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000483 bool Extend = false;
484
485 // If a FP immediate is precise when represented as a float and if the
486 // target can do an extending load from float to double, we put it into
487 // the constant pool as a float, even if it's is statically typed as a
488 // double.
489 MVT::ValueType VT = CFP->getValueType(0);
490 bool isDouble = VT == MVT::f64;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000491 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000492 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000493 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000494 if (VT!=MVT::f64 && VT!=MVT::f32)
495 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000496 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
497 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000498 }
499
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000500 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000501 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000502 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000503 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
504 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
505 VT = MVT::f32;
506 Extend = true;
507 }
508
509 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
510 if (Extend) {
511 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
512 CPIdx, NULL, 0, MVT::f32);
513 } else {
514 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
515 }
516}
517
Chris Lattner6831a812006-02-13 09:18:02 +0000518
Evan Cheng912095b2007-01-04 21:56:39 +0000519/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
520/// operations.
521static
522SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
523 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000524 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000525 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000526 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
527 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000528 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000529
Evan Cheng912095b2007-01-04 21:56:39 +0000530 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000531 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000532 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
533 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000534 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000535 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000536 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000537 // Shift right or sign-extend it if the two operands have different types.
538 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
539 if (SizeDiff > 0) {
540 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
541 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
542 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
543 } else if (SizeDiff < 0)
544 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000545
546 // Clear the sign bit of first operand.
547 SDOperand Mask2 = (VT == MVT::f64)
548 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
549 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
550 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000551 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000552 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
553
554 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000555 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
556 return Result;
557}
558
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000559/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
560static
561SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
562 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000563 SDOperand Chain = ST->getChain();
564 SDOperand Ptr = ST->getBasePtr();
565 SDOperand Val = ST->getValue();
566 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000567 int Alignment = ST->getAlignment();
568 int SVOffset = ST->getSrcValueOffset();
569 if (MVT::isFloatingPoint(ST->getStoredVT())) {
570 // Expand to a bitconvert of the value to the integer type of the
571 // same size, then a (misaligned) int store.
572 MVT::ValueType intVT;
573 if (VT==MVT::f64)
574 intVT = MVT::i64;
575 else if (VT==MVT::f32)
576 intVT = MVT::i32;
577 else
578 assert(0 && "Unaligned load of unsupported floating point type");
579
580 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
581 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
582 SVOffset, ST->isVolatile(), Alignment);
583 }
584 assert(MVT::isInteger(ST->getStoredVT()) &&
585 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000586 // Get the half-size VT
587 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
588 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000589 int IncrementSize = NumBits / 8;
590
591 // Divide the stored value in two parts.
592 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
593 SDOperand Lo = Val;
594 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
595
596 // Store the two parts
597 SDOperand Store1, Store2;
598 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
599 ST->getSrcValue(), SVOffset, NewStoredVT,
600 ST->isVolatile(), Alignment);
601 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
602 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000603 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000604 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
605 ST->getSrcValue(), SVOffset + IncrementSize,
606 NewStoredVT, ST->isVolatile(), Alignment);
607
608 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
609}
610
611/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
612static
613SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
614 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000615 int SVOffset = LD->getSrcValueOffset();
616 SDOperand Chain = LD->getChain();
617 SDOperand Ptr = LD->getBasePtr();
618 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000619 MVT::ValueType LoadedVT = LD->getLoadedVT();
Chris Lattnere400af82007-11-19 21:38:03 +0000620 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000621 // Expand to a (misaligned) integer load of the same size,
622 // then bitconvert to floating point.
623 MVT::ValueType intVT;
Chris Lattnere400af82007-11-19 21:38:03 +0000624 if (LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000625 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000626 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000627 intVT = MVT::i32;
628 else
629 assert(0 && "Unaligned load of unsupported floating point type");
630
631 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
632 SVOffset, LD->isVolatile(),
633 LD->getAlignment());
634 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
635 if (LoadedVT != VT)
636 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
637
638 SDOperand Ops[] = { Result, Chain };
639 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
640 Ops, 2);
641 }
Chris Lattnere400af82007-11-19 21:38:03 +0000642 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
643 "Unaligned load of unsupported type.");
644
645 // Compute the new VT that is half the size of the old one. We either have an
646 // integer MVT or we have a vector MVT.
647 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
648 MVT::ValueType NewLoadedVT;
649 if (!MVT::isVector(LoadedVT)) {
650 NewLoadedVT = MVT::getIntegerType(NumBits/2);
651 } else {
652 // FIXME: This is not right for <1 x anything> it is also not right for
653 // non-power-of-two vectors.
654 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
655 MVT::getVectorNumElements(LoadedVT)/2);
656 }
657 NumBits >>= 1;
658
659 unsigned Alignment = LD->getAlignment();
660 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000661 ISD::LoadExtType HiExtType = LD->getExtensionType();
662
663 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
664 if (HiExtType == ISD::NON_EXTLOAD)
665 HiExtType = ISD::ZEXTLOAD;
666
667 // Load the value in two parts
668 SDOperand Lo, Hi;
669 if (TLI.isLittleEndian()) {
670 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
671 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
672 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
673 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
674 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
675 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000676 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000677 } else {
678 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
679 NewLoadedVT,LD->isVolatile(), Alignment);
680 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
681 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
682 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
683 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000684 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000685 }
686
687 // aggregate the two parts
688 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
689 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
690 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
691
692 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
693 Hi.getValue(1));
694
695 SDOperand Ops[] = { Result, TF };
696 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
697}
Evan Cheng912095b2007-01-04 21:56:39 +0000698
Dan Gohman82669522007-10-11 23:57:53 +0000699/// UnrollVectorOp - We know that the given vector has a legal type, however
700/// the operation it performs is not legal and is an operation that we have
701/// no way of lowering. "Unroll" the vector, splitting out the scalars and
702/// operating on each element individually.
703SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
704 MVT::ValueType VT = Op.getValueType();
705 assert(isTypeLegal(VT) &&
706 "Caller should expand or promote operands that are not legal!");
707 assert(Op.Val->getNumValues() == 1 &&
708 "Can't unroll a vector with multiple results!");
709 unsigned NE = MVT::getVectorNumElements(VT);
710 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
711
712 SmallVector<SDOperand, 8> Scalars;
713 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
714 for (unsigned i = 0; i != NE; ++i) {
715 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
716 SDOperand Operand = Op.getOperand(j);
717 MVT::ValueType OperandVT = Operand.getValueType();
718 if (MVT::isVector(OperandVT)) {
719 // A vector operand; extract a single element.
720 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
721 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
722 OperandEltVT,
723 Operand,
724 DAG.getConstant(i, MVT::i32));
725 } else {
726 // A scalar operand; just use it as is.
727 Operands[j] = Operand;
728 }
729 }
730 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
731 &Operands[0], Operands.size()));
732 }
733
734 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
735}
736
Duncan Sands007f9842008-01-10 10:28:30 +0000737/// GetFPLibCall - Return the right libcall for the given floating point type.
738static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
739 RTLIB::Libcall Call_F32,
740 RTLIB::Libcall Call_F64,
741 RTLIB::Libcall Call_F80,
742 RTLIB::Libcall Call_PPCF128) {
743 return
744 VT == MVT::f32 ? Call_F32 :
745 VT == MVT::f64 ? Call_F64 :
746 VT == MVT::f80 ? Call_F80 :
747 VT == MVT::ppcf128 ? Call_PPCF128 :
748 RTLIB::UNKNOWN_LIBCALL;
749}
750
Dan Gohmana3466152007-07-13 20:14:11 +0000751/// LegalizeOp - We know that the specified value has a legal type, and
752/// that its operands are legal. Now ensure that the operation itself
753/// is legal, recursively ensuring that the operands' operations remain
754/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000755SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000756 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
757 return Op;
758
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000759 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000760 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000761 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000762
Chris Lattner3e928bb2005-01-07 07:47:09 +0000763 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000764 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000765 if (Node->getNumValues() > 1) {
766 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000767 if (getTypeAction(Node->getValueType(i)) != Legal) {
768 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000770 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000771 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000772 }
773 }
774
Chris Lattner45982da2005-05-12 16:53:42 +0000775 // Note that LegalizeOp may be reentered even from single-use nodes, which
776 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000777 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000778 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000779
Nate Begeman9373a812005-08-10 20:51:12 +0000780 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000781 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000782 bool isCustom = false;
783
Chris Lattner3e928bb2005-01-07 07:47:09 +0000784 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000785 case ISD::FrameIndex:
786 case ISD::EntryToken:
787 case ISD::Register:
788 case ISD::BasicBlock:
789 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000790 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000791 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000792 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000793 case ISD::TargetConstantPool:
794 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000795 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000796 case ISD::TargetExternalSymbol:
797 case ISD::VALUETYPE:
798 case ISD::SRCVALUE:
799 case ISD::STRING:
800 case ISD::CONDCODE:
801 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000802 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000803 "This must be legal!");
804 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000805 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000806 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
807 // If this is a target node, legalize it by legalizing the operands then
808 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000809 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000810 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000811 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000812
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000813 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000814
815 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
816 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
817 return Result.getValue(Op.ResNo);
818 }
819 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000820#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000821 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000822#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000823 assert(0 && "Do not know how to legalize this operator!");
824 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000825 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000826 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000827 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000828 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000829 case ISD::ConstantPool:
830 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000831 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
832 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000833 case TargetLowering::Custom:
834 Tmp1 = TLI.LowerOperation(Op, DAG);
835 if (Tmp1.Val) Result = Tmp1;
836 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000837 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000838 break;
839 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000840 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000841 case ISD::FRAMEADDR:
842 case ISD::RETURNADDR:
843 // The only option for these nodes is to custom lower them. If the target
844 // does not custom lower them, then return zero.
845 Tmp1 = TLI.LowerOperation(Op, DAG);
846 if (Tmp1.Val)
847 Result = Tmp1;
848 else
849 Result = DAG.getConstant(0, TLI.getPointerTy());
850 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000851 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000852 MVT::ValueType VT = Node->getValueType(0);
853 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
854 default: assert(0 && "This action is not supported yet!");
855 case TargetLowering::Custom:
856 Result = TLI.LowerOperation(Op, DAG);
857 if (Result.Val) break;
858 // Fall Thru
859 case TargetLowering::Legal:
860 Result = DAG.getConstant(0, VT);
861 break;
862 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000863 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000864 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000865 case ISD::EXCEPTIONADDR: {
866 Tmp1 = LegalizeOp(Node->getOperand(0));
867 MVT::ValueType VT = Node->getValueType(0);
868 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
869 default: assert(0 && "This action is not supported yet!");
870 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000871 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000872 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000873 }
874 break;
875 case TargetLowering::Custom:
876 Result = TLI.LowerOperation(Op, DAG);
877 if (Result.Val) break;
878 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000879 case TargetLowering::Legal: {
880 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
881 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000882 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000883 break;
884 }
885 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000886 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000887 if (Result.Val->getNumValues() == 1) break;
888
889 assert(Result.Val->getNumValues() == 2 &&
890 "Cannot return more than two values!");
891
892 // Since we produced two values, make sure to remember that we
893 // legalized both of them.
894 Tmp1 = LegalizeOp(Result);
895 Tmp2 = LegalizeOp(Result.getValue(1));
896 AddLegalizedOperand(Op.getValue(0), Tmp1);
897 AddLegalizedOperand(Op.getValue(1), Tmp2);
898 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000899 case ISD::EHSELECTION: {
900 Tmp1 = LegalizeOp(Node->getOperand(0));
901 Tmp2 = LegalizeOp(Node->getOperand(1));
902 MVT::ValueType VT = Node->getValueType(0);
903 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
904 default: assert(0 && "This action is not supported yet!");
905 case TargetLowering::Expand: {
906 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000907 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000908 }
909 break;
910 case TargetLowering::Custom:
911 Result = TLI.LowerOperation(Op, DAG);
912 if (Result.Val) break;
913 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000914 case TargetLowering::Legal: {
915 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
916 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000917 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000918 break;
919 }
920 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000921 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000922 if (Result.Val->getNumValues() == 1) break;
923
924 assert(Result.Val->getNumValues() == 2 &&
925 "Cannot return more than two values!");
926
927 // Since we produced two values, make sure to remember that we
928 // legalized both of them.
929 Tmp1 = LegalizeOp(Result);
930 Tmp2 = LegalizeOp(Result.getValue(1));
931 AddLegalizedOperand(Op.getValue(0), Tmp1);
932 AddLegalizedOperand(Op.getValue(1), Tmp2);
933 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000934 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000935 MVT::ValueType VT = Node->getValueType(0);
936 // The only "good" option for this node is to custom lower it.
937 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
938 default: assert(0 && "This action is not supported at all!");
939 case TargetLowering::Custom:
940 Result = TLI.LowerOperation(Op, DAG);
941 if (Result.Val) break;
942 // Fall Thru
943 case TargetLowering::Legal:
944 // Target does not know, how to lower this, lower to noop
945 Result = LegalizeOp(Node->getOperand(0));
946 break;
947 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000948 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000949 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000950 case ISD::AssertSext:
951 case ISD::AssertZext:
952 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000953 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000954 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000955 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000956 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000957 Result = Node->getOperand(Op.ResNo);
958 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000959 case ISD::CopyFromReg:
960 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000961 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000962 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000964 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000965 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000966 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000967 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000968 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
969 } else {
970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
971 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000972 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
973 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000974 // Since CopyFromReg produces two values, make sure to remember that we
975 // legalized both of them.
976 AddLegalizedOperand(Op.getValue(0), Result);
977 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
978 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000979 case ISD::UNDEF: {
980 MVT::ValueType VT = Op.getValueType();
981 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000982 default: assert(0 && "This action is not supported yet!");
983 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000984 if (MVT::isInteger(VT))
985 Result = DAG.getConstant(0, VT);
986 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000987 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
988 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000989 else
990 assert(0 && "Unknown value type!");
991 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000992 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000993 break;
994 }
995 break;
996 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000997
Chris Lattner48b61a72006-03-28 00:40:33 +0000998 case ISD::INTRINSIC_W_CHAIN:
999 case ISD::INTRINSIC_WO_CHAIN:
1000 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001001 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001002 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1003 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001004 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001005
1006 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001007 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001008 TargetLowering::Custom) {
1009 Tmp3 = TLI.LowerOperation(Result, DAG);
1010 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001011 }
1012
1013 if (Result.Val->getNumValues() == 1) break;
1014
1015 // Must have return value and chain result.
1016 assert(Result.Val->getNumValues() == 2 &&
1017 "Cannot return more than two values!");
1018
1019 // Since loads produce two values, make sure to remember that we
1020 // legalized both of them.
1021 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1022 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1023 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001024 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001025
1026 case ISD::LOCATION:
1027 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1028 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1029
1030 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1031 case TargetLowering::Promote:
1032 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001033 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001034 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001035 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001036 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001037
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001038 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001039 const std::string &FName =
1040 cast<StringSDNode>(Node->getOperand(3))->getValue();
1041 const std::string &DirName =
1042 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001043 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001044
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001045 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001046 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001047 SDOperand LineOp = Node->getOperand(1);
1048 SDOperand ColOp = Node->getOperand(2);
1049
1050 if (useDEBUG_LOC) {
1051 Ops.push_back(LineOp); // line #
1052 Ops.push_back(ColOp); // col #
1053 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001054 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001055 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001056 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1057 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001058 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001059 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +00001060 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001061 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001062 } else {
1063 Result = Tmp1; // chain
1064 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001065 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001066 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001067 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001068 if (Tmp1 != Node->getOperand(0) ||
1069 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001070 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001071 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001072 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1073 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1074 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1075 } else {
1076 // Otherwise promote them.
1077 Ops.push_back(PromoteOp(Node->getOperand(1)));
1078 Ops.push_back(PromoteOp(Node->getOperand(2)));
1079 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001080 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1081 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001082 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001083 }
1084 break;
1085 }
1086 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001087
1088 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001089 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001090 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001091 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001092 case TargetLowering::Legal:
1093 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1094 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1095 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1096 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001098 break;
1099 }
1100 break;
1101
Jim Laskey1ee29252007-01-26 14:34:52 +00001102 case ISD::LABEL:
1103 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1104 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001105 default: assert(0 && "This action is not supported yet!");
1106 case TargetLowering::Legal:
1107 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1108 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001109 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001110 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001111 case TargetLowering::Expand:
1112 Result = LegalizeOp(Node->getOperand(0));
1113 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001114 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001115 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001116
Scott Michelc1513d22007-08-08 23:23:31 +00001117 case ISD::Constant: {
1118 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1119 unsigned opAction =
1120 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1121
Chris Lattner3e928bb2005-01-07 07:47:09 +00001122 // We know we don't need to expand constants here, constants only have one
1123 // value and we check that it is fine above.
1124
Scott Michelc1513d22007-08-08 23:23:31 +00001125 if (opAction == TargetLowering::Custom) {
1126 Tmp1 = TLI.LowerOperation(Result, DAG);
1127 if (Tmp1.Val)
1128 Result = Tmp1;
1129 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001130 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001131 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001132 case ISD::ConstantFP: {
1133 // Spill FP immediates to the constant pool if the target cannot directly
1134 // codegen them. Targets often have some immediate values that can be
1135 // efficiently generated into an FP register without a load. We explicitly
1136 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001137 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1138
1139 // Check to see if this FP immediate is already legal.
1140 bool isLegal = false;
1141 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1142 E = TLI.legal_fpimm_end(); I != E; ++I)
1143 if (CFP->isExactlyValue(*I)) {
1144 isLegal = true;
1145 break;
1146 }
1147
Chris Lattner3181a772006-01-29 06:26:56 +00001148 // If this is a legal constant, turn it into a TargetConstantFP node.
1149 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001150 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1151 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001152 break;
1153 }
1154
1155 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1156 default: assert(0 && "This action is not supported yet!");
1157 case TargetLowering::Custom:
1158 Tmp3 = TLI.LowerOperation(Result, DAG);
1159 if (Tmp3.Val) {
1160 Result = Tmp3;
1161 break;
1162 }
1163 // FALLTHROUGH
1164 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001165 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001166 }
1167 break;
1168 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001169 case ISD::TokenFactor:
1170 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001171 Tmp1 = LegalizeOp(Node->getOperand(0));
1172 Tmp2 = LegalizeOp(Node->getOperand(1));
1173 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1174 } else if (Node->getNumOperands() == 3) {
1175 Tmp1 = LegalizeOp(Node->getOperand(0));
1176 Tmp2 = LegalizeOp(Node->getOperand(1));
1177 Tmp3 = LegalizeOp(Node->getOperand(2));
1178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001179 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001180 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001181 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001182 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1183 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001184 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001185 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001186 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001187
1188 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001189 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001190 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001191 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1192 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001193
1194 // The number of incoming and outgoing values should match; unless the final
1195 // outgoing value is a flag.
1196 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1197 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1198 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1199 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001200 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001201
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001202 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001203 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001204 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001205 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1206 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001207 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001208 if (Op.ResNo == i)
1209 Tmp2 = Tmp1;
1210 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1211 }
1212 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001213 case ISD::EXTRACT_SUBREG: {
1214 Tmp1 = LegalizeOp(Node->getOperand(0));
1215 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1216 assert(idx && "Operand must be a constant");
1217 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1219 }
1220 break;
1221 case ISD::INSERT_SUBREG: {
1222 Tmp1 = LegalizeOp(Node->getOperand(0));
1223 Tmp2 = LegalizeOp(Node->getOperand(1));
1224 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1225 assert(idx && "Operand must be a constant");
1226 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1227 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1228 }
1229 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001230 case ISD::BUILD_VECTOR:
1231 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001232 default: assert(0 && "This action is not supported yet!");
1233 case TargetLowering::Custom:
1234 Tmp3 = TLI.LowerOperation(Result, DAG);
1235 if (Tmp3.Val) {
1236 Result = Tmp3;
1237 break;
1238 }
1239 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001240 case TargetLowering::Expand:
1241 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001242 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001243 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001244 break;
1245 case ISD::INSERT_VECTOR_ELT:
1246 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1247 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1248 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1250
1251 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1252 Node->getValueType(0))) {
1253 default: assert(0 && "This action is not supported yet!");
1254 case TargetLowering::Legal:
1255 break;
1256 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001257 Tmp4 = TLI.LowerOperation(Result, DAG);
1258 if (Tmp4.Val) {
1259 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001260 break;
1261 }
1262 // FALLTHROUGH
1263 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001264 // If the insert index is a constant, codegen this as a scalar_to_vector,
1265 // then a shuffle that inserts it into the right position in the vector.
1266 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1267 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1268 Tmp1.getValueType(), Tmp2);
1269
1270 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1271 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001272 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001273
1274 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1275 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1276 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001277 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001278 for (unsigned i = 0; i != NumElts; ++i) {
1279 if (i != InsertPos->getValue())
1280 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1281 else
1282 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1283 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001284 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1285 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001286
1287 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1288 Tmp1, ScVec, ShufMask);
1289 Result = LegalizeOp(Result);
1290 break;
1291 }
1292
Chris Lattner2332b9f2006-03-19 01:17:20 +00001293 // If the target doesn't support this, we have to spill the input vector
1294 // to a temporary stack slot, update the element, then reload it. This is
1295 // badness. We could also load the value into a vector register (either
1296 // with a "move to register" or "extload into register" instruction, then
1297 // permute it into place, if the idx is a constant and if the idx is
1298 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001299 MVT::ValueType VT = Tmp1.getValueType();
1300 MVT::ValueType EltVT = Tmp2.getValueType();
1301 MVT::ValueType IdxVT = Tmp3.getValueType();
1302 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001303 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001304 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001305 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001306
1307 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001308 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1309 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001310 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001311 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1312 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1313 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001314 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001315 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001316 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001317 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001318 break;
1319 }
1320 }
1321 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001322 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001323 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1324 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1325 break;
1326 }
1327
Chris Lattnerce872152006-03-19 06:31:19 +00001328 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1329 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1330 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1331 Node->getValueType(0))) {
1332 default: assert(0 && "This action is not supported yet!");
1333 case TargetLowering::Legal:
1334 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001335 case TargetLowering::Custom:
1336 Tmp3 = TLI.LowerOperation(Result, DAG);
1337 if (Tmp3.Val) {
1338 Result = Tmp3;
1339 break;
1340 }
1341 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001342 case TargetLowering::Expand:
1343 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001344 break;
1345 }
Chris Lattnerce872152006-03-19 06:31:19 +00001346 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001347 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1349 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1351
1352 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001353 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1354 default: assert(0 && "Unknown operation action!");
1355 case TargetLowering::Legal:
1356 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1357 "vector shuffle should not be created if not legal!");
1358 break;
1359 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001360 Tmp3 = TLI.LowerOperation(Result, DAG);
1361 if (Tmp3.Val) {
1362 Result = Tmp3;
1363 break;
1364 }
1365 // FALLTHROUGH
1366 case TargetLowering::Expand: {
1367 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001368 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001369 MVT::ValueType PtrVT = TLI.getPointerTy();
1370 SDOperand Mask = Node->getOperand(2);
1371 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001372 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001373 for (unsigned i = 0; i != NumElems; ++i) {
1374 SDOperand Arg = Mask.getOperand(i);
1375 if (Arg.getOpcode() == ISD::UNDEF) {
1376 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1377 } else {
1378 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1379 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1380 if (Idx < NumElems)
1381 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1382 DAG.getConstant(Idx, PtrVT)));
1383 else
1384 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1385 DAG.getConstant(Idx - NumElems, PtrVT)));
1386 }
1387 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001388 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001389 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001390 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001391 case TargetLowering::Promote: {
1392 // Change base type to a different vector type.
1393 MVT::ValueType OVT = Node->getValueType(0);
1394 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1395
1396 // Cast the two input vectors.
1397 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1398 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1399
1400 // Convert the shuffle mask to the right # elements.
1401 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1402 assert(Tmp3.Val && "Shuffle not legal?");
1403 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1404 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1405 break;
1406 }
Chris Lattner87100e02006-03-20 01:52:29 +00001407 }
1408 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001409
1410 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001411 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001412 Tmp2 = LegalizeOp(Node->getOperand(1));
1413 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001414 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001415 break;
1416
Dan Gohman7f321562007-06-25 16:23:39 +00001417 case ISD::EXTRACT_SUBVECTOR:
1418 Tmp1 = Node->getOperand(0);
1419 Tmp2 = LegalizeOp(Node->getOperand(1));
1420 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1421 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001422 break;
1423
Chris Lattner6831a812006-02-13 09:18:02 +00001424 case ISD::CALLSEQ_START: {
1425 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1426
1427 // Recursively Legalize all of the inputs of the call end that do not lead
1428 // to this call start. This ensures that any libcalls that need be inserted
1429 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001430 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001431 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001432 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1433 NodesLeadingTo);
1434 }
Chris Lattner6831a812006-02-13 09:18:02 +00001435
1436 // Now that we legalized all of the inputs (which may have inserted
1437 // libcalls) create the new CALLSEQ_START node.
1438 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1439
1440 // Merge in the last call, to ensure that this call start after the last
1441 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001442 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001443 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1444 Tmp1 = LegalizeOp(Tmp1);
1445 }
Chris Lattner6831a812006-02-13 09:18:02 +00001446
1447 // Do not try to legalize the target-specific arguments (#1+).
1448 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001449 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001450 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001451 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001452 }
1453
1454 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001455 AddLegalizedOperand(Op.getValue(0), Result);
1456 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1457 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1458
Chris Lattner6831a812006-02-13 09:18:02 +00001459 // Now that the callseq_start and all of the non-call nodes above this call
1460 // sequence have been legalized, legalize the call itself. During this
1461 // process, no libcalls can/will be inserted, guaranteeing that no calls
1462 // can overlap.
1463 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1464 SDOperand InCallSEQ = LastCALLSEQ_END;
1465 // Note that we are selecting this call!
1466 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1467 IsLegalizingCall = true;
1468
1469 // Legalize the call, starting from the CALLSEQ_END.
1470 LegalizeOp(LastCALLSEQ_END);
1471 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1472 return Result;
1473 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001474 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001475 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1476 // will cause this node to be legalized as well as handling libcalls right.
1477 if (LastCALLSEQ_END.Val != Node) {
1478 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001479 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001480 assert(I != LegalizedNodes.end() &&
1481 "Legalizing the call start should have legalized this node!");
1482 return I->second;
1483 }
1484
1485 // Otherwise, the call start has been legalized and everything is going
1486 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001487 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001488 // Do not try to legalize the target-specific arguments (#1+), except for
1489 // an optional flag input.
1490 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1491 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001492 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001493 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001494 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001495 }
1496 } else {
1497 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1498 if (Tmp1 != Node->getOperand(0) ||
1499 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001500 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001501 Ops[0] = Tmp1;
1502 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001503 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001504 }
Chris Lattner6a542892006-01-24 05:48:21 +00001505 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001506 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001507 // This finishes up call legalization.
1508 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001509
1510 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1511 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1512 if (Node->getNumValues() == 2)
1513 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1514 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001515 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001516 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001517 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1518 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1519 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001520 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001521
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001522 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001523 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001524 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001525 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001526 case TargetLowering::Expand: {
1527 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1528 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1529 " not tell us which reg is the stack pointer!");
1530 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001531
1532 // Chain the dynamic stack allocation so that it doesn't modify the stack
1533 // pointer when other instructions are using the stack.
1534 Chain = DAG.getCALLSEQ_START(Chain,
1535 DAG.getConstant(0, TLI.getPointerTy()));
1536
Chris Lattner903d2782006-01-15 08:54:32 +00001537 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001538 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1539 Chain = SP.getValue(1);
1540 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1541 unsigned StackAlign =
1542 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1543 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001544 SP = DAG.getNode(ISD::AND, VT, SP,
1545 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001546 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001547 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1548
1549 Tmp2 =
1550 DAG.getCALLSEQ_END(Chain,
1551 DAG.getConstant(0, TLI.getPointerTy()),
1552 DAG.getConstant(0, TLI.getPointerTy()),
1553 SDOperand());
1554
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001555 Tmp1 = LegalizeOp(Tmp1);
1556 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001557 break;
1558 }
1559 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001560 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1561 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001562 Tmp1 = LegalizeOp(Tmp3);
1563 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001564 }
Chris Lattner903d2782006-01-15 08:54:32 +00001565 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001566 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001567 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001568 }
Chris Lattner903d2782006-01-15 08:54:32 +00001569 // Since this op produce two values, make sure to remember that we
1570 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001571 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1572 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001573 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001574 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001575 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001576 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001577 bool Changed = false;
1578 // Legalize all of the operands of the inline asm, in case they are nodes
1579 // that need to be expanded or something. Note we skip the asm string and
1580 // all of the TargetConstant flags.
1581 SDOperand Op = LegalizeOp(Ops[0]);
1582 Changed = Op != Ops[0];
1583 Ops[0] = Op;
1584
1585 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1586 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1587 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1588 for (++i; NumVals; ++i, --NumVals) {
1589 SDOperand Op = LegalizeOp(Ops[i]);
1590 if (Op != Ops[i]) {
1591 Changed = true;
1592 Ops[i] = Op;
1593 }
1594 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001595 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001596
1597 if (HasInFlag) {
1598 Op = LegalizeOp(Ops.back());
1599 Changed |= Op != Ops.back();
1600 Ops.back() = Op;
1601 }
1602
1603 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001604 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001605
1606 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001607 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001608 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1609 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001610 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001611 case ISD::BR:
1612 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001613 // Ensure that libcalls are emitted before a branch.
1614 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1615 Tmp1 = LegalizeOp(Tmp1);
1616 LastCALLSEQ_END = DAG.getEntryNode();
1617
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001618 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001619 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001620 case ISD::BRIND:
1621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1622 // Ensure that libcalls are emitted before a branch.
1623 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1624 Tmp1 = LegalizeOp(Tmp1);
1625 LastCALLSEQ_END = DAG.getEntryNode();
1626
1627 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1628 default: assert(0 && "Indirect target must be legal type (pointer)!");
1629 case Legal:
1630 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1631 break;
1632 }
1633 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1634 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001635 case ISD::BR_JT:
1636 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1637 // Ensure that libcalls are emitted before a branch.
1638 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1639 Tmp1 = LegalizeOp(Tmp1);
1640 LastCALLSEQ_END = DAG.getEntryNode();
1641
1642 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1644
1645 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1646 default: assert(0 && "This action is not supported yet!");
1647 case TargetLowering::Legal: break;
1648 case TargetLowering::Custom:
1649 Tmp1 = TLI.LowerOperation(Result, DAG);
1650 if (Tmp1.Val) Result = Tmp1;
1651 break;
1652 case TargetLowering::Expand: {
1653 SDOperand Chain = Result.getOperand(0);
1654 SDOperand Table = Result.getOperand(1);
1655 SDOperand Index = Result.getOperand(2);
1656
1657 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001658 MachineFunction &MF = DAG.getMachineFunction();
1659 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001660 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1661 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001662
1663 SDOperand LD;
1664 switch (EntrySize) {
1665 default: assert(0 && "Size of jump table not supported yet."); break;
1666 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1667 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1668 }
1669
Evan Chengcc415862007-11-09 01:32:10 +00001670 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001671 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001672 // For PIC, the sequence is:
1673 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001674 // RelocBase can be JumpTable, GOT or some sort of global base.
1675 if (PTy != MVT::i32)
1676 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1677 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1678 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001679 }
Evan Chengcc415862007-11-09 01:32:10 +00001680 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001681 }
1682 }
1683 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001684 case ISD::BRCOND:
1685 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001686 // Ensure that libcalls are emitted before a return.
1687 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1688 Tmp1 = LegalizeOp(Tmp1);
1689 LastCALLSEQ_END = DAG.getEntryNode();
1690
Chris Lattner47e92232005-01-18 19:27:06 +00001691 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1692 case Expand: assert(0 && "It's impossible to expand bools");
1693 case Legal:
1694 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1695 break;
1696 case Promote:
1697 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001698
1699 // The top bits of the promoted condition are not necessarily zero, ensure
1700 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001701 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001702 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1703 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001704 break;
1705 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001706
1707 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001708 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001709
1710 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1711 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001712 case TargetLowering::Legal: break;
1713 case TargetLowering::Custom:
1714 Tmp1 = TLI.LowerOperation(Result, DAG);
1715 if (Tmp1.Val) Result = Tmp1;
1716 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001717 case TargetLowering::Expand:
1718 // Expand brcond's setcc into its constituent parts and create a BR_CC
1719 // Node.
1720 if (Tmp2.getOpcode() == ISD::SETCC) {
1721 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1722 Tmp2.getOperand(0), Tmp2.getOperand(1),
1723 Node->getOperand(2));
1724 } else {
1725 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1726 DAG.getCondCode(ISD::SETNE), Tmp2,
1727 DAG.getConstant(0, Tmp2.getValueType()),
1728 Node->getOperand(2));
1729 }
1730 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001731 }
1732 break;
1733 case ISD::BR_CC:
1734 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001735 // Ensure that libcalls are emitted before a branch.
1736 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1737 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001738 Tmp2 = Node->getOperand(2); // LHS
1739 Tmp3 = Node->getOperand(3); // RHS
1740 Tmp4 = Node->getOperand(1); // CC
1741
1742 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001743 LastCALLSEQ_END = DAG.getEntryNode();
1744
Nate Begeman750ac1b2006-02-01 07:19:44 +00001745 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1746 // the LHS is a legal SETCC itself. In this case, we need to compare
1747 // the result against zero to select between true and false values.
1748 if (Tmp3.Val == 0) {
1749 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1750 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001751 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001752
1753 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1754 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001755
Chris Lattner181b7a32005-12-17 23:46:46 +00001756 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1757 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001758 case TargetLowering::Legal: break;
1759 case TargetLowering::Custom:
1760 Tmp4 = TLI.LowerOperation(Result, DAG);
1761 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001762 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001763 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001764 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001765 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001766 LoadSDNode *LD = cast<LoadSDNode>(Node);
1767 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1768 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001769
Evan Cheng466685d2006-10-09 20:57:25 +00001770 ISD::LoadExtType ExtType = LD->getExtensionType();
1771 if (ExtType == ISD::NON_EXTLOAD) {
1772 MVT::ValueType VT = Node->getValueType(0);
1773 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1774 Tmp3 = Result.getValue(0);
1775 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001776
Evan Cheng466685d2006-10-09 20:57:25 +00001777 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1778 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001779 case TargetLowering::Legal:
1780 // If this is an unaligned load and the target doesn't support it,
1781 // expand it.
1782 if (!TLI.allowsUnalignedMemoryAccesses()) {
1783 unsigned ABIAlignment = TLI.getTargetData()->
1784 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1785 if (LD->getAlignment() < ABIAlignment){
1786 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1787 TLI);
1788 Tmp3 = Result.getOperand(0);
1789 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001790 Tmp3 = LegalizeOp(Tmp3);
1791 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001792 }
1793 }
1794 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001795 case TargetLowering::Custom:
1796 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1797 if (Tmp1.Val) {
1798 Tmp3 = LegalizeOp(Tmp1);
1799 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001800 }
Evan Cheng466685d2006-10-09 20:57:25 +00001801 break;
1802 case TargetLowering::Promote: {
1803 // Only promote a load of vector type to another.
1804 assert(MVT::isVector(VT) && "Cannot promote this load!");
1805 // Change base type to a different vector type.
1806 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1807
1808 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001809 LD->getSrcValueOffset(),
1810 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001811 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1812 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001813 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001814 }
Evan Cheng466685d2006-10-09 20:57:25 +00001815 }
1816 // Since loads produce two values, make sure to remember that we
1817 // legalized both of them.
1818 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1819 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1820 return Op.ResNo ? Tmp4 : Tmp3;
1821 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001822 MVT::ValueType SrcVT = LD->getLoadedVT();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001823 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1824 int SVOffset = LD->getSrcValueOffset();
1825 unsigned Alignment = LD->getAlignment();
1826 bool isVolatile = LD->isVolatile();
1827
1828 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1829 // Some targets pretend to have an i1 loading operation, and actually
1830 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1831 // bits are guaranteed to be zero; it helps the optimizers understand
1832 // that these bits are zero. It is also useful for EXTLOAD, since it
1833 // tells the optimizers that those bits are undefined. It would be
1834 // nice to have an effective generic way of getting these benefits...
1835 // Until such a way is found, don't insist on promoting i1 here.
1836 (SrcVT != MVT::i1 ||
1837 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1838 // Promote to a byte-sized load if not loading an integral number of
1839 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1840 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1841 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1842 SDOperand Ch;
1843
1844 // The extra bits are guaranteed to be zero, since we stored them that
1845 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1846
1847 ISD::LoadExtType NewExtType =
1848 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1849
1850 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1851 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1852 NVT, isVolatile, Alignment);
1853
1854 Ch = Result.getValue(1); // The chain.
1855
1856 if (ExtType == ISD::SEXTLOAD)
1857 // Having the top bits zero doesn't help when sign extending.
1858 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1859 Result, DAG.getValueType(SrcVT));
1860 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1861 // All the top bits are guaranteed to be zero - inform the optimizers.
1862 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1863 DAG.getValueType(SrcVT));
1864
1865 Tmp1 = LegalizeOp(Result);
1866 Tmp2 = LegalizeOp(Ch);
1867 } else if (SrcWidth & (SrcWidth - 1)) {
1868 // If not loading a power-of-2 number of bits, expand as two loads.
1869 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1870 "Unsupported extload!");
1871 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1872 assert(RoundWidth < SrcWidth);
1873 unsigned ExtraWidth = SrcWidth - RoundWidth;
1874 assert(ExtraWidth < RoundWidth);
1875 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1876 "Load size not an integral number of bytes!");
1877 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1878 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1879 SDOperand Lo, Hi, Ch;
1880 unsigned IncrementSize;
1881
1882 if (TLI.isLittleEndian()) {
1883 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1884 // Load the bottom RoundWidth bits.
1885 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1886 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1887 Alignment);
1888
1889 // Load the remaining ExtraWidth bits.
1890 IncrementSize = RoundWidth / 8;
1891 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1892 DAG.getIntPtrConstant(IncrementSize));
1893 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1894 LD->getSrcValue(), SVOffset + IncrementSize,
1895 ExtraVT, isVolatile,
1896 MinAlign(Alignment, IncrementSize));
1897
1898 // Build a factor node to remember that this load is independent of the
1899 // other one.
1900 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1901 Hi.getValue(1));
1902
1903 // Move the top bits to the right place.
1904 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
1905 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1906
1907 // Join the hi and lo parts.
1908 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001909 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001910 // Big endian - avoid unaligned loads.
1911 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1912 // Load the top RoundWidth bits.
1913 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1914 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1915 Alignment);
1916
1917 // Load the remaining ExtraWidth bits.
1918 IncrementSize = RoundWidth / 8;
1919 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1920 DAG.getIntPtrConstant(IncrementSize));
1921 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1922 LD->getSrcValue(), SVOffset + IncrementSize,
1923 ExtraVT, isVolatile,
1924 MinAlign(Alignment, IncrementSize));
1925
1926 // Build a factor node to remember that this load is independent of the
1927 // other one.
1928 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1929 Hi.getValue(1));
1930
1931 // Move the top bits to the right place.
1932 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
1933 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1934
1935 // Join the hi and lo parts.
1936 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
1937 }
1938
1939 Tmp1 = LegalizeOp(Result);
1940 Tmp2 = LegalizeOp(Ch);
1941 } else {
1942 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1943 default: assert(0 && "This action is not supported yet!");
1944 case TargetLowering::Custom:
1945 isCustom = true;
1946 // FALLTHROUGH
1947 case TargetLowering::Legal:
1948 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1949 Tmp1 = Result.getValue(0);
1950 Tmp2 = Result.getValue(1);
1951
1952 if (isCustom) {
1953 Tmp3 = TLI.LowerOperation(Result, DAG);
1954 if (Tmp3.Val) {
1955 Tmp1 = LegalizeOp(Tmp3);
1956 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1957 }
1958 } else {
1959 // If this is an unaligned load and the target doesn't support it,
1960 // expand it.
1961 if (!TLI.allowsUnalignedMemoryAccesses()) {
1962 unsigned ABIAlignment = TLI.getTargetData()->
1963 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1964 if (LD->getAlignment() < ABIAlignment){
1965 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1966 TLI);
1967 Tmp1 = Result.getOperand(0);
1968 Tmp2 = Result.getOperand(1);
1969 Tmp1 = LegalizeOp(Tmp1);
1970 Tmp2 = LegalizeOp(Tmp2);
1971 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001972 }
1973 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001974 break;
1975 case TargetLowering::Expand:
1976 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1977 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1978 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1979 LD->getSrcValueOffset(),
1980 LD->isVolatile(), LD->getAlignment());
1981 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1982 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1983 Tmp2 = LegalizeOp(Load.getValue(1));
1984 break;
1985 }
1986 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
1987 // Turn the unsupported load into an EXTLOAD followed by an explicit
1988 // zero/sign extend inreg.
1989 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1990 Tmp1, Tmp2, LD->getSrcValue(),
1991 LD->getSrcValueOffset(), SrcVT,
1992 LD->isVolatile(), LD->getAlignment());
1993 SDOperand ValRes;
1994 if (ExtType == ISD::SEXTLOAD)
1995 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1996 Result, DAG.getValueType(SrcVT));
1997 else
1998 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1999 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2000 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002001 break;
2002 }
Evan Cheng466685d2006-10-09 20:57:25 +00002003 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002004
Evan Cheng466685d2006-10-09 20:57:25 +00002005 // Since loads produce two values, make sure to remember that we legalized
2006 // both of them.
2007 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2008 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2009 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002010 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002011 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002012 case ISD::EXTRACT_ELEMENT: {
2013 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2014 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002015 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002016 case Legal:
2017 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2018 // 1 -> Hi
2019 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2020 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2021 TLI.getShiftAmountTy()));
2022 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2023 } else {
2024 // 0 -> Lo
2025 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2026 Node->getOperand(0));
2027 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002028 break;
2029 case Expand:
2030 // Get both the low and high parts.
2031 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2032 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2033 Result = Tmp2; // 1 -> Hi
2034 else
2035 Result = Tmp1; // 0 -> Lo
2036 break;
2037 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002038 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002039 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002040
2041 case ISD::CopyToReg:
2042 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002043
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002044 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002045 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002046 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002047 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002048 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002049 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002050 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002051 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002052 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002053 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002054 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2055 Tmp3);
2056 } else {
2057 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002058 }
2059
2060 // Since this produces two values, make sure to remember that we legalized
2061 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002062 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00002063 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002064 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002065 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002066 break;
2067
2068 case ISD::RET:
2069 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002070
2071 // Ensure that libcalls are emitted before a return.
2072 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2073 Tmp1 = LegalizeOp(Tmp1);
2074 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002075
Chris Lattner3e928bb2005-01-07 07:47:09 +00002076 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002077 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002078 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002079 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002080 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002081 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002082 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002083 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002084 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00002085 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00002086 SDOperand Lo, Hi;
2087 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002088
2089 // Big endian systems want the hi reg first.
2090 if (!TLI.isLittleEndian())
2091 std::swap(Lo, Hi);
2092
Evan Cheng13acce32006-12-11 19:27:14 +00002093 if (Hi.Val)
2094 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2095 else
2096 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002097 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002098 } else {
2099 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002100 int InIx = Tmp2.ResNo;
2101 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2102 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00002103
Dan Gohman7f321562007-06-25 16:23:39 +00002104 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002105 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00002106 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002107 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002108 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002109 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002110 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002111 } else if (NumElems == 1) {
2112 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002113 Tmp2 = ScalarizeVectorOp(Tmp2);
2114 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002116
2117 // FIXME: Returns of gcc generic vectors smaller than a legal type
2118 // should be returned in integer registers!
2119
Chris Lattnerf87324e2006-04-11 01:31:51 +00002120 // The scalarized value type may not be legal, e.g. it might require
2121 // promotion or expansion. Relegalize the return.
2122 Result = LegalizeOp(Result);
2123 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002124 // FIXME: Returns of gcc generic vectors larger than a legal vector
2125 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002126 SDOperand Lo, Hi;
2127 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002128 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002129 Result = LegalizeOp(Result);
2130 }
2131 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002132 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002133 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002134 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002135 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002136 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002137 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002138 }
2139 break;
2140 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002141 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002142 break;
2143 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002144 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002145 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002146 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002147 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2148 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002149 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002150 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002151 break;
2152 case Expand: {
2153 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002154 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002155 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002156 ExpandOp(Node->getOperand(i), Lo, Hi);
2157 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002158 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002159 if (Hi.Val) {
2160 NewValues.push_back(Hi);
2161 NewValues.push_back(Node->getOperand(i+1));
2162 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002163 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002164 }
2165 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002166 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002167 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002168
2169 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002170 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002171 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002172 Result = DAG.getNode(ISD::RET, MVT::Other,
2173 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002174 break;
2175 }
2176 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002177
Chris Lattner6862dbc2006-01-29 21:02:23 +00002178 if (Result.getOpcode() == ISD::RET) {
2179 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2180 default: assert(0 && "This action is not supported yet!");
2181 case TargetLowering::Legal: break;
2182 case TargetLowering::Custom:
2183 Tmp1 = TLI.LowerOperation(Result, DAG);
2184 if (Tmp1.Val) Result = Tmp1;
2185 break;
2186 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002187 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002188 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002189 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002190 StoreSDNode *ST = cast<StoreSDNode>(Node);
2191 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2192 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002193 int SVOffset = ST->getSrcValueOffset();
2194 unsigned Alignment = ST->getAlignment();
2195 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002196
Evan Cheng8b2794a2006-10-13 21:14:26 +00002197 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002198 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2199 // FIXME: We shouldn't do this for TargetConstantFP's.
2200 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2201 // to phase ordering between legalized code and the dag combiner. This
2202 // probably means that we need to integrate dag combiner and legalizer
2203 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002204 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002205 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002206 if (CFP->getValueType(0) == MVT::f32 &&
2207 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002208 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2209 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002210 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002211 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2212 SVOffset, isVolatile, Alignment);
2213 break;
2214 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002215 // If this target supports 64-bit registers, do a single 64-bit store.
2216 if (getTypeAction(MVT::i64) == Legal) {
2217 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2218 getZExtValue(), MVT::i64);
2219 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2220 SVOffset, isVolatile, Alignment);
2221 break;
2222 } else if (getTypeAction(MVT::i32) == Legal) {
2223 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2224 // stores. If the target supports neither 32- nor 64-bits, this
2225 // xform is certainly not worth it.
2226 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2227 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2228 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2229 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2230
2231 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2232 SVOffset, isVolatile, Alignment);
2233 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002234 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002235 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002236 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002237
2238 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2239 break;
2240 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002241 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002242 }
2243
Evan Cheng8b2794a2006-10-13 21:14:26 +00002244 switch (getTypeAction(ST->getStoredVT())) {
2245 case Legal: {
2246 Tmp3 = LegalizeOp(ST->getValue());
2247 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2248 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002249
Evan Cheng8b2794a2006-10-13 21:14:26 +00002250 MVT::ValueType VT = Tmp3.getValueType();
2251 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2252 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002253 case TargetLowering::Legal:
2254 // If this is an unaligned store and the target doesn't support it,
2255 // expand it.
2256 if (!TLI.allowsUnalignedMemoryAccesses()) {
2257 unsigned ABIAlignment = TLI.getTargetData()->
2258 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2259 if (ST->getAlignment() < ABIAlignment)
2260 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2261 TLI);
2262 }
2263 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002264 case TargetLowering::Custom:
2265 Tmp1 = TLI.LowerOperation(Result, DAG);
2266 if (Tmp1.Val) Result = Tmp1;
2267 break;
2268 case TargetLowering::Promote:
2269 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2270 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2271 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2272 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002273 ST->getSrcValue(), SVOffset, isVolatile,
2274 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002275 break;
2276 }
2277 break;
2278 }
2279 case Promote:
2280 // Truncate the value and store the result.
2281 Tmp3 = PromoteOp(ST->getValue());
2282 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002283 SVOffset, ST->getStoredVT(),
2284 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002285 break;
2286
2287 case Expand:
2288 unsigned IncrementSize = 0;
2289 SDOperand Lo, Hi;
2290
2291 // If this is a vector type, then we have to calculate the increment as
2292 // the product of the element size in bytes, and the number of elements
2293 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002294 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002295 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002296 int InIx = ST->getValue().ResNo;
Chris Lattner0bd48932008-01-17 07:00:52 +00002297 MVT::ValueType InVT = InVal->getValueType(InIx);
2298 unsigned NumElems = MVT::getVectorNumElements(InVT);
2299 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002300
Dan Gohman7f321562007-06-25 16:23:39 +00002301 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002302 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002303 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002304 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002305 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002306 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002307 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002308 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002309 Result = LegalizeOp(Result);
2310 break;
2311 } else if (NumElems == 1) {
2312 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002313 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002314 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002315 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002316 // The scalarized value type may not be legal, e.g. it might require
2317 // promotion or expansion. Relegalize the scalar store.
2318 Result = LegalizeOp(Result);
2319 break;
2320 } else {
2321 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002322 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2323 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002324 }
2325 } else {
2326 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002327 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002328
2329 if (!TLI.isLittleEndian())
2330 std::swap(Lo, Hi);
2331 }
2332
2333 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002334 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002335
2336 if (Hi.Val == NULL) {
2337 // Must be int <-> float one-to-one expansion.
2338 Result = Lo;
2339 break;
2340 }
2341
Evan Cheng8b2794a2006-10-13 21:14:26 +00002342 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002343 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002344 assert(isTypeLegal(Tmp2.getValueType()) &&
2345 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002346 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002347 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002348 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002349 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002350 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2351 break;
2352 }
2353 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002354 switch (getTypeAction(ST->getValue().getValueType())) {
2355 case Legal:
2356 Tmp3 = LegalizeOp(ST->getValue());
2357 break;
2358 case Promote:
2359 // We can promote the value, the truncstore will still take care of it.
2360 Tmp3 = PromoteOp(ST->getValue());
2361 break;
2362 case Expand:
2363 // Just store the low part. This may become a non-trunc store, so make
2364 // sure to use getTruncStore, not UpdateNodeOperands below.
2365 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2366 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2367 SVOffset, MVT::i8, isVolatile, Alignment);
2368 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002369
Duncan Sands7e857202008-01-22 07:17:34 +00002370 MVT::ValueType StVT = ST->getStoredVT();
2371 unsigned StWidth = MVT::getSizeInBits(StVT);
2372
2373 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2374 // Promote to a byte-sized store with upper bits zero if not
2375 // storing an integral number of bytes. For example, promote
2376 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2377 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2378 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2379 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2380 SVOffset, NVT, isVolatile, Alignment);
2381 } else if (StWidth & (StWidth - 1)) {
2382 // If not storing a power-of-2 number of bits, expand as two stores.
2383 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2384 "Unsupported truncstore!");
2385 unsigned RoundWidth = 1 << Log2_32(StWidth);
2386 assert(RoundWidth < StWidth);
2387 unsigned ExtraWidth = StWidth - RoundWidth;
2388 assert(ExtraWidth < RoundWidth);
2389 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2390 "Store size not an integral number of bytes!");
2391 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2392 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2393 SDOperand Lo, Hi;
2394 unsigned IncrementSize;
2395
2396 if (TLI.isLittleEndian()) {
2397 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2398 // Store the bottom RoundWidth bits.
2399 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2400 SVOffset, RoundVT,
2401 isVolatile, Alignment);
2402
2403 // Store the remaining ExtraWidth bits.
2404 IncrementSize = RoundWidth / 8;
2405 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2406 DAG.getIntPtrConstant(IncrementSize));
2407 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2408 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2409 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2410 SVOffset + IncrementSize, ExtraVT, isVolatile,
2411 MinAlign(Alignment, IncrementSize));
2412 } else {
2413 // Big endian - avoid unaligned stores.
2414 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2415 // Store the top RoundWidth bits.
2416 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2417 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2418 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2419 RoundVT, isVolatile, Alignment);
2420
2421 // Store the remaining ExtraWidth bits.
2422 IncrementSize = RoundWidth / 8;
2423 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2424 DAG.getIntPtrConstant(IncrementSize));
2425 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2426 SVOffset + IncrementSize, ExtraVT, isVolatile,
2427 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002428 }
Duncan Sands7e857202008-01-22 07:17:34 +00002429
2430 // The order of the stores doesn't matter.
2431 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2432 } else {
2433 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2434 Tmp2 != ST->getBasePtr())
2435 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2436 ST->getOffset());
2437
2438 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2439 default: assert(0 && "This action is not supported yet!");
2440 case TargetLowering::Legal:
2441 // If this is an unaligned store and the target doesn't support it,
2442 // expand it.
2443 if (!TLI.allowsUnalignedMemoryAccesses()) {
2444 unsigned ABIAlignment = TLI.getTargetData()->
2445 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2446 if (ST->getAlignment() < ABIAlignment)
2447 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2448 TLI);
2449 }
2450 break;
2451 case TargetLowering::Custom:
2452 Result = TLI.LowerOperation(Result, DAG);
2453 break;
2454 case Expand:
2455 // TRUNCSTORE:i16 i32 -> STORE i16
2456 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2457 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2458 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2459 isVolatile, Alignment);
2460 break;
2461 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002462 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002463 }
2464 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002465 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002466 case ISD::PCMARKER:
2467 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002469 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002470 case ISD::STACKSAVE:
2471 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002472 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2473 Tmp1 = Result.getValue(0);
2474 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002475
Chris Lattner140d53c2006-01-13 02:50:02 +00002476 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2477 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002478 case TargetLowering::Legal: break;
2479 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002480 Tmp3 = TLI.LowerOperation(Result, DAG);
2481 if (Tmp3.Val) {
2482 Tmp1 = LegalizeOp(Tmp3);
2483 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002484 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002485 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002486 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002487 // Expand to CopyFromReg if the target set
2488 // StackPointerRegisterToSaveRestore.
2489 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002490 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002491 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002492 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002493 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002494 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2495 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002496 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002497 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002498 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002499
2500 // Since stacksave produce two values, make sure to remember that we
2501 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002502 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2503 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2504 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002505
Chris Lattner140d53c2006-01-13 02:50:02 +00002506 case ISD::STACKRESTORE:
2507 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2508 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002509 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002510
2511 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2512 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002513 case TargetLowering::Legal: break;
2514 case TargetLowering::Custom:
2515 Tmp1 = TLI.LowerOperation(Result, DAG);
2516 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002517 break;
2518 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002519 // Expand to CopyToReg if the target set
2520 // StackPointerRegisterToSaveRestore.
2521 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2522 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2523 } else {
2524 Result = Tmp1;
2525 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002526 break;
2527 }
2528 break;
2529
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002530 case ISD::READCYCLECOUNTER:
2531 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002532 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002533 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2534 Node->getValueType(0))) {
2535 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002536 case TargetLowering::Legal:
2537 Tmp1 = Result.getValue(0);
2538 Tmp2 = Result.getValue(1);
2539 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002540 case TargetLowering::Custom:
2541 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002542 Tmp1 = LegalizeOp(Result.getValue(0));
2543 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002544 break;
2545 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002546
2547 // Since rdcc produce two values, make sure to remember that we legalized
2548 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002549 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2550 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002551 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002552
Chris Lattner2ee743f2005-01-14 22:08:15 +00002553 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002554 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2555 case Expand: assert(0 && "It's impossible to expand bools");
2556 case Legal:
2557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2558 break;
2559 case Promote:
2560 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002561 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002562 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002563 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2564 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002565 break;
2566 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002567 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002568 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002569
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002570 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002571
Nate Begemanb942a3d2005-08-23 04:29:48 +00002572 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002573 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002574 case TargetLowering::Legal: break;
2575 case TargetLowering::Custom: {
2576 Tmp1 = TLI.LowerOperation(Result, DAG);
2577 if (Tmp1.Val) Result = Tmp1;
2578 break;
2579 }
Nate Begeman9373a812005-08-10 20:51:12 +00002580 case TargetLowering::Expand:
2581 if (Tmp1.getOpcode() == ISD::SETCC) {
2582 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2583 Tmp2, Tmp3,
2584 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2585 } else {
2586 Result = DAG.getSelectCC(Tmp1,
2587 DAG.getConstant(0, Tmp1.getValueType()),
2588 Tmp2, Tmp3, ISD::SETNE);
2589 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002590 break;
2591 case TargetLowering::Promote: {
2592 MVT::ValueType NVT =
2593 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2594 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002595 if (MVT::isVector(Tmp2.getValueType())) {
2596 ExtOp = ISD::BIT_CONVERT;
2597 TruncOp = ISD::BIT_CONVERT;
2598 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002599 ExtOp = ISD::ANY_EXTEND;
2600 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002601 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002602 ExtOp = ISD::FP_EXTEND;
2603 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002604 }
2605 // Promote each of the values to the new type.
2606 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2607 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2608 // Perform the larger operation, then round down.
2609 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002610 if (TruncOp != ISD::FP_ROUND)
2611 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2612 else
2613 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2614 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002615 break;
2616 }
2617 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002618 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002619 case ISD::SELECT_CC: {
2620 Tmp1 = Node->getOperand(0); // LHS
2621 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002622 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2623 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002624 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002625
Nate Begeman750ac1b2006-02-01 07:19:44 +00002626 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2627
2628 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2629 // the LHS is a legal SETCC itself. In this case, we need to compare
2630 // the result against zero to select between true and false values.
2631 if (Tmp2.Val == 0) {
2632 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2633 CC = DAG.getCondCode(ISD::SETNE);
2634 }
2635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2636
2637 // Everything is legal, see if we should expand this op or something.
2638 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2639 default: assert(0 && "This action is not supported yet!");
2640 case TargetLowering::Legal: break;
2641 case TargetLowering::Custom:
2642 Tmp1 = TLI.LowerOperation(Result, DAG);
2643 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002644 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002645 }
2646 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002647 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002648 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002649 Tmp1 = Node->getOperand(0);
2650 Tmp2 = Node->getOperand(1);
2651 Tmp3 = Node->getOperand(2);
2652 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2653
2654 // If we had to Expand the SetCC operands into a SELECT node, then it may
2655 // not always be possible to return a true LHS & RHS. In this case, just
2656 // return the value we legalized, returned in the LHS
2657 if (Tmp2.Val == 0) {
2658 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002659 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002660 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002661
Chris Lattner73e142f2006-01-30 22:43:50 +00002662 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002663 default: assert(0 && "Cannot handle this action for SETCC yet!");
2664 case TargetLowering::Custom:
2665 isCustom = true;
2666 // FALLTHROUGH.
2667 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002668 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002669 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002670 Tmp4 = TLI.LowerOperation(Result, DAG);
2671 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002672 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002673 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002674 case TargetLowering::Promote: {
2675 // First step, figure out the appropriate operation to use.
2676 // Allow SETCC to not be supported for all legal data types
2677 // Mostly this targets FP
2678 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002679 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002680
2681 // Scan for the appropriate larger type to use.
2682 while (1) {
2683 NewInTy = (MVT::ValueType)(NewInTy+1);
2684
2685 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2686 "Fell off of the edge of the integer world");
2687 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2688 "Fell off of the edge of the floating point world");
2689
2690 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002691 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002692 break;
2693 }
2694 if (MVT::isInteger(NewInTy))
2695 assert(0 && "Cannot promote Legal Integer SETCC yet");
2696 else {
2697 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2698 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2699 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002700 Tmp1 = LegalizeOp(Tmp1);
2701 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002702 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002703 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002704 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002705 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002706 case TargetLowering::Expand:
2707 // Expand a setcc node into a select_cc of the same condition, lhs, and
2708 // rhs that selects between const 1 (true) and const 0 (false).
2709 MVT::ValueType VT = Node->getValueType(0);
2710 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2711 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002712 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002713 break;
2714 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002715 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002716 case ISD::MEMSET:
2717 case ISD::MEMCPY:
2718 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002719 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002720 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2721
2722 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2723 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2724 case Expand: assert(0 && "Cannot expand a byte!");
2725 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002726 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002727 break;
2728 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002729 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002730 break;
2731 }
2732 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002733 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002734 }
Chris Lattner272455b2005-02-02 03:44:41 +00002735
2736 SDOperand Tmp4;
2737 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002738 case Expand: {
2739 // Length is too big, just take the lo-part of the length.
2740 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002741 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002742 break;
2743 }
Chris Lattnere5605212005-01-28 22:29:18 +00002744 case Legal:
2745 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002746 break;
2747 case Promote:
2748 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002749 break;
2750 }
2751
2752 SDOperand Tmp5;
2753 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2754 case Expand: assert(0 && "Cannot expand this yet!");
2755 case Legal:
2756 Tmp5 = LegalizeOp(Node->getOperand(4));
2757 break;
2758 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002759 Tmp5 = PromoteOp(Node->getOperand(4));
2760 break;
2761 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002762
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002763 SDOperand Tmp6;
2764 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2765 case Expand: assert(0 && "Cannot expand this yet!");
2766 case Legal:
2767 Tmp6 = LegalizeOp(Node->getOperand(5));
2768 break;
2769 case Promote:
2770 Tmp6 = PromoteOp(Node->getOperand(5));
2771 break;
2772 }
2773
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002774 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2775 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002776 case TargetLowering::Custom:
2777 isCustom = true;
2778 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002779 case TargetLowering::Legal: {
2780 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2781 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002782 if (isCustom) {
2783 Tmp1 = TLI.LowerOperation(Result, DAG);
2784 if (Tmp1.Val) Result = Tmp1;
2785 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002786 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002787 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002788 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002789 // Otherwise, the target does not support this operation. Lower the
2790 // operation to an explicit libcall as appropriate.
2791 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002792 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002793 TargetLowering::ArgListTy Args;
2794 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002795
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002796 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002797 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002798 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002799 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002800 // Extend the (previously legalized) ubyte argument to be an int value
2801 // for the call.
2802 if (Tmp3.getValueType() > MVT::i32)
2803 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2804 else
2805 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002806 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002807 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002808 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002809 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002810
2811 FnName = "memset";
2812 } else if (Node->getOpcode() == ISD::MEMCPY ||
2813 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002814 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002815 Entry.Node = Tmp2; Args.push_back(Entry);
2816 Entry.Node = Tmp3; Args.push_back(Entry);
2817 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002818 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2819 } else {
2820 assert(0 && "Unknown op!");
2821 }
Chris Lattner45982da2005-05-12 16:53:42 +00002822
Chris Lattnere1bd8222005-01-11 05:57:22 +00002823 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002824 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002825 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002826 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002827 break;
2828 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002829 }
2830 break;
2831 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002832
Chris Lattner5b359c62005-04-02 04:00:59 +00002833 case ISD::SHL_PARTS:
2834 case ISD::SRA_PARTS:
2835 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002836 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002837 bool Changed = false;
2838 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2839 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2840 Changed |= Ops.back() != Node->getOperand(i);
2841 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002842 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002843 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002844
Evan Cheng05a2d562006-01-09 18:31:59 +00002845 switch (TLI.getOperationAction(Node->getOpcode(),
2846 Node->getValueType(0))) {
2847 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002848 case TargetLowering::Legal: break;
2849 case TargetLowering::Custom:
2850 Tmp1 = TLI.LowerOperation(Result, DAG);
2851 if (Tmp1.Val) {
2852 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002853 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002854 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002855 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2856 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002857 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002858 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002859 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002860 return RetVal;
2861 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002862 break;
2863 }
2864
Chris Lattner2c8086f2005-04-02 05:00:07 +00002865 // Since these produce multiple values, make sure to remember that we
2866 // legalized all of them.
2867 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2868 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2869 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002870 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002871
2872 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002873 case ISD::ADD:
2874 case ISD::SUB:
2875 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002876 case ISD::MULHS:
2877 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002878 case ISD::UDIV:
2879 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002880 case ISD::AND:
2881 case ISD::OR:
2882 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002883 case ISD::SHL:
2884 case ISD::SRL:
2885 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002886 case ISD::FADD:
2887 case ISD::FSUB:
2888 case ISD::FMUL:
2889 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002890 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002891 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002892 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2893 case Expand: assert(0 && "Not possible");
2894 case Legal:
2895 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2896 break;
2897 case Promote:
2898 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2899 break;
2900 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002901
2902 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002903
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002904 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002905 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002906 case TargetLowering::Legal: break;
2907 case TargetLowering::Custom:
2908 Tmp1 = TLI.LowerOperation(Result, DAG);
2909 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002910 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002911 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002912 MVT::ValueType VT = Op.getValueType();
2913
2914 // See if multiply or divide can be lowered using two-result operations.
2915 SDVTList VTs = DAG.getVTList(VT, VT);
2916 if (Node->getOpcode() == ISD::MUL) {
2917 // We just need the low half of the multiply; try both the signed
2918 // and unsigned forms. If the target supports both SMUL_LOHI and
2919 // UMUL_LOHI, form a preference by checking which forms of plain
2920 // MULH it supports.
2921 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2922 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2923 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2924 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2925 unsigned OpToUse = 0;
2926 if (HasSMUL_LOHI && !HasMULHS) {
2927 OpToUse = ISD::SMUL_LOHI;
2928 } else if (HasUMUL_LOHI && !HasMULHU) {
2929 OpToUse = ISD::UMUL_LOHI;
2930 } else if (HasSMUL_LOHI) {
2931 OpToUse = ISD::SMUL_LOHI;
2932 } else if (HasUMUL_LOHI) {
2933 OpToUse = ISD::UMUL_LOHI;
2934 }
2935 if (OpToUse) {
2936 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2937 break;
2938 }
2939 }
2940 if (Node->getOpcode() == ISD::MULHS &&
2941 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2942 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2943 break;
2944 }
2945 if (Node->getOpcode() == ISD::MULHU &&
2946 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2947 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2948 break;
2949 }
2950 if (Node->getOpcode() == ISD::SDIV &&
2951 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2952 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2953 break;
2954 }
2955 if (Node->getOpcode() == ISD::UDIV &&
2956 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2957 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2958 break;
2959 }
2960
Dan Gohman82669522007-10-11 23:57:53 +00002961 // Check to see if we have a libcall for this operator.
2962 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2963 bool isSigned = false;
2964 switch (Node->getOpcode()) {
2965 case ISD::UDIV:
2966 case ISD::SDIV:
2967 if (VT == MVT::i32) {
2968 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002969 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002970 isSigned = Node->getOpcode() == ISD::SDIV;
2971 }
2972 break;
2973 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002974 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2975 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002976 break;
2977 default: break;
2978 }
2979 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2980 SDOperand Dummy;
2981 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002982 break;
2983 }
2984
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002985 assert(MVT::isVector(Node->getValueType(0)) &&
2986 "Cannot expand this binary operator!");
2987 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00002988 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002989 break;
2990 }
Evan Chengcc987612006-04-12 21:20:24 +00002991 case TargetLowering::Promote: {
2992 switch (Node->getOpcode()) {
2993 default: assert(0 && "Do not know how to promote this BinOp!");
2994 case ISD::AND:
2995 case ISD::OR:
2996 case ISD::XOR: {
2997 MVT::ValueType OVT = Node->getValueType(0);
2998 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2999 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3000 // Bit convert each of the values to the new type.
3001 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3002 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3003 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3004 // Bit convert the result back the original type.
3005 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3006 break;
3007 }
3008 }
3009 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003010 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003011 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003012
Dan Gohmane14ea862007-10-05 14:17:22 +00003013 case ISD::SMUL_LOHI:
3014 case ISD::UMUL_LOHI:
3015 case ISD::SDIVREM:
3016 case ISD::UDIVREM:
3017 // These nodes will only be produced by target-specific lowering, so
3018 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003019 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003020 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003021
3022 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3023 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003025 break;
3026
Chris Lattnera09f8482006-03-05 05:09:38 +00003027 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3028 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3029 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3030 case Expand: assert(0 && "Not possible");
3031 case Legal:
3032 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3033 break;
3034 case Promote:
3035 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3036 break;
3037 }
3038
3039 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3040
3041 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3042 default: assert(0 && "Operation not supported");
3043 case TargetLowering::Custom:
3044 Tmp1 = TLI.LowerOperation(Result, DAG);
3045 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003046 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003047 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003048 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003049 // If this target supports fabs/fneg natively and select is cheap,
3050 // do this efficiently.
3051 if (!TLI.isSelectExpensive() &&
3052 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3053 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003054 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003055 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003056 // Get the sign bit of the RHS.
3057 MVT::ValueType IVT =
3058 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3059 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3060 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3061 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3062 // Get the absolute value of the result.
3063 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3064 // Select between the nabs and abs value based on the sign bit of
3065 // the input.
3066 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3067 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3068 AbsVal),
3069 AbsVal);
3070 Result = LegalizeOp(Result);
3071 break;
3072 }
3073
3074 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00003075 MVT::ValueType NVT =
3076 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3077 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3078 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3079 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003080 break;
3081 }
Evan Cheng912095b2007-01-04 21:56:39 +00003082 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003083 break;
3084
Nate Begeman551bf3f2006-02-17 05:43:56 +00003085 case ISD::ADDC:
3086 case ISD::SUBC:
3087 Tmp1 = LegalizeOp(Node->getOperand(0));
3088 Tmp2 = LegalizeOp(Node->getOperand(1));
3089 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3090 // Since this produces two values, make sure to remember that we legalized
3091 // both of them.
3092 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3093 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3094 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003095
Nate Begeman551bf3f2006-02-17 05:43:56 +00003096 case ISD::ADDE:
3097 case ISD::SUBE:
3098 Tmp1 = LegalizeOp(Node->getOperand(0));
3099 Tmp2 = LegalizeOp(Node->getOperand(1));
3100 Tmp3 = LegalizeOp(Node->getOperand(2));
3101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3102 // Since this produces two values, make sure to remember that we legalized
3103 // both of them.
3104 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3105 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3106 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003107
Nate Begeman419f8b62005-10-18 00:27:41 +00003108 case ISD::BUILD_PAIR: {
3109 MVT::ValueType PairTy = Node->getValueType(0);
3110 // TODO: handle the case where the Lo and Hi operands are not of legal type
3111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3113 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003114 case TargetLowering::Promote:
3115 case TargetLowering::Custom:
3116 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003117 case TargetLowering::Legal:
3118 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3119 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3120 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003121 case TargetLowering::Expand:
3122 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3123 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3124 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3125 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3126 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003127 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003128 break;
3129 }
3130 break;
3131 }
3132
Nate Begemanc105e192005-04-06 00:23:54 +00003133 case ISD::UREM:
3134 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003135 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003136 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3137 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003138
Nate Begemanc105e192005-04-06 00:23:54 +00003139 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003140 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3141 case TargetLowering::Custom:
3142 isCustom = true;
3143 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003144 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003145 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003146 if (isCustom) {
3147 Tmp1 = TLI.LowerOperation(Result, DAG);
3148 if (Tmp1.Val) Result = Tmp1;
3149 }
Nate Begemanc105e192005-04-06 00:23:54 +00003150 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003151 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003152 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003153 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00003154 MVT::ValueType VT = Node->getValueType(0);
3155
3156 // See if remainder can be lowered using two-result operations.
3157 SDVTList VTs = DAG.getVTList(VT, VT);
3158 if (Node->getOpcode() == ISD::SREM &&
3159 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3160 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3161 break;
3162 }
3163 if (Node->getOpcode() == ISD::UREM &&
3164 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3165 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3166 break;
3167 }
3168
3169 if (MVT::isInteger(VT)) {
3170 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003171 TargetLowering::Legal) {
3172 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003173 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003174 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3175 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00003176 } else if (MVT::isVector(VT)) {
3177 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003178 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003179 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003180 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003181 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3182 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003183 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003184 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003185 }
Dan Gohman0d974262007-11-06 22:11:54 +00003186 } else {
3187 assert(MVT::isFloatingPoint(VT) &&
3188 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003189 if (MVT::isVector(VT)) {
3190 Result = LegalizeOp(UnrollVectorOp(Op));
3191 } else {
3192 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003193 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3194 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003195 SDOperand Dummy;
3196 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3197 false/*sign irrelevant*/, Dummy);
3198 }
Nate Begemanc105e192005-04-06 00:23:54 +00003199 }
3200 break;
3201 }
Dan Gohman525178c2007-10-08 18:33:35 +00003202 }
Nate Begemanc105e192005-04-06 00:23:54 +00003203 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003204 case ISD::VAARG: {
3205 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3206 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3207
Chris Lattner5c62f332006-01-28 07:42:08 +00003208 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003209 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3210 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003211 case TargetLowering::Custom:
3212 isCustom = true;
3213 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003214 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003215 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3216 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003217 Tmp1 = Result.getValue(1);
3218
3219 if (isCustom) {
3220 Tmp2 = TLI.LowerOperation(Result, DAG);
3221 if (Tmp2.Val) {
3222 Result = LegalizeOp(Tmp2);
3223 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3224 }
3225 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003226 break;
3227 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00003228 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00003229 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003230 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003231 // Increment the pointer, VAList, to the next vaarg
3232 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3233 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3234 TLI.getPointerTy()));
3235 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003236 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3237 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003238 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003239 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003240 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003241 Result = LegalizeOp(Result);
3242 break;
3243 }
3244 }
3245 // Since VAARG produces two values, make sure to remember that we
3246 // legalized both of them.
3247 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003248 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3249 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003250 }
3251
3252 case ISD::VACOPY:
3253 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3254 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3255 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3256
3257 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3258 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003259 case TargetLowering::Custom:
3260 isCustom = true;
3261 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003262 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003263 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3264 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003265 if (isCustom) {
3266 Tmp1 = TLI.LowerOperation(Result, DAG);
3267 if (Tmp1.Val) Result = Tmp1;
3268 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003269 break;
3270 case TargetLowering::Expand:
3271 // This defaults to loading a pointer from the input and storing it to the
3272 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00003273 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003274 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00003275 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3276 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003277 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3278 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00003279 break;
3280 }
3281 break;
3282
3283 case ISD::VAEND:
3284 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3285 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3286
3287 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3288 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003289 case TargetLowering::Custom:
3290 isCustom = true;
3291 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003292 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003294 if (isCustom) {
3295 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3296 if (Tmp1.Val) Result = Tmp1;
3297 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003298 break;
3299 case TargetLowering::Expand:
3300 Result = Tmp1; // Default to a no-op, return the chain
3301 break;
3302 }
3303 break;
3304
3305 case ISD::VASTART:
3306 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3307 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3308
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003309 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3310
Nate Begemanacc398c2006-01-25 18:21:52 +00003311 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3312 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003313 case TargetLowering::Legal: break;
3314 case TargetLowering::Custom:
3315 Tmp1 = TLI.LowerOperation(Result, DAG);
3316 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003317 break;
3318 }
3319 break;
3320
Nate Begeman35ef9132006-01-11 21:21:00 +00003321 case ISD::ROTL:
3322 case ISD::ROTR:
3323 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3324 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003326 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3327 default:
3328 assert(0 && "ROTL/ROTR legalize operation not supported");
3329 break;
3330 case TargetLowering::Legal:
3331 break;
3332 case TargetLowering::Custom:
3333 Tmp1 = TLI.LowerOperation(Result, DAG);
3334 if (Tmp1.Val) Result = Tmp1;
3335 break;
3336 case TargetLowering::Promote:
3337 assert(0 && "Do not know how to promote ROTL/ROTR");
3338 break;
3339 case TargetLowering::Expand:
3340 assert(0 && "Do not know how to expand ROTL/ROTR");
3341 break;
3342 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003343 break;
3344
Nate Begemand88fc032006-01-14 03:14:10 +00003345 case ISD::BSWAP:
3346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3347 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003348 case TargetLowering::Custom:
3349 assert(0 && "Cannot custom legalize this yet!");
3350 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003351 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003352 break;
3353 case TargetLowering::Promote: {
3354 MVT::ValueType OVT = Tmp1.getValueType();
3355 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003356 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003357
Chris Lattner456a93a2006-01-28 07:39:30 +00003358 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3359 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3360 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3361 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3362 break;
3363 }
3364 case TargetLowering::Expand:
3365 Result = ExpandBSWAP(Tmp1);
3366 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003367 }
3368 break;
3369
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003370 case ISD::CTPOP:
3371 case ISD::CTTZ:
3372 case ISD::CTLZ:
3373 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3374 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003375 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003376 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003377 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003378 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003379 TargetLowering::Custom) {
3380 Tmp1 = TLI.LowerOperation(Result, DAG);
3381 if (Tmp1.Val) {
3382 Result = Tmp1;
3383 }
Scott Michel910b66d2007-07-30 21:00:31 +00003384 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003385 break;
3386 case TargetLowering::Promote: {
3387 MVT::ValueType OVT = Tmp1.getValueType();
3388 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003389
3390 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003391 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3392 // Perform the larger operation, then subtract if needed.
3393 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003394 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003395 case ISD::CTPOP:
3396 Result = Tmp1;
3397 break;
3398 case ISD::CTTZ:
3399 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003400 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003401 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003402 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003403 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003404 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003405 break;
3406 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003407 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003408 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003409 DAG.getConstant(MVT::getSizeInBits(NVT) -
3410 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003411 break;
3412 }
3413 break;
3414 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003415 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003416 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003417 break;
3418 }
3419 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003420
Chris Lattner2c8086f2005-04-02 05:00:07 +00003421 // Unary operators
3422 case ISD::FABS:
3423 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003424 case ISD::FSQRT:
3425 case ISD::FSIN:
3426 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003427 Tmp1 = LegalizeOp(Node->getOperand(0));
3428 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003429 case TargetLowering::Promote:
3430 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003431 isCustom = true;
3432 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003433 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003434 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003435 if (isCustom) {
3436 Tmp1 = TLI.LowerOperation(Result, DAG);
3437 if (Tmp1.Val) Result = Tmp1;
3438 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003439 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003440 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003441 switch (Node->getOpcode()) {
3442 default: assert(0 && "Unreachable!");
3443 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003444 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3445 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003446 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003447 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003448 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003449 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3450 MVT::ValueType VT = Node->getValueType(0);
3451 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003452 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003453 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3454 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003455 break;
3456 }
3457 case ISD::FSQRT:
3458 case ISD::FSIN:
3459 case ISD::FCOS: {
3460 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003461
3462 // Expand unsupported unary vector operators by unrolling them.
3463 if (MVT::isVector(VT)) {
3464 Result = LegalizeOp(UnrollVectorOp(Op));
3465 break;
3466 }
3467
Evan Cheng56966222007-01-12 02:11:51 +00003468 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003469 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003470 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003471 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3472 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003473 break;
3474 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003475 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3476 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003477 break;
3478 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003479 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3480 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003481 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003482 default: assert(0 && "Unreachable!");
3483 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003484 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003485 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3486 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003487 break;
3488 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003489 }
3490 break;
3491 }
3492 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003493 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003494 MVT::ValueType VT = Node->getValueType(0);
3495
3496 // Expand unsupported unary vector operators by unrolling them.
3497 if (MVT::isVector(VT)) {
3498 Result = LegalizeOp(UnrollVectorOp(Op));
3499 break;
3500 }
3501
3502 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003503 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3504 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003505 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003506 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3507 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003508 break;
3509 }
Chris Lattner35481892005-12-23 00:16:34 +00003510 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003511 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003512 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3513 Node->getValueType(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003514 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3515 // The input has to be a vector type, we have to either scalarize it, pack
3516 // it, or convert it based on whether the input vector type is legal.
3517 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003518 int InIx = Node->getOperand(0).ResNo;
3519 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3520 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003521
3522 // Figure out if there is a simple type corresponding to this Vector
3523 // type. If so, convert to the vector type.
3524 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3525 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003526 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003527 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3528 LegalizeOp(Node->getOperand(0)));
3529 break;
3530 } else if (NumElems == 1) {
3531 // Turn this into a bit convert of the scalar input.
3532 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3533 ScalarizeVectorOp(Node->getOperand(0)));
3534 break;
3535 } else {
3536 // FIXME: UNIMP! Store then reload
3537 assert(0 && "Cast from unsupported vector type not implemented yet!");
3538 }
Chris Lattner67993f72006-01-23 07:30:46 +00003539 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003540 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3541 Node->getOperand(0).getValueType())) {
3542 default: assert(0 && "Unknown operation action!");
3543 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003544 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3545 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003546 break;
3547 case TargetLowering::Legal:
3548 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003549 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003550 break;
3551 }
3552 }
3553 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003554
Chris Lattner2c8086f2005-04-02 05:00:07 +00003555 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003556 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003557 case ISD::UINT_TO_FP: {
3558 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003559 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3560 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003561 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003562 Node->getOperand(0).getValueType())) {
3563 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003564 case TargetLowering::Custom:
3565 isCustom = true;
3566 // FALLTHROUGH
3567 case TargetLowering::Legal:
3568 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003569 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003570 if (isCustom) {
3571 Tmp1 = TLI.LowerOperation(Result, DAG);
3572 if (Tmp1.Val) Result = Tmp1;
3573 }
3574 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003575 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003576 Result = ExpandLegalINT_TO_FP(isSigned,
3577 LegalizeOp(Node->getOperand(0)),
3578 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003579 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003580 case TargetLowering::Promote:
3581 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3582 Node->getValueType(0),
3583 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003584 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003585 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003586 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003587 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003588 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3589 Node->getValueType(0), Node->getOperand(0));
3590 break;
3591 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003592 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003593 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003594 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3595 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003596 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003597 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3598 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003599 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003600 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3601 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003602 break;
3603 }
3604 break;
3605 }
3606 case ISD::TRUNCATE:
3607 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3608 case Legal:
3609 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003610 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003611 break;
3612 case Expand:
3613 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3614
3615 // Since the result is legal, we should just be able to truncate the low
3616 // part of the source.
3617 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3618 break;
3619 case Promote:
3620 Result = PromoteOp(Node->getOperand(0));
3621 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3622 break;
3623 }
3624 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003625
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003626 case ISD::FP_TO_SINT:
3627 case ISD::FP_TO_UINT:
3628 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3629 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003630 Tmp1 = LegalizeOp(Node->getOperand(0));
3631
Chris Lattner1618beb2005-07-29 00:11:56 +00003632 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3633 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003634 case TargetLowering::Custom:
3635 isCustom = true;
3636 // FALLTHROUGH
3637 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003638 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003639 if (isCustom) {
3640 Tmp1 = TLI.LowerOperation(Result, DAG);
3641 if (Tmp1.Val) Result = Tmp1;
3642 }
3643 break;
3644 case TargetLowering::Promote:
3645 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3646 Node->getOpcode() == ISD::FP_TO_SINT);
3647 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003648 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003649 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3650 SDOperand True, False;
3651 MVT::ValueType VT = Node->getOperand(0).getValueType();
3652 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003653 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003654 const uint64_t zero[] = {0, 0};
3655 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3656 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003657 (void)apf.convertFromZeroExtendedInteger
3658 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003659 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003660 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3661 Node->getOperand(0), Tmp2, ISD::SETLT);
3662 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3663 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003664 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003665 Tmp2));
3666 False = DAG.getNode(ISD::XOR, NVT, False,
3667 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003668 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3669 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003670 } else {
3671 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3672 }
3673 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003674 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003675 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003676 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003677 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003678 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003679 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003680 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003681 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3682 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3683 Node->getOperand(0), DAG.getValueType(MVT::f64));
3684 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3685 DAG.getIntPtrConstant(1));
3686 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3687 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003688 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3689 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3690 Tmp2 = DAG.getConstantFP(apf, OVT);
3691 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3692 // FIXME: generated code sucks.
3693 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3694 DAG.getNode(ISD::ADD, MVT::i32,
3695 DAG.getNode(ISD::FP_TO_SINT, VT,
3696 DAG.getNode(ISD::FSUB, OVT,
3697 Node->getOperand(0), Tmp2)),
3698 DAG.getConstant(0x80000000, MVT::i32)),
3699 DAG.getNode(ISD::FP_TO_SINT, VT,
3700 Node->getOperand(0)),
3701 DAG.getCondCode(ISD::SETGE));
3702 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003703 break;
3704 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003705 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003706 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003707 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003708 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003709 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003710 LC = (VT == MVT::i32)
3711 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003712 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003713 LC = (VT == MVT::i32)
3714 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003715 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003716 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003717 LC = RTLIB::FPTOSINT_F80_I64;
3718 }
3719 else if (OVT == MVT::ppcf128) {
3720 assert(VT == MVT::i64);
3721 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003722 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003723 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003724 }
3725 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003726 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003727 LC = (VT == MVT::i32)
3728 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003729 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003730 LC = (VT == MVT::i32)
3731 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003732 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003733 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003734 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3735 }
3736 else if (OVT == MVT::ppcf128) {
3737 assert(VT == MVT::i64);
3738 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003739 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003740 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003741 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003742 default: assert(0 && "Unreachable!");
3743 }
3744 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003745 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3746 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003747 break;
3748 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003749 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003750 Tmp1 = PromoteOp(Node->getOperand(0));
3751 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3752 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003753 break;
3754 }
3755 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003756
Chris Lattnerf2670a82008-01-16 06:57:07 +00003757 case ISD::FP_EXTEND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003758 MVT::ValueType DstVT = Op.getValueType();
3759 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3760 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3761 // The only other way we can lower this is to turn it into a STORE,
3762 // LOAD pair, targetting a temporary location (a stack slot).
3763 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3764 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003765 }
3766 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3767 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3768 case Legal:
3769 Tmp1 = LegalizeOp(Node->getOperand(0));
3770 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3771 break;
3772 case Promote:
3773 Tmp1 = PromoteOp(Node->getOperand(0));
3774 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3775 break;
3776 }
3777 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003778 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003779 case ISD::FP_ROUND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003780 MVT::ValueType DstVT = Op.getValueType();
3781 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3782 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3783 if (SrcVT == MVT::ppcf128) {
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003784 SDOperand Lo;
3785 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003786 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003787 if (DstVT!=MVT::f64)
3788 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003789 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003790 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003791 // The only other way we can lower this is to turn it into a STORE,
3792 // LOAD pair, targetting a temporary location (a stack slot).
3793 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3794 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003795 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003796 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3797 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3798 case Legal:
3799 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003800 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003801 break;
3802 case Promote:
3803 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003804 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3805 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003806 break;
3807 }
3808 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003809 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003810 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003811 case ISD::ZERO_EXTEND:
3812 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003813 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003814 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003815 case Legal:
3816 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003817 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003818 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003819 case Promote:
3820 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003821 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003822 Tmp1 = PromoteOp(Node->getOperand(0));
3823 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003824 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003825 case ISD::ZERO_EXTEND:
3826 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003827 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003828 Result = DAG.getZeroExtendInReg(Result,
3829 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003830 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003831 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003832 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003833 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003834 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003835 Result,
3836 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003837 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003838 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003839 }
3840 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003841 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003842 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003843 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003844 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003845
3846 // If this operation is not supported, convert it to a shl/shr or load/store
3847 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003848 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3849 default: assert(0 && "This action not supported for this op yet!");
3850 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003851 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003852 break;
3853 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003854 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003855 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003856 // NOTE: we could fall back on load/store here too for targets without
3857 // SAR. However, it is doubtful that any exist.
3858 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3859 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003860 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003861 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3862 Node->getOperand(0), ShiftCst);
3863 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3864 Result, ShiftCst);
3865 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003866 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003867 // EXTLOAD pair, targetting a temporary location (a stack slot).
3868
3869 // NOTE: there is a choice here between constantly creating new stack
3870 // slots and always reusing the same one. We currently always create
3871 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00003872 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3873 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00003874 } else {
3875 assert(0 && "Unknown op");
3876 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003877 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003878 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003879 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003880 }
Duncan Sands36397f52007-07-27 12:58:54 +00003881 case ISD::TRAMPOLINE: {
3882 SDOperand Ops[6];
3883 for (unsigned i = 0; i != 6; ++i)
3884 Ops[i] = LegalizeOp(Node->getOperand(i));
3885 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3886 // The only option for this node is to custom lower it.
3887 Result = TLI.LowerOperation(Result, DAG);
3888 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003889
3890 // Since trampoline produces two values, make sure to remember that we
3891 // legalized both of them.
3892 Tmp1 = LegalizeOp(Result.getValue(1));
3893 Result = LegalizeOp(Result);
3894 AddLegalizedOperand(SDOperand(Node, 0), Result);
3895 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3896 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003897 }
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003898 case ISD::FLT_ROUNDS: {
3899 MVT::ValueType VT = Node->getValueType(0);
3900 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3901 default: assert(0 && "This action not supported for this op yet!");
3902 case TargetLowering::Custom:
3903 Result = TLI.LowerOperation(Op, DAG);
3904 if (Result.Val) break;
3905 // Fall Thru
3906 case TargetLowering::Legal:
3907 // If this operation is not supported, lower it to constant 1
3908 Result = DAG.getConstant(1, VT);
3909 break;
3910 }
3911 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003912 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003913 MVT::ValueType VT = Node->getValueType(0);
3914 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3915 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003916 case TargetLowering::Legal:
3917 Tmp1 = LegalizeOp(Node->getOperand(0));
3918 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3919 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003920 case TargetLowering::Custom:
3921 Result = TLI.LowerOperation(Op, DAG);
3922 if (Result.Val) break;
3923 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003924 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003925 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003926 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003927 TargetLowering::ArgListTy Args;
3928 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner41bab0b2008-01-15 21:58:08 +00003929 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00003930 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3931 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003932 Result = CallResult.second;
3933 break;
3934 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003935 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003936 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003937 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003938
Chris Lattner4ddd2832006-04-08 04:13:17 +00003939 assert(Result.getValueType() == Op.getValueType() &&
3940 "Bad legalization!");
3941
Chris Lattner456a93a2006-01-28 07:39:30 +00003942 // Make sure that the generated code is itself legal.
3943 if (Result != Op)
3944 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003945
Chris Lattner45982da2005-05-12 16:53:42 +00003946 // Note that LegalizeOp may be reentered even from single-use nodes, which
3947 // means that we always must cache transformed nodes.
3948 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003949 return Result;
3950}
3951
Chris Lattner8b6fa222005-01-15 22:16:26 +00003952/// PromoteOp - Given an operation that produces a value in an invalid type,
3953/// promote it to compute the value into a larger type. The produced value will
3954/// have the correct bits for the low portion of the register, but no guarantee
3955/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003956SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3957 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003958 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003959 assert(getTypeAction(VT) == Promote &&
3960 "Caller should expand or legalize operands that are not promotable!");
3961 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3962 "Cannot promote to smaller type!");
3963
Chris Lattner03c85462005-01-15 05:21:40 +00003964 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003965 SDOperand Result;
3966 SDNode *Node = Op.Val;
3967
Chris Lattner40030bf2007-02-04 01:17:38 +00003968 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003969 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003970
Chris Lattner03c85462005-01-15 05:21:40 +00003971 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003972 case ISD::CopyFromReg:
3973 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003974 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003975#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003976 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003977#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003978 assert(0 && "Do not know how to promote this operator!");
3979 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003980 case ISD::UNDEF:
3981 Result = DAG.getNode(ISD::UNDEF, NVT);
3982 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003983 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003984 if (VT != MVT::i1)
3985 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3986 else
3987 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003988 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3989 break;
3990 case ISD::ConstantFP:
3991 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3992 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3993 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003994
Chris Lattner82fbfb62005-01-18 02:59:52 +00003995 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003996 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003997 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3998 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003999 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004000
Chris Lattner03c85462005-01-15 05:21:40 +00004001 case ISD::TRUNCATE:
4002 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4003 case Legal:
4004 Result = LegalizeOp(Node->getOperand(0));
4005 assert(Result.getValueType() >= NVT &&
4006 "This truncation doesn't make sense!");
4007 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4008 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4009 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004010 case Promote:
4011 // The truncation is not required, because we don't guarantee anything
4012 // about high bits anyway.
4013 Result = PromoteOp(Node->getOperand(0));
4014 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004015 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004016 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4017 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004018 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004019 }
4020 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004021 case ISD::SIGN_EXTEND:
4022 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004023 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004024 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4025 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4026 case Legal:
4027 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004028 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004029 break;
4030 case Promote:
4031 // Promote the reg if it's smaller.
4032 Result = PromoteOp(Node->getOperand(0));
4033 // The high bits are not guaranteed to be anything. Insert an extend.
4034 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004035 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004036 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004037 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004038 Result = DAG.getZeroExtendInReg(Result,
4039 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004040 break;
4041 }
4042 break;
Chris Lattner35481892005-12-23 00:16:34 +00004043 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004044 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4045 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004046 Result = PromoteOp(Result);
4047 break;
4048
Chris Lattner8b6fa222005-01-15 22:16:26 +00004049 case ISD::FP_EXTEND:
4050 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4051 case ISD::FP_ROUND:
4052 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4053 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4054 case Promote: assert(0 && "Unreachable with 2 FP types!");
4055 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004056 if (Node->getConstantOperandVal(1) == 0) {
4057 // Input is legal? Do an FP_ROUND_INREG.
4058 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4059 DAG.getValueType(VT));
4060 } else {
4061 // Just remove the truncate, it isn't affecting the value.
4062 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4063 Node->getOperand(1));
4064 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004065 break;
4066 }
4067 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004068 case ISD::SINT_TO_FP:
4069 case ISD::UINT_TO_FP:
4070 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4071 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004072 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004073 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004074 break;
4075
4076 case Promote:
4077 Result = PromoteOp(Node->getOperand(0));
4078 if (Node->getOpcode() == ISD::SINT_TO_FP)
4079 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004080 Result,
4081 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004082 else
Chris Lattner23993562005-04-13 02:38:47 +00004083 Result = DAG.getZeroExtendInReg(Result,
4084 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004085 // No extra round required here.
4086 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004087 break;
4088 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004089 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4090 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004091 // Round if we cannot tolerate excess precision.
4092 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004093 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4094 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004095 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004096 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004097 break;
4098
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004099 case ISD::SIGN_EXTEND_INREG:
4100 Result = PromoteOp(Node->getOperand(0));
4101 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4102 Node->getOperand(1));
4103 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004104 case ISD::FP_TO_SINT:
4105 case ISD::FP_TO_UINT:
4106 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4107 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004108 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004109 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004110 break;
4111 case Promote:
4112 // The input result is prerounded, so we don't have to do anything
4113 // special.
4114 Tmp1 = PromoteOp(Node->getOperand(0));
4115 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004116 }
Nate Begemand2558e32005-08-14 01:20:53 +00004117 // If we're promoting a UINT to a larger size, check to see if the new node
4118 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4119 // we can use that instead. This allows us to generate better code for
4120 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4121 // legal, such as PowerPC.
4122 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004123 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004124 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4125 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004126 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4127 } else {
4128 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4129 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004130 break;
4131
Chris Lattner2c8086f2005-04-02 05:00:07 +00004132 case ISD::FABS:
4133 case ISD::FNEG:
4134 Tmp1 = PromoteOp(Node->getOperand(0));
4135 assert(Tmp1.getValueType() == NVT);
4136 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4137 // NOTE: we do not have to do any extra rounding here for
4138 // NoExcessFPPrecision, because we know the input will have the appropriate
4139 // precision, and these operations don't modify precision at all.
4140 break;
4141
Chris Lattnerda6ba872005-04-28 21:44:33 +00004142 case ISD::FSQRT:
4143 case ISD::FSIN:
4144 case ISD::FCOS:
4145 Tmp1 = PromoteOp(Node->getOperand(0));
4146 assert(Tmp1.getValueType() == NVT);
4147 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004148 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004149 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4150 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004151 break;
4152
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004153 case ISD::FPOWI: {
4154 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4155 // directly as well, which may be better.
4156 Tmp1 = PromoteOp(Node->getOperand(0));
4157 assert(Tmp1.getValueType() == NVT);
4158 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4159 if (NoExcessFPPrecision)
4160 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4161 DAG.getValueType(VT));
4162 break;
4163 }
4164
Chris Lattner03c85462005-01-15 05:21:40 +00004165 case ISD::AND:
4166 case ISD::OR:
4167 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004168 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004169 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004170 case ISD::MUL:
4171 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004172 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004173 // that too is okay if they are integer operations.
4174 Tmp1 = PromoteOp(Node->getOperand(0));
4175 Tmp2 = PromoteOp(Node->getOperand(1));
4176 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4177 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004178 break;
4179 case ISD::FADD:
4180 case ISD::FSUB:
4181 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004182 Tmp1 = PromoteOp(Node->getOperand(0));
4183 Tmp2 = PromoteOp(Node->getOperand(1));
4184 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4185 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4186
4187 // Floating point operations will give excess precision that we may not be
4188 // able to tolerate. If we DO allow excess precision, just leave it,
4189 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004190 // FIXME: Why would we need to round FP ops more than integer ones?
4191 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004192 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004193 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4194 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004195 break;
4196
Chris Lattner8b6fa222005-01-15 22:16:26 +00004197 case ISD::SDIV:
4198 case ISD::SREM:
4199 // These operators require that their input be sign extended.
4200 Tmp1 = PromoteOp(Node->getOperand(0));
4201 Tmp2 = PromoteOp(Node->getOperand(1));
4202 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004203 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4204 DAG.getValueType(VT));
4205 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4206 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004207 }
4208 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4209
4210 // Perform FP_ROUND: this is probably overly pessimistic.
4211 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004212 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4213 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004214 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004215 case ISD::FDIV:
4216 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004217 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004218 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004219 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004220 case Expand: assert(0 && "not implemented");
4221 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4222 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004223 }
4224 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004225 case Expand: assert(0 && "not implemented");
4226 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4227 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004228 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004229 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4230
4231 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004232 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004233 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4234 DAG.getValueType(VT));
4235 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004236
4237 case ISD::UDIV:
4238 case ISD::UREM:
4239 // These operators require that their input be zero extended.
4240 Tmp1 = PromoteOp(Node->getOperand(0));
4241 Tmp2 = PromoteOp(Node->getOperand(1));
4242 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004243 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4244 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004245 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4246 break;
4247
4248 case ISD::SHL:
4249 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004250 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004251 break;
4252 case ISD::SRA:
4253 // The input value must be properly sign extended.
4254 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004255 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4256 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004257 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004258 break;
4259 case ISD::SRL:
4260 // The input value must be properly zero extended.
4261 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004262 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004263 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004264 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004265
4266 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004267 Tmp1 = Node->getOperand(0); // Get the chain.
4268 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004269 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4270 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4271 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4272 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00004273 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00004274 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00004275 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004276 // Increment the pointer, VAList, to the next vaarg
4277 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4278 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4279 TLI.getPointerTy()));
4280 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00004281 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4282 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00004283 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004284 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004285 }
4286 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004287 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004288 break;
4289
Evan Cheng466685d2006-10-09 20:57:25 +00004290 case ISD::LOAD: {
4291 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004292 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4293 ? ISD::EXTLOAD : LD->getExtensionType();
4294 Result = DAG.getExtLoad(ExtType, NVT,
4295 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004296 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004297 LD->getLoadedVT(),
4298 LD->isVolatile(),
4299 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004300 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004301 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004302 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004303 }
Chris Lattner03c85462005-01-15 05:21:40 +00004304 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004305 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4306 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004307 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004308 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004309 case ISD::SELECT_CC:
4310 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4311 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4312 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004313 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004314 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004315 case ISD::BSWAP:
4316 Tmp1 = Node->getOperand(0);
4317 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4318 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4319 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004320 DAG.getConstant(MVT::getSizeInBits(NVT) -
4321 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004322 TLI.getShiftAmountTy()));
4323 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004324 case ISD::CTPOP:
4325 case ISD::CTTZ:
4326 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004327 // Zero extend the argument
4328 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004329 // Perform the larger operation, then subtract if needed.
4330 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004331 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004332 case ISD::CTPOP:
4333 Result = Tmp1;
4334 break;
4335 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004336 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004337 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004338 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4339 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004340 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004341 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004342 break;
4343 case ISD::CTLZ:
4344 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004345 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004346 DAG.getConstant(MVT::getSizeInBits(NVT) -
4347 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004348 break;
4349 }
4350 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004351 case ISD::EXTRACT_SUBVECTOR:
4352 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004353 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004354 case ISD::EXTRACT_VECTOR_ELT:
4355 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4356 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004357 }
4358
4359 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004360
4361 // Make sure the result is itself legal.
4362 Result = LegalizeOp(Result);
4363
4364 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004365 AddPromotedOperand(Op, Result);
4366 return Result;
4367}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004368
Dan Gohman7f321562007-06-25 16:23:39 +00004369/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4370/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4371/// based on the vector type. The return type of this matches the element type
4372/// of the vector, which may not be legal for the target.
4373SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004374 // We know that operand #0 is the Vec vector. If the index is a constant
4375 // or if the invec is a supported hardware type, we can use it. Otherwise,
4376 // lower to a store then an indexed load.
4377 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004378 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004379
Dan Gohmane40c7b02007-09-24 15:54:53 +00004380 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004381 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004382
Dan Gohman7f321562007-06-25 16:23:39 +00004383 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4384 default: assert(0 && "This action is not supported yet!");
4385 case TargetLowering::Custom: {
4386 Vec = LegalizeOp(Vec);
4387 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4388 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4389 if (Tmp3.Val)
4390 return Tmp3;
4391 break;
4392 }
4393 case TargetLowering::Legal:
4394 if (isTypeLegal(TVT)) {
4395 Vec = LegalizeOp(Vec);
4396 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004397 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004398 }
4399 break;
4400 case TargetLowering::Expand:
4401 break;
4402 }
4403
4404 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004405 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004406 Op = ScalarizeVectorOp(Vec);
4407 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4408 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004409 SDOperand Lo, Hi;
4410 SplitVectorOp(Vec, Lo, Hi);
4411 if (CIdx->getValue() < NumElems/2) {
4412 Vec = Lo;
4413 } else {
4414 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00004415 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4416 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004417 }
Dan Gohman7f321562007-06-25 16:23:39 +00004418
Chris Lattner15972212006-03-31 17:55:51 +00004419 // It's now an extract from the appropriate high or low part. Recurse.
4420 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004421 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004422 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004423 // Store the value to a temporary stack slot, then LOAD the scalar
4424 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004425 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004426 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4427
4428 // Add the offset to the index.
4429 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4430 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4431 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004432
4433 if (MVT::getSizeInBits(Idx.getValueType()) >
4434 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004435 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004436 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004437 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004438
Dan Gohman7f321562007-06-25 16:23:39 +00004439 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4440
4441 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004442 }
Dan Gohman7f321562007-06-25 16:23:39 +00004443 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004444}
4445
Dan Gohman7f321562007-06-25 16:23:39 +00004446/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004447/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004448SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004449 // We know that operand #0 is the Vec vector. For now we assume the index
4450 // is a constant and that the extracted result is a supported hardware type.
4451 SDOperand Vec = Op.getOperand(0);
4452 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4453
Dan Gohman7f321562007-06-25 16:23:39 +00004454 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004455
4456 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4457 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004458 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004459 }
4460
4461 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4462 SDOperand Lo, Hi;
4463 SplitVectorOp(Vec, Lo, Hi);
4464 if (CIdx->getValue() < NumElems/2) {
4465 Vec = Lo;
4466 } else {
4467 Vec = Hi;
4468 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4469 }
4470
4471 // It's now an extract from the appropriate high or low part. Recurse.
4472 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004473 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004474}
4475
Nate Begeman750ac1b2006-02-01 07:19:44 +00004476/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4477/// with condition CC on the current target. This usually involves legalizing
4478/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4479/// there may be no choice but to create a new SetCC node to represent the
4480/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4481/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4482void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4483 SDOperand &RHS,
4484 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004485 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004486
4487 switch (getTypeAction(LHS.getValueType())) {
4488 case Legal:
4489 Tmp1 = LegalizeOp(LHS); // LHS
4490 Tmp2 = LegalizeOp(RHS); // RHS
4491 break;
4492 case Promote:
4493 Tmp1 = PromoteOp(LHS); // LHS
4494 Tmp2 = PromoteOp(RHS); // RHS
4495
4496 // If this is an FP compare, the operands have already been extended.
4497 if (MVT::isInteger(LHS.getValueType())) {
4498 MVT::ValueType VT = LHS.getValueType();
4499 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4500
4501 // Otherwise, we have to insert explicit sign or zero extends. Note
4502 // that we could insert sign extends for ALL conditions, but zero extend
4503 // is cheaper on many machines (an AND instead of two shifts), so prefer
4504 // it.
4505 switch (cast<CondCodeSDNode>(CC)->get()) {
4506 default: assert(0 && "Unknown integer comparison!");
4507 case ISD::SETEQ:
4508 case ISD::SETNE:
4509 case ISD::SETUGE:
4510 case ISD::SETUGT:
4511 case ISD::SETULE:
4512 case ISD::SETULT:
4513 // ALL of these operations will work if we either sign or zero extend
4514 // the operands (including the unsigned comparisons!). Zero extend is
4515 // usually a simpler/cheaper operation, so prefer it.
4516 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4517 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4518 break;
4519 case ISD::SETGE:
4520 case ISD::SETGT:
4521 case ISD::SETLT:
4522 case ISD::SETLE:
4523 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4524 DAG.getValueType(VT));
4525 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4526 DAG.getValueType(VT));
4527 break;
4528 }
4529 }
4530 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004531 case Expand: {
4532 MVT::ValueType VT = LHS.getValueType();
4533 if (VT == MVT::f32 || VT == MVT::f64) {
4534 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004535 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004536 switch (cast<CondCodeSDNode>(CC)->get()) {
4537 case ISD::SETEQ:
4538 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004539 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004540 break;
4541 case ISD::SETNE:
4542 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004543 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004544 break;
4545 case ISD::SETGE:
4546 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004547 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004548 break;
4549 case ISD::SETLT:
4550 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004551 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004552 break;
4553 case ISD::SETLE:
4554 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004555 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004556 break;
4557 case ISD::SETGT:
4558 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004559 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004560 break;
4561 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004562 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4563 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004564 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004565 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004566 break;
4567 default:
Evan Cheng56966222007-01-12 02:11:51 +00004568 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004569 switch (cast<CondCodeSDNode>(CC)->get()) {
4570 case ISD::SETONE:
4571 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004572 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004573 // Fallthrough
4574 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004575 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004576 break;
4577 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004578 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004579 break;
4580 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004581 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004582 break;
4583 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004584 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004585 break;
Evan Cheng56966222007-01-12 02:11:51 +00004586 case ISD::SETUEQ:
4587 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004588 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004589 default: assert(0 && "Unsupported FP setcc!");
4590 }
4591 }
4592
4593 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004594 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004595 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004596 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004597 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004598 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004599 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004600 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004601 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004602 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004603 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004604 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004605 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004606 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4607 Tmp2 = SDOperand();
4608 }
4609 LHS = Tmp1;
4610 RHS = Tmp2;
4611 return;
4612 }
4613
Nate Begeman750ac1b2006-02-01 07:19:44 +00004614 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4615 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004616 ExpandOp(RHS, RHSLo, RHSHi);
4617 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4618
4619 if (VT==MVT::ppcf128) {
4620 // FIXME: This generated code sucks. We want to generate
4621 // FCMP crN, hi1, hi2
4622 // BNE crN, L:
4623 // FCMP crN, lo1, lo2
4624 // The following can be improved, but not that much.
4625 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4626 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4627 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4628 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4629 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4630 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4631 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4632 Tmp2 = SDOperand();
4633 break;
4634 }
4635
4636 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004637 case ISD::SETEQ:
4638 case ISD::SETNE:
4639 if (RHSLo == RHSHi)
4640 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4641 if (RHSCST->isAllOnesValue()) {
4642 // Comparison to -1.
4643 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4644 Tmp2 = RHSLo;
4645 break;
4646 }
4647
4648 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4649 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4650 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4651 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4652 break;
4653 default:
4654 // If this is a comparison of the sign bit, just look at the top part.
4655 // X > -1, x < 0
4656 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4657 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4658 CST->getValue() == 0) || // X < 0
4659 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4660 CST->isAllOnesValue())) { // X > -1
4661 Tmp1 = LHSHi;
4662 Tmp2 = RHSHi;
4663 break;
4664 }
4665
4666 // FIXME: This generated code sucks.
4667 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004668 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004669 default: assert(0 && "Unknown integer setcc!");
4670 case ISD::SETLT:
4671 case ISD::SETULT: LowCC = ISD::SETULT; break;
4672 case ISD::SETGT:
4673 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4674 case ISD::SETLE:
4675 case ISD::SETULE: LowCC = ISD::SETULE; break;
4676 case ISD::SETGE:
4677 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4678 }
4679
4680 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4681 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4682 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4683
4684 // NOTE: on targets without efficient SELECT of bools, we can always use
4685 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004686 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4687 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4688 false, DagCombineInfo);
4689 if (!Tmp1.Val)
4690 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4691 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4692 CCCode, false, DagCombineInfo);
4693 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004694 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004695
4696 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4697 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4698 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4699 (Tmp2C && Tmp2C->getValue() == 0 &&
4700 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4701 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4702 (Tmp2C && Tmp2C->getValue() == 1 &&
4703 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4704 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4705 // low part is known false, returns high part.
4706 // For LE / GE, if high part is known false, ignore the low part.
4707 // For LT / GT, if high part is known true, ignore the low part.
4708 Tmp1 = Tmp2;
4709 Tmp2 = SDOperand();
4710 } else {
4711 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4712 ISD::SETEQ, false, DagCombineInfo);
4713 if (!Result.Val)
4714 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4715 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4716 Result, Tmp1, Tmp2));
4717 Tmp1 = Result;
4718 Tmp2 = SDOperand();
4719 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004720 }
4721 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004722 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004723 LHS = Tmp1;
4724 RHS = Tmp2;
4725}
4726
Chris Lattner1401d152008-01-16 07:45:30 +00004727/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4728/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4729/// a load from the stack slot to DestVT, extending it if needed.
4730/// The resultant code need not be legal.
4731SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4732 MVT::ValueType SlotVT,
4733 MVT::ValueType DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004734 // Create the stack frame object.
Chris Lattner1401d152008-01-16 07:45:30 +00004735 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4736
4737 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4738 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4739 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004740
Chris Lattner1401d152008-01-16 07:45:30 +00004741 // Emit a store to the stack slot. Use a truncstore if the input value is
4742 // later than DestVT.
4743 SDOperand Store;
4744 if (SrcSize > SlotSize)
4745 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0,SlotVT);
4746 else {
4747 assert(SrcSize == SlotSize && "Invalid store");
4748 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
4749 }
4750
Chris Lattner35481892005-12-23 00:16:34 +00004751 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004752 if (SlotSize == DestSize)
4753 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4754
4755 assert(SlotSize < DestSize && "Unknown extension!");
4756 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner35481892005-12-23 00:16:34 +00004757}
4758
Chris Lattner4352cc92006-04-04 17:23:26 +00004759SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4760 // Create a vector sized/aligned stack slot, store the value to element #0,
4761 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004762 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004763 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004764 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004765 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004766}
4767
4768
Chris Lattnerce872152006-03-19 06:31:19 +00004769/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004770/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004771SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4772
4773 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004774 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004775 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004776 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004777 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004778 std::map<SDOperand, std::vector<unsigned> > Values;
4779 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004780 bool isConstant = true;
4781 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4782 SplatValue.getOpcode() != ISD::UNDEF)
4783 isConstant = false;
4784
Evan Cheng033e6812006-03-24 01:17:21 +00004785 for (unsigned i = 1; i < NumElems; ++i) {
4786 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004787 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004788 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004789 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004790 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004791 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004792
4793 // If this isn't a constant element or an undef, we can't use a constant
4794 // pool load.
4795 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4796 V.getOpcode() != ISD::UNDEF)
4797 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004798 }
4799
4800 if (isOnlyLowElement) {
4801 // If the low element is an undef too, then this whole things is an undef.
4802 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4803 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4804 // Otherwise, turn this into a scalar_to_vector node.
4805 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4806 Node->getOperand(0));
4807 }
4808
Chris Lattner2eb86532006-03-24 07:29:17 +00004809 // If all elements are constants, create a load from the constant pool.
4810 if (isConstant) {
4811 MVT::ValueType VT = Node->getValueType(0);
4812 const Type *OpNTy =
4813 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4814 std::vector<Constant*> CV;
4815 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4816 if (ConstantFPSDNode *V =
4817 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004818 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004819 } else if (ConstantSDNode *V =
4820 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004821 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004822 } else {
4823 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4824 CV.push_back(UndefValue::get(OpNTy));
4825 }
4826 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004827 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004828 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004829 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004830 }
4831
Chris Lattner87100e02006-03-20 01:52:29 +00004832 if (SplatValue.Val) { // Splat of one value?
4833 // Build the shuffle constant vector: <0, 0, 0, 0>
4834 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004835 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004836 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004837 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004838 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4839 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004840
4841 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004842 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004843 // Get the splatted value into the low element of a vector register.
4844 SDOperand LowValVec =
4845 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4846
4847 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4848 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4849 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4850 SplatMask);
4851 }
4852 }
4853
Evan Cheng033e6812006-03-24 01:17:21 +00004854 // If there are only two unique elements, we may be able to turn this into a
4855 // vector shuffle.
4856 if (Values.size() == 2) {
4857 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4858 MVT::ValueType MaskVT =
4859 MVT::getIntVectorWithNumElements(NumElems);
4860 std::vector<SDOperand> MaskVec(NumElems);
4861 unsigned i = 0;
4862 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4863 E = Values.end(); I != E; ++I) {
4864 for (std::vector<unsigned>::iterator II = I->second.begin(),
4865 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004866 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004867 i += NumElems;
4868 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004869 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4870 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004871
4872 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004873 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4874 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004875 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004876 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4877 E = Values.end(); I != E; ++I) {
4878 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4879 I->first);
4880 Ops.push_back(Op);
4881 }
4882 Ops.push_back(ShuffleMask);
4883
4884 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004885 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4886 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004887 }
4888 }
Chris Lattnerce872152006-03-19 06:31:19 +00004889
4890 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4891 // aligned object on the stack, store each element into it, then load
4892 // the result as a vector.
4893 MVT::ValueType VT = Node->getValueType(0);
4894 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004895 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004896
4897 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004898 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004899 unsigned TypeByteSize =
4900 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004901 // Store (in the right endianness) the elements to memory.
4902 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4903 // Ignore undef elements.
4904 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4905
Chris Lattner841c8822006-03-22 01:46:54 +00004906 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004907
4908 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4909 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4910
Evan Cheng786225a2006-10-05 23:01:46 +00004911 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004912 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004913 }
4914
4915 SDOperand StoreChain;
4916 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004917 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4918 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004919 else
4920 StoreChain = DAG.getEntryNode();
4921
4922 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004923 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004924}
4925
Chris Lattner5b359c62005-04-02 04:00:59 +00004926void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4927 SDOperand Op, SDOperand Amt,
4928 SDOperand &Lo, SDOperand &Hi) {
4929 // Expand the subcomponents.
4930 SDOperand LHSL, LHSH;
4931 ExpandOp(Op, LHSL, LHSH);
4932
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004933 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004934 MVT::ValueType VT = LHSL.getValueType();
4935 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004936 Hi = Lo.getValue(1);
4937}
4938
4939
Chris Lattnere34b3962005-01-19 04:19:40 +00004940/// ExpandShift - Try to find a clever way to expand this shift operation out to
4941/// smaller elements. If we can't find a way that is more efficient than a
4942/// libcall on this target, return false. Otherwise, return true with the
4943/// low-parts expanded into Lo and Hi.
4944bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4945 SDOperand &Lo, SDOperand &Hi) {
4946 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4947 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004948
Chris Lattnere34b3962005-01-19 04:19:40 +00004949 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004950 SDOperand ShAmt = LegalizeOp(Amt);
4951 MVT::ValueType ShTy = ShAmt.getValueType();
4952 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4953 unsigned NVTBits = MVT::getSizeInBits(NVT);
4954
Chris Lattner7a3c8552007-10-14 20:35:12 +00004955 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004956 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4957 unsigned Cst = CN->getValue();
4958 // Expand the incoming operand to be shifted, so that we have its parts
4959 SDOperand InL, InH;
4960 ExpandOp(Op, InL, InH);
4961 switch(Opc) {
4962 case ISD::SHL:
4963 if (Cst > VTBits) {
4964 Lo = DAG.getConstant(0, NVT);
4965 Hi = DAG.getConstant(0, NVT);
4966 } else if (Cst > NVTBits) {
4967 Lo = DAG.getConstant(0, NVT);
4968 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004969 } else if (Cst == NVTBits) {
4970 Lo = DAG.getConstant(0, NVT);
4971 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004972 } else {
4973 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4974 Hi = DAG.getNode(ISD::OR, NVT,
4975 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4976 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4977 }
4978 return true;
4979 case ISD::SRL:
4980 if (Cst > VTBits) {
4981 Lo = DAG.getConstant(0, NVT);
4982 Hi = DAG.getConstant(0, NVT);
4983 } else if (Cst > NVTBits) {
4984 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4985 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004986 } else if (Cst == NVTBits) {
4987 Lo = InH;
4988 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004989 } else {
4990 Lo = DAG.getNode(ISD::OR, NVT,
4991 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4992 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4993 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4994 }
4995 return true;
4996 case ISD::SRA:
4997 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004998 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004999 DAG.getConstant(NVTBits-1, ShTy));
5000 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005001 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005002 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005003 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005004 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005005 } else if (Cst == NVTBits) {
5006 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005007 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005008 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005009 } else {
5010 Lo = DAG.getNode(ISD::OR, NVT,
5011 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5012 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5013 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5014 }
5015 return true;
5016 }
5017 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005018
5019 // Okay, the shift amount isn't constant. However, if we can tell that it is
5020 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
5021 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005022 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005023
5024 // If we know that the high bit of the shift amount is one, then we can do
5025 // this as a couple of simple shifts.
5026 if (KnownOne & Mask) {
5027 // Mask out the high bit, which we know is set.
5028 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
5029 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5030
5031 // Expand the incoming operand to be shifted, so that we have its parts
5032 SDOperand InL, InH;
5033 ExpandOp(Op, InL, InH);
5034 switch(Opc) {
5035 case ISD::SHL:
5036 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5037 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5038 return true;
5039 case ISD::SRL:
5040 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5041 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5042 return true;
5043 case ISD::SRA:
5044 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5045 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5046 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5047 return true;
5048 }
5049 }
5050
5051 // If we know that the high bit of the shift amount is zero, then we can do
5052 // this as a couple of simple shifts.
5053 if (KnownZero & Mask) {
5054 // Compute 32-amt.
5055 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5056 DAG.getConstant(NVTBits, Amt.getValueType()),
5057 Amt);
5058
5059 // Expand the incoming operand to be shifted, so that we have its parts
5060 SDOperand InL, InH;
5061 ExpandOp(Op, InL, InH);
5062 switch(Opc) {
5063 case ISD::SHL:
5064 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5065 Hi = DAG.getNode(ISD::OR, NVT,
5066 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5067 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5068 return true;
5069 case ISD::SRL:
5070 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5071 Lo = DAG.getNode(ISD::OR, NVT,
5072 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5073 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5074 return true;
5075 case ISD::SRA:
5076 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5077 Lo = DAG.getNode(ISD::OR, NVT,
5078 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5079 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5080 return true;
5081 }
5082 }
5083
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005084 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005085}
Chris Lattner77e77a62005-01-21 06:05:23 +00005086
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005087
Chris Lattner77e77a62005-01-21 06:05:23 +00005088// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5089// does not fit into a register, return the lo part and set the hi part to the
5090// by-reg argument. If it does fit into a single register, return the result
5091// and leave the Hi part unset.
5092SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00005093 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005094 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5095 // The input chain to this libcall is the entry node of the function.
5096 // Legalizing the call will automatically add the previous call to the
5097 // dependence.
5098 SDOperand InChain = DAG.getEntryNode();
5099
Chris Lattner77e77a62005-01-21 06:05:23 +00005100 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005101 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005102 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5103 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5104 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00005105 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005106 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005107 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005108 }
5109 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005110
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005111 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00005112 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005113 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00005114 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00005115 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005116
Chris Lattner6831a812006-02-13 09:18:02 +00005117 // Legalize the call sequence, starting with the chain. This will advance
5118 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5119 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5120 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00005121 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005122 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005123 default: assert(0 && "Unknown thing");
5124 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005125 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005126 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005127 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005128 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005129 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005130 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005131 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005132}
5133
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005134
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005135/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5136///
Chris Lattner77e77a62005-01-21 06:05:23 +00005137SDOperand SelectionDAGLegalize::
5138ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005139 assert(getTypeAction(Source.getValueType()) == Expand &&
5140 "This is not an expansion!");
5141 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
5142
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005143 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00005144 assert(Source.getValueType() == MVT::i64 &&
5145 "This only works for 64-bit -> FP");
5146 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
5147 // incoming integer is set. To handle this, we dynamically test to see if
5148 // it is set, and, if so, add a fudge factor.
5149 SDOperand Lo, Hi;
5150 ExpandOp(Source, Lo, Hi);
5151
Chris Lattner66de05b2005-05-13 04:45:13 +00005152 // If this is unsigned, and not supported, first perform the conversion to
5153 // signed, then adjust the result if the sign bit is set.
5154 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
5155 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
5156
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005157 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5158 DAG.getConstant(0, Hi.getValueType()),
5159 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005160 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005161 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5162 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005163 uint64_t FF = 0x5f800000ULL;
5164 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005165 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005166
Chris Lattner5839bf22005-08-26 17:15:30 +00005167 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005168 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5169 SDOperand FudgeInReg;
5170 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005171 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005172 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005173 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005174 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005175 CPIdx, NULL, 0, MVT::f32);
5176 else
5177 assert(0 && "Unexpected conversion");
5178
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005179 MVT::ValueType SCVT = SignedConv.getValueType();
5180 if (SCVT != DestTy) {
5181 // Destination type needs to be expanded as well. The FADD now we are
5182 // constructing will be expanded into a libcall.
5183 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5184 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5185 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5186 SignedConv, SignedConv.getValue(1));
5187 }
5188 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5189 }
Chris Lattner473a9902005-09-29 06:44:39 +00005190 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005191 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005192
Chris Lattnera88a2602005-05-14 05:33:54 +00005193 // Check to see if the target has a custom way to lower this. If so, use it.
5194 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5195 default: assert(0 && "This action not implemented for this operation!");
5196 case TargetLowering::Legal:
5197 case TargetLowering::Expand:
5198 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005199 case TargetLowering::Custom: {
5200 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5201 Source), DAG);
5202 if (NV.Val)
5203 return LegalizeOp(NV);
5204 break; // The target decided this was legal after all
5205 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005206 }
5207
Chris Lattner13689e22005-05-12 07:00:44 +00005208 // Expand the source, then glue it back together for the call. We must expand
5209 // the source in case it is shared (this pass of legalize must traverse it).
5210 SDOperand SrcLo, SrcHi;
5211 ExpandOp(Source, SrcLo, SrcHi);
5212 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5213
Evan Cheng56966222007-01-12 02:11:51 +00005214 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005215 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005216 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005217 else {
5218 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00005219 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005220 }
Chris Lattner6831a812006-02-13 09:18:02 +00005221
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005222 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005223 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5224 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00005225 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5226 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00005227}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005228
Chris Lattner22cde6a2006-01-28 08:25:58 +00005229/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5230/// INT_TO_FP operation of the specified operand when the target requests that
5231/// we expand it. At this point, we know that the result and operand types are
5232/// legal for the target.
5233SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5234 SDOperand Op0,
5235 MVT::ValueType DestVT) {
5236 if (Op0.getValueType() == MVT::i32) {
5237 // simple 32-bit [signed|unsigned] integer to float/double expansion
5238
Chris Lattner23594d42008-01-16 07:03:22 +00005239 // Get the stack frame index of a 8 byte buffer.
5240 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5241
Chris Lattner22cde6a2006-01-28 08:25:58 +00005242 // word offset constant for Hi/Lo address computation
5243 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5244 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005245 SDOperand Hi = StackSlot;
5246 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5247 if (TLI.isLittleEndian())
5248 std::swap(Hi, Lo);
5249
Chris Lattner22cde6a2006-01-28 08:25:58 +00005250 // if signed map to unsigned space
5251 SDOperand Op0Mapped;
5252 if (isSigned) {
5253 // constant used to invert sign bit (signed to unsigned mapping)
5254 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5255 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5256 } else {
5257 Op0Mapped = Op0;
5258 }
5259 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005260 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005261 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005262 // initial hi portion of constructed double
5263 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5264 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005265 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005266 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005267 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005268 // FP constant to bias correct the final result
5269 SDOperand Bias = DAG.getConstantFP(isSigned ?
5270 BitsToDouble(0x4330000080000000ULL)
5271 : BitsToDouble(0x4330000000000000ULL),
5272 MVT::f64);
5273 // subtract the bias
5274 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5275 // final result
5276 SDOperand Result;
5277 // handle final rounding
5278 if (DestVT == MVT::f64) {
5279 // do nothing
5280 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005281 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005282 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5283 DAG.getIntPtrConstant(0));
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005284 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5285 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005286 }
5287 return Result;
5288 }
5289 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5290 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5291
5292 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5293 DAG.getConstant(0, Op0.getValueType()),
5294 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005295 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005296 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5297 SignSet, Four, Zero);
5298
5299 // If the sign bit of the integer is set, the large number will be treated
5300 // as a negative number. To counteract this, the dynamic code adds an
5301 // offset depending on the data type.
5302 uint64_t FF;
5303 switch (Op0.getValueType()) {
5304 default: assert(0 && "Unsupported integer type!");
5305 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5306 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5307 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5308 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5309 }
5310 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005311 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005312
5313 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5314 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5315 SDOperand FudgeInReg;
5316 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00005317 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005318 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005319 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005320 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00005321 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005322 }
5323
5324 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5325}
5326
5327/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5328/// *INT_TO_FP operation of the specified operand when the target requests that
5329/// we promote it. At this point, we know that the result and operand types are
5330/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5331/// operation that takes a larger input.
5332SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5333 MVT::ValueType DestVT,
5334 bool isSigned) {
5335 // First step, figure out the appropriate *INT_TO_FP operation to use.
5336 MVT::ValueType NewInTy = LegalOp.getValueType();
5337
5338 unsigned OpToUse = 0;
5339
5340 // Scan for the appropriate larger type to use.
5341 while (1) {
5342 NewInTy = (MVT::ValueType)(NewInTy+1);
5343 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5344
5345 // If the target supports SINT_TO_FP of this type, use it.
5346 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5347 default: break;
5348 case TargetLowering::Legal:
5349 if (!TLI.isTypeLegal(NewInTy))
5350 break; // Can't use this datatype.
5351 // FALL THROUGH.
5352 case TargetLowering::Custom:
5353 OpToUse = ISD::SINT_TO_FP;
5354 break;
5355 }
5356 if (OpToUse) break;
5357 if (isSigned) continue;
5358
5359 // If the target supports UINT_TO_FP of this type, use it.
5360 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5361 default: break;
5362 case TargetLowering::Legal:
5363 if (!TLI.isTypeLegal(NewInTy))
5364 break; // Can't use this datatype.
5365 // FALL THROUGH.
5366 case TargetLowering::Custom:
5367 OpToUse = ISD::UINT_TO_FP;
5368 break;
5369 }
5370 if (OpToUse) break;
5371
5372 // Otherwise, try a larger type.
5373 }
5374
5375 // Okay, we found the operation and type to use. Zero extend our input to the
5376 // desired type then run the operation on it.
5377 return DAG.getNode(OpToUse, DestVT,
5378 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5379 NewInTy, LegalOp));
5380}
5381
5382/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5383/// FP_TO_*INT operation of the specified operand when the target requests that
5384/// we promote it. At this point, we know that the result and operand types are
5385/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5386/// operation that returns a larger result.
5387SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5388 MVT::ValueType DestVT,
5389 bool isSigned) {
5390 // First step, figure out the appropriate FP_TO*INT operation to use.
5391 MVT::ValueType NewOutTy = DestVT;
5392
5393 unsigned OpToUse = 0;
5394
5395 // Scan for the appropriate larger type to use.
5396 while (1) {
5397 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5398 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5399
5400 // If the target supports FP_TO_SINT returning this type, use it.
5401 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5402 default: break;
5403 case TargetLowering::Legal:
5404 if (!TLI.isTypeLegal(NewOutTy))
5405 break; // Can't use this datatype.
5406 // FALL THROUGH.
5407 case TargetLowering::Custom:
5408 OpToUse = ISD::FP_TO_SINT;
5409 break;
5410 }
5411 if (OpToUse) break;
5412
5413 // If the target supports FP_TO_UINT of this type, use it.
5414 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5415 default: break;
5416 case TargetLowering::Legal:
5417 if (!TLI.isTypeLegal(NewOutTy))
5418 break; // Can't use this datatype.
5419 // FALL THROUGH.
5420 case TargetLowering::Custom:
5421 OpToUse = ISD::FP_TO_UINT;
5422 break;
5423 }
5424 if (OpToUse) break;
5425
5426 // Otherwise, try a larger type.
5427 }
5428
Chris Lattner27a6c732007-11-24 07:07:01 +00005429
5430 // Okay, we found the operation and type to use.
5431 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5432
5433 // If the operation produces an invalid type, it must be custom lowered. Use
5434 // the target lowering hooks to expand it. Just keep the low part of the
5435 // expanded operation, we know that we're truncating anyway.
5436 if (getTypeAction(NewOutTy) == Expand) {
5437 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5438 assert(Operation.Val && "Didn't return anything");
5439 }
5440
5441 // Truncate the result of the extended FP_TO_*INT operation to the desired
5442 // size.
5443 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005444}
5445
5446/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5447///
5448SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5449 MVT::ValueType VT = Op.getValueType();
5450 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5451 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5452 switch (VT) {
5453 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5454 case MVT::i16:
5455 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5456 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5457 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5458 case MVT::i32:
5459 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5460 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5461 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5462 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5463 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5464 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5465 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5466 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5467 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5468 case MVT::i64:
5469 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5470 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5471 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5472 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5473 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5474 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5475 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5476 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5477 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5478 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5479 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5480 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5481 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5482 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5483 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5484 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5485 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5486 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5487 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5488 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5489 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5490 }
5491}
5492
5493/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5494///
5495SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5496 switch (Opc) {
5497 default: assert(0 && "Cannot expand this yet!");
5498 case ISD::CTPOP: {
5499 static const uint64_t mask[6] = {
5500 0x5555555555555555ULL, 0x3333333333333333ULL,
5501 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5502 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5503 };
5504 MVT::ValueType VT = Op.getValueType();
5505 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005506 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005507 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5508 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5509 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5510 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5511 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5512 DAG.getNode(ISD::AND, VT,
5513 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5514 }
5515 return Op;
5516 }
5517 case ISD::CTLZ: {
5518 // for now, we do this:
5519 // x = x | (x >> 1);
5520 // x = x | (x >> 2);
5521 // ...
5522 // x = x | (x >>16);
5523 // x = x | (x >>32); // for 64-bit input
5524 // return popcount(~x);
5525 //
5526 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5527 MVT::ValueType VT = Op.getValueType();
5528 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005529 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005530 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5531 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5532 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5533 }
5534 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5535 return DAG.getNode(ISD::CTPOP, VT, Op);
5536 }
5537 case ISD::CTTZ: {
5538 // for now, we use: { return popcount(~x & (x - 1)); }
5539 // unless the target has ctlz but not ctpop, in which case we use:
5540 // { return 32 - nlz(~x & (x-1)); }
5541 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5542 MVT::ValueType VT = Op.getValueType();
5543 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5544 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5545 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5546 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5547 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5548 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5549 TLI.isOperationLegal(ISD::CTLZ, VT))
5550 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005551 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005552 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5553 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5554 }
5555 }
5556}
Chris Lattnere34b3962005-01-19 04:19:40 +00005557
Chris Lattner3e928bb2005-01-07 07:47:09 +00005558/// ExpandOp - Expand the specified SDOperand into its two component pieces
5559/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5560/// LegalizeNodes map is filled in for any results that are not expanded, the
5561/// ExpandedNodes map is filled in for any results that are expanded, and the
5562/// Lo/Hi values are returned.
5563void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5564 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005565 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005566 SDNode *Node = Op.Val;
5567 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005568 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005569 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005570 "Cannot expand to FP value or to larger int value!");
5571
Chris Lattner6fdcb252005-09-02 20:32:45 +00005572 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005573 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005574 = ExpandedNodes.find(Op);
5575 if (I != ExpandedNodes.end()) {
5576 Lo = I->second.first;
5577 Hi = I->second.second;
5578 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005579 }
5580
Chris Lattner3e928bb2005-01-07 07:47:09 +00005581 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005582 case ISD::CopyFromReg:
5583 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005584 case ISD::FP_ROUND_INREG:
5585 if (VT == MVT::ppcf128 &&
5586 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5587 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005588 SDOperand SrcLo, SrcHi, Src;
5589 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5590 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5591 SDOperand Result = TLI.LowerOperation(
5592 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005593 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5594 Lo = Result.Val->getOperand(0);
5595 Hi = Result.Val->getOperand(1);
5596 break;
5597 }
5598 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005599 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005600#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005601 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005602#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005603 assert(0 && "Do not know how to expand this operator!");
5604 abort();
Dale Johannesen25f1d082007-10-31 00:32:36 +00005605 case ISD::EXTRACT_VECTOR_ELT:
5606 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5607 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5608 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5609 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005610 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005611 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005612 Lo = DAG.getNode(ISD::UNDEF, NVT);
5613 Hi = DAG.getNode(ISD::UNDEF, NVT);
5614 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005615 case ISD::Constant: {
5616 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5617 Lo = DAG.getConstant(Cst, NVT);
5618 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5619 break;
5620 }
Evan Cheng00495212006-12-12 21:32:44 +00005621 case ISD::ConstantFP: {
5622 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005623 if (CFP->getValueType(0) == MVT::ppcf128) {
5624 APInt api = CFP->getValueAPF().convertToAPInt();
5625 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5626 MVT::f64);
5627 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5628 MVT::f64);
5629 break;
5630 }
Evan Cheng279101e2006-12-12 22:19:28 +00005631 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005632 if (getTypeAction(Lo.getValueType()) == Expand)
5633 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005634 break;
5635 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005636 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005637 // Return the operands.
5638 Lo = Node->getOperand(0);
5639 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005640 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005641
5642 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005643 if (Node->getNumValues() == 1) {
5644 ExpandOp(Op.getOperand(0), Lo, Hi);
5645 break;
5646 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005647 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5648 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5649 Op.getValue(1).getValueType() == MVT::Other &&
5650 "unhandled MERGE_VALUES");
5651 ExpandOp(Op.getOperand(0), Lo, Hi);
5652 // Remember that we legalized the chain.
5653 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5654 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005655
5656 case ISD::SIGN_EXTEND_INREG:
5657 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005658 // sext_inreg the low part if needed.
5659 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5660
5661 // The high part gets the sign extension from the lo-part. This handles
5662 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005663 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5664 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5665 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005666 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005667
Nate Begemand88fc032006-01-14 03:14:10 +00005668 case ISD::BSWAP: {
5669 ExpandOp(Node->getOperand(0), Lo, Hi);
5670 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5671 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5672 Lo = TempLo;
5673 break;
5674 }
5675
Chris Lattneredb1add2005-05-11 04:51:16 +00005676 case ISD::CTPOP:
5677 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005678 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5679 DAG.getNode(ISD::CTPOP, NVT, Lo),
5680 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005681 Hi = DAG.getConstant(0, NVT);
5682 break;
5683
Chris Lattner39a8f332005-05-12 19:05:01 +00005684 case ISD::CTLZ: {
5685 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005686 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005687 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5688 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005689 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5690 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005691 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5692 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5693
5694 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5695 Hi = DAG.getConstant(0, NVT);
5696 break;
5697 }
5698
5699 case ISD::CTTZ: {
5700 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005701 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005702 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5703 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005704 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5705 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005706 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5707 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5708
5709 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5710 Hi = DAG.getConstant(0, NVT);
5711 break;
5712 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005713
Nate Begemanacc398c2006-01-25 18:21:52 +00005714 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005715 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5716 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005717 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5718 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5719
5720 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005721 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005722 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5723 if (!TLI.isLittleEndian())
5724 std::swap(Lo, Hi);
5725 break;
5726 }
5727
Chris Lattner3e928bb2005-01-07 07:47:09 +00005728 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005729 LoadSDNode *LD = cast<LoadSDNode>(Node);
5730 SDOperand Ch = LD->getChain(); // Legalize the chain.
5731 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5732 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005733 int SVOffset = LD->getSrcValueOffset();
5734 unsigned Alignment = LD->getAlignment();
5735 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005736
Evan Cheng466685d2006-10-09 20:57:25 +00005737 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005738 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5739 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005740 if (VT == MVT::f32 || VT == MVT::f64) {
5741 // f32->i32 or f64->i64 one to one expansion.
5742 // Remember that we legalized the chain.
5743 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005744 // Recursively expand the new load.
5745 if (getTypeAction(NVT) == Expand)
5746 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005747 break;
5748 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005749
Evan Cheng466685d2006-10-09 20:57:25 +00005750 // Increment the pointer to the other half.
5751 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5752 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00005753 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005754 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005755 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005756 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5757 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005758
Evan Cheng466685d2006-10-09 20:57:25 +00005759 // Build a factor node to remember that this load is independent of the
5760 // other one.
5761 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5762 Hi.getValue(1));
5763
5764 // Remember that we legalized the chain.
5765 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5766 if (!TLI.isLittleEndian())
5767 std::swap(Lo, Hi);
5768 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005769 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005770
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005771 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5772 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005773 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5774 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005775 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005776 // Remember that we legalized the chain.
5777 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5778 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5779 break;
5780 }
Evan Cheng466685d2006-10-09 20:57:25 +00005781
5782 if (EVT == NVT)
5783 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005784 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005785 else
5786 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005787 SVOffset, EVT, isVolatile,
5788 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005789
5790 // Remember that we legalized the chain.
5791 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5792
5793 if (ExtType == ISD::SEXTLOAD) {
5794 // The high part is obtained by SRA'ing all but one of the bits of the
5795 // lo part.
5796 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5797 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5798 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5799 } else if (ExtType == ISD::ZEXTLOAD) {
5800 // The high part is just a zero.
5801 Hi = DAG.getConstant(0, NVT);
5802 } else /* if (ExtType == ISD::EXTLOAD) */ {
5803 // The high part is undefined.
5804 Hi = DAG.getNode(ISD::UNDEF, NVT);
5805 }
5806 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005807 break;
5808 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005809 case ISD::AND:
5810 case ISD::OR:
5811 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5812 SDOperand LL, LH, RL, RH;
5813 ExpandOp(Node->getOperand(0), LL, LH);
5814 ExpandOp(Node->getOperand(1), RL, RH);
5815 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5816 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5817 break;
5818 }
5819 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005820 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005821 ExpandOp(Node->getOperand(1), LL, LH);
5822 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005823 if (getTypeAction(NVT) == Expand)
5824 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005825 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005826 if (VT != MVT::f32)
5827 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005828 break;
5829 }
Nate Begeman9373a812005-08-10 20:51:12 +00005830 case ISD::SELECT_CC: {
5831 SDOperand TL, TH, FL, FH;
5832 ExpandOp(Node->getOperand(2), TL, TH);
5833 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005834 if (getTypeAction(NVT) == Expand)
5835 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005836 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5837 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005838 if (VT != MVT::f32)
5839 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5840 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005841 break;
5842 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005843 case ISD::ANY_EXTEND:
5844 // The low part is any extension of the input (which degenerates to a copy).
5845 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5846 // The high part is undefined.
5847 Hi = DAG.getNode(ISD::UNDEF, NVT);
5848 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005849 case ISD::SIGN_EXTEND: {
5850 // The low part is just a sign extension of the input (which degenerates to
5851 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005852 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005853
Chris Lattner3e928bb2005-01-07 07:47:09 +00005854 // The high part is obtained by SRA'ing all but one of the bits of the lo
5855 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005856 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005857 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5858 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005859 break;
5860 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005861 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005862 // The low part is just a zero extension of the input (which degenerates to
5863 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005864 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005865
Chris Lattner3e928bb2005-01-07 07:47:09 +00005866 // The high part is just a zero.
5867 Hi = DAG.getConstant(0, NVT);
5868 break;
Chris Lattner35481892005-12-23 00:16:34 +00005869
Chris Lattner4c948eb2007-02-13 23:55:16 +00005870 case ISD::TRUNCATE: {
5871 // The input value must be larger than this value. Expand *it*.
5872 SDOperand NewLo;
5873 ExpandOp(Node->getOperand(0), NewLo, Hi);
5874
5875 // The low part is now either the right size, or it is closer. If not the
5876 // right size, make an illegal truncate so we recursively expand it.
5877 if (NewLo.getValueType() != Node->getValueType(0))
5878 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5879 ExpandOp(NewLo, Lo, Hi);
5880 break;
5881 }
5882
Chris Lattner35481892005-12-23 00:16:34 +00005883 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005884 SDOperand Tmp;
5885 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5886 // If the target wants to, allow it to lower this itself.
5887 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5888 case Expand: assert(0 && "cannot expand FP!");
5889 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5890 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5891 }
5892 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5893 }
5894
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005895 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005896 if (VT == MVT::f32 || VT == MVT::f64) {
5897 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005898 if (getTypeAction(NVT) == Expand)
5899 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005900 break;
5901 }
5902
5903 // If source operand will be expanded to the same type as VT, i.e.
5904 // i64 <- f64, i32 <- f32, expand the source operand instead.
5905 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5906 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5907 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005908 break;
5909 }
5910
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005911 // Turn this into a load/store pair by default.
5912 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00005913 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005914
Chris Lattner35481892005-12-23 00:16:34 +00005915 ExpandOp(Tmp, Lo, Hi);
5916 break;
5917 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005918
Chris Lattner27a6c732007-11-24 07:07:01 +00005919 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00005920 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5921 TargetLowering::Custom &&
5922 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00005923 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5924 assert(Tmp.Val && "Node must be custom expanded!");
5925 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00005926 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00005927 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005928 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005929 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005930
Chris Lattner4e6c7462005-01-08 19:27:05 +00005931 // These operators cannot be expanded directly, emit them as calls to
5932 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005933 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005934 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005935 SDOperand Op;
5936 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5937 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005938 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5939 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005940 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005941
Chris Lattnerf20d1832005-07-30 01:40:57 +00005942 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5943
Chris Lattner80a3e942005-07-29 00:33:32 +00005944 // Now that the custom expander is done, expand the result, which is still
5945 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005946 if (Op.Val) {
5947 ExpandOp(Op, Lo, Hi);
5948 break;
5949 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005950 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005951
Dale Johannesen161e8972007-10-05 20:04:43 +00005952 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005953 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005954 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005955 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005956 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005957 else if (Node->getOperand(0).getValueType() == MVT::f80)
5958 LC = RTLIB::FPTOSINT_F80_I64;
5959 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5960 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005961 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5962 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005963 break;
Evan Cheng56966222007-01-12 02:11:51 +00005964 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005965
Evan Cheng56966222007-01-12 02:11:51 +00005966 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005967 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005968 SDOperand Op;
5969 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5970 case Expand: assert(0 && "cannot expand FP!");
5971 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5972 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5973 }
5974
5975 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5976
5977 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005978 if (Op.Val) {
5979 ExpandOp(Op, Lo, Hi);
5980 break;
5981 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005982 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005983
Evan Chengdaccea182007-10-05 01:09:32 +00005984 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005985 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005986 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00005987 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005988 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005989 else if (Node->getOperand(0).getValueType() == MVT::f80)
5990 LC = RTLIB::FPTOUINT_F80_I64;
5991 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5992 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005993 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5994 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005995 break;
Evan Cheng56966222007-01-12 02:11:51 +00005996 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005997
Evan Cheng05a2d562006-01-09 18:31:59 +00005998 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005999 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006000 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006001 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006002 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006003 Op = TLI.LowerOperation(Op, DAG);
6004 if (Op.Val) {
6005 // Now that the custom expander is done, expand the result, which is
6006 // still VT.
6007 ExpandOp(Op, Lo, Hi);
6008 break;
6009 }
6010 }
6011
Chris Lattner79980b02006-09-13 03:50:39 +00006012 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6013 // this X << 1 as X+X.
6014 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6015 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6016 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6017 SDOperand LoOps[2], HiOps[3];
6018 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6019 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6020 LoOps[1] = LoOps[0];
6021 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6022
6023 HiOps[1] = HiOps[0];
6024 HiOps[2] = Lo.getValue(1);
6025 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6026 break;
6027 }
6028 }
6029
Chris Lattnere34b3962005-01-19 04:19:40 +00006030 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006031 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006032 break;
Chris Lattner47599822005-04-02 03:38:53 +00006033
6034 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006035 TargetLowering::LegalizeAction Action =
6036 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6037 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6038 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006039 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006040 break;
6041 }
6042
Chris Lattnere34b3962005-01-19 04:19:40 +00006043 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006044 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6045 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006046 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006047 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006048
Evan Cheng05a2d562006-01-09 18:31:59 +00006049 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006050 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006051 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006052 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006053 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006054 Op = TLI.LowerOperation(Op, DAG);
6055 if (Op.Val) {
6056 // Now that the custom expander is done, expand the result, which is
6057 // still VT.
6058 ExpandOp(Op, Lo, Hi);
6059 break;
6060 }
6061 }
6062
Chris Lattnere34b3962005-01-19 04:19:40 +00006063 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006064 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006065 break;
Chris Lattner47599822005-04-02 03:38:53 +00006066
6067 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006068 TargetLowering::LegalizeAction Action =
6069 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6070 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6071 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006072 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006073 break;
6074 }
6075
Chris Lattnere34b3962005-01-19 04:19:40 +00006076 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006077 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6078 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006079 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006080 }
6081
6082 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006083 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006084 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006085 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006086 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006087 Op = TLI.LowerOperation(Op, DAG);
6088 if (Op.Val) {
6089 // Now that the custom expander is done, expand the result, which is
6090 // still VT.
6091 ExpandOp(Op, Lo, Hi);
6092 break;
6093 }
6094 }
6095
Chris Lattnere34b3962005-01-19 04:19:40 +00006096 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006097 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006098 break;
Chris Lattner47599822005-04-02 03:38:53 +00006099
6100 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006101 TargetLowering::LegalizeAction Action =
6102 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6103 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6104 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006105 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006106 break;
6107 }
6108
Chris Lattnere34b3962005-01-19 04:19:40 +00006109 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006110 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6111 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006112 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006113 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006114
Misha Brukmanedf128a2005-04-21 22:36:52 +00006115 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006116 case ISD::SUB: {
6117 // If the target wants to custom expand this, let them.
6118 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6119 TargetLowering::Custom) {
6120 Op = TLI.LowerOperation(Op, DAG);
6121 if (Op.Val) {
6122 ExpandOp(Op, Lo, Hi);
6123 break;
6124 }
6125 }
6126
6127 // Expand the subcomponents.
6128 SDOperand LHSL, LHSH, RHSL, RHSH;
6129 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6130 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006131 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6132 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006133 LoOps[0] = LHSL;
6134 LoOps[1] = RHSL;
6135 HiOps[0] = LHSH;
6136 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006137 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00006138 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006139 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006140 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006141 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00006142 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006143 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006144 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006145 }
Chris Lattner84f67882005-01-20 18:52:28 +00006146 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006147 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006148
6149 case ISD::ADDC:
6150 case ISD::SUBC: {
6151 // Expand the subcomponents.
6152 SDOperand LHSL, LHSH, RHSL, RHSH;
6153 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6154 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6155 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6156 SDOperand LoOps[2] = { LHSL, RHSL };
6157 SDOperand HiOps[3] = { LHSH, RHSH };
6158
6159 if (Node->getOpcode() == ISD::ADDC) {
6160 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6161 HiOps[2] = Lo.getValue(1);
6162 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6163 } else {
6164 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6165 HiOps[2] = Lo.getValue(1);
6166 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6167 }
6168 // Remember that we legalized the flag.
6169 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6170 break;
6171 }
6172 case ISD::ADDE:
6173 case ISD::SUBE: {
6174 // Expand the subcomponents.
6175 SDOperand LHSL, LHSH, RHSL, RHSH;
6176 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6177 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6178 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6179 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6180 SDOperand HiOps[3] = { LHSH, RHSH };
6181
6182 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6183 HiOps[2] = Lo.getValue(1);
6184 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6185
6186 // Remember that we legalized the flag.
6187 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6188 break;
6189 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006190 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006191 // If the target wants to custom expand this, let them.
6192 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006193 SDOperand New = TLI.LowerOperation(Op, DAG);
6194 if (New.Val) {
6195 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006196 break;
6197 }
6198 }
6199
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006200 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6201 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006202 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6203 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6204 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006205 SDOperand LL, LH, RL, RH;
6206 ExpandOp(Node->getOperand(0), LL, LH);
6207 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00006208 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
6209 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6210 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6211 // FIXME: generalize this to handle other bit sizes
6212 if (LHSSB == 32 && RHSSB == 32 &&
6213 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
6214 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
6215 // The inputs are both zero-extended.
6216 if (HasUMUL_LOHI) {
6217 // We can emit a umul_lohi.
6218 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6219 Hi = SDOperand(Lo.Val, 1);
6220 break;
6221 }
6222 if (HasMULHU) {
6223 // We can emit a mulhu+mul.
6224 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6225 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6226 break;
6227 }
Dan Gohman525178c2007-10-08 18:33:35 +00006228 }
6229 if (LHSSB > BitSize && RHSSB > BitSize) {
6230 // The input values are both sign-extended.
6231 if (HasSMUL_LOHI) {
6232 // We can emit a smul_lohi.
6233 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6234 Hi = SDOperand(Lo.Val, 1);
6235 break;
6236 }
6237 if (HasMULHS) {
6238 // We can emit a mulhs+mul.
6239 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6240 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6241 break;
6242 }
6243 }
6244 if (HasUMUL_LOHI) {
6245 // Lo,Hi = umul LHS, RHS.
6246 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6247 DAG.getVTList(NVT, NVT), LL, RL);
6248 Lo = UMulLOHI;
6249 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006250 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6251 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6252 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6253 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006254 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006255 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006256 if (HasMULHU) {
6257 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6258 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6259 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6260 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6261 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6262 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6263 break;
6264 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006265 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006266
Dan Gohman525178c2007-10-08 18:33:35 +00006267 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006268 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6269 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006270 break;
6271 }
Evan Cheng56966222007-01-12 02:11:51 +00006272 case ISD::SDIV:
6273 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6274 break;
6275 case ISD::UDIV:
6276 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6277 break;
6278 case ISD::SREM:
6279 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6280 break;
6281 case ISD::UREM:
6282 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6283 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006284
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006285 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006286 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6287 RTLIB::ADD_F64,
6288 RTLIB::ADD_F80,
6289 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006290 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006291 break;
6292 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006293 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6294 RTLIB::SUB_F64,
6295 RTLIB::SUB_F80,
6296 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006297 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006298 break;
6299 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006300 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6301 RTLIB::MUL_F64,
6302 RTLIB::MUL_F80,
6303 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006304 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006305 break;
6306 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006307 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6308 RTLIB::DIV_F64,
6309 RTLIB::DIV_F80,
6310 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006311 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006312 break;
6313 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006314 if (VT == MVT::ppcf128) {
6315 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6316 Node->getOperand(0).getValueType()==MVT::f64);
6317 const uint64_t zero = 0;
6318 if (Node->getOperand(0).getValueType()==MVT::f32)
6319 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6320 else
6321 Hi = Node->getOperand(0);
6322 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6323 break;
6324 }
Evan Cheng56966222007-01-12 02:11:51 +00006325 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006326 break;
6327 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006328 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006329 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006330 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006331 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6332 RTLIB::POWI_F64,
6333 RTLIB::POWI_F80,
6334 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006335 Node, false, Hi);
6336 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006337 case ISD::FSQRT:
6338 case ISD::FSIN:
6339 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006340 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006341 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006342 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006343 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6344 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006345 break;
6346 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006347 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6348 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006349 break;
6350 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006351 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6352 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006353 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006354 default: assert(0 && "Unreachable!");
6355 }
Evan Cheng56966222007-01-12 02:11:51 +00006356 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006357 break;
6358 }
Evan Cheng966bf242006-12-16 00:52:40 +00006359 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006360 if (VT == MVT::ppcf128) {
6361 SDOperand Tmp;
6362 ExpandOp(Node->getOperand(0), Lo, Tmp);
6363 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6364 // lo = hi==fabs(hi) ? lo : -lo;
6365 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6366 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6367 DAG.getCondCode(ISD::SETEQ));
6368 break;
6369 }
Evan Cheng966bf242006-12-16 00:52:40 +00006370 SDOperand Mask = (VT == MVT::f64)
6371 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6372 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6373 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6374 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6375 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6376 if (getTypeAction(NVT) == Expand)
6377 ExpandOp(Lo, Lo, Hi);
6378 break;
6379 }
6380 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006381 if (VT == MVT::ppcf128) {
6382 ExpandOp(Node->getOperand(0), Lo, Hi);
6383 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6384 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6385 break;
6386 }
Evan Cheng966bf242006-12-16 00:52:40 +00006387 SDOperand Mask = (VT == MVT::f64)
6388 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6389 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6390 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6391 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6392 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6393 if (getTypeAction(NVT) == Expand)
6394 ExpandOp(Lo, Lo, Hi);
6395 break;
6396 }
Evan Cheng912095b2007-01-04 21:56:39 +00006397 case ISD::FCOPYSIGN: {
6398 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6399 if (getTypeAction(NVT) == Expand)
6400 ExpandOp(Lo, Lo, Hi);
6401 break;
6402 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006403 case ISD::SINT_TO_FP:
6404 case ISD::UINT_TO_FP: {
6405 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6406 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006407 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006408 static uint64_t zero = 0;
6409 if (isSigned) {
6410 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6411 Node->getOperand(0)));
6412 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6413 } else {
6414 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6415 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6416 Node->getOperand(0)));
6417 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6418 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006419 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006420 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6421 DAG.getConstant(0, MVT::i32),
6422 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6423 DAG.getConstantFP(
6424 APFloat(APInt(128, 2, TwoE32)),
6425 MVT::ppcf128)),
6426 Hi,
6427 DAG.getCondCode(ISD::SETLT)),
6428 Lo, Hi);
6429 }
6430 break;
6431 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006432 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6433 // si64->ppcf128 done by libcall, below
6434 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6435 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6436 Lo, Hi);
6437 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6438 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6439 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6440 DAG.getConstant(0, MVT::i64),
6441 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6442 DAG.getConstantFP(
6443 APFloat(APInt(128, 2, TwoE64)),
6444 MVT::ppcf128)),
6445 Hi,
6446 DAG.getCondCode(ISD::SETLT)),
6447 Lo, Hi);
6448 break;
6449 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006450 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006451 if (Node->getOperand(0).getValueType() == MVT::i64) {
6452 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006453 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006454 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006455 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006456 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006457 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006458 LC = RTLIB::SINTTOFP_I64_F80;
6459 }
6460 else if (VT == MVT::ppcf128) {
6461 assert(isSigned);
6462 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006463 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006464 } else {
6465 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006466 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006467 else
Evan Cheng56966222007-01-12 02:11:51 +00006468 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006469 }
6470
6471 // Promote the operand if needed.
6472 if (getTypeAction(SrcVT) == Promote) {
6473 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6474 Tmp = isSigned
6475 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6476 DAG.getValueType(SrcVT))
6477 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6478 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6479 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006480
6481 const char *LibCall = TLI.getLibcallName(LC);
6482 if (LibCall)
6483 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6484 else {
6485 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6486 Node->getOperand(0));
6487 if (getTypeAction(Lo.getValueType()) == Expand)
6488 ExpandOp(Lo, Lo, Hi);
6489 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006490 break;
6491 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006492 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006493
Chris Lattner83397362005-12-21 18:02:52 +00006494 // Make sure the resultant values have been legalized themselves, unless this
6495 // is a type that requires multi-step expansion.
6496 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6497 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006498 if (Hi.Val)
6499 // Don't legalize the high part if it is expanded to a single node.
6500 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006501 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006502
6503 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006504 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006505 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006506}
6507
Dan Gohman7f321562007-06-25 16:23:39 +00006508/// SplitVectorOp - Given an operand of vector type, break it down into
6509/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006510void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6511 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006512 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006513 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006514 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006515 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006516
Dan Gohmane40c7b02007-09-24 15:54:53 +00006517 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006518
6519 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6520 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6521
6522 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6523 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6524
Chris Lattnerc7029802006-03-18 01:44:44 +00006525 // See if we already split it.
6526 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6527 = SplitNodes.find(Op);
6528 if (I != SplitNodes.end()) {
6529 Lo = I->second.first;
6530 Hi = I->second.second;
6531 return;
6532 }
6533
6534 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006535 default:
6536#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006537 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006538#endif
6539 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006540 case ISD::UNDEF:
6541 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6542 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6543 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006544 case ISD::BUILD_PAIR:
6545 Lo = Node->getOperand(0);
6546 Hi = Node->getOperand(1);
6547 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006548 case ISD::INSERT_VECTOR_ELT: {
6549 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6550 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6551 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006552 if (Index < NewNumElts_Lo)
6553 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006554 DAG.getConstant(Index, TLI.getPointerTy()));
6555 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006556 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6557 DAG.getConstant(Index - NewNumElts_Lo,
6558 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006559 break;
6560 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006561 case ISD::VECTOR_SHUFFLE: {
6562 // Build the low part.
6563 SDOperand Mask = Node->getOperand(2);
6564 SmallVector<SDOperand, 8> Ops;
6565 MVT::ValueType PtrVT = TLI.getPointerTy();
6566
6567 // Insert all of the elements from the input that are needed. We use
6568 // buildvector of extractelement here because the input vectors will have
6569 // to be legalized, so this makes the code simpler.
6570 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6571 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6572 SDOperand InVec = Node->getOperand(0);
6573 if (Idx >= NumElements) {
6574 InVec = Node->getOperand(1);
6575 Idx -= NumElements;
6576 }
6577 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6578 DAG.getConstant(Idx, PtrVT)));
6579 }
6580 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6581 Ops.clear();
6582
6583 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6584 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6585 SDOperand InVec = Node->getOperand(0);
6586 if (Idx >= NumElements) {
6587 InVec = Node->getOperand(1);
6588 Idx -= NumElements;
6589 }
6590 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6591 DAG.getConstant(Idx, PtrVT)));
6592 }
6593 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6594 break;
6595 }
Dan Gohman7f321562007-06-25 16:23:39 +00006596 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006597 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006598 Node->op_begin()+NewNumElts_Lo);
6599 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006600
Nate Begeman5db1afb2007-11-15 21:15:26 +00006601 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006602 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006603 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006604 break;
6605 }
Dan Gohman7f321562007-06-25 16:23:39 +00006606 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006607 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006608 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6609 if (NewNumSubvectors == 1) {
6610 Lo = Node->getOperand(0);
6611 Hi = Node->getOperand(1);
6612 } else {
6613 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6614 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006615 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006616
Dan Gohman7f321562007-06-25 16:23:39 +00006617 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6618 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006619 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006620 }
Dan Gohman65956352007-06-13 15:12:02 +00006621 break;
6622 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006623 case ISD::SELECT: {
6624 SDOperand Cond = Node->getOperand(0);
6625
6626 SDOperand LL, LH, RL, RH;
6627 SplitVectorOp(Node->getOperand(1), LL, LH);
6628 SplitVectorOp(Node->getOperand(2), RL, RH);
6629
6630 if (MVT::isVector(Cond.getValueType())) {
6631 // Handle a vector merge.
6632 SDOperand CL, CH;
6633 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006634 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6635 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006636 } else {
6637 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006638 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6639 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006640 }
6641 break;
6642 }
Dan Gohman7f321562007-06-25 16:23:39 +00006643 case ISD::ADD:
6644 case ISD::SUB:
6645 case ISD::MUL:
6646 case ISD::FADD:
6647 case ISD::FSUB:
6648 case ISD::FMUL:
6649 case ISD::SDIV:
6650 case ISD::UDIV:
6651 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006652 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006653 case ISD::AND:
6654 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006655 case ISD::XOR:
6656 case ISD::UREM:
6657 case ISD::SREM:
6658 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006659 SDOperand LL, LH, RL, RH;
6660 SplitVectorOp(Node->getOperand(0), LL, LH);
6661 SplitVectorOp(Node->getOperand(1), RL, RH);
6662
Nate Begeman5db1afb2007-11-15 21:15:26 +00006663 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6664 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006665 break;
6666 }
Dan Gohman82669522007-10-11 23:57:53 +00006667 case ISD::FPOWI: {
6668 SDOperand L, H;
6669 SplitVectorOp(Node->getOperand(0), L, H);
6670
Nate Begeman5db1afb2007-11-15 21:15:26 +00006671 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6672 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006673 break;
6674 }
6675 case ISD::CTTZ:
6676 case ISD::CTLZ:
6677 case ISD::CTPOP:
6678 case ISD::FNEG:
6679 case ISD::FABS:
6680 case ISD::FSQRT:
6681 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006682 case ISD::FCOS:
6683 case ISD::FP_TO_SINT:
6684 case ISD::FP_TO_UINT:
6685 case ISD::SINT_TO_FP:
6686 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006687 SDOperand L, H;
6688 SplitVectorOp(Node->getOperand(0), L, H);
6689
Nate Begeman5db1afb2007-11-15 21:15:26 +00006690 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6691 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006692 break;
6693 }
Dan Gohman7f321562007-06-25 16:23:39 +00006694 case ISD::LOAD: {
6695 LoadSDNode *LD = cast<LoadSDNode>(Node);
6696 SDOperand Ch = LD->getChain();
6697 SDOperand Ptr = LD->getBasePtr();
6698 const Value *SV = LD->getSrcValue();
6699 int SVOffset = LD->getSrcValueOffset();
6700 unsigned Alignment = LD->getAlignment();
6701 bool isVolatile = LD->isVolatile();
6702
Nate Begeman5db1afb2007-11-15 21:15:26 +00006703 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6704 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006705 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006706 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006707 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006708 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006709 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006710
6711 // Build a factor node to remember that this load is independent of the
6712 // other one.
6713 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6714 Hi.getValue(1));
6715
6716 // Remember that we legalized the chain.
6717 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006718 break;
6719 }
Dan Gohman7f321562007-06-25 16:23:39 +00006720 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006721 // We know the result is a vector. The input may be either a vector or a
6722 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006723 SDOperand InOp = Node->getOperand(0);
6724 if (!MVT::isVector(InOp.getValueType()) ||
6725 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6726 // The input is a scalar or single-element vector.
6727 // Lower to a store/load so that it can be split.
6728 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006729 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00006730
Evan Cheng786225a2006-10-05 23:01:46 +00006731 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00006732 InOp, Ptr, NULL, 0);
6733 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006734 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006735 // Split the vector and convert each of the pieces now.
6736 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006737 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6738 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006739 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006740 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006741 }
6742
6743 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006744 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006745 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006746 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006747}
6748
6749
Dan Gohman89b20c02007-06-27 14:06:22 +00006750/// ScalarizeVectorOp - Given an operand of single-element vector type
6751/// (e.g. v1f32), convert it into the equivalent operation that returns a
6752/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006753SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6754 assert(MVT::isVector(Op.getValueType()) &&
6755 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006756 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006757 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6758 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006759
Dan Gohman7f321562007-06-25 16:23:39 +00006760 // See if we already scalarized it.
6761 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6762 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006763
6764 SDOperand Result;
6765 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006766 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006767#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006768 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006769#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006770 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6771 case ISD::ADD:
6772 case ISD::FADD:
6773 case ISD::SUB:
6774 case ISD::FSUB:
6775 case ISD::MUL:
6776 case ISD::FMUL:
6777 case ISD::SDIV:
6778 case ISD::UDIV:
6779 case ISD::FDIV:
6780 case ISD::SREM:
6781 case ISD::UREM:
6782 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006783 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006784 case ISD::AND:
6785 case ISD::OR:
6786 case ISD::XOR:
6787 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006788 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006789 ScalarizeVectorOp(Node->getOperand(0)),
6790 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006791 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006792 case ISD::FNEG:
6793 case ISD::FABS:
6794 case ISD::FSQRT:
6795 case ISD::FSIN:
6796 case ISD::FCOS:
6797 Result = DAG.getNode(Node->getOpcode(),
6798 NewVT,
6799 ScalarizeVectorOp(Node->getOperand(0)));
6800 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006801 case ISD::FPOWI:
6802 Result = DAG.getNode(Node->getOpcode(),
6803 NewVT,
6804 ScalarizeVectorOp(Node->getOperand(0)),
6805 Node->getOperand(1));
6806 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006807 case ISD::LOAD: {
6808 LoadSDNode *LD = cast<LoadSDNode>(Node);
6809 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6810 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006811
Dan Gohman7f321562007-06-25 16:23:39 +00006812 const Value *SV = LD->getSrcValue();
6813 int SVOffset = LD->getSrcValueOffset();
6814 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6815 LD->isVolatile(), LD->getAlignment());
6816
Chris Lattnerc7029802006-03-18 01:44:44 +00006817 // Remember that we legalized the chain.
6818 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6819 break;
6820 }
Dan Gohman7f321562007-06-25 16:23:39 +00006821 case ISD::BUILD_VECTOR:
6822 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006823 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006824 case ISD::INSERT_VECTOR_ELT:
6825 // Returning the inserted scalar element.
6826 Result = Node->getOperand(1);
6827 break;
6828 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006829 assert(Node->getOperand(0).getValueType() == NewVT &&
6830 "Concat of non-legal vectors not yet supported!");
6831 Result = Node->getOperand(0);
6832 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006833 case ISD::VECTOR_SHUFFLE: {
6834 // Figure out if the scalar is the LHS or RHS and return it.
6835 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6836 if (cast<ConstantSDNode>(EltNum)->getValue())
6837 Result = ScalarizeVectorOp(Node->getOperand(1));
6838 else
6839 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006840 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006841 }
6842 case ISD::EXTRACT_SUBVECTOR:
6843 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006844 assert(Result.getValueType() == NewVT);
6845 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006846 case ISD::BIT_CONVERT:
6847 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006848 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006849 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006850 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006851 ScalarizeVectorOp(Op.getOperand(1)),
6852 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006853 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006854 }
6855
6856 if (TLI.isTypeLegal(NewVT))
6857 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006858 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6859 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006860 return Result;
6861}
6862
Chris Lattner3e928bb2005-01-07 07:47:09 +00006863
6864// SelectionDAG::Legalize - This is the entry point for the file.
6865//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006866void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006867 if (ViewLegalizeDAGs) viewGraph();
6868
Chris Lattner3e928bb2005-01-07 07:47:09 +00006869 /// run - This is the main entry point to this class.
6870 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006871 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006872}
6873