blob: 9a4bec2acc570b551957c18ff048fec704a0e7da [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"
Dan Gohmanc6c391d2008-01-31 00:25:39 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000022#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000025#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000026#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000027#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000030#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000032#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000033#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000034#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000035using namespace llvm;
36
Chris Lattner5e46a192006-04-02 03:07:27 +000037#ifndef NDEBUG
38static cl::opt<bool>
39ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
40 cl::desc("Pop up a window to show dags before legalize"));
41#else
42static const bool ViewLegalizeDAGs = 0;
43#endif
44
Chris Lattner3e928bb2005-01-07 07:47:09 +000045//===----------------------------------------------------------------------===//
46/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
47/// hacks on it until the target machine can handle it. This involves
48/// eliminating value sizes the machine cannot handle (promoting small sizes to
49/// large sizes or splitting up large values into small values) as well as
50/// eliminating operations the machine cannot handle.
51///
52/// This code also does a small amount of optimization and recognition of idioms
53/// as part of its processing. For example, if a target does not support a
54/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
55/// will attempt merge setcc and brc instructions into brcc's.
56///
57namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000058class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000059 TargetLowering &TLI;
60 SelectionDAG &DAG;
61
Chris Lattner6831a812006-02-13 09:18:02 +000062 // Libcall insertion helpers.
63
64 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
65 /// legalized. We use this to ensure that calls are properly serialized
66 /// against each other, including inserted libcalls.
67 SDOperand LastCALLSEQ_END;
68
69 /// IsLegalizingCall - This member is used *only* for purposes of providing
70 /// helpful assertions that a libcall isn't created while another call is
71 /// being legalized (which could lead to non-serialized call sequences).
72 bool IsLegalizingCall;
73
Chris Lattner3e928bb2005-01-07 07:47:09 +000074 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000075 Legal, // The target natively supports this operation.
76 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000077 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 };
Chris Lattner6831a812006-02-13 09:18:02 +000079
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 /// ValueTypeActions - This is a bitvector that contains two bits for each
81 /// value type, where the two bits correspond to the LegalizeAction enum.
82 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000083 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000084
Chris Lattner3e928bb2005-01-07 07:47:09 +000085 /// LegalizedNodes - For nodes that are of legal width, and that have more
86 /// than one use, this map indicates what regularized operand to use. This
87 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000088 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
Chris Lattner03c85462005-01-15 05:21:40 +000090 /// PromotedNodes - For nodes that are below legal width, and that have more
91 /// than one use, this map indicates what promoted value to use. This allows
92 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000093 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000094
Chris Lattnerc7029802006-03-18 01:44:44 +000095 /// ExpandedNodes - For nodes that need to be expanded this map indicates
96 /// which which operands are the expanded version of the input. This allows
97 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000098 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000099
Chris Lattnerc7029802006-03-18 01:44:44 +0000100 /// SplitNodes - For vector nodes that need to be split, this map indicates
101 /// which which operands are the split version of the input. This allows us
102 /// to avoid splitting the same node more than once.
103 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
104
Dan Gohman7f321562007-06-25 16:23:39 +0000105 /// ScalarizedNodes - For nodes that need to be converted from vector types to
106 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000107 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000108 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000109
Chris Lattner8afc48e2005-01-07 22:28:47 +0000110 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000111 LegalizedNodes.insert(std::make_pair(From, To));
112 // If someone requests legalization of the new node, return itself.
113 if (From != To)
114 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000115 }
Chris Lattner03c85462005-01-15 05:21:40 +0000116 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000117 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000118 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000119 // If someone requests legalization of the new node, return itself.
120 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000121 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000122
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123public:
124
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000125 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000126
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127 /// getTypeAction - Return how we should legalize values of this type, either
128 /// it is already legal or we need to expand it into multiple registers of
129 /// smaller integer type, or we need to promote it to a larger type.
130 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000131 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 }
133
134 /// isTypeLegal - Return true if this type is legal on this target.
135 ///
136 bool isTypeLegal(MVT::ValueType VT) const {
137 return getTypeAction(VT) == Legal;
138 }
139
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140 void LegalizeDAG();
141
Chris Lattner456a93a2006-01-28 07:39:30 +0000142private:
Dan Gohman7f321562007-06-25 16:23:39 +0000143 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000144 /// appropriate for its type.
145 void HandleOp(SDOperand Op);
146
147 /// LegalizeOp - We know that the specified value has a legal type.
148 /// Recursively ensure that the operands have legal types, then return the
149 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000150 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000151
Dan Gohman82669522007-10-11 23:57:53 +0000152 /// UnrollVectorOp - We know that the given vector has a legal type, however
153 /// the operation it performs is not legal and is an operation that we have
154 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
155 /// operating on each element individually.
156 SDOperand UnrollVectorOp(SDOperand O);
157
Chris Lattnerc7029802006-03-18 01:44:44 +0000158 /// PromoteOp - Given an operation that produces a value in an invalid type,
159 /// promote it to compute the value into a larger type. The produced value
160 /// will have the correct bits for the low portion of the register, but no
161 /// guarantee is made about the top bits: it may be zero, sign-extended, or
162 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000163 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164
Chris Lattnerc7029802006-03-18 01:44:44 +0000165 /// ExpandOp - Expand the specified SDOperand into its two component pieces
166 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
167 /// the LegalizeNodes map is filled in for any results that are not expanded,
168 /// the ExpandedNodes map is filled in for any results that are expanded, and
169 /// the Lo/Hi values are returned. This applies to integer types and Vector
170 /// types.
171 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
172
Dan Gohman7f321562007-06-25 16:23:39 +0000173 /// SplitVectorOp - Given an operand of vector type, break it down into
174 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000175 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
176
Dan Gohman89b20c02007-06-27 14:06:22 +0000177 /// ScalarizeVectorOp - Given an operand of single-element vector type
178 /// (e.g. v1f32), convert it into the equivalent operation that returns a
179 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000180 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000181
Chris Lattner4352cc92006-04-04 17:23:26 +0000182 /// isShuffleLegal - Return true if a vector shuffle is legal with the
183 /// specified mask and type. Targets can specify exactly which masks they
184 /// support and the code generator is tasked with not creating illegal masks.
185 ///
186 /// Note that this will also return true for shuffles that are promoted to a
187 /// different type.
188 ///
189 /// If this is a legal shuffle, this method returns the (possibly promoted)
190 /// build_vector Mask. If it's not a legal shuffle, it returns null.
191 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
192
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000193 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000194 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000195
Nate Begeman750ac1b2006-02-01 07:19:44 +0000196 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
197
Reid Spencer47857812006-12-31 05:55:36 +0000198 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000199 SDOperand &Hi);
200 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
201 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000202
Chris Lattner1401d152008-01-16 07:45:30 +0000203 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
204 MVT::ValueType DestVT);
Chris Lattnerce872152006-03-19 06:31:19 +0000205 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000206 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000207 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
208 SDOperand LegalOp,
209 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000210 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000212 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
213 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000214
Chris Lattner456a93a2006-01-28 07:39:30 +0000215 SDOperand ExpandBSWAP(SDOperand Op);
216 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000217 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000219 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
220 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000221
Dan Gohman7f321562007-06-25 16:23:39 +0000222 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000223 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224};
225}
226
Chris Lattner4352cc92006-04-04 17:23:26 +0000227/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
228/// specified mask and type. Targets can specify exactly which masks they
229/// support and the code generator is tasked with not creating illegal masks.
230///
231/// Note that this will also return true for shuffles that are promoted to a
232/// different type.
233SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
234 SDOperand Mask) const {
235 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
236 default: return 0;
237 case TargetLowering::Legal:
238 case TargetLowering::Custom:
239 break;
240 case TargetLowering::Promote: {
241 // If this is promoted to a different type, convert the shuffle mask and
242 // ask if it is legal in the promoted type!
243 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
244
245 // If we changed # elements, change the shuffle mask.
246 unsigned NumEltsGrowth =
247 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
248 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
249 if (NumEltsGrowth > 1) {
250 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000251 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000252 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
253 SDOperand InOp = Mask.getOperand(i);
254 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
255 if (InOp.getOpcode() == ISD::UNDEF)
256 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
257 else {
258 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
259 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
260 }
261 }
262 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000263 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000264 }
265 VT = NVT;
266 break;
267 }
268 }
269 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
270}
271
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000272SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
273 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
274 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000275 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000276 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000277}
278
Chris Lattnere10e6f72007-06-18 21:28:10 +0000279/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
280/// contains all of a nodes operands before it contains the node.
281static void ComputeTopDownOrdering(SelectionDAG &DAG,
282 SmallVector<SDNode*, 64> &Order) {
283
284 DenseMap<SDNode*, unsigned> Visited;
285 std::vector<SDNode*> Worklist;
286 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000287
Chris Lattnere10e6f72007-06-18 21:28:10 +0000288 // Compute ordering from all of the leaves in the graphs, those (like the
289 // entry node) that have no operands.
290 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
291 E = DAG.allnodes_end(); I != E; ++I) {
292 if (I->getNumOperands() == 0) {
293 Visited[I] = 0 - 1U;
294 Worklist.push_back(I);
295 }
Chris Lattner32fca002005-10-06 01:20:27 +0000296 }
297
Chris Lattnere10e6f72007-06-18 21:28:10 +0000298 while (!Worklist.empty()) {
299 SDNode *N = Worklist.back();
300 Worklist.pop_back();
301
302 if (++Visited[N] != N->getNumOperands())
303 continue; // Haven't visited all operands yet
304
305 Order.push_back(N);
306
307 // Now that we have N in, add anything that uses it if all of their operands
308 // are now done.
309 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
310 UI != E; ++UI)
311 Worklist.push_back(*UI);
312 }
313
314 assert(Order.size() == Visited.size() &&
315 Order.size() ==
316 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
317 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000318}
319
Chris Lattner1618beb2005-07-29 00:11:56 +0000320
Chris Lattner3e928bb2005-01-07 07:47:09 +0000321void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000322 LastCALLSEQ_END = DAG.getEntryNode();
323 IsLegalizingCall = false;
324
Chris Lattnerab510a72005-10-02 17:49:46 +0000325 // The legalize process is inherently a bottom-up recursive process (users
326 // legalize their uses before themselves). Given infinite stack space, we
327 // could just start legalizing on the root and traverse the whole graph. In
328 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000329 // blocks. To avoid this problem, compute an ordering of the nodes where each
330 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000331 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000332 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000333
Chris Lattnerc7029802006-03-18 01:44:44 +0000334 for (unsigned i = 0, e = Order.size(); i != e; ++i)
335 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000336
337 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000338 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000339 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
340 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000341
342 ExpandedNodes.clear();
343 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000344 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000345 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000346 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347
348 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000349 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000350}
351
Chris Lattner6831a812006-02-13 09:18:02 +0000352
353/// FindCallEndFromCallStart - Given a chained node that is part of a call
354/// sequence, find the CALLSEQ_END node that terminates the call sequence.
355static SDNode *FindCallEndFromCallStart(SDNode *Node) {
356 if (Node->getOpcode() == ISD::CALLSEQ_END)
357 return Node;
358 if (Node->use_empty())
359 return 0; // No CallSeqEnd
360
361 // The chain is usually at the end.
362 SDOperand TheChain(Node, Node->getNumValues()-1);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Sometimes it's at the beginning.
365 TheChain = SDOperand(Node, 0);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Otherwise, hunt for it.
368 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
369 if (Node->getValueType(i) == MVT::Other) {
370 TheChain = SDOperand(Node, i);
371 break;
372 }
373
374 // Otherwise, we walked into a node without a chain.
375 if (TheChain.getValueType() != MVT::Other)
376 return 0;
377 }
378 }
379
380 for (SDNode::use_iterator UI = Node->use_begin(),
381 E = Node->use_end(); UI != E; ++UI) {
382
383 // Make sure to only follow users of our token chain.
384 SDNode *User = *UI;
385 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
386 if (User->getOperand(i) == TheChain)
387 if (SDNode *Result = FindCallEndFromCallStart(User))
388 return Result;
389 }
390 return 0;
391}
392
393/// FindCallStartFromCallEnd - Given a chained node that is part of a call
394/// sequence, find the CALLSEQ_START node that initiates the call sequence.
395static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
396 assert(Node && "Didn't find callseq_start for a call??");
397 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
398
399 assert(Node->getOperand(0).getValueType() == MVT::Other &&
400 "Node doesn't have a token chain argument!");
401 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
402}
403
404/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
405/// see if any uses can reach Dest. If no dest operands can get to dest,
406/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000407///
408/// Keep track of the nodes we fine that actually do lead to Dest in
409/// NodesLeadingTo. This avoids retraversing them exponential number of times.
410///
411bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000412 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000413 if (N == Dest) return true; // N certainly leads to Dest :)
414
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000415 // If we've already processed this node and it does lead to Dest, there is no
416 // need to reprocess it.
417 if (NodesLeadingTo.count(N)) return true;
418
Chris Lattner6831a812006-02-13 09:18:02 +0000419 // If the first result of this node has been already legalized, then it cannot
420 // reach N.
421 switch (getTypeAction(N->getValueType(0))) {
422 case Legal:
423 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Promote:
426 if (PromotedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Expand:
429 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 }
432
433 // Okay, this node has not already been legalized. Check and legalize all
434 // operands. If none lead to Dest, then we can legalize this node.
435 bool OperandsLeadToDest = false;
436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
437 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000438 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000439
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000440 if (OperandsLeadToDest) {
441 NodesLeadingTo.insert(N);
442 return true;
443 }
Chris Lattner6831a812006-02-13 09:18:02 +0000444
445 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000446 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000447 return false;
448}
449
Dan Gohman7f321562007-06-25 16:23:39 +0000450/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000451/// appropriate for its type.
452void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000453 MVT::ValueType VT = Op.getValueType();
454 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000455 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000456 case Legal: (void)LegalizeOp(Op); break;
457 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000458 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000459 if (!MVT::isVector(VT)) {
460 // If this is an illegal scalar, expand it into its two component
461 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000462 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000463 if (Op.getOpcode() == ISD::TargetConstant)
464 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000465 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000466 } else if (MVT::getVectorNumElements(VT) == 1) {
467 // If this is an illegal single element vector, convert it to a
468 // scalar operation.
469 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000470 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000471 // Otherwise, this is an illegal multiple element vector.
472 // Split it in half and legalize both parts.
473 SDOperand X, Y;
474 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000475 }
476 break;
477 }
478}
Chris Lattner6831a812006-02-13 09:18:02 +0000479
Evan Cheng9f877882006-12-13 20:57:08 +0000480/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
481/// a load from the constant pool.
482static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000483 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000484 bool Extend = false;
485
486 // If a FP immediate is precise when represented as a float and if the
487 // target can do an extending load from float to double, we put it into
488 // the constant pool as a float, even if it's is statically typed as a
489 // double.
490 MVT::ValueType VT = CFP->getValueType(0);
491 bool isDouble = VT == MVT::f64;
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000492 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000493 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000494 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000495 if (VT!=MVT::f64 && VT!=MVT::f32)
496 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000497 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
498 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000499 }
500
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000501 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng00495212006-12-12 21:32:44 +0000502 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000503 // Do not try to be clever about long doubles (so far)
Evan Cheng00495212006-12-12 21:32:44 +0000504 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
505 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
506 VT = MVT::f32;
507 Extend = true;
508 }
509
510 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
511 if (Extend) {
512 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000513 CPIdx, &PseudoSourceValue::CPRel, 0, MVT::f32);
Evan Cheng00495212006-12-12 21:32:44 +0000514 } else {
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000515 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
516 &PseudoSourceValue::CPRel, 0);
Evan Cheng00495212006-12-12 21:32:44 +0000517 }
518}
519
Chris Lattner6831a812006-02-13 09:18:02 +0000520
Evan Cheng912095b2007-01-04 21:56:39 +0000521/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
522/// operations.
523static
524SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
525 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000526 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000527 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000528 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
529 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000530 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000531
Evan Cheng912095b2007-01-04 21:56:39 +0000532 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000533 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000534 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
535 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000536 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000537 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000538 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000539 // Shift right or sign-extend it if the two operands have different types.
540 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
541 if (SizeDiff > 0) {
542 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
543 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
544 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
545 } else if (SizeDiff < 0)
546 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000547
548 // Clear the sign bit of first operand.
549 SDOperand Mask2 = (VT == MVT::f64)
550 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
551 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
552 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000553 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000554 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
555
556 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000557 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
558 return Result;
559}
560
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000561/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
562static
563SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
564 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000565 SDOperand Chain = ST->getChain();
566 SDOperand Ptr = ST->getBasePtr();
567 SDOperand Val = ST->getValue();
568 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000569 int Alignment = ST->getAlignment();
570 int SVOffset = ST->getSrcValueOffset();
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000571 if (MVT::isFloatingPoint(ST->getMemoryVT())) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000572 // Expand to a bitconvert of the value to the integer type of the
573 // same size, then a (misaligned) int store.
574 MVT::ValueType intVT;
575 if (VT==MVT::f64)
576 intVT = MVT::i64;
577 else if (VT==MVT::f32)
578 intVT = MVT::i32;
579 else
580 assert(0 && "Unaligned load of unsupported floating point type");
581
582 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
583 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
584 SVOffset, ST->isVolatile(), Alignment);
585 }
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000586 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000587 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000588 // Get the half-size VT
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000589 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000590 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000591 int IncrementSize = NumBits / 8;
592
593 // Divide the stored value in two parts.
594 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
595 SDOperand Lo = Val;
596 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
597
598 // Store the two parts
599 SDOperand Store1, Store2;
600 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
601 ST->getSrcValue(), SVOffset, NewStoredVT,
602 ST->isVolatile(), Alignment);
603 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
604 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000605 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000606 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
607 ST->getSrcValue(), SVOffset + IncrementSize,
608 NewStoredVT, ST->isVolatile(), Alignment);
609
610 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
611}
612
613/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
614static
615SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
616 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000617 int SVOffset = LD->getSrcValueOffset();
618 SDOperand Chain = LD->getChain();
619 SDOperand Ptr = LD->getBasePtr();
620 MVT::ValueType VT = LD->getValueType(0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000621 MVT::ValueType LoadedVT = LD->getMemoryVT();
Chris Lattnere400af82007-11-19 21:38:03 +0000622 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000623 // Expand to a (misaligned) integer load of the same size,
624 // then bitconvert to floating point.
625 MVT::ValueType intVT;
Chris Lattnere400af82007-11-19 21:38:03 +0000626 if (LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000627 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000628 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000629 intVT = MVT::i32;
630 else
631 assert(0 && "Unaligned load of unsupported floating point type");
632
633 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
634 SVOffset, LD->isVolatile(),
635 LD->getAlignment());
636 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
637 if (LoadedVT != VT)
638 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
639
640 SDOperand Ops[] = { Result, Chain };
641 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
642 Ops, 2);
643 }
Chris Lattnere400af82007-11-19 21:38:03 +0000644 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
645 "Unaligned load of unsupported type.");
646
647 // Compute the new VT that is half the size of the old one. We either have an
648 // integer MVT or we have a vector MVT.
649 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
650 MVT::ValueType NewLoadedVT;
651 if (!MVT::isVector(LoadedVT)) {
652 NewLoadedVT = MVT::getIntegerType(NumBits/2);
653 } else {
654 // FIXME: This is not right for <1 x anything> it is also not right for
655 // non-power-of-two vectors.
656 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
657 MVT::getVectorNumElements(LoadedVT)/2);
658 }
659 NumBits >>= 1;
660
661 unsigned Alignment = LD->getAlignment();
662 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000663 ISD::LoadExtType HiExtType = LD->getExtensionType();
664
665 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
666 if (HiExtType == ISD::NON_EXTLOAD)
667 HiExtType = ISD::ZEXTLOAD;
668
669 // Load the value in two parts
670 SDOperand Lo, Hi;
671 if (TLI.isLittleEndian()) {
672 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
673 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
674 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
675 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
676 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
677 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000678 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000679 } else {
680 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
681 NewLoadedVT,LD->isVolatile(), Alignment);
682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
683 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
684 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
685 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000686 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000687 }
688
689 // aggregate the two parts
690 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
691 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
692 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
693
694 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
695 Hi.getValue(1));
696
697 SDOperand Ops[] = { Result, TF };
698 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
699}
Evan Cheng912095b2007-01-04 21:56:39 +0000700
Dan Gohman82669522007-10-11 23:57:53 +0000701/// UnrollVectorOp - We know that the given vector has a legal type, however
702/// the operation it performs is not legal and is an operation that we have
703/// no way of lowering. "Unroll" the vector, splitting out the scalars and
704/// operating on each element individually.
705SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
706 MVT::ValueType VT = Op.getValueType();
707 assert(isTypeLegal(VT) &&
708 "Caller should expand or promote operands that are not legal!");
709 assert(Op.Val->getNumValues() == 1 &&
710 "Can't unroll a vector with multiple results!");
711 unsigned NE = MVT::getVectorNumElements(VT);
712 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
713
714 SmallVector<SDOperand, 8> Scalars;
715 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
716 for (unsigned i = 0; i != NE; ++i) {
717 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
718 SDOperand Operand = Op.getOperand(j);
719 MVT::ValueType OperandVT = Operand.getValueType();
720 if (MVT::isVector(OperandVT)) {
721 // A vector operand; extract a single element.
722 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
723 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
724 OperandEltVT,
725 Operand,
726 DAG.getConstant(i, MVT::i32));
727 } else {
728 // A scalar operand; just use it as is.
729 Operands[j] = Operand;
730 }
731 }
732 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
733 &Operands[0], Operands.size()));
734 }
735
736 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
737}
738
Duncan Sands007f9842008-01-10 10:28:30 +0000739/// GetFPLibCall - Return the right libcall for the given floating point type.
740static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
741 RTLIB::Libcall Call_F32,
742 RTLIB::Libcall Call_F64,
743 RTLIB::Libcall Call_F80,
744 RTLIB::Libcall Call_PPCF128) {
745 return
746 VT == MVT::f32 ? Call_F32 :
747 VT == MVT::f64 ? Call_F64 :
748 VT == MVT::f80 ? Call_F80 :
749 VT == MVT::ppcf128 ? Call_PPCF128 :
750 RTLIB::UNKNOWN_LIBCALL;
751}
752
Dan Gohmana3466152007-07-13 20:14:11 +0000753/// LegalizeOp - We know that the specified value has a legal type, and
754/// that its operands are legal. Now ensure that the operation itself
755/// is legal, recursively ensuring that the operands' operations remain
756/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000757SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000758 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
759 return Op;
760
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000761 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000762 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000763 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000764
Chris Lattner3e928bb2005-01-07 07:47:09 +0000765 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000766 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000767 if (Node->getNumValues() > 1) {
768 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000769 if (getTypeAction(Node->getValueType(i)) != Legal) {
770 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000771 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000772 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000773 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000774 }
775 }
776
Chris Lattner45982da2005-05-12 16:53:42 +0000777 // Note that LegalizeOp may be reentered even from single-use nodes, which
778 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000779 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000780 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000781
Nate Begeman9373a812005-08-10 20:51:12 +0000782 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000783 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000784 bool isCustom = false;
785
Chris Lattner3e928bb2005-01-07 07:47:09 +0000786 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000787 case ISD::FrameIndex:
788 case ISD::EntryToken:
789 case ISD::Register:
790 case ISD::BasicBlock:
791 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000792 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000793 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000794 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000795 case ISD::TargetConstantPool:
796 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000797 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000798 case ISD::TargetExternalSymbol:
799 case ISD::VALUETYPE:
800 case ISD::SRCVALUE:
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000801 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000802 case ISD::STRING:
803 case ISD::CONDCODE:
804 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000805 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000806 "This must be legal!");
807 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000808 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000809 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
810 // If this is a target node, legalize it by legalizing the operands then
811 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000812 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000813 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000814 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000815
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000816 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000817
818 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
819 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
820 return Result.getValue(Op.ResNo);
821 }
822 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000823#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000824 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000825#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000826 assert(0 && "Do not know how to legalize this operator!");
827 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000828 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000830 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000831 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000832 case ISD::ConstantPool:
833 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000834 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
835 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000836 case TargetLowering::Custom:
837 Tmp1 = TLI.LowerOperation(Op, DAG);
838 if (Tmp1.Val) Result = Tmp1;
839 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000840 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000841 break;
842 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000843 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000844 case ISD::FRAMEADDR:
845 case ISD::RETURNADDR:
846 // The only option for these nodes is to custom lower them. If the target
847 // does not custom lower them, then return zero.
848 Tmp1 = TLI.LowerOperation(Op, DAG);
849 if (Tmp1.Val)
850 Result = Tmp1;
851 else
852 Result = DAG.getConstant(0, TLI.getPointerTy());
853 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000854 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000855 MVT::ValueType VT = Node->getValueType(0);
856 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
857 default: assert(0 && "This action is not supported yet!");
858 case TargetLowering::Custom:
859 Result = TLI.LowerOperation(Op, DAG);
860 if (Result.Val) break;
861 // Fall Thru
862 case TargetLowering::Legal:
863 Result = DAG.getConstant(0, VT);
864 break;
865 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000866 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000867 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000868 case ISD::EXCEPTIONADDR: {
869 Tmp1 = LegalizeOp(Node->getOperand(0));
870 MVT::ValueType VT = Node->getValueType(0);
871 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
872 default: assert(0 && "This action is not supported yet!");
873 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000874 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000875 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000876 }
877 break;
878 case TargetLowering::Custom:
879 Result = TLI.LowerOperation(Op, DAG);
880 if (Result.Val) break;
881 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000882 case TargetLowering::Legal: {
883 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
884 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000885 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000886 break;
887 }
888 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000889 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000890 if (Result.Val->getNumValues() == 1) break;
891
892 assert(Result.Val->getNumValues() == 2 &&
893 "Cannot return more than two values!");
894
895 // Since we produced two values, make sure to remember that we
896 // legalized both of them.
897 Tmp1 = LegalizeOp(Result);
898 Tmp2 = LegalizeOp(Result.getValue(1));
899 AddLegalizedOperand(Op.getValue(0), Tmp1);
900 AddLegalizedOperand(Op.getValue(1), Tmp2);
901 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000902 case ISD::EHSELECTION: {
903 Tmp1 = LegalizeOp(Node->getOperand(0));
904 Tmp2 = LegalizeOp(Node->getOperand(1));
905 MVT::ValueType VT = Node->getValueType(0);
906 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
907 default: assert(0 && "This action is not supported yet!");
908 case TargetLowering::Expand: {
909 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000910 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000911 }
912 break;
913 case TargetLowering::Custom:
914 Result = TLI.LowerOperation(Op, DAG);
915 if (Result.Val) break;
916 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000917 case TargetLowering::Legal: {
918 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
919 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000920 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000921 break;
922 }
923 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000924 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000925 if (Result.Val->getNumValues() == 1) break;
926
927 assert(Result.Val->getNumValues() == 2 &&
928 "Cannot return more than two values!");
929
930 // Since we produced two values, make sure to remember that we
931 // legalized both of them.
932 Tmp1 = LegalizeOp(Result);
933 Tmp2 = LegalizeOp(Result.getValue(1));
934 AddLegalizedOperand(Op.getValue(0), Tmp1);
935 AddLegalizedOperand(Op.getValue(1), Tmp2);
936 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000937 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000938 MVT::ValueType VT = Node->getValueType(0);
939 // The only "good" option for this node is to custom lower it.
940 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
941 default: assert(0 && "This action is not supported at all!");
942 case TargetLowering::Custom:
943 Result = TLI.LowerOperation(Op, DAG);
944 if (Result.Val) break;
945 // Fall Thru
946 case TargetLowering::Legal:
947 // Target does not know, how to lower this, lower to noop
948 Result = LegalizeOp(Node->getOperand(0));
949 break;
950 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000951 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000952 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000953 case ISD::AssertSext:
954 case ISD::AssertZext:
955 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000957 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000958 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000959 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000960 Result = Node->getOperand(Op.ResNo);
961 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000962 case ISD::CopyFromReg:
963 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000964 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000965 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000966 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000967 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000968 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000969 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000970 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000971 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
972 } else {
973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
974 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000975 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
976 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000977 // Since CopyFromReg produces two values, make sure to remember that we
978 // legalized both of them.
979 AddLegalizedOperand(Op.getValue(0), Result);
980 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
981 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000982 case ISD::UNDEF: {
983 MVT::ValueType VT = Op.getValueType();
984 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000985 default: assert(0 && "This action is not supported yet!");
986 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000987 if (MVT::isInteger(VT))
988 Result = DAG.getConstant(0, VT);
989 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000990 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
991 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000992 else
993 assert(0 && "Unknown value type!");
994 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000995 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000996 break;
997 }
998 break;
999 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001000
Chris Lattner48b61a72006-03-28 00:40:33 +00001001 case ISD::INTRINSIC_W_CHAIN:
1002 case ISD::INTRINSIC_WO_CHAIN:
1003 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001004 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001005 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1006 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001007 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001008
1009 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001010 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001011 TargetLowering::Custom) {
1012 Tmp3 = TLI.LowerOperation(Result, DAG);
1013 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001014 }
1015
1016 if (Result.Val->getNumValues() == 1) break;
1017
1018 // Must have return value and chain result.
1019 assert(Result.Val->getNumValues() == 2 &&
1020 "Cannot return more than two values!");
1021
1022 // Since loads produce two values, make sure to remember that we
1023 // legalized both of them.
1024 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1025 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1026 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001027 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001028
1029 case ISD::LOCATION:
1030 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1031 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1032
1033 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1034 case TargetLowering::Promote:
1035 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001036 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001037 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001038 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001039 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001040
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001041 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001042 const std::string &FName =
1043 cast<StringSDNode>(Node->getOperand(3))->getValue();
1044 const std::string &DirName =
1045 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001046 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001047
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001048 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001049 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001050 SDOperand LineOp = Node->getOperand(1);
1051 SDOperand ColOp = Node->getOperand(2);
1052
1053 if (useDEBUG_LOC) {
1054 Ops.push_back(LineOp); // line #
1055 Ops.push_back(ColOp); // col #
1056 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001057 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001058 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001059 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1060 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001061 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001062 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Chengbb81d972008-01-31 09:59:15 +00001063 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1064 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001065 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001066 } else {
1067 Result = Tmp1; // chain
1068 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001069 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001070 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001071 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001072 if (Tmp1 != Node->getOperand(0) ||
1073 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001074 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001075 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001076 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1077 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1078 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1079 } else {
1080 // Otherwise promote them.
1081 Ops.push_back(PromoteOp(Node->getOperand(1)));
1082 Ops.push_back(PromoteOp(Node->getOperand(2)));
1083 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001084 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1085 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001086 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001087 }
1088 break;
1089 }
1090 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001091
1092 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001093 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001094 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001095 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001096 case TargetLowering::Legal:
1097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1098 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1099 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1100 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001102 break;
1103 }
1104 break;
1105
Jim Laskey1ee29252007-01-26 14:34:52 +00001106 case ISD::LABEL:
Evan Chengbb81d972008-01-31 09:59:15 +00001107 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskey1ee29252007-01-26 14:34:52 +00001108 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Legal:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Chengbb81d972008-01-31 09:59:15 +00001113 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1114 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001115 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001116 case TargetLowering::Expand:
1117 Result = LegalizeOp(Node->getOperand(0));
1118 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001119 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001120 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001121
Scott Michelc1513d22007-08-08 23:23:31 +00001122 case ISD::Constant: {
1123 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1124 unsigned opAction =
1125 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1126
Chris Lattner3e928bb2005-01-07 07:47:09 +00001127 // We know we don't need to expand constants here, constants only have one
1128 // value and we check that it is fine above.
1129
Scott Michelc1513d22007-08-08 23:23:31 +00001130 if (opAction == TargetLowering::Custom) {
1131 Tmp1 = TLI.LowerOperation(Result, DAG);
1132 if (Tmp1.Val)
1133 Result = Tmp1;
1134 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001135 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001136 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001137 case ISD::ConstantFP: {
1138 // Spill FP immediates to the constant pool if the target cannot directly
1139 // codegen them. Targets often have some immediate values that can be
1140 // efficiently generated into an FP register without a load. We explicitly
1141 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001142 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1143
1144 // Check to see if this FP immediate is already legal.
1145 bool isLegal = false;
1146 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1147 E = TLI.legal_fpimm_end(); I != E; ++I)
1148 if (CFP->isExactlyValue(*I)) {
1149 isLegal = true;
1150 break;
1151 }
1152
Chris Lattner3181a772006-01-29 06:26:56 +00001153 // If this is a legal constant, turn it into a TargetConstantFP node.
1154 if (isLegal) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001155 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1156 CFP->getValueType(0));
Chris Lattner3181a772006-01-29 06:26:56 +00001157 break;
1158 }
1159
1160 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1161 default: assert(0 && "This action is not supported yet!");
1162 case TargetLowering::Custom:
1163 Tmp3 = TLI.LowerOperation(Result, DAG);
1164 if (Tmp3.Val) {
1165 Result = Tmp3;
1166 break;
1167 }
1168 // FALLTHROUGH
1169 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001170 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001171 }
1172 break;
1173 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001174 case ISD::TokenFactor:
1175 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001176 Tmp1 = LegalizeOp(Node->getOperand(0));
1177 Tmp2 = LegalizeOp(Node->getOperand(1));
1178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1179 } else if (Node->getNumOperands() == 3) {
1180 Tmp1 = LegalizeOp(Node->getOperand(0));
1181 Tmp2 = LegalizeOp(Node->getOperand(1));
1182 Tmp3 = LegalizeOp(Node->getOperand(2));
1183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001184 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001185 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001186 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001187 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1188 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001189 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001190 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001191 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001192
1193 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001194 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001195 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001196 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1197 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001198
1199 // The number of incoming and outgoing values should match; unless the final
1200 // outgoing value is a flag.
1201 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1202 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1203 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1204 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001205 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001206
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001207 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001208 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001209 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001210 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1211 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001212 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001213 if (Op.ResNo == i)
1214 Tmp2 = Tmp1;
1215 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1216 }
1217 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001218 case ISD::EXTRACT_SUBREG: {
1219 Tmp1 = LegalizeOp(Node->getOperand(0));
1220 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1221 assert(idx && "Operand must be a constant");
1222 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1223 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1224 }
1225 break;
1226 case ISD::INSERT_SUBREG: {
1227 Tmp1 = LegalizeOp(Node->getOperand(0));
1228 Tmp2 = LegalizeOp(Node->getOperand(1));
1229 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1230 assert(idx && "Operand must be a constant");
1231 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1232 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1233 }
1234 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001235 case ISD::BUILD_VECTOR:
1236 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001237 default: assert(0 && "This action is not supported yet!");
1238 case TargetLowering::Custom:
1239 Tmp3 = TLI.LowerOperation(Result, DAG);
1240 if (Tmp3.Val) {
1241 Result = Tmp3;
1242 break;
1243 }
1244 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001245 case TargetLowering::Expand:
1246 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001247 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001248 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001249 break;
1250 case ISD::INSERT_VECTOR_ELT:
1251 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1252 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1253 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1254 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1255
1256 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1257 Node->getValueType(0))) {
1258 default: assert(0 && "This action is not supported yet!");
1259 case TargetLowering::Legal:
1260 break;
1261 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001262 Tmp4 = TLI.LowerOperation(Result, DAG);
1263 if (Tmp4.Val) {
1264 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001265 break;
1266 }
1267 // FALLTHROUGH
1268 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001269 // If the insert index is a constant, codegen this as a scalar_to_vector,
1270 // then a shuffle that inserts it into the right position in the vector.
1271 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1272 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1273 Tmp1.getValueType(), Tmp2);
1274
1275 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1276 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001277 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001278
1279 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1280 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1281 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001282 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001283 for (unsigned i = 0; i != NumElts; ++i) {
1284 if (i != InsertPos->getValue())
1285 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1286 else
1287 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1288 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001289 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1290 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001291
1292 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1293 Tmp1, ScVec, ShufMask);
1294 Result = LegalizeOp(Result);
1295 break;
1296 }
1297
Chris Lattner2332b9f2006-03-19 01:17:20 +00001298 // If the target doesn't support this, we have to spill the input vector
1299 // to a temporary stack slot, update the element, then reload it. This is
1300 // badness. We could also load the value into a vector register (either
1301 // with a "move to register" or "extload into register" instruction, then
1302 // permute it into place, if the idx is a constant and if the idx is
1303 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001304 MVT::ValueType VT = Tmp1.getValueType();
1305 MVT::ValueType EltVT = Tmp2.getValueType();
1306 MVT::ValueType IdxVT = Tmp3.getValueType();
1307 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001308 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohmanc6c391d2008-01-31 00:25:39 +00001309
1310 FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(StackPtr.Val);
1311 assert(StackPtrFI);
1312 int SPFI = StackPtrFI->getIndex();
1313
Evan Chengeb0b4612006-03-31 01:27:51 +00001314 // Store the vector.
Dan Gohmanc6c391d2008-01-31 00:25:39 +00001315 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
1316 &PseudoSourceValue::FPRel, SPFI);
Evan Chengeb0b4612006-03-31 01:27:51 +00001317
1318 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001319 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1320 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001321 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001322 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1323 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1324 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001325 // Store the scalar value.
Dan Gohmanc6c391d2008-01-31 00:25:39 +00001326 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, &PseudoSourceValue::FPRel, SPFI);
Evan Chengeb0b4612006-03-31 01:27:51 +00001327 // Load the updated vector.
Dan Gohmanc6c391d2008-01-31 00:25:39 +00001328 Result = DAG.getLoad(VT, Ch, StackPtr, &PseudoSourceValue::FPRel, SPFI);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001329 break;
1330 }
1331 }
1332 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001333 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001334 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1335 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1336 break;
1337 }
1338
Chris Lattnerce872152006-03-19 06:31:19 +00001339 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1340 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1341 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1342 Node->getValueType(0))) {
1343 default: assert(0 && "This action is not supported yet!");
1344 case TargetLowering::Legal:
1345 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001346 case TargetLowering::Custom:
1347 Tmp3 = TLI.LowerOperation(Result, DAG);
1348 if (Tmp3.Val) {
1349 Result = Tmp3;
1350 break;
1351 }
1352 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001353 case TargetLowering::Expand:
1354 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001355 break;
1356 }
Chris Lattnerce872152006-03-19 06:31:19 +00001357 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001358 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001359 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1360 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1361 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1362
1363 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001364 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1365 default: assert(0 && "Unknown operation action!");
1366 case TargetLowering::Legal:
1367 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1368 "vector shuffle should not be created if not legal!");
1369 break;
1370 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001371 Tmp3 = TLI.LowerOperation(Result, DAG);
1372 if (Tmp3.Val) {
1373 Result = Tmp3;
1374 break;
1375 }
1376 // FALLTHROUGH
1377 case TargetLowering::Expand: {
1378 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001379 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001380 MVT::ValueType PtrVT = TLI.getPointerTy();
1381 SDOperand Mask = Node->getOperand(2);
1382 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001383 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001384 for (unsigned i = 0; i != NumElems; ++i) {
1385 SDOperand Arg = Mask.getOperand(i);
1386 if (Arg.getOpcode() == ISD::UNDEF) {
1387 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1388 } else {
1389 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1390 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1391 if (Idx < NumElems)
1392 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1393 DAG.getConstant(Idx, PtrVT)));
1394 else
1395 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1396 DAG.getConstant(Idx - NumElems, PtrVT)));
1397 }
1398 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001399 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001400 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001401 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001402 case TargetLowering::Promote: {
1403 // Change base type to a different vector type.
1404 MVT::ValueType OVT = Node->getValueType(0);
1405 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1406
1407 // Cast the two input vectors.
1408 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1409 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1410
1411 // Convert the shuffle mask to the right # elements.
1412 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1413 assert(Tmp3.Val && "Shuffle not legal?");
1414 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1415 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1416 break;
1417 }
Chris Lattner87100e02006-03-20 01:52:29 +00001418 }
1419 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001420
1421 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001422 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001423 Tmp2 = LegalizeOp(Node->getOperand(1));
1424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001425 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001426 break;
1427
Dan Gohman7f321562007-06-25 16:23:39 +00001428 case ISD::EXTRACT_SUBVECTOR:
1429 Tmp1 = Node->getOperand(0);
1430 Tmp2 = LegalizeOp(Node->getOperand(1));
1431 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1432 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001433 break;
1434
Chris Lattner6831a812006-02-13 09:18:02 +00001435 case ISD::CALLSEQ_START: {
1436 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1437
1438 // Recursively Legalize all of the inputs of the call end that do not lead
1439 // to this call start. This ensures that any libcalls that need be inserted
1440 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001441 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001442 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001443 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1444 NodesLeadingTo);
1445 }
Chris Lattner6831a812006-02-13 09:18:02 +00001446
1447 // Now that we legalized all of the inputs (which may have inserted
1448 // libcalls) create the new CALLSEQ_START node.
1449 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1450
1451 // Merge in the last call, to ensure that this call start after the last
1452 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001453 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001454 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1455 Tmp1 = LegalizeOp(Tmp1);
1456 }
Chris Lattner6831a812006-02-13 09:18:02 +00001457
1458 // Do not try to legalize the target-specific arguments (#1+).
1459 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001460 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001461 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001462 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001463 }
1464
1465 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001466 AddLegalizedOperand(Op.getValue(0), Result);
1467 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1468 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1469
Chris Lattner6831a812006-02-13 09:18:02 +00001470 // Now that the callseq_start and all of the non-call nodes above this call
1471 // sequence have been legalized, legalize the call itself. During this
1472 // process, no libcalls can/will be inserted, guaranteeing that no calls
1473 // can overlap.
1474 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1475 SDOperand InCallSEQ = LastCALLSEQ_END;
1476 // Note that we are selecting this call!
1477 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1478 IsLegalizingCall = true;
1479
1480 // Legalize the call, starting from the CALLSEQ_END.
1481 LegalizeOp(LastCALLSEQ_END);
1482 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1483 return Result;
1484 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001485 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001486 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1487 // will cause this node to be legalized as well as handling libcalls right.
1488 if (LastCALLSEQ_END.Val != Node) {
1489 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001490 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001491 assert(I != LegalizedNodes.end() &&
1492 "Legalizing the call start should have legalized this node!");
1493 return I->second;
1494 }
1495
1496 // Otherwise, the call start has been legalized and everything is going
1497 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001498 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001499 // Do not try to legalize the target-specific arguments (#1+), except for
1500 // an optional flag input.
1501 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1502 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001503 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001504 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001505 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001506 }
1507 } else {
1508 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1509 if (Tmp1 != Node->getOperand(0) ||
1510 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001511 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001512 Ops[0] = Tmp1;
1513 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001514 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001515 }
Chris Lattner6a542892006-01-24 05:48:21 +00001516 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001517 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001518 // This finishes up call legalization.
1519 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001520
1521 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1522 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1523 if (Node->getNumValues() == 2)
1524 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1525 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001526 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001527 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001528 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1529 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1530 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001531 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001532
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001533 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001534 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001535 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001536 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001537 case TargetLowering::Expand: {
1538 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1539 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1540 " not tell us which reg is the stack pointer!");
1541 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001542
1543 // Chain the dynamic stack allocation so that it doesn't modify the stack
1544 // pointer when other instructions are using the stack.
1545 Chain = DAG.getCALLSEQ_START(Chain,
1546 DAG.getConstant(0, TLI.getPointerTy()));
1547
Chris Lattner903d2782006-01-15 08:54:32 +00001548 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001549 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1550 Chain = SP.getValue(1);
1551 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1552 unsigned StackAlign =
1553 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1554 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001555 SP = DAG.getNode(ISD::AND, VT, SP,
1556 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001557 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001558 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1559
1560 Tmp2 =
1561 DAG.getCALLSEQ_END(Chain,
1562 DAG.getConstant(0, TLI.getPointerTy()),
1563 DAG.getConstant(0, TLI.getPointerTy()),
1564 SDOperand());
1565
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001566 Tmp1 = LegalizeOp(Tmp1);
1567 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001568 break;
1569 }
1570 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001571 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1572 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001573 Tmp1 = LegalizeOp(Tmp3);
1574 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001575 }
Chris Lattner903d2782006-01-15 08:54:32 +00001576 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001577 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001578 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001579 }
Chris Lattner903d2782006-01-15 08:54:32 +00001580 // Since this op produce two values, make sure to remember that we
1581 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001582 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1583 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001584 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001585 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001586 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001587 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001588 bool Changed = false;
1589 // Legalize all of the operands of the inline asm, in case they are nodes
1590 // that need to be expanded or something. Note we skip the asm string and
1591 // all of the TargetConstant flags.
1592 SDOperand Op = LegalizeOp(Ops[0]);
1593 Changed = Op != Ops[0];
1594 Ops[0] = Op;
1595
1596 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1597 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1598 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1599 for (++i; NumVals; ++i, --NumVals) {
1600 SDOperand Op = LegalizeOp(Ops[i]);
1601 if (Op != Ops[i]) {
1602 Changed = true;
1603 Ops[i] = Op;
1604 }
1605 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001606 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001607
1608 if (HasInFlag) {
1609 Op = LegalizeOp(Ops.back());
1610 Changed |= Op != Ops.back();
1611 Ops.back() = Op;
1612 }
1613
1614 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001615 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001616
1617 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001618 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001619 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1620 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001621 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001622 case ISD::BR:
1623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001624 // Ensure that libcalls are emitted before a branch.
1625 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1626 Tmp1 = LegalizeOp(Tmp1);
1627 LastCALLSEQ_END = DAG.getEntryNode();
1628
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001629 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001630 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001631 case ISD::BRIND:
1632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1633 // Ensure that libcalls are emitted before a branch.
1634 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1635 Tmp1 = LegalizeOp(Tmp1);
1636 LastCALLSEQ_END = DAG.getEntryNode();
1637
1638 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1639 default: assert(0 && "Indirect target must be legal type (pointer)!");
1640 case Legal:
1641 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1642 break;
1643 }
1644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1645 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001646 case ISD::BR_JT:
1647 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1648 // Ensure that libcalls are emitted before a branch.
1649 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1650 Tmp1 = LegalizeOp(Tmp1);
1651 LastCALLSEQ_END = DAG.getEntryNode();
1652
1653 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1654 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1655
1656 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1657 default: assert(0 && "This action is not supported yet!");
1658 case TargetLowering::Legal: break;
1659 case TargetLowering::Custom:
1660 Tmp1 = TLI.LowerOperation(Result, DAG);
1661 if (Tmp1.Val) Result = Tmp1;
1662 break;
1663 case TargetLowering::Expand: {
1664 SDOperand Chain = Result.getOperand(0);
1665 SDOperand Table = Result.getOperand(1);
1666 SDOperand Index = Result.getOperand(2);
1667
1668 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001669 MachineFunction &MF = DAG.getMachineFunction();
1670 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001671 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1672 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001673
1674 SDOperand LD;
1675 switch (EntrySize) {
1676 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohmanc6c391d2008-01-31 00:25:39 +00001677 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
1678 &PseudoSourceValue::JTRel, 0); break;
1679 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
1680 &PseudoSourceValue::JTRel, 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001681 }
1682
Evan Chengcc415862007-11-09 01:32:10 +00001683 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001684 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001685 // For PIC, the sequence is:
1686 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001687 // RelocBase can be JumpTable, GOT or some sort of global base.
1688 if (PTy != MVT::i32)
1689 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1690 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1691 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001692 }
Evan Chengcc415862007-11-09 01:32:10 +00001693 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001694 }
1695 }
1696 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001697 case ISD::BRCOND:
1698 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001699 // Ensure that libcalls are emitted before a return.
1700 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1701 Tmp1 = LegalizeOp(Tmp1);
1702 LastCALLSEQ_END = DAG.getEntryNode();
1703
Chris Lattner47e92232005-01-18 19:27:06 +00001704 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1705 case Expand: assert(0 && "It's impossible to expand bools");
1706 case Legal:
1707 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1708 break;
1709 case Promote:
1710 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001711
1712 // The top bits of the promoted condition are not necessarily zero, ensure
1713 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001714 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001715 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1716 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001717 break;
1718 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001719
1720 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001721 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001722
1723 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1724 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001725 case TargetLowering::Legal: break;
1726 case TargetLowering::Custom:
1727 Tmp1 = TLI.LowerOperation(Result, DAG);
1728 if (Tmp1.Val) Result = Tmp1;
1729 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001730 case TargetLowering::Expand:
1731 // Expand brcond's setcc into its constituent parts and create a BR_CC
1732 // Node.
1733 if (Tmp2.getOpcode() == ISD::SETCC) {
1734 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1735 Tmp2.getOperand(0), Tmp2.getOperand(1),
1736 Node->getOperand(2));
1737 } else {
1738 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1739 DAG.getCondCode(ISD::SETNE), Tmp2,
1740 DAG.getConstant(0, Tmp2.getValueType()),
1741 Node->getOperand(2));
1742 }
1743 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001744 }
1745 break;
1746 case ISD::BR_CC:
1747 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001748 // Ensure that libcalls are emitted before a branch.
1749 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1750 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001751 Tmp2 = Node->getOperand(2); // LHS
1752 Tmp3 = Node->getOperand(3); // RHS
1753 Tmp4 = Node->getOperand(1); // CC
1754
1755 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001756 LastCALLSEQ_END = DAG.getEntryNode();
1757
Nate Begeman750ac1b2006-02-01 07:19:44 +00001758 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1759 // the LHS is a legal SETCC itself. In this case, we need to compare
1760 // the result against zero to select between true and false values.
1761 if (Tmp3.Val == 0) {
1762 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1763 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001764 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001765
1766 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1767 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001768
Chris Lattner181b7a32005-12-17 23:46:46 +00001769 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1770 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001771 case TargetLowering::Legal: break;
1772 case TargetLowering::Custom:
1773 Tmp4 = TLI.LowerOperation(Result, DAG);
1774 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001775 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001776 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001777 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001778 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001779 LoadSDNode *LD = cast<LoadSDNode>(Node);
1780 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1781 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001782
Evan Cheng466685d2006-10-09 20:57:25 +00001783 ISD::LoadExtType ExtType = LD->getExtensionType();
1784 if (ExtType == ISD::NON_EXTLOAD) {
1785 MVT::ValueType VT = Node->getValueType(0);
1786 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1787 Tmp3 = Result.getValue(0);
1788 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001789
Evan Cheng466685d2006-10-09 20:57:25 +00001790 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1791 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001792 case TargetLowering::Legal:
1793 // If this is an unaligned load and the target doesn't support it,
1794 // expand it.
1795 if (!TLI.allowsUnalignedMemoryAccesses()) {
1796 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001797 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001798 if (LD->getAlignment() < ABIAlignment){
1799 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1800 TLI);
1801 Tmp3 = Result.getOperand(0);
1802 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001803 Tmp3 = LegalizeOp(Tmp3);
1804 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001805 }
1806 }
1807 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001808 case TargetLowering::Custom:
1809 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1810 if (Tmp1.Val) {
1811 Tmp3 = LegalizeOp(Tmp1);
1812 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001813 }
Evan Cheng466685d2006-10-09 20:57:25 +00001814 break;
1815 case TargetLowering::Promote: {
1816 // Only promote a load of vector type to another.
1817 assert(MVT::isVector(VT) && "Cannot promote this load!");
1818 // Change base type to a different vector type.
1819 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1820
1821 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001822 LD->getSrcValueOffset(),
1823 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001824 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1825 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001826 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001827 }
Evan Cheng466685d2006-10-09 20:57:25 +00001828 }
1829 // Since loads produce two values, make sure to remember that we
1830 // legalized both of them.
1831 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1832 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1833 return Op.ResNo ? Tmp4 : Tmp3;
1834 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001835 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001836 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1837 int SVOffset = LD->getSrcValueOffset();
1838 unsigned Alignment = LD->getAlignment();
1839 bool isVolatile = LD->isVolatile();
1840
1841 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1842 // Some targets pretend to have an i1 loading operation, and actually
1843 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1844 // bits are guaranteed to be zero; it helps the optimizers understand
1845 // that these bits are zero. It is also useful for EXTLOAD, since it
1846 // tells the optimizers that those bits are undefined. It would be
1847 // nice to have an effective generic way of getting these benefits...
1848 // Until such a way is found, don't insist on promoting i1 here.
1849 (SrcVT != MVT::i1 ||
1850 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1851 // Promote to a byte-sized load if not loading an integral number of
1852 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1853 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1854 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1855 SDOperand Ch;
1856
1857 // The extra bits are guaranteed to be zero, since we stored them that
1858 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1859
1860 ISD::LoadExtType NewExtType =
1861 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1862
1863 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1864 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1865 NVT, isVolatile, Alignment);
1866
1867 Ch = Result.getValue(1); // The chain.
1868
1869 if (ExtType == ISD::SEXTLOAD)
1870 // Having the top bits zero doesn't help when sign extending.
1871 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1872 Result, DAG.getValueType(SrcVT));
1873 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1874 // All the top bits are guaranteed to be zero - inform the optimizers.
1875 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1876 DAG.getValueType(SrcVT));
1877
1878 Tmp1 = LegalizeOp(Result);
1879 Tmp2 = LegalizeOp(Ch);
1880 } else if (SrcWidth & (SrcWidth - 1)) {
1881 // If not loading a power-of-2 number of bits, expand as two loads.
1882 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1883 "Unsupported extload!");
1884 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1885 assert(RoundWidth < SrcWidth);
1886 unsigned ExtraWidth = SrcWidth - RoundWidth;
1887 assert(ExtraWidth < RoundWidth);
1888 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1889 "Load size not an integral number of bytes!");
1890 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1891 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1892 SDOperand Lo, Hi, Ch;
1893 unsigned IncrementSize;
1894
1895 if (TLI.isLittleEndian()) {
1896 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1897 // Load the bottom RoundWidth bits.
1898 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1899 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1900 Alignment);
1901
1902 // Load the remaining ExtraWidth bits.
1903 IncrementSize = RoundWidth / 8;
1904 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1905 DAG.getIntPtrConstant(IncrementSize));
1906 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1907 LD->getSrcValue(), SVOffset + IncrementSize,
1908 ExtraVT, isVolatile,
1909 MinAlign(Alignment, IncrementSize));
1910
1911 // Build a factor node to remember that this load is independent of the
1912 // other one.
1913 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1914 Hi.getValue(1));
1915
1916 // Move the top bits to the right place.
1917 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
1918 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
1919
1920 // Join the hi and lo parts.
1921 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001922 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001923 // Big endian - avoid unaligned loads.
1924 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
1925 // Load the top RoundWidth bits.
1926 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1927 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1928 Alignment);
1929
1930 // Load the remaining ExtraWidth bits.
1931 IncrementSize = RoundWidth / 8;
1932 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1933 DAG.getIntPtrConstant(IncrementSize));
1934 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1935 LD->getSrcValue(), SVOffset + IncrementSize,
1936 ExtraVT, isVolatile,
1937 MinAlign(Alignment, IncrementSize));
1938
1939 // Build a factor node to remember that this load is independent of the
1940 // other one.
1941 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1942 Hi.getValue(1));
1943
1944 // Move the top bits to the right place.
1945 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
1946 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
1947
1948 // Join the hi and lo parts.
1949 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
1950 }
1951
1952 Tmp1 = LegalizeOp(Result);
1953 Tmp2 = LegalizeOp(Ch);
1954 } else {
1955 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1956 default: assert(0 && "This action is not supported yet!");
1957 case TargetLowering::Custom:
1958 isCustom = true;
1959 // FALLTHROUGH
1960 case TargetLowering::Legal:
1961 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1962 Tmp1 = Result.getValue(0);
1963 Tmp2 = Result.getValue(1);
1964
1965 if (isCustom) {
1966 Tmp3 = TLI.LowerOperation(Result, DAG);
1967 if (Tmp3.Val) {
1968 Tmp1 = LegalizeOp(Tmp3);
1969 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1970 }
1971 } else {
1972 // If this is an unaligned load and the target doesn't support it,
1973 // expand it.
1974 if (!TLI.allowsUnalignedMemoryAccesses()) {
1975 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001976 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001977 if (LD->getAlignment() < ABIAlignment){
1978 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1979 TLI);
1980 Tmp1 = Result.getOperand(0);
1981 Tmp2 = Result.getOperand(1);
1982 Tmp1 = LegalizeOp(Tmp1);
1983 Tmp2 = LegalizeOp(Tmp2);
1984 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001985 }
1986 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001987 break;
1988 case TargetLowering::Expand:
1989 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1990 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1991 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1992 LD->getSrcValueOffset(),
1993 LD->isVolatile(), LD->getAlignment());
1994 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1995 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1996 Tmp2 = LegalizeOp(Load.getValue(1));
1997 break;
1998 }
1999 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2000 // Turn the unsupported load into an EXTLOAD followed by an explicit
2001 // zero/sign extend inreg.
2002 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2003 Tmp1, Tmp2, LD->getSrcValue(),
2004 LD->getSrcValueOffset(), SrcVT,
2005 LD->isVolatile(), LD->getAlignment());
2006 SDOperand ValRes;
2007 if (ExtType == ISD::SEXTLOAD)
2008 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2009 Result, DAG.getValueType(SrcVT));
2010 else
2011 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2012 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2013 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002014 break;
2015 }
Evan Cheng466685d2006-10-09 20:57:25 +00002016 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002017
Evan Cheng466685d2006-10-09 20:57:25 +00002018 // Since loads produce two values, make sure to remember that we legalized
2019 // both of them.
2020 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2021 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2022 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002023 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002024 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002025 case ISD::EXTRACT_ELEMENT: {
2026 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2027 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002028 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002029 case Legal:
2030 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2031 // 1 -> Hi
2032 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2033 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2034 TLI.getShiftAmountTy()));
2035 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2036 } else {
2037 // 0 -> Lo
2038 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2039 Node->getOperand(0));
2040 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002041 break;
2042 case Expand:
2043 // Get both the low and high parts.
2044 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2045 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2046 Result = Tmp2; // 1 -> Hi
2047 else
2048 Result = Tmp1; // 0 -> Lo
2049 break;
2050 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002051 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002052 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002053
2054 case ISD::CopyToReg:
2055 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002056
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002057 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002058 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002059 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002060 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002061 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002063 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002064 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002065 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002066 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2068 Tmp3);
2069 } else {
2070 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002071 }
2072
2073 // Since this produces two values, make sure to remember that we legalized
2074 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002075 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00002076 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002077 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002078 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002079 break;
2080
2081 case ISD::RET:
2082 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002083
2084 // Ensure that libcalls are emitted before a return.
2085 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2086 Tmp1 = LegalizeOp(Tmp1);
2087 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002088
Chris Lattner3e928bb2005-01-07 07:47:09 +00002089 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002090 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002091 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002092 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002093 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002094 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002095 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002096 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002097 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00002098 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00002099 SDOperand Lo, Hi;
2100 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002101
2102 // Big endian systems want the hi reg first.
2103 if (!TLI.isLittleEndian())
2104 std::swap(Lo, Hi);
2105
Evan Cheng13acce32006-12-11 19:27:14 +00002106 if (Hi.Val)
2107 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2108 else
2109 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002110 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002111 } else {
2112 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002113 int InIx = Tmp2.ResNo;
2114 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2115 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00002116
Dan Gohman7f321562007-06-25 16:23:39 +00002117 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002118 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00002119 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002120 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002121 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002122 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002123 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002124 } else if (NumElems == 1) {
2125 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002126 Tmp2 = ScalarizeVectorOp(Tmp2);
2127 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002129
2130 // FIXME: Returns of gcc generic vectors smaller than a legal type
2131 // should be returned in integer registers!
2132
Chris Lattnerf87324e2006-04-11 01:31:51 +00002133 // The scalarized value type may not be legal, e.g. it might require
2134 // promotion or expansion. Relegalize the return.
2135 Result = LegalizeOp(Result);
2136 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002137 // FIXME: Returns of gcc generic vectors larger than a legal vector
2138 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002139 SDOperand Lo, Hi;
2140 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002141 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002142 Result = LegalizeOp(Result);
2143 }
2144 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002145 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002146 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002147 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002149 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002150 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002151 }
2152 break;
2153 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002154 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002155 break;
2156 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002157 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002158 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002159 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002160 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2161 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002162 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002163 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002164 break;
2165 case Expand: {
2166 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002167 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002168 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002169 ExpandOp(Node->getOperand(i), Lo, Hi);
2170 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002171 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002172 if (Hi.Val) {
2173 NewValues.push_back(Hi);
2174 NewValues.push_back(Node->getOperand(i+1));
2175 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002176 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002177 }
2178 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002179 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002180 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002181
2182 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002183 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002184 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002185 Result = DAG.getNode(ISD::RET, MVT::Other,
2186 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002187 break;
2188 }
2189 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002190
Chris Lattner6862dbc2006-01-29 21:02:23 +00002191 if (Result.getOpcode() == ISD::RET) {
2192 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2193 default: assert(0 && "This action is not supported yet!");
2194 case TargetLowering::Legal: break;
2195 case TargetLowering::Custom:
2196 Tmp1 = TLI.LowerOperation(Result, DAG);
2197 if (Tmp1.Val) Result = Tmp1;
2198 break;
2199 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002200 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002201 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002202 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002203 StoreSDNode *ST = cast<StoreSDNode>(Node);
2204 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2205 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002206 int SVOffset = ST->getSrcValueOffset();
2207 unsigned Alignment = ST->getAlignment();
2208 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002209
Evan Cheng8b2794a2006-10-13 21:14:26 +00002210 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002211 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2212 // FIXME: We shouldn't do this for TargetConstantFP's.
2213 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2214 // to phase ordering between legalized code and the dag combiner. This
2215 // probably means that we need to integrate dag combiner and legalizer
2216 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002217 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002218 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002219 if (CFP->getValueType(0) == MVT::f32 &&
2220 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002221 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2222 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002223 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002224 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2225 SVOffset, isVolatile, Alignment);
2226 break;
2227 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002228 // If this target supports 64-bit registers, do a single 64-bit store.
2229 if (getTypeAction(MVT::i64) == Legal) {
2230 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2231 getZExtValue(), MVT::i64);
2232 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2233 SVOffset, isVolatile, Alignment);
2234 break;
2235 } else if (getTypeAction(MVT::i32) == Legal) {
2236 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2237 // stores. If the target supports neither 32- nor 64-bits, this
2238 // xform is certainly not worth it.
2239 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2240 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2241 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2242 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2243
2244 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2245 SVOffset, isVolatile, Alignment);
2246 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002247 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002248 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002249 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002250
2251 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2252 break;
2253 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002254 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002255 }
2256
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002257 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002258 case Legal: {
2259 Tmp3 = LegalizeOp(ST->getValue());
2260 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2261 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002262
Evan Cheng8b2794a2006-10-13 21:14:26 +00002263 MVT::ValueType VT = Tmp3.getValueType();
2264 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2265 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002266 case TargetLowering::Legal:
2267 // If this is an unaligned store and the target doesn't support it,
2268 // expand it.
2269 if (!TLI.allowsUnalignedMemoryAccesses()) {
2270 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002271 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002272 if (ST->getAlignment() < ABIAlignment)
2273 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2274 TLI);
2275 }
2276 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002277 case TargetLowering::Custom:
2278 Tmp1 = TLI.LowerOperation(Result, DAG);
2279 if (Tmp1.Val) Result = Tmp1;
2280 break;
2281 case TargetLowering::Promote:
2282 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2283 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2284 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2285 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002286 ST->getSrcValue(), SVOffset, isVolatile,
2287 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002288 break;
2289 }
2290 break;
2291 }
2292 case Promote:
2293 // Truncate the value and store the result.
2294 Tmp3 = PromoteOp(ST->getValue());
2295 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002296 SVOffset, ST->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002297 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002298 break;
2299
2300 case Expand:
2301 unsigned IncrementSize = 0;
2302 SDOperand Lo, Hi;
2303
2304 // If this is a vector type, then we have to calculate the increment as
2305 // the product of the element size in bytes, and the number of elements
2306 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002307 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002308 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002309 int InIx = ST->getValue().ResNo;
Chris Lattner0bd48932008-01-17 07:00:52 +00002310 MVT::ValueType InVT = InVal->getValueType(InIx);
2311 unsigned NumElems = MVT::getVectorNumElements(InVT);
2312 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002313
Dan Gohman7f321562007-06-25 16:23:39 +00002314 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002315 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002316 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002317 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002318 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002319 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002320 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002321 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002322 Result = LegalizeOp(Result);
2323 break;
2324 } else if (NumElems == 1) {
2325 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002326 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002327 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002328 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002329 // The scalarized value type may not be legal, e.g. it might require
2330 // promotion or expansion. Relegalize the scalar store.
2331 Result = LegalizeOp(Result);
2332 break;
2333 } else {
2334 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002335 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2336 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002337 }
2338 } else {
2339 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002340 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002341
2342 if (!TLI.isLittleEndian())
2343 std::swap(Lo, Hi);
2344 }
2345
2346 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002347 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002348
2349 if (Hi.Val == NULL) {
2350 // Must be int <-> float one-to-one expansion.
2351 Result = Lo;
2352 break;
2353 }
2354
Evan Cheng8b2794a2006-10-13 21:14:26 +00002355 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002356 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002357 assert(isTypeLegal(Tmp2.getValueType()) &&
2358 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002359 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002360 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002361 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002362 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002363 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2364 break;
2365 }
2366 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002367 switch (getTypeAction(ST->getValue().getValueType())) {
2368 case Legal:
2369 Tmp3 = LegalizeOp(ST->getValue());
2370 break;
2371 case Promote:
2372 // We can promote the value, the truncstore will still take care of it.
2373 Tmp3 = PromoteOp(ST->getValue());
2374 break;
2375 case Expand:
2376 // Just store the low part. This may become a non-trunc store, so make
2377 // sure to use getTruncStore, not UpdateNodeOperands below.
2378 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2379 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2380 SVOffset, MVT::i8, isVolatile, Alignment);
2381 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002382
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002383 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands7e857202008-01-22 07:17:34 +00002384 unsigned StWidth = MVT::getSizeInBits(StVT);
2385
2386 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2387 // Promote to a byte-sized store with upper bits zero if not
2388 // storing an integral number of bytes. For example, promote
2389 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2390 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2391 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2392 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2393 SVOffset, NVT, isVolatile, Alignment);
2394 } else if (StWidth & (StWidth - 1)) {
2395 // If not storing a power-of-2 number of bits, expand as two stores.
2396 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2397 "Unsupported truncstore!");
2398 unsigned RoundWidth = 1 << Log2_32(StWidth);
2399 assert(RoundWidth < StWidth);
2400 unsigned ExtraWidth = StWidth - RoundWidth;
2401 assert(ExtraWidth < RoundWidth);
2402 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2403 "Store size not an integral number of bytes!");
2404 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2405 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2406 SDOperand Lo, Hi;
2407 unsigned IncrementSize;
2408
2409 if (TLI.isLittleEndian()) {
2410 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2411 // Store the bottom RoundWidth bits.
2412 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2413 SVOffset, RoundVT,
2414 isVolatile, Alignment);
2415
2416 // Store the remaining ExtraWidth bits.
2417 IncrementSize = RoundWidth / 8;
2418 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2419 DAG.getIntPtrConstant(IncrementSize));
2420 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2421 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2422 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2423 SVOffset + IncrementSize, ExtraVT, isVolatile,
2424 MinAlign(Alignment, IncrementSize));
2425 } else {
2426 // Big endian - avoid unaligned stores.
2427 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2428 // Store the top RoundWidth bits.
2429 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2430 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2431 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2432 RoundVT, isVolatile, Alignment);
2433
2434 // Store the remaining ExtraWidth bits.
2435 IncrementSize = RoundWidth / 8;
2436 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2437 DAG.getIntPtrConstant(IncrementSize));
2438 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2439 SVOffset + IncrementSize, ExtraVT, isVolatile,
2440 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002441 }
Duncan Sands7e857202008-01-22 07:17:34 +00002442
2443 // The order of the stores doesn't matter.
2444 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2445 } else {
2446 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2447 Tmp2 != ST->getBasePtr())
2448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2449 ST->getOffset());
2450
2451 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2452 default: assert(0 && "This action is not supported yet!");
2453 case TargetLowering::Legal:
2454 // If this is an unaligned store and the target doesn't support it,
2455 // expand it.
2456 if (!TLI.allowsUnalignedMemoryAccesses()) {
2457 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002458 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands7e857202008-01-22 07:17:34 +00002459 if (ST->getAlignment() < ABIAlignment)
2460 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2461 TLI);
2462 }
2463 break;
2464 case TargetLowering::Custom:
2465 Result = TLI.LowerOperation(Result, DAG);
2466 break;
2467 case Expand:
2468 // TRUNCSTORE:i16 i32 -> STORE i16
2469 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2470 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2471 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2472 isVolatile, Alignment);
2473 break;
2474 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002475 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002476 }
2477 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002478 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002479 case ISD::PCMARKER:
2480 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002481 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002482 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002483 case ISD::STACKSAVE:
2484 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002485 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2486 Tmp1 = Result.getValue(0);
2487 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002488
Chris Lattner140d53c2006-01-13 02:50:02 +00002489 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2490 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002491 case TargetLowering::Legal: break;
2492 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002493 Tmp3 = TLI.LowerOperation(Result, DAG);
2494 if (Tmp3.Val) {
2495 Tmp1 = LegalizeOp(Tmp3);
2496 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002497 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002498 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002499 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002500 // Expand to CopyFromReg if the target set
2501 // StackPointerRegisterToSaveRestore.
2502 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002503 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002504 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002505 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002506 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002507 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2508 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002509 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002510 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002511 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002512
2513 // Since stacksave produce two values, make sure to remember that we
2514 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002515 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2516 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2517 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002518
Chris Lattner140d53c2006-01-13 02:50:02 +00002519 case ISD::STACKRESTORE:
2520 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2521 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002522 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002523
2524 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2525 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002526 case TargetLowering::Legal: break;
2527 case TargetLowering::Custom:
2528 Tmp1 = TLI.LowerOperation(Result, DAG);
2529 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002530 break;
2531 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002532 // Expand to CopyToReg if the target set
2533 // StackPointerRegisterToSaveRestore.
2534 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2535 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2536 } else {
2537 Result = Tmp1;
2538 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002539 break;
2540 }
2541 break;
2542
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002543 case ISD::READCYCLECOUNTER:
2544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002545 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002546 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2547 Node->getValueType(0))) {
2548 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002549 case TargetLowering::Legal:
2550 Tmp1 = Result.getValue(0);
2551 Tmp2 = Result.getValue(1);
2552 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002553 case TargetLowering::Custom:
2554 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002555 Tmp1 = LegalizeOp(Result.getValue(0));
2556 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002557 break;
2558 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002559
2560 // Since rdcc produce two values, make sure to remember that we legalized
2561 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002562 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2563 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002564 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002565
Chris Lattner2ee743f2005-01-14 22:08:15 +00002566 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002567 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2568 case Expand: assert(0 && "It's impossible to expand bools");
2569 case Legal:
2570 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2571 break;
2572 case Promote:
2573 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002574 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002575 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002576 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2577 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002578 break;
2579 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002580 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002581 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002582
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002583 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002584
Nate Begemanb942a3d2005-08-23 04:29:48 +00002585 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002586 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002587 case TargetLowering::Legal: break;
2588 case TargetLowering::Custom: {
2589 Tmp1 = TLI.LowerOperation(Result, DAG);
2590 if (Tmp1.Val) Result = Tmp1;
2591 break;
2592 }
Nate Begeman9373a812005-08-10 20:51:12 +00002593 case TargetLowering::Expand:
2594 if (Tmp1.getOpcode() == ISD::SETCC) {
2595 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2596 Tmp2, Tmp3,
2597 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2598 } else {
2599 Result = DAG.getSelectCC(Tmp1,
2600 DAG.getConstant(0, Tmp1.getValueType()),
2601 Tmp2, Tmp3, ISD::SETNE);
2602 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002603 break;
2604 case TargetLowering::Promote: {
2605 MVT::ValueType NVT =
2606 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2607 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002608 if (MVT::isVector(Tmp2.getValueType())) {
2609 ExtOp = ISD::BIT_CONVERT;
2610 TruncOp = ISD::BIT_CONVERT;
2611 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002612 ExtOp = ISD::ANY_EXTEND;
2613 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002614 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002615 ExtOp = ISD::FP_EXTEND;
2616 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002617 }
2618 // Promote each of the values to the new type.
2619 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2620 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2621 // Perform the larger operation, then round down.
2622 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002623 if (TruncOp != ISD::FP_ROUND)
2624 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2625 else
2626 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2627 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002628 break;
2629 }
2630 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002631 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002632 case ISD::SELECT_CC: {
2633 Tmp1 = Node->getOperand(0); // LHS
2634 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002635 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2636 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002637 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002638
Nate Begeman750ac1b2006-02-01 07:19:44 +00002639 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2640
2641 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2642 // the LHS is a legal SETCC itself. In this case, we need to compare
2643 // the result against zero to select between true and false values.
2644 if (Tmp2.Val == 0) {
2645 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2646 CC = DAG.getCondCode(ISD::SETNE);
2647 }
2648 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2649
2650 // Everything is legal, see if we should expand this op or something.
2651 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2652 default: assert(0 && "This action is not supported yet!");
2653 case TargetLowering::Legal: break;
2654 case TargetLowering::Custom:
2655 Tmp1 = TLI.LowerOperation(Result, DAG);
2656 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002657 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002658 }
2659 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002660 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002661 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002662 Tmp1 = Node->getOperand(0);
2663 Tmp2 = Node->getOperand(1);
2664 Tmp3 = Node->getOperand(2);
2665 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2666
2667 // If we had to Expand the SetCC operands into a SELECT node, then it may
2668 // not always be possible to return a true LHS & RHS. In this case, just
2669 // return the value we legalized, returned in the LHS
2670 if (Tmp2.Val == 0) {
2671 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002672 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002673 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002674
Chris Lattner73e142f2006-01-30 22:43:50 +00002675 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002676 default: assert(0 && "Cannot handle this action for SETCC yet!");
2677 case TargetLowering::Custom:
2678 isCustom = true;
2679 // FALLTHROUGH.
2680 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002681 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002682 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002683 Tmp4 = TLI.LowerOperation(Result, DAG);
2684 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002685 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002686 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002687 case TargetLowering::Promote: {
2688 // First step, figure out the appropriate operation to use.
2689 // Allow SETCC to not be supported for all legal data types
2690 // Mostly this targets FP
2691 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002692 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002693
2694 // Scan for the appropriate larger type to use.
2695 while (1) {
2696 NewInTy = (MVT::ValueType)(NewInTy+1);
2697
2698 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2699 "Fell off of the edge of the integer world");
2700 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2701 "Fell off of the edge of the floating point world");
2702
2703 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002704 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002705 break;
2706 }
2707 if (MVT::isInteger(NewInTy))
2708 assert(0 && "Cannot promote Legal Integer SETCC yet");
2709 else {
2710 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2711 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2712 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002713 Tmp1 = LegalizeOp(Tmp1);
2714 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002715 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002716 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002717 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002718 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002719 case TargetLowering::Expand:
2720 // Expand a setcc node into a select_cc of the same condition, lhs, and
2721 // rhs that selects between const 1 (true) and const 0 (false).
2722 MVT::ValueType VT = Node->getValueType(0);
2723 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2724 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002725 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002726 break;
2727 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002728 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002729 case ISD::MEMSET:
2730 case ISD::MEMCPY:
2731 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002732 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002733 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2734
2735 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2736 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2737 case Expand: assert(0 && "Cannot expand a byte!");
2738 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002739 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002740 break;
2741 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002742 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002743 break;
2744 }
2745 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002746 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002747 }
Chris Lattner272455b2005-02-02 03:44:41 +00002748
2749 SDOperand Tmp4;
2750 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002751 case Expand: {
2752 // Length is too big, just take the lo-part of the length.
2753 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002754 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002755 break;
2756 }
Chris Lattnere5605212005-01-28 22:29:18 +00002757 case Legal:
2758 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002759 break;
2760 case Promote:
2761 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002762 break;
2763 }
2764
2765 SDOperand Tmp5;
2766 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2767 case Expand: assert(0 && "Cannot expand this yet!");
2768 case Legal:
2769 Tmp5 = LegalizeOp(Node->getOperand(4));
2770 break;
2771 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002772 Tmp5 = PromoteOp(Node->getOperand(4));
2773 break;
2774 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002775
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002776 SDOperand Tmp6;
2777 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2778 case Expand: assert(0 && "Cannot expand this yet!");
2779 case Legal:
2780 Tmp6 = LegalizeOp(Node->getOperand(5));
2781 break;
2782 case Promote:
2783 Tmp6 = PromoteOp(Node->getOperand(5));
2784 break;
2785 }
2786
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002787 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2788 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002789 case TargetLowering::Custom:
2790 isCustom = true;
2791 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002792 case TargetLowering::Legal: {
2793 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2794 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002795 if (isCustom) {
2796 Tmp1 = TLI.LowerOperation(Result, DAG);
2797 if (Tmp1.Val) Result = Tmp1;
2798 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002799 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002800 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002801 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002802 // Otherwise, the target does not support this operation. Lower the
2803 // operation to an explicit libcall as appropriate.
2804 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002805 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002806 TargetLowering::ArgListTy Args;
2807 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002808
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002809 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002810 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002811 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002812 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002813 // Extend the (previously legalized) ubyte argument to be an int value
2814 // for the call.
2815 if (Tmp3.getValueType() > MVT::i32)
2816 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2817 else
2818 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002819 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002820 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002821 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002822 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002823
2824 FnName = "memset";
2825 } else if (Node->getOpcode() == ISD::MEMCPY ||
2826 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002827 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002828 Entry.Node = Tmp2; Args.push_back(Entry);
2829 Entry.Node = Tmp3; Args.push_back(Entry);
2830 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002831 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2832 } else {
2833 assert(0 && "Unknown op!");
2834 }
Chris Lattner45982da2005-05-12 16:53:42 +00002835
Chris Lattnere1bd8222005-01-11 05:57:22 +00002836 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002837 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002838 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002839 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002840 break;
2841 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002842 }
2843 break;
2844 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002845
Chris Lattner5b359c62005-04-02 04:00:59 +00002846 case ISD::SHL_PARTS:
2847 case ISD::SRA_PARTS:
2848 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002849 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002850 bool Changed = false;
2851 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2852 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2853 Changed |= Ops.back() != Node->getOperand(i);
2854 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002855 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002856 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002857
Evan Cheng05a2d562006-01-09 18:31:59 +00002858 switch (TLI.getOperationAction(Node->getOpcode(),
2859 Node->getValueType(0))) {
2860 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002861 case TargetLowering::Legal: break;
2862 case TargetLowering::Custom:
2863 Tmp1 = TLI.LowerOperation(Result, DAG);
2864 if (Tmp1.Val) {
2865 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002866 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002867 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002868 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2869 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002870 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002871 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002872 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002873 return RetVal;
2874 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002875 break;
2876 }
2877
Chris Lattner2c8086f2005-04-02 05:00:07 +00002878 // Since these produce multiple values, make sure to remember that we
2879 // legalized all of them.
2880 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2881 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2882 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002883 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002884
2885 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002886 case ISD::ADD:
2887 case ISD::SUB:
2888 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002889 case ISD::MULHS:
2890 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002891 case ISD::UDIV:
2892 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002893 case ISD::AND:
2894 case ISD::OR:
2895 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002896 case ISD::SHL:
2897 case ISD::SRL:
2898 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002899 case ISD::FADD:
2900 case ISD::FSUB:
2901 case ISD::FMUL:
2902 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002903 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002904 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002905 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2906 case Expand: assert(0 && "Not possible");
2907 case Legal:
2908 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2909 break;
2910 case Promote:
2911 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2912 break;
2913 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002914
2915 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002916
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002917 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002918 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002919 case TargetLowering::Legal: break;
2920 case TargetLowering::Custom:
2921 Tmp1 = TLI.LowerOperation(Result, DAG);
2922 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002923 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002924 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002925 MVT::ValueType VT = Op.getValueType();
2926
2927 // See if multiply or divide can be lowered using two-result operations.
2928 SDVTList VTs = DAG.getVTList(VT, VT);
2929 if (Node->getOpcode() == ISD::MUL) {
2930 // We just need the low half of the multiply; try both the signed
2931 // and unsigned forms. If the target supports both SMUL_LOHI and
2932 // UMUL_LOHI, form a preference by checking which forms of plain
2933 // MULH it supports.
2934 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2935 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2936 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2937 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2938 unsigned OpToUse = 0;
2939 if (HasSMUL_LOHI && !HasMULHS) {
2940 OpToUse = ISD::SMUL_LOHI;
2941 } else if (HasUMUL_LOHI && !HasMULHU) {
2942 OpToUse = ISD::UMUL_LOHI;
2943 } else if (HasSMUL_LOHI) {
2944 OpToUse = ISD::SMUL_LOHI;
2945 } else if (HasUMUL_LOHI) {
2946 OpToUse = ISD::UMUL_LOHI;
2947 }
2948 if (OpToUse) {
2949 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2950 break;
2951 }
2952 }
2953 if (Node->getOpcode() == ISD::MULHS &&
2954 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2955 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2956 break;
2957 }
2958 if (Node->getOpcode() == ISD::MULHU &&
2959 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2960 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2961 break;
2962 }
2963 if (Node->getOpcode() == ISD::SDIV &&
2964 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2965 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2966 break;
2967 }
2968 if (Node->getOpcode() == ISD::UDIV &&
2969 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2970 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2971 break;
2972 }
2973
Dan Gohman82669522007-10-11 23:57:53 +00002974 // Check to see if we have a libcall for this operator.
2975 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2976 bool isSigned = false;
2977 switch (Node->getOpcode()) {
2978 case ISD::UDIV:
2979 case ISD::SDIV:
2980 if (VT == MVT::i32) {
2981 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002982 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002983 isSigned = Node->getOpcode() == ISD::SDIV;
2984 }
2985 break;
2986 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002987 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2988 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002989 break;
2990 default: break;
2991 }
2992 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2993 SDOperand Dummy;
2994 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002995 break;
2996 }
2997
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002998 assert(MVT::isVector(Node->getValueType(0)) &&
2999 "Cannot expand this binary operator!");
3000 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003001 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003002 break;
3003 }
Evan Chengcc987612006-04-12 21:20:24 +00003004 case TargetLowering::Promote: {
3005 switch (Node->getOpcode()) {
3006 default: assert(0 && "Do not know how to promote this BinOp!");
3007 case ISD::AND:
3008 case ISD::OR:
3009 case ISD::XOR: {
3010 MVT::ValueType OVT = Node->getValueType(0);
3011 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3012 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3013 // Bit convert each of the values to the new type.
3014 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3015 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3016 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3017 // Bit convert the result back the original type.
3018 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3019 break;
3020 }
3021 }
3022 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003023 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003024 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003025
Dan Gohmane14ea862007-10-05 14:17:22 +00003026 case ISD::SMUL_LOHI:
3027 case ISD::UMUL_LOHI:
3028 case ISD::SDIVREM:
3029 case ISD::UDIVREM:
3030 // These nodes will only be produced by target-specific lowering, so
3031 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003032 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003033 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003034
3035 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3036 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3037 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003038 break;
3039
Chris Lattnera09f8482006-03-05 05:09:38 +00003040 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3041 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3042 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3043 case Expand: assert(0 && "Not possible");
3044 case Legal:
3045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3046 break;
3047 case Promote:
3048 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3049 break;
3050 }
3051
3052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3053
3054 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3055 default: assert(0 && "Operation not supported");
3056 case TargetLowering::Custom:
3057 Tmp1 = TLI.LowerOperation(Result, DAG);
3058 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003059 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003060 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003061 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003062 // If this target supports fabs/fneg natively and select is cheap,
3063 // do this efficiently.
3064 if (!TLI.isSelectExpensive() &&
3065 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3066 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003067 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003068 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003069 // Get the sign bit of the RHS.
3070 MVT::ValueType IVT =
3071 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3072 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3073 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3074 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3075 // Get the absolute value of the result.
3076 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3077 // Select between the nabs and abs value based on the sign bit of
3078 // the input.
3079 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3080 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3081 AbsVal),
3082 AbsVal);
3083 Result = LegalizeOp(Result);
3084 break;
3085 }
3086
3087 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00003088 MVT::ValueType NVT =
3089 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3090 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3091 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3092 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003093 break;
3094 }
Evan Cheng912095b2007-01-04 21:56:39 +00003095 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003096 break;
3097
Nate Begeman551bf3f2006-02-17 05:43:56 +00003098 case ISD::ADDC:
3099 case ISD::SUBC:
3100 Tmp1 = LegalizeOp(Node->getOperand(0));
3101 Tmp2 = LegalizeOp(Node->getOperand(1));
3102 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3103 // Since this produces two values, make sure to remember that we legalized
3104 // both of them.
3105 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3106 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3107 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003108
Nate Begeman551bf3f2006-02-17 05:43:56 +00003109 case ISD::ADDE:
3110 case ISD::SUBE:
3111 Tmp1 = LegalizeOp(Node->getOperand(0));
3112 Tmp2 = LegalizeOp(Node->getOperand(1));
3113 Tmp3 = LegalizeOp(Node->getOperand(2));
3114 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3115 // Since this produces two values, make sure to remember that we legalized
3116 // both of them.
3117 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3118 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3119 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003120
Nate Begeman419f8b62005-10-18 00:27:41 +00003121 case ISD::BUILD_PAIR: {
3122 MVT::ValueType PairTy = Node->getValueType(0);
3123 // TODO: handle the case where the Lo and Hi operands are not of legal type
3124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3125 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3126 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003127 case TargetLowering::Promote:
3128 case TargetLowering::Custom:
3129 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003130 case TargetLowering::Legal:
3131 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3132 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3133 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003134 case TargetLowering::Expand:
3135 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3136 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3137 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3138 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3139 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003140 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003141 break;
3142 }
3143 break;
3144 }
3145
Nate Begemanc105e192005-04-06 00:23:54 +00003146 case ISD::UREM:
3147 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003148 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003149 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3150 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003151
Nate Begemanc105e192005-04-06 00:23:54 +00003152 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003153 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3154 case TargetLowering::Custom:
3155 isCustom = true;
3156 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003157 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003159 if (isCustom) {
3160 Tmp1 = TLI.LowerOperation(Result, DAG);
3161 if (Tmp1.Val) Result = Tmp1;
3162 }
Nate Begemanc105e192005-04-06 00:23:54 +00003163 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003164 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003165 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003166 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00003167 MVT::ValueType VT = Node->getValueType(0);
3168
3169 // See if remainder can be lowered using two-result operations.
3170 SDVTList VTs = DAG.getVTList(VT, VT);
3171 if (Node->getOpcode() == ISD::SREM &&
3172 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3173 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3174 break;
3175 }
3176 if (Node->getOpcode() == ISD::UREM &&
3177 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3178 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3179 break;
3180 }
3181
3182 if (MVT::isInteger(VT)) {
3183 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003184 TargetLowering::Legal) {
3185 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003186 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003187 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3188 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00003189 } else if (MVT::isVector(VT)) {
3190 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003191 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003192 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003193 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003194 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3195 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003196 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003197 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003198 }
Dan Gohman0d974262007-11-06 22:11:54 +00003199 } else {
3200 assert(MVT::isFloatingPoint(VT) &&
3201 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003202 if (MVT::isVector(VT)) {
3203 Result = LegalizeOp(UnrollVectorOp(Op));
3204 } else {
3205 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003206 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3207 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003208 SDOperand Dummy;
3209 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3210 false/*sign irrelevant*/, Dummy);
3211 }
Nate Begemanc105e192005-04-06 00:23:54 +00003212 }
3213 break;
3214 }
Dan Gohman525178c2007-10-08 18:33:35 +00003215 }
Nate Begemanc105e192005-04-06 00:23:54 +00003216 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003217 case ISD::VAARG: {
3218 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3219 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3220
Chris Lattner5c62f332006-01-28 07:42:08 +00003221 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003222 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3223 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003224 case TargetLowering::Custom:
3225 isCustom = true;
3226 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003227 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003228 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3229 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003230 Tmp1 = Result.getValue(1);
3231
3232 if (isCustom) {
3233 Tmp2 = TLI.LowerOperation(Result, DAG);
3234 if (Tmp2.Val) {
3235 Result = LegalizeOp(Tmp2);
3236 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3237 }
3238 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003239 break;
3240 case TargetLowering::Expand: {
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003241 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3242 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003243 // Increment the pointer, VAList, to the next vaarg
3244 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3245 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3246 TLI.getPointerTy()));
3247 // Store the incremented VAList to the legalized pointer
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003248 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003249 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003250 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003251 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003252 Result = LegalizeOp(Result);
3253 break;
3254 }
3255 }
3256 // Since VAARG produces two values, make sure to remember that we
3257 // legalized both of them.
3258 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003259 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3260 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003261 }
3262
3263 case ISD::VACOPY:
3264 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3265 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3266 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3267
3268 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3269 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003270 case TargetLowering::Custom:
3271 isCustom = true;
3272 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003273 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003274 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3275 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003276 if (isCustom) {
3277 Tmp1 = TLI.LowerOperation(Result, DAG);
3278 if (Tmp1.Val) Result = Tmp1;
3279 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003280 break;
3281 case TargetLowering::Expand:
3282 // This defaults to loading a pointer from the input and storing it to the
3283 // output, returning the chain.
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003284 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3285 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3286 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3287 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003288 break;
3289 }
3290 break;
3291
3292 case ISD::VAEND:
3293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3294 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3295
3296 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3297 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003298 case TargetLowering::Custom:
3299 isCustom = true;
3300 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003301 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003302 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003303 if (isCustom) {
3304 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3305 if (Tmp1.Val) Result = Tmp1;
3306 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003307 break;
3308 case TargetLowering::Expand:
3309 Result = Tmp1; // Default to a no-op, return the chain
3310 break;
3311 }
3312 break;
3313
3314 case ISD::VASTART:
3315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3316 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3317
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3319
Nate Begemanacc398c2006-01-25 18:21:52 +00003320 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3321 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003322 case TargetLowering::Legal: break;
3323 case TargetLowering::Custom:
3324 Tmp1 = TLI.LowerOperation(Result, DAG);
3325 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003326 break;
3327 }
3328 break;
3329
Nate Begeman35ef9132006-01-11 21:21:00 +00003330 case ISD::ROTL:
3331 case ISD::ROTR:
3332 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3333 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003334 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003335 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3336 default:
3337 assert(0 && "ROTL/ROTR legalize operation not supported");
3338 break;
3339 case TargetLowering::Legal:
3340 break;
3341 case TargetLowering::Custom:
3342 Tmp1 = TLI.LowerOperation(Result, DAG);
3343 if (Tmp1.Val) Result = Tmp1;
3344 break;
3345 case TargetLowering::Promote:
3346 assert(0 && "Do not know how to promote ROTL/ROTR");
3347 break;
3348 case TargetLowering::Expand:
3349 assert(0 && "Do not know how to expand ROTL/ROTR");
3350 break;
3351 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003352 break;
3353
Nate Begemand88fc032006-01-14 03:14:10 +00003354 case ISD::BSWAP:
3355 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3356 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003357 case TargetLowering::Custom:
3358 assert(0 && "Cannot custom legalize this yet!");
3359 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003360 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003361 break;
3362 case TargetLowering::Promote: {
3363 MVT::ValueType OVT = Tmp1.getValueType();
3364 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003365 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003366
Chris Lattner456a93a2006-01-28 07:39:30 +00003367 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3368 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3369 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3370 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3371 break;
3372 }
3373 case TargetLowering::Expand:
3374 Result = ExpandBSWAP(Tmp1);
3375 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003376 }
3377 break;
3378
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003379 case ISD::CTPOP:
3380 case ISD::CTTZ:
3381 case ISD::CTLZ:
3382 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3383 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003384 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003385 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003386 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003387 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003388 TargetLowering::Custom) {
3389 Tmp1 = TLI.LowerOperation(Result, DAG);
3390 if (Tmp1.Val) {
3391 Result = Tmp1;
3392 }
Scott Michel910b66d2007-07-30 21:00:31 +00003393 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003394 break;
3395 case TargetLowering::Promote: {
3396 MVT::ValueType OVT = Tmp1.getValueType();
3397 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003398
3399 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003400 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3401 // Perform the larger operation, then subtract if needed.
3402 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003403 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003404 case ISD::CTPOP:
3405 Result = Tmp1;
3406 break;
3407 case ISD::CTTZ:
3408 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003409 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003410 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003411 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003412 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003413 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003414 break;
3415 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003416 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003417 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003418 DAG.getConstant(MVT::getSizeInBits(NVT) -
3419 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003420 break;
3421 }
3422 break;
3423 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003424 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003425 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003426 break;
3427 }
3428 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003429
Chris Lattner2c8086f2005-04-02 05:00:07 +00003430 // Unary operators
3431 case ISD::FABS:
3432 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003433 case ISD::FSQRT:
3434 case ISD::FSIN:
3435 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003436 Tmp1 = LegalizeOp(Node->getOperand(0));
3437 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003438 case TargetLowering::Promote:
3439 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003440 isCustom = true;
3441 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003442 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003443 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003444 if (isCustom) {
3445 Tmp1 = TLI.LowerOperation(Result, DAG);
3446 if (Tmp1.Val) Result = Tmp1;
3447 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003448 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003449 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003450 switch (Node->getOpcode()) {
3451 default: assert(0 && "Unreachable!");
3452 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003453 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3454 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003455 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003456 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003457 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003458 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3459 MVT::ValueType VT = Node->getValueType(0);
3460 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003461 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003462 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3463 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003464 break;
3465 }
3466 case ISD::FSQRT:
3467 case ISD::FSIN:
3468 case ISD::FCOS: {
3469 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003470
3471 // Expand unsupported unary vector operators by unrolling them.
3472 if (MVT::isVector(VT)) {
3473 Result = LegalizeOp(UnrollVectorOp(Op));
3474 break;
3475 }
3476
Evan Cheng56966222007-01-12 02:11:51 +00003477 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003478 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003479 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003480 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3481 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003482 break;
3483 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003484 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3485 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003486 break;
3487 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003488 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3489 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003490 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003491 default: assert(0 && "Unreachable!");
3492 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003493 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003494 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3495 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003496 break;
3497 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003498 }
3499 break;
3500 }
3501 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003502 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003503 MVT::ValueType VT = Node->getValueType(0);
3504
3505 // Expand unsupported unary vector operators by unrolling them.
3506 if (MVT::isVector(VT)) {
3507 Result = LegalizeOp(UnrollVectorOp(Op));
3508 break;
3509 }
3510
3511 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003512 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3513 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003514 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003515 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3516 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003517 break;
3518 }
Chris Lattner35481892005-12-23 00:16:34 +00003519 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003520 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003521 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3522 Node->getValueType(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003523 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3524 // The input has to be a vector type, we have to either scalarize it, pack
3525 // it, or convert it based on whether the input vector type is legal.
3526 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003527 int InIx = Node->getOperand(0).ResNo;
3528 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3529 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003530
3531 // Figure out if there is a simple type corresponding to this Vector
3532 // type. If so, convert to the vector type.
3533 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3534 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003535 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003536 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3537 LegalizeOp(Node->getOperand(0)));
3538 break;
3539 } else if (NumElems == 1) {
3540 // Turn this into a bit convert of the scalar input.
3541 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3542 ScalarizeVectorOp(Node->getOperand(0)));
3543 break;
3544 } else {
3545 // FIXME: UNIMP! Store then reload
3546 assert(0 && "Cast from unsupported vector type not implemented yet!");
3547 }
Chris Lattner67993f72006-01-23 07:30:46 +00003548 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003549 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3550 Node->getOperand(0).getValueType())) {
3551 default: assert(0 && "Unknown operation action!");
3552 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003553 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3554 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003555 break;
3556 case TargetLowering::Legal:
3557 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003558 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003559 break;
3560 }
3561 }
3562 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003563
Chris Lattner2c8086f2005-04-02 05:00:07 +00003564 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003565 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003566 case ISD::UINT_TO_FP: {
3567 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003568 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3569 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003570 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003571 Node->getOperand(0).getValueType())) {
3572 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003573 case TargetLowering::Custom:
3574 isCustom = true;
3575 // FALLTHROUGH
3576 case TargetLowering::Legal:
3577 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003578 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003579 if (isCustom) {
3580 Tmp1 = TLI.LowerOperation(Result, DAG);
3581 if (Tmp1.Val) Result = Tmp1;
3582 }
3583 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003584 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003585 Result = ExpandLegalINT_TO_FP(isSigned,
3586 LegalizeOp(Node->getOperand(0)),
3587 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003588 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003589 case TargetLowering::Promote:
3590 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3591 Node->getValueType(0),
3592 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003593 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003594 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003595 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003596 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003597 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3598 Node->getValueType(0), Node->getOperand(0));
3599 break;
3600 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003601 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003602 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003603 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3604 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003605 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003606 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3607 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003608 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003609 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3610 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003611 break;
3612 }
3613 break;
3614 }
3615 case ISD::TRUNCATE:
3616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3617 case Legal:
3618 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003619 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003620 break;
3621 case Expand:
3622 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3623
3624 // Since the result is legal, we should just be able to truncate the low
3625 // part of the source.
3626 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3627 break;
3628 case Promote:
3629 Result = PromoteOp(Node->getOperand(0));
3630 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3631 break;
3632 }
3633 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003634
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003635 case ISD::FP_TO_SINT:
3636 case ISD::FP_TO_UINT:
3637 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3638 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003639 Tmp1 = LegalizeOp(Node->getOperand(0));
3640
Chris Lattner1618beb2005-07-29 00:11:56 +00003641 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3642 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003643 case TargetLowering::Custom:
3644 isCustom = true;
3645 // FALLTHROUGH
3646 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003647 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003648 if (isCustom) {
3649 Tmp1 = TLI.LowerOperation(Result, DAG);
3650 if (Tmp1.Val) Result = Tmp1;
3651 }
3652 break;
3653 case TargetLowering::Promote:
3654 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3655 Node->getOpcode() == ISD::FP_TO_SINT);
3656 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003657 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003658 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3659 SDOperand True, False;
3660 MVT::ValueType VT = Node->getOperand(0).getValueType();
3661 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenf4d48322007-09-19 17:53:26 +00003662 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen73328d12007-09-19 23:55:34 +00003663 const uint64_t zero[] = {0, 0};
3664 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3665 uint64_t x = 1ULL << ShiftAmt;
Neil Boothccf596a2007-10-07 11:45:55 +00003666 (void)apf.convertFromZeroExtendedInteger
3667 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003668 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003669 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3670 Node->getOperand(0), Tmp2, ISD::SETLT);
3671 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3672 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003673 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003674 Tmp2));
3675 False = DAG.getNode(ISD::XOR, NVT, False,
3676 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003677 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3678 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003679 } else {
3680 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3681 }
3682 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003683 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003684 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003685 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003686 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003687 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003688 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003689 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003690 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3691 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3692 Node->getOperand(0), DAG.getValueType(MVT::f64));
3693 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3694 DAG.getIntPtrConstant(1));
3695 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3696 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003697 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3698 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3699 Tmp2 = DAG.getConstantFP(apf, OVT);
3700 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3701 // FIXME: generated code sucks.
3702 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3703 DAG.getNode(ISD::ADD, MVT::i32,
3704 DAG.getNode(ISD::FP_TO_SINT, VT,
3705 DAG.getNode(ISD::FSUB, OVT,
3706 Node->getOperand(0), Tmp2)),
3707 DAG.getConstant(0x80000000, MVT::i32)),
3708 DAG.getNode(ISD::FP_TO_SINT, VT,
3709 Node->getOperand(0)),
3710 DAG.getCondCode(ISD::SETGE));
3711 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003712 break;
3713 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003714 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003715 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003716 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003717 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003718 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003719 LC = (VT == MVT::i32)
3720 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003721 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003722 LC = (VT == MVT::i32)
3723 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003724 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003725 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003726 LC = RTLIB::FPTOSINT_F80_I64;
3727 }
3728 else if (OVT == MVT::ppcf128) {
3729 assert(VT == MVT::i64);
3730 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003731 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003732 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003733 }
3734 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003735 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003736 LC = (VT == MVT::i32)
3737 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003738 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003739 LC = (VT == MVT::i32)
3740 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003741 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003742 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003743 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3744 }
3745 else if (OVT == MVT::ppcf128) {
3746 assert(VT == MVT::i64);
3747 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003748 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003749 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003750 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003751 default: assert(0 && "Unreachable!");
3752 }
3753 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003754 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3755 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003756 break;
3757 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003758 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003759 Tmp1 = PromoteOp(Node->getOperand(0));
3760 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3761 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003762 break;
3763 }
3764 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003765
Chris Lattnerf2670a82008-01-16 06:57:07 +00003766 case ISD::FP_EXTEND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003767 MVT::ValueType DstVT = Op.getValueType();
3768 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3769 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3770 // The only other way we can lower this is to turn it into a STORE,
3771 // LOAD pair, targetting a temporary location (a stack slot).
3772 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3773 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003774 }
3775 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3776 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3777 case Legal:
3778 Tmp1 = LegalizeOp(Node->getOperand(0));
3779 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3780 break;
3781 case Promote:
3782 Tmp1 = PromoteOp(Node->getOperand(0));
3783 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3784 break;
3785 }
3786 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003787 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003788 case ISD::FP_ROUND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003789 MVT::ValueType DstVT = Op.getValueType();
3790 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3791 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3792 if (SrcVT == MVT::ppcf128) {
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003793 SDOperand Lo;
3794 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003795 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003796 if (DstVT!=MVT::f64)
3797 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003798 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003799 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003800 // The only other way we can lower this is to turn it into a STORE,
3801 // LOAD pair, targetting a temporary location (a stack slot).
3802 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3803 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003804 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003805 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3806 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3807 case Legal:
3808 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003809 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003810 break;
3811 case Promote:
3812 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003813 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3814 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003815 break;
3816 }
3817 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003818 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003819 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003820 case ISD::ZERO_EXTEND:
3821 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003822 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003823 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003824 case Legal:
3825 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003826 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003827 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003828 case Promote:
3829 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003830 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003831 Tmp1 = PromoteOp(Node->getOperand(0));
3832 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003833 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003834 case ISD::ZERO_EXTEND:
3835 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003836 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003837 Result = DAG.getZeroExtendInReg(Result,
3838 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003839 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003840 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003841 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003842 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003843 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003844 Result,
3845 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003846 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003847 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003848 }
3849 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003850 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003851 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003852 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003853 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003854
3855 // If this operation is not supported, convert it to a shl/shr or load/store
3856 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003857 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3858 default: assert(0 && "This action not supported for this op yet!");
3859 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003861 break;
3862 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003863 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003864 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003865 // NOTE: we could fall back on load/store here too for targets without
3866 // SAR. However, it is doubtful that any exist.
3867 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3868 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003869 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003870 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3871 Node->getOperand(0), ShiftCst);
3872 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3873 Result, ShiftCst);
3874 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003875 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003876 // EXTLOAD pair, targetting a temporary location (a stack slot).
3877
3878 // NOTE: there is a choice here between constantly creating new stack
3879 // slots and always reusing the same one. We currently always create
3880 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00003881 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3882 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00003883 } else {
3884 assert(0 && "Unknown op");
3885 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003886 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003887 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003888 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003889 }
Duncan Sands36397f52007-07-27 12:58:54 +00003890 case ISD::TRAMPOLINE: {
3891 SDOperand Ops[6];
3892 for (unsigned i = 0; i != 6; ++i)
3893 Ops[i] = LegalizeOp(Node->getOperand(i));
3894 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3895 // The only option for this node is to custom lower it.
3896 Result = TLI.LowerOperation(Result, DAG);
3897 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003898
3899 // Since trampoline produces two values, make sure to remember that we
3900 // legalized both of them.
3901 Tmp1 = LegalizeOp(Result.getValue(1));
3902 Result = LegalizeOp(Result);
3903 AddLegalizedOperand(SDOperand(Node, 0), Result);
3904 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3905 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003906 }
Dan Gohman1a024862008-01-31 00:41:03 +00003907 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003908 MVT::ValueType VT = Node->getValueType(0);
3909 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3910 default: assert(0 && "This action not supported for this op yet!");
3911 case TargetLowering::Custom:
3912 Result = TLI.LowerOperation(Op, DAG);
3913 if (Result.Val) break;
3914 // Fall Thru
3915 case TargetLowering::Legal:
3916 // If this operation is not supported, lower it to constant 1
3917 Result = DAG.getConstant(1, VT);
3918 break;
3919 }
3920 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003921 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003922 MVT::ValueType VT = Node->getValueType(0);
3923 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3924 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003925 case TargetLowering::Legal:
3926 Tmp1 = LegalizeOp(Node->getOperand(0));
3927 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3928 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003929 case TargetLowering::Custom:
3930 Result = TLI.LowerOperation(Op, DAG);
3931 if (Result.Val) break;
3932 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003933 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003934 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003935 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003936 TargetLowering::ArgListTy Args;
3937 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner41bab0b2008-01-15 21:58:08 +00003938 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00003939 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3940 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003941 Result = CallResult.second;
3942 break;
3943 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003944 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003945 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003946 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003947
Chris Lattner4ddd2832006-04-08 04:13:17 +00003948 assert(Result.getValueType() == Op.getValueType() &&
3949 "Bad legalization!");
3950
Chris Lattner456a93a2006-01-28 07:39:30 +00003951 // Make sure that the generated code is itself legal.
3952 if (Result != Op)
3953 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003954
Chris Lattner45982da2005-05-12 16:53:42 +00003955 // Note that LegalizeOp may be reentered even from single-use nodes, which
3956 // means that we always must cache transformed nodes.
3957 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003958 return Result;
3959}
3960
Chris Lattner8b6fa222005-01-15 22:16:26 +00003961/// PromoteOp - Given an operation that produces a value in an invalid type,
3962/// promote it to compute the value into a larger type. The produced value will
3963/// have the correct bits for the low portion of the register, but no guarantee
3964/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003965SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3966 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003967 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003968 assert(getTypeAction(VT) == Promote &&
3969 "Caller should expand or legalize operands that are not promotable!");
3970 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3971 "Cannot promote to smaller type!");
3972
Chris Lattner03c85462005-01-15 05:21:40 +00003973 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003974 SDOperand Result;
3975 SDNode *Node = Op.Val;
3976
Chris Lattner40030bf2007-02-04 01:17:38 +00003977 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003978 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003979
Chris Lattner03c85462005-01-15 05:21:40 +00003980 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003981 case ISD::CopyFromReg:
3982 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003983 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003984#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003985 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003986#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003987 assert(0 && "Do not know how to promote this operator!");
3988 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003989 case ISD::UNDEF:
3990 Result = DAG.getNode(ISD::UNDEF, NVT);
3991 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003992 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003993 if (VT != MVT::i1)
3994 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3995 else
3996 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003997 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3998 break;
3999 case ISD::ConstantFP:
4000 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4001 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4002 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004003
Chris Lattner82fbfb62005-01-18 02:59:52 +00004004 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004005 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004006 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4007 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004008 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004009
Chris Lattner03c85462005-01-15 05:21:40 +00004010 case ISD::TRUNCATE:
4011 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4012 case Legal:
4013 Result = LegalizeOp(Node->getOperand(0));
4014 assert(Result.getValueType() >= NVT &&
4015 "This truncation doesn't make sense!");
4016 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4017 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4018 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004019 case Promote:
4020 // The truncation is not required, because we don't guarantee anything
4021 // about high bits anyway.
4022 Result = PromoteOp(Node->getOperand(0));
4023 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004024 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004025 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4026 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004027 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004028 }
4029 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004030 case ISD::SIGN_EXTEND:
4031 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004032 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004033 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4034 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4035 case Legal:
4036 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004037 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004038 break;
4039 case Promote:
4040 // Promote the reg if it's smaller.
4041 Result = PromoteOp(Node->getOperand(0));
4042 // The high bits are not guaranteed to be anything. Insert an extend.
4043 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004044 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004045 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004046 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004047 Result = DAG.getZeroExtendInReg(Result,
4048 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004049 break;
4050 }
4051 break;
Chris Lattner35481892005-12-23 00:16:34 +00004052 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004053 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4054 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004055 Result = PromoteOp(Result);
4056 break;
4057
Chris Lattner8b6fa222005-01-15 22:16:26 +00004058 case ISD::FP_EXTEND:
4059 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4060 case ISD::FP_ROUND:
4061 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4062 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4063 case Promote: assert(0 && "Unreachable with 2 FP types!");
4064 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004065 if (Node->getConstantOperandVal(1) == 0) {
4066 // Input is legal? Do an FP_ROUND_INREG.
4067 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4068 DAG.getValueType(VT));
4069 } else {
4070 // Just remove the truncate, it isn't affecting the value.
4071 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4072 Node->getOperand(1));
4073 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004074 break;
4075 }
4076 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004077 case ISD::SINT_TO_FP:
4078 case ISD::UINT_TO_FP:
4079 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4080 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004081 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004082 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004083 break;
4084
4085 case Promote:
4086 Result = PromoteOp(Node->getOperand(0));
4087 if (Node->getOpcode() == ISD::SINT_TO_FP)
4088 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004089 Result,
4090 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004091 else
Chris Lattner23993562005-04-13 02:38:47 +00004092 Result = DAG.getZeroExtendInReg(Result,
4093 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004094 // No extra round required here.
4095 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004096 break;
4097 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004098 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4099 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004100 // Round if we cannot tolerate excess precision.
4101 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004102 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4103 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004104 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004105 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004106 break;
4107
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004108 case ISD::SIGN_EXTEND_INREG:
4109 Result = PromoteOp(Node->getOperand(0));
4110 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4111 Node->getOperand(1));
4112 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004113 case ISD::FP_TO_SINT:
4114 case ISD::FP_TO_UINT:
4115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4116 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004117 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004118 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004119 break;
4120 case Promote:
4121 // The input result is prerounded, so we don't have to do anything
4122 // special.
4123 Tmp1 = PromoteOp(Node->getOperand(0));
4124 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004125 }
Nate Begemand2558e32005-08-14 01:20:53 +00004126 // If we're promoting a UINT to a larger size, check to see if the new node
4127 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4128 // we can use that instead. This allows us to generate better code for
4129 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4130 // legal, such as PowerPC.
4131 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004132 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004133 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4134 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004135 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4136 } else {
4137 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4138 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004139 break;
4140
Chris Lattner2c8086f2005-04-02 05:00:07 +00004141 case ISD::FABS:
4142 case ISD::FNEG:
4143 Tmp1 = PromoteOp(Node->getOperand(0));
4144 assert(Tmp1.getValueType() == NVT);
4145 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4146 // NOTE: we do not have to do any extra rounding here for
4147 // NoExcessFPPrecision, because we know the input will have the appropriate
4148 // precision, and these operations don't modify precision at all.
4149 break;
4150
Chris Lattnerda6ba872005-04-28 21:44:33 +00004151 case ISD::FSQRT:
4152 case ISD::FSIN:
4153 case ISD::FCOS:
4154 Tmp1 = PromoteOp(Node->getOperand(0));
4155 assert(Tmp1.getValueType() == NVT);
4156 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004157 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004158 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4159 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004160 break;
4161
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004162 case ISD::FPOWI: {
4163 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4164 // directly as well, which may be better.
4165 Tmp1 = PromoteOp(Node->getOperand(0));
4166 assert(Tmp1.getValueType() == NVT);
4167 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4168 if (NoExcessFPPrecision)
4169 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4170 DAG.getValueType(VT));
4171 break;
4172 }
4173
Chris Lattner03c85462005-01-15 05:21:40 +00004174 case ISD::AND:
4175 case ISD::OR:
4176 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004177 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004178 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004179 case ISD::MUL:
4180 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004181 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004182 // that too is okay if they are integer operations.
4183 Tmp1 = PromoteOp(Node->getOperand(0));
4184 Tmp2 = PromoteOp(Node->getOperand(1));
4185 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4186 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004187 break;
4188 case ISD::FADD:
4189 case ISD::FSUB:
4190 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004191 Tmp1 = PromoteOp(Node->getOperand(0));
4192 Tmp2 = PromoteOp(Node->getOperand(1));
4193 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4194 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4195
4196 // Floating point operations will give excess precision that we may not be
4197 // able to tolerate. If we DO allow excess precision, just leave it,
4198 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004199 // FIXME: Why would we need to round FP ops more than integer ones?
4200 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004201 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004202 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4203 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004204 break;
4205
Chris Lattner8b6fa222005-01-15 22:16:26 +00004206 case ISD::SDIV:
4207 case ISD::SREM:
4208 // These operators require that their input be sign extended.
4209 Tmp1 = PromoteOp(Node->getOperand(0));
4210 Tmp2 = PromoteOp(Node->getOperand(1));
4211 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004212 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4213 DAG.getValueType(VT));
4214 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4215 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004216 }
4217 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4218
4219 // Perform FP_ROUND: this is probably overly pessimistic.
4220 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004221 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4222 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004223 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004224 case ISD::FDIV:
4225 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004226 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004227 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004228 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004229 case Expand: assert(0 && "not implemented");
4230 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4231 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004232 }
4233 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004234 case Expand: assert(0 && "not implemented");
4235 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4236 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004237 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004238 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4239
4240 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004241 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004242 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4243 DAG.getValueType(VT));
4244 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004245
4246 case ISD::UDIV:
4247 case ISD::UREM:
4248 // These operators require that their input be zero extended.
4249 Tmp1 = PromoteOp(Node->getOperand(0));
4250 Tmp2 = PromoteOp(Node->getOperand(1));
4251 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004252 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4253 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004254 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4255 break;
4256
4257 case ISD::SHL:
4258 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004259 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004260 break;
4261 case ISD::SRA:
4262 // The input value must be properly sign extended.
4263 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004264 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4265 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004266 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004267 break;
4268 case ISD::SRL:
4269 // The input value must be properly zero extended.
4270 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004271 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004272 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004273 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004274
4275 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004276 Tmp1 = Node->getOperand(0); // Get the chain.
4277 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004278 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4279 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4280 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4281 } else {
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004282 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4283 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004284 // Increment the pointer, VAList, to the next vaarg
4285 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4286 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4287 TLI.getPointerTy()));
4288 // Store the incremented VAList to the legalized pointer
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004289 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004290 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004291 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004292 }
4293 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004294 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004295 break;
4296
Evan Cheng466685d2006-10-09 20:57:25 +00004297 case ISD::LOAD: {
4298 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004299 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4300 ? ISD::EXTLOAD : LD->getExtensionType();
4301 Result = DAG.getExtLoad(ExtType, NVT,
4302 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004303 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004304 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004305 LD->isVolatile(),
4306 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004307 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004308 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004309 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004310 }
Chris Lattner03c85462005-01-15 05:21:40 +00004311 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004312 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4313 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004314 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004315 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004316 case ISD::SELECT_CC:
4317 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4318 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4319 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004320 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004321 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004322 case ISD::BSWAP:
4323 Tmp1 = Node->getOperand(0);
4324 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4325 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4326 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004327 DAG.getConstant(MVT::getSizeInBits(NVT) -
4328 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004329 TLI.getShiftAmountTy()));
4330 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004331 case ISD::CTPOP:
4332 case ISD::CTTZ:
4333 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004334 // Zero extend the argument
4335 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004336 // Perform the larger operation, then subtract if needed.
4337 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004338 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004339 case ISD::CTPOP:
4340 Result = Tmp1;
4341 break;
4342 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004343 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004344 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004345 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4346 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004347 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004348 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004349 break;
4350 case ISD::CTLZ:
4351 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004352 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004353 DAG.getConstant(MVT::getSizeInBits(NVT) -
4354 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004355 break;
4356 }
4357 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004358 case ISD::EXTRACT_SUBVECTOR:
4359 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004360 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004361 case ISD::EXTRACT_VECTOR_ELT:
4362 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4363 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004364 }
4365
4366 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004367
4368 // Make sure the result is itself legal.
4369 Result = LegalizeOp(Result);
4370
4371 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004372 AddPromotedOperand(Op, Result);
4373 return Result;
4374}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004375
Dan Gohman7f321562007-06-25 16:23:39 +00004376/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4377/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4378/// based on the vector type. The return type of this matches the element type
4379/// of the vector, which may not be legal for the target.
4380SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004381 // We know that operand #0 is the Vec vector. If the index is a constant
4382 // or if the invec is a supported hardware type, we can use it. Otherwise,
4383 // lower to a store then an indexed load.
4384 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004385 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004386
Dan Gohmane40c7b02007-09-24 15:54:53 +00004387 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004388 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004389
Dan Gohman7f321562007-06-25 16:23:39 +00004390 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4391 default: assert(0 && "This action is not supported yet!");
4392 case TargetLowering::Custom: {
4393 Vec = LegalizeOp(Vec);
4394 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4395 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4396 if (Tmp3.Val)
4397 return Tmp3;
4398 break;
4399 }
4400 case TargetLowering::Legal:
4401 if (isTypeLegal(TVT)) {
4402 Vec = LegalizeOp(Vec);
4403 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004404 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004405 }
4406 break;
4407 case TargetLowering::Expand:
4408 break;
4409 }
4410
4411 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004412 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004413 Op = ScalarizeVectorOp(Vec);
4414 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004415 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004416 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004417 SDOperand Lo, Hi;
4418 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman55030dc2008-01-29 02:24:00 +00004419 if (CIdx->getValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004420 Vec = Lo;
4421 } else {
4422 Vec = Hi;
Nate Begeman55030dc2008-01-29 02:24:00 +00004423 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004424 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004425 }
Dan Gohman7f321562007-06-25 16:23:39 +00004426
Chris Lattner15972212006-03-31 17:55:51 +00004427 // It's now an extract from the appropriate high or low part. Recurse.
4428 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004429 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004430 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004431 // Store the value to a temporary stack slot, then LOAD the scalar
4432 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004433 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004434 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4435
4436 // Add the offset to the index.
4437 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4438 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4439 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004440
4441 if (MVT::getSizeInBits(Idx.getValueType()) >
4442 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004443 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004444 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004445 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004446
Dan Gohman7f321562007-06-25 16:23:39 +00004447 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4448
4449 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004450 }
Dan Gohman7f321562007-06-25 16:23:39 +00004451 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004452}
4453
Dan Gohman7f321562007-06-25 16:23:39 +00004454/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004455/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004456SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004457 // We know that operand #0 is the Vec vector. For now we assume the index
4458 // is a constant and that the extracted result is a supported hardware type.
4459 SDOperand Vec = Op.getOperand(0);
4460 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4461
Dan Gohman7f321562007-06-25 16:23:39 +00004462 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004463
4464 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4465 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004466 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004467 }
4468
4469 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4470 SDOperand Lo, Hi;
4471 SplitVectorOp(Vec, Lo, Hi);
4472 if (CIdx->getValue() < NumElems/2) {
4473 Vec = Lo;
4474 } else {
4475 Vec = Hi;
4476 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4477 }
4478
4479 // It's now an extract from the appropriate high or low part. Recurse.
4480 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004481 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004482}
4483
Nate Begeman750ac1b2006-02-01 07:19:44 +00004484/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4485/// with condition CC on the current target. This usually involves legalizing
4486/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4487/// there may be no choice but to create a new SetCC node to represent the
4488/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4489/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4490void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4491 SDOperand &RHS,
4492 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004493 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004494
4495 switch (getTypeAction(LHS.getValueType())) {
4496 case Legal:
4497 Tmp1 = LegalizeOp(LHS); // LHS
4498 Tmp2 = LegalizeOp(RHS); // RHS
4499 break;
4500 case Promote:
4501 Tmp1 = PromoteOp(LHS); // LHS
4502 Tmp2 = PromoteOp(RHS); // RHS
4503
4504 // If this is an FP compare, the operands have already been extended.
4505 if (MVT::isInteger(LHS.getValueType())) {
4506 MVT::ValueType VT = LHS.getValueType();
4507 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4508
4509 // Otherwise, we have to insert explicit sign or zero extends. Note
4510 // that we could insert sign extends for ALL conditions, but zero extend
4511 // is cheaper on many machines (an AND instead of two shifts), so prefer
4512 // it.
4513 switch (cast<CondCodeSDNode>(CC)->get()) {
4514 default: assert(0 && "Unknown integer comparison!");
4515 case ISD::SETEQ:
4516 case ISD::SETNE:
4517 case ISD::SETUGE:
4518 case ISD::SETUGT:
4519 case ISD::SETULE:
4520 case ISD::SETULT:
4521 // ALL of these operations will work if we either sign or zero extend
4522 // the operands (including the unsigned comparisons!). Zero extend is
4523 // usually a simpler/cheaper operation, so prefer it.
4524 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4525 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4526 break;
4527 case ISD::SETGE:
4528 case ISD::SETGT:
4529 case ISD::SETLT:
4530 case ISD::SETLE:
4531 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4532 DAG.getValueType(VT));
4533 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4534 DAG.getValueType(VT));
4535 break;
4536 }
4537 }
4538 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004539 case Expand: {
4540 MVT::ValueType VT = LHS.getValueType();
4541 if (VT == MVT::f32 || VT == MVT::f64) {
4542 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004543 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004544 switch (cast<CondCodeSDNode>(CC)->get()) {
4545 case ISD::SETEQ:
4546 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004547 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004548 break;
4549 case ISD::SETNE:
4550 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004551 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004552 break;
4553 case ISD::SETGE:
4554 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004555 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004556 break;
4557 case ISD::SETLT:
4558 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004559 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004560 break;
4561 case ISD::SETLE:
4562 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004563 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004564 break;
4565 case ISD::SETGT:
4566 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004567 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004568 break;
4569 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004570 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4571 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004572 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004573 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004574 break;
4575 default:
Evan Cheng56966222007-01-12 02:11:51 +00004576 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004577 switch (cast<CondCodeSDNode>(CC)->get()) {
4578 case ISD::SETONE:
4579 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004580 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004581 // Fallthrough
4582 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004583 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004584 break;
4585 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004586 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004587 break;
4588 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004589 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004590 break;
4591 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004592 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004593 break;
Evan Cheng56966222007-01-12 02:11:51 +00004594 case ISD::SETUEQ:
4595 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004596 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004597 default: assert(0 && "Unsupported FP setcc!");
4598 }
4599 }
4600
4601 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004602 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004603 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004604 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004605 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004606 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004607 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004608 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004609 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004610 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004611 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004612 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004613 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004614 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4615 Tmp2 = SDOperand();
4616 }
4617 LHS = Tmp1;
4618 RHS = Tmp2;
4619 return;
4620 }
4621
Nate Begeman750ac1b2006-02-01 07:19:44 +00004622 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4623 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004624 ExpandOp(RHS, RHSLo, RHSHi);
4625 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4626
4627 if (VT==MVT::ppcf128) {
4628 // FIXME: This generated code sucks. We want to generate
4629 // FCMP crN, hi1, hi2
4630 // BNE crN, L:
4631 // FCMP crN, lo1, lo2
4632 // The following can be improved, but not that much.
4633 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4634 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4635 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4636 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4637 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4638 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4639 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4640 Tmp2 = SDOperand();
4641 break;
4642 }
4643
4644 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004645 case ISD::SETEQ:
4646 case ISD::SETNE:
4647 if (RHSLo == RHSHi)
4648 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4649 if (RHSCST->isAllOnesValue()) {
4650 // Comparison to -1.
4651 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4652 Tmp2 = RHSLo;
4653 break;
4654 }
4655
4656 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4657 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4658 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4659 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4660 break;
4661 default:
4662 // If this is a comparison of the sign bit, just look at the top part.
4663 // X > -1, x < 0
4664 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4665 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4666 CST->getValue() == 0) || // X < 0
4667 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4668 CST->isAllOnesValue())) { // X > -1
4669 Tmp1 = LHSHi;
4670 Tmp2 = RHSHi;
4671 break;
4672 }
4673
4674 // FIXME: This generated code sucks.
4675 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004676 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004677 default: assert(0 && "Unknown integer setcc!");
4678 case ISD::SETLT:
4679 case ISD::SETULT: LowCC = ISD::SETULT; break;
4680 case ISD::SETGT:
4681 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4682 case ISD::SETLE:
4683 case ISD::SETULE: LowCC = ISD::SETULE; break;
4684 case ISD::SETGE:
4685 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4686 }
4687
4688 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4689 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4690 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4691
4692 // NOTE: on targets without efficient SELECT of bools, we can always use
4693 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004694 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4695 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4696 false, DagCombineInfo);
4697 if (!Tmp1.Val)
4698 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4699 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4700 CCCode, false, DagCombineInfo);
4701 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004702 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004703
4704 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4705 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4706 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4707 (Tmp2C && Tmp2C->getValue() == 0 &&
4708 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4709 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4710 (Tmp2C && Tmp2C->getValue() == 1 &&
4711 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4712 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4713 // low part is known false, returns high part.
4714 // For LE / GE, if high part is known false, ignore the low part.
4715 // For LT / GT, if high part is known true, ignore the low part.
4716 Tmp1 = Tmp2;
4717 Tmp2 = SDOperand();
4718 } else {
4719 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4720 ISD::SETEQ, false, DagCombineInfo);
4721 if (!Result.Val)
4722 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4723 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4724 Result, Tmp1, Tmp2));
4725 Tmp1 = Result;
4726 Tmp2 = SDOperand();
4727 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004728 }
4729 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004730 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004731 LHS = Tmp1;
4732 RHS = Tmp2;
4733}
4734
Chris Lattner1401d152008-01-16 07:45:30 +00004735/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4736/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4737/// a load from the stack slot to DestVT, extending it if needed.
4738/// The resultant code need not be legal.
4739SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4740 MVT::ValueType SlotVT,
4741 MVT::ValueType DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004742 // Create the stack frame object.
Chris Lattner1401d152008-01-16 07:45:30 +00004743 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4744
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004745 FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(FIPtr);
4746 assert(StackPtrFI);
4747 int SPFI = StackPtrFI->getIndex();
4748
Chris Lattner1401d152008-01-16 07:45:30 +00004749 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4750 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4751 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004752
Chris Lattner1401d152008-01-16 07:45:30 +00004753 // Emit a store to the stack slot. Use a truncstore if the input value is
4754 // later than DestVT.
4755 SDOperand Store;
4756 if (SrcSize > SlotSize)
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004757 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
4758 &PseudoSourceValue::FPRel, SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004759 else {
4760 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004761 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
4762 &PseudoSourceValue::FPRel, SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004763 }
4764
Chris Lattner35481892005-12-23 00:16:34 +00004765 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004766 if (SlotSize == DestSize)
4767 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4768
4769 assert(SlotSize < DestSize && "Unknown extension!");
4770 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner35481892005-12-23 00:16:34 +00004771}
4772
Chris Lattner4352cc92006-04-04 17:23:26 +00004773SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4774 // Create a vector sized/aligned stack slot, store the value to element #0,
4775 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004776 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004777
4778 FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(StackPtr);
4779 assert(StackPtrFI);
4780 int SPFI = StackPtrFI->getIndex();
4781
Evan Cheng786225a2006-10-05 23:01:46 +00004782 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004783 &PseudoSourceValue::FPRel, SPFI);
4784 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
4785 &PseudoSourceValue::FPRel, SPFI);
Chris Lattner4352cc92006-04-04 17:23:26 +00004786}
4787
4788
Chris Lattnerce872152006-03-19 06:31:19 +00004789/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004790/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004791SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4792
4793 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004794 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004795 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004796 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004797 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004798 std::map<SDOperand, std::vector<unsigned> > Values;
4799 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004800 bool isConstant = true;
4801 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4802 SplatValue.getOpcode() != ISD::UNDEF)
4803 isConstant = false;
4804
Evan Cheng033e6812006-03-24 01:17:21 +00004805 for (unsigned i = 1; i < NumElems; ++i) {
4806 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004807 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004808 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004809 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004810 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004811 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004812
4813 // If this isn't a constant element or an undef, we can't use a constant
4814 // pool load.
4815 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4816 V.getOpcode() != ISD::UNDEF)
4817 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004818 }
4819
4820 if (isOnlyLowElement) {
4821 // If the low element is an undef too, then this whole things is an undef.
4822 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4823 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4824 // Otherwise, turn this into a scalar_to_vector node.
4825 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4826 Node->getOperand(0));
4827 }
4828
Chris Lattner2eb86532006-03-24 07:29:17 +00004829 // If all elements are constants, create a load from the constant pool.
4830 if (isConstant) {
4831 MVT::ValueType VT = Node->getValueType(0);
4832 const Type *OpNTy =
4833 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4834 std::vector<Constant*> CV;
4835 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4836 if (ConstantFPSDNode *V =
4837 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004838 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004839 } else if (ConstantSDNode *V =
4840 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004841 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004842 } else {
4843 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4844 CV.push_back(UndefValue::get(OpNTy));
4845 }
4846 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004847 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004848 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohmanc6c391d2008-01-31 00:25:39 +00004849 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4850 &PseudoSourceValue::CPRel, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004851 }
4852
Chris Lattner87100e02006-03-20 01:52:29 +00004853 if (SplatValue.Val) { // Splat of one value?
4854 // Build the shuffle constant vector: <0, 0, 0, 0>
4855 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004856 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004857 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004858 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004859 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4860 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004861
4862 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004863 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004864 // Get the splatted value into the low element of a vector register.
4865 SDOperand LowValVec =
4866 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4867
4868 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4869 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4870 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4871 SplatMask);
4872 }
4873 }
4874
Evan Cheng033e6812006-03-24 01:17:21 +00004875 // If there are only two unique elements, we may be able to turn this into a
4876 // vector shuffle.
4877 if (Values.size() == 2) {
4878 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4879 MVT::ValueType MaskVT =
4880 MVT::getIntVectorWithNumElements(NumElems);
4881 std::vector<SDOperand> MaskVec(NumElems);
4882 unsigned i = 0;
4883 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4884 E = Values.end(); I != E; ++I) {
4885 for (std::vector<unsigned>::iterator II = I->second.begin(),
4886 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004887 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004888 i += NumElems;
4889 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004890 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4891 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004892
4893 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004894 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4895 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004896 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004897 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4898 E = Values.end(); I != E; ++I) {
4899 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4900 I->first);
4901 Ops.push_back(Op);
4902 }
4903 Ops.push_back(ShuffleMask);
4904
4905 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004906 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4907 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004908 }
4909 }
Chris Lattnerce872152006-03-19 06:31:19 +00004910
4911 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4912 // aligned object on the stack, store each element into it, then load
4913 // the result as a vector.
4914 MVT::ValueType VT = Node->getValueType(0);
4915 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004916 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004917
4918 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004919 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004920 unsigned TypeByteSize =
4921 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004922 // Store (in the right endianness) the elements to memory.
4923 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4924 // Ignore undef elements.
4925 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4926
Chris Lattner841c8822006-03-22 01:46:54 +00004927 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004928
4929 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4930 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4931
Evan Cheng786225a2006-10-05 23:01:46 +00004932 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004933 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004934 }
4935
4936 SDOperand StoreChain;
4937 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004938 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4939 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004940 else
4941 StoreChain = DAG.getEntryNode();
4942
4943 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004944 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004945}
4946
Chris Lattner5b359c62005-04-02 04:00:59 +00004947void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4948 SDOperand Op, SDOperand Amt,
4949 SDOperand &Lo, SDOperand &Hi) {
4950 // Expand the subcomponents.
4951 SDOperand LHSL, LHSH;
4952 ExpandOp(Op, LHSL, LHSH);
4953
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004954 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004955 MVT::ValueType VT = LHSL.getValueType();
4956 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004957 Hi = Lo.getValue(1);
4958}
4959
4960
Chris Lattnere34b3962005-01-19 04:19:40 +00004961/// ExpandShift - Try to find a clever way to expand this shift operation out to
4962/// smaller elements. If we can't find a way that is more efficient than a
4963/// libcall on this target, return false. Otherwise, return true with the
4964/// low-parts expanded into Lo and Hi.
4965bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4966 SDOperand &Lo, SDOperand &Hi) {
4967 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4968 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004969
Chris Lattnere34b3962005-01-19 04:19:40 +00004970 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004971 SDOperand ShAmt = LegalizeOp(Amt);
4972 MVT::ValueType ShTy = ShAmt.getValueType();
4973 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4974 unsigned NVTBits = MVT::getSizeInBits(NVT);
4975
Chris Lattner7a3c8552007-10-14 20:35:12 +00004976 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004977 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4978 unsigned Cst = CN->getValue();
4979 // Expand the incoming operand to be shifted, so that we have its parts
4980 SDOperand InL, InH;
4981 ExpandOp(Op, InL, InH);
4982 switch(Opc) {
4983 case ISD::SHL:
4984 if (Cst > VTBits) {
4985 Lo = DAG.getConstant(0, NVT);
4986 Hi = DAG.getConstant(0, NVT);
4987 } else if (Cst > NVTBits) {
4988 Lo = DAG.getConstant(0, NVT);
4989 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004990 } else if (Cst == NVTBits) {
4991 Lo = DAG.getConstant(0, NVT);
4992 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004993 } else {
4994 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4995 Hi = DAG.getNode(ISD::OR, NVT,
4996 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4997 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4998 }
4999 return true;
5000 case ISD::SRL:
5001 if (Cst > VTBits) {
5002 Lo = DAG.getConstant(0, NVT);
5003 Hi = DAG.getConstant(0, NVT);
5004 } else if (Cst > NVTBits) {
5005 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5006 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005007 } else if (Cst == NVTBits) {
5008 Lo = InH;
5009 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005010 } else {
5011 Lo = DAG.getNode(ISD::OR, NVT,
5012 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5013 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5014 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5015 }
5016 return true;
5017 case ISD::SRA:
5018 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005019 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005020 DAG.getConstant(NVTBits-1, ShTy));
5021 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005022 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005023 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005024 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005025 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005026 } else if (Cst == NVTBits) {
5027 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005028 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005029 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005030 } else {
5031 Lo = DAG.getNode(ISD::OR, NVT,
5032 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5033 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5034 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5035 }
5036 return true;
5037 }
5038 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005039
5040 // Okay, the shift amount isn't constant. However, if we can tell that it is
5041 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
5042 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005043 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005044
5045 // If we know that the high bit of the shift amount is one, then we can do
5046 // this as a couple of simple shifts.
5047 if (KnownOne & Mask) {
5048 // Mask out the high bit, which we know is set.
5049 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
5050 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5051
5052 // Expand the incoming operand to be shifted, so that we have its parts
5053 SDOperand InL, InH;
5054 ExpandOp(Op, InL, InH);
5055 switch(Opc) {
5056 case ISD::SHL:
5057 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5058 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5059 return true;
5060 case ISD::SRL:
5061 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5062 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5063 return true;
5064 case ISD::SRA:
5065 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5066 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5067 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5068 return true;
5069 }
5070 }
5071
5072 // If we know that the high bit of the shift amount is zero, then we can do
5073 // this as a couple of simple shifts.
5074 if (KnownZero & Mask) {
5075 // Compute 32-amt.
5076 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5077 DAG.getConstant(NVTBits, Amt.getValueType()),
5078 Amt);
5079
5080 // Expand the incoming operand to be shifted, so that we have its parts
5081 SDOperand InL, InH;
5082 ExpandOp(Op, InL, InH);
5083 switch(Opc) {
5084 case ISD::SHL:
5085 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5086 Hi = DAG.getNode(ISD::OR, NVT,
5087 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5088 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5089 return true;
5090 case ISD::SRL:
5091 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5092 Lo = DAG.getNode(ISD::OR, NVT,
5093 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5094 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5095 return true;
5096 case ISD::SRA:
5097 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5098 Lo = DAG.getNode(ISD::OR, NVT,
5099 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5100 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5101 return true;
5102 }
5103 }
5104
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005105 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005106}
Chris Lattner77e77a62005-01-21 06:05:23 +00005107
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005108
Chris Lattner77e77a62005-01-21 06:05:23 +00005109// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5110// does not fit into a register, return the lo part and set the hi part to the
5111// by-reg argument. If it does fit into a single register, return the result
5112// and leave the Hi part unset.
5113SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00005114 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005115 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5116 // The input chain to this libcall is the entry node of the function.
5117 // Legalizing the call will automatically add the previous call to the
5118 // dependence.
5119 SDOperand InChain = DAG.getEntryNode();
5120
Chris Lattner77e77a62005-01-21 06:05:23 +00005121 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005122 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005123 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5124 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5125 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00005126 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005127 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005128 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005129 }
5130 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005131
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005132 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00005133 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005134 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00005135 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00005136 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005137
Chris Lattner6831a812006-02-13 09:18:02 +00005138 // Legalize the call sequence, starting with the chain. This will advance
5139 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5140 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5141 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00005142 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005143 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005144 default: assert(0 && "Unknown thing");
5145 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005146 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005147 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005148 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005149 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005150 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005151 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005152 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005153}
5154
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005155
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005156/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5157///
Chris Lattner77e77a62005-01-21 06:05:23 +00005158SDOperand SelectionDAGLegalize::
5159ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005160 assert(getTypeAction(Source.getValueType()) == Expand &&
5161 "This is not an expansion!");
5162 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
5163
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005164 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00005165 assert(Source.getValueType() == MVT::i64 &&
5166 "This only works for 64-bit -> FP");
5167 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
5168 // incoming integer is set. To handle this, we dynamically test to see if
5169 // it is set, and, if so, add a fudge factor.
5170 SDOperand Lo, Hi;
5171 ExpandOp(Source, Lo, Hi);
5172
Chris Lattner66de05b2005-05-13 04:45:13 +00005173 // If this is unsigned, and not supported, first perform the conversion to
5174 // signed, then adjust the result if the sign bit is set.
5175 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
5176 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
5177
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005178 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5179 DAG.getConstant(0, Hi.getValueType()),
5180 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005181 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005182 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5183 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005184 uint64_t FF = 0x5f800000ULL;
5185 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005186 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005187
Chris Lattner5839bf22005-08-26 17:15:30 +00005188 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005189 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5190 SDOperand FudgeInReg;
5191 if (DestTy == MVT::f32)
Dan Gohmanc6c391d2008-01-31 00:25:39 +00005192 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
5193 &PseudoSourceValue::CPRel, 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005194 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005195 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005196 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohmanc6c391d2008-01-31 00:25:39 +00005197 CPIdx, &PseudoSourceValue::CPRel, 0,
5198 MVT::f32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005199 else
5200 assert(0 && "Unexpected conversion");
5201
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005202 MVT::ValueType SCVT = SignedConv.getValueType();
5203 if (SCVT != DestTy) {
5204 // Destination type needs to be expanded as well. The FADD now we are
5205 // constructing will be expanded into a libcall.
5206 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5207 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5208 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5209 SignedConv, SignedConv.getValue(1));
5210 }
5211 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5212 }
Chris Lattner473a9902005-09-29 06:44:39 +00005213 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005214 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005215
Chris Lattnera88a2602005-05-14 05:33:54 +00005216 // Check to see if the target has a custom way to lower this. If so, use it.
5217 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5218 default: assert(0 && "This action not implemented for this operation!");
5219 case TargetLowering::Legal:
5220 case TargetLowering::Expand:
5221 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005222 case TargetLowering::Custom: {
5223 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5224 Source), DAG);
5225 if (NV.Val)
5226 return LegalizeOp(NV);
5227 break; // The target decided this was legal after all
5228 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005229 }
5230
Chris Lattner13689e22005-05-12 07:00:44 +00005231 // Expand the source, then glue it back together for the call. We must expand
5232 // the source in case it is shared (this pass of legalize must traverse it).
5233 SDOperand SrcLo, SrcHi;
5234 ExpandOp(Source, SrcLo, SrcHi);
5235 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5236
Evan Cheng56966222007-01-12 02:11:51 +00005237 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005238 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005239 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005240 else {
5241 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00005242 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005243 }
Chris Lattner6831a812006-02-13 09:18:02 +00005244
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005245 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005246 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5247 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00005248 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5249 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00005250}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005251
Chris Lattner22cde6a2006-01-28 08:25:58 +00005252/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5253/// INT_TO_FP operation of the specified operand when the target requests that
5254/// we expand it. At this point, we know that the result and operand types are
5255/// legal for the target.
5256SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5257 SDOperand Op0,
5258 MVT::ValueType DestVT) {
5259 if (Op0.getValueType() == MVT::i32) {
5260 // simple 32-bit [signed|unsigned] integer to float/double expansion
5261
Chris Lattner23594d42008-01-16 07:03:22 +00005262 // Get the stack frame index of a 8 byte buffer.
5263 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5264
Chris Lattner22cde6a2006-01-28 08:25:58 +00005265 // word offset constant for Hi/Lo address computation
5266 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5267 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005268 SDOperand Hi = StackSlot;
5269 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5270 if (TLI.isLittleEndian())
5271 std::swap(Hi, Lo);
5272
Chris Lattner22cde6a2006-01-28 08:25:58 +00005273 // if signed map to unsigned space
5274 SDOperand Op0Mapped;
5275 if (isSigned) {
5276 // constant used to invert sign bit (signed to unsigned mapping)
5277 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5278 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5279 } else {
5280 Op0Mapped = Op0;
5281 }
5282 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005283 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005284 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005285 // initial hi portion of constructed double
5286 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5287 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005288 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005289 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005290 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005291 // FP constant to bias correct the final result
5292 SDOperand Bias = DAG.getConstantFP(isSigned ?
5293 BitsToDouble(0x4330000080000000ULL)
5294 : BitsToDouble(0x4330000000000000ULL),
5295 MVT::f64);
5296 // subtract the bias
5297 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5298 // final result
5299 SDOperand Result;
5300 // handle final rounding
5301 if (DestVT == MVT::f64) {
5302 // do nothing
5303 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005304 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005305 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5306 DAG.getIntPtrConstant(0));
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005307 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5308 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005309 }
5310 return Result;
5311 }
5312 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5313 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5314
5315 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5316 DAG.getConstant(0, Op0.getValueType()),
5317 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005318 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005319 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5320 SignSet, Four, Zero);
5321
5322 // If the sign bit of the integer is set, the large number will be treated
5323 // as a negative number. To counteract this, the dynamic code adds an
5324 // offset depending on the data type.
5325 uint64_t FF;
5326 switch (Op0.getValueType()) {
5327 default: assert(0 && "Unsupported integer type!");
5328 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5329 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5330 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5331 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5332 }
5333 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005334 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005335
5336 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5337 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5338 SDOperand FudgeInReg;
5339 if (DestVT == MVT::f32)
Dan Gohmanc6c391d2008-01-31 00:25:39 +00005340 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
5341 &PseudoSourceValue::CPRel, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005342 else {
Dale Johannesen73328d12007-09-19 23:55:34 +00005343 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005344 DAG.getEntryNode(), CPIdx,
Dan Gohmanc6c391d2008-01-31 00:25:39 +00005345 &PseudoSourceValue::CPRel, 0,
5346 MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005347 }
5348
5349 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5350}
5351
5352/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5353/// *INT_TO_FP operation of the specified operand when the target requests that
5354/// we promote it. At this point, we know that the result and operand types are
5355/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5356/// operation that takes a larger input.
5357SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5358 MVT::ValueType DestVT,
5359 bool isSigned) {
5360 // First step, figure out the appropriate *INT_TO_FP operation to use.
5361 MVT::ValueType NewInTy = LegalOp.getValueType();
5362
5363 unsigned OpToUse = 0;
5364
5365 // Scan for the appropriate larger type to use.
5366 while (1) {
5367 NewInTy = (MVT::ValueType)(NewInTy+1);
5368 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5369
5370 // If the target supports SINT_TO_FP of this type, use it.
5371 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5372 default: break;
5373 case TargetLowering::Legal:
5374 if (!TLI.isTypeLegal(NewInTy))
5375 break; // Can't use this datatype.
5376 // FALL THROUGH.
5377 case TargetLowering::Custom:
5378 OpToUse = ISD::SINT_TO_FP;
5379 break;
5380 }
5381 if (OpToUse) break;
5382 if (isSigned) continue;
5383
5384 // If the target supports UINT_TO_FP of this type, use it.
5385 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5386 default: break;
5387 case TargetLowering::Legal:
5388 if (!TLI.isTypeLegal(NewInTy))
5389 break; // Can't use this datatype.
5390 // FALL THROUGH.
5391 case TargetLowering::Custom:
5392 OpToUse = ISD::UINT_TO_FP;
5393 break;
5394 }
5395 if (OpToUse) break;
5396
5397 // Otherwise, try a larger type.
5398 }
5399
5400 // Okay, we found the operation and type to use. Zero extend our input to the
5401 // desired type then run the operation on it.
5402 return DAG.getNode(OpToUse, DestVT,
5403 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5404 NewInTy, LegalOp));
5405}
5406
5407/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5408/// FP_TO_*INT operation of the specified operand when the target requests that
5409/// we promote it. At this point, we know that the result and operand types are
5410/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5411/// operation that returns a larger result.
5412SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5413 MVT::ValueType DestVT,
5414 bool isSigned) {
5415 // First step, figure out the appropriate FP_TO*INT operation to use.
5416 MVT::ValueType NewOutTy = DestVT;
5417
5418 unsigned OpToUse = 0;
5419
5420 // Scan for the appropriate larger type to use.
5421 while (1) {
5422 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5423 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5424
5425 // If the target supports FP_TO_SINT returning this type, use it.
5426 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5427 default: break;
5428 case TargetLowering::Legal:
5429 if (!TLI.isTypeLegal(NewOutTy))
5430 break; // Can't use this datatype.
5431 // FALL THROUGH.
5432 case TargetLowering::Custom:
5433 OpToUse = ISD::FP_TO_SINT;
5434 break;
5435 }
5436 if (OpToUse) break;
5437
5438 // If the target supports FP_TO_UINT of this type, use it.
5439 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5440 default: break;
5441 case TargetLowering::Legal:
5442 if (!TLI.isTypeLegal(NewOutTy))
5443 break; // Can't use this datatype.
5444 // FALL THROUGH.
5445 case TargetLowering::Custom:
5446 OpToUse = ISD::FP_TO_UINT;
5447 break;
5448 }
5449 if (OpToUse) break;
5450
5451 // Otherwise, try a larger type.
5452 }
5453
Chris Lattner27a6c732007-11-24 07:07:01 +00005454
5455 // Okay, we found the operation and type to use.
5456 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5457
5458 // If the operation produces an invalid type, it must be custom lowered. Use
5459 // the target lowering hooks to expand it. Just keep the low part of the
5460 // expanded operation, we know that we're truncating anyway.
5461 if (getTypeAction(NewOutTy) == Expand) {
5462 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5463 assert(Operation.Val && "Didn't return anything");
5464 }
5465
5466 // Truncate the result of the extended FP_TO_*INT operation to the desired
5467 // size.
5468 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005469}
5470
5471/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5472///
5473SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5474 MVT::ValueType VT = Op.getValueType();
5475 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5476 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5477 switch (VT) {
5478 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5479 case MVT::i16:
5480 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5481 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5482 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5483 case MVT::i32:
5484 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5485 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5486 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5487 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5488 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5489 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5490 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5491 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5492 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5493 case MVT::i64:
5494 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5495 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5496 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5497 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5498 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5499 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5500 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5501 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5502 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5503 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5504 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5505 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5506 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5507 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5508 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5509 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5510 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5511 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5512 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5513 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5514 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5515 }
5516}
5517
5518/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5519///
5520SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5521 switch (Opc) {
5522 default: assert(0 && "Cannot expand this yet!");
5523 case ISD::CTPOP: {
5524 static const uint64_t mask[6] = {
5525 0x5555555555555555ULL, 0x3333333333333333ULL,
5526 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5527 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5528 };
5529 MVT::ValueType VT = Op.getValueType();
5530 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005531 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005532 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5533 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5534 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5535 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5536 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5537 DAG.getNode(ISD::AND, VT,
5538 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5539 }
5540 return Op;
5541 }
5542 case ISD::CTLZ: {
5543 // for now, we do this:
5544 // x = x | (x >> 1);
5545 // x = x | (x >> 2);
5546 // ...
5547 // x = x | (x >>16);
5548 // x = x | (x >>32); // for 64-bit input
5549 // return popcount(~x);
5550 //
5551 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5552 MVT::ValueType VT = Op.getValueType();
5553 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005554 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005555 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5556 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5557 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5558 }
5559 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5560 return DAG.getNode(ISD::CTPOP, VT, Op);
5561 }
5562 case ISD::CTTZ: {
5563 // for now, we use: { return popcount(~x & (x - 1)); }
5564 // unless the target has ctlz but not ctpop, in which case we use:
5565 // { return 32 - nlz(~x & (x-1)); }
5566 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5567 MVT::ValueType VT = Op.getValueType();
5568 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5569 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5570 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5571 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5572 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5573 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5574 TLI.isOperationLegal(ISD::CTLZ, VT))
5575 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005576 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005577 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5578 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5579 }
5580 }
5581}
Chris Lattnere34b3962005-01-19 04:19:40 +00005582
Chris Lattner3e928bb2005-01-07 07:47:09 +00005583/// ExpandOp - Expand the specified SDOperand into its two component pieces
5584/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5585/// LegalizeNodes map is filled in for any results that are not expanded, the
5586/// ExpandedNodes map is filled in for any results that are expanded, and the
5587/// Lo/Hi values are returned.
5588void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5589 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005590 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005591 SDNode *Node = Op.Val;
5592 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005593 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005594 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005595 "Cannot expand to FP value or to larger int value!");
5596
Chris Lattner6fdcb252005-09-02 20:32:45 +00005597 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005598 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005599 = ExpandedNodes.find(Op);
5600 if (I != ExpandedNodes.end()) {
5601 Lo = I->second.first;
5602 Hi = I->second.second;
5603 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005604 }
5605
Chris Lattner3e928bb2005-01-07 07:47:09 +00005606 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005607 case ISD::CopyFromReg:
5608 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005609 case ISD::FP_ROUND_INREG:
5610 if (VT == MVT::ppcf128 &&
5611 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5612 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005613 SDOperand SrcLo, SrcHi, Src;
5614 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5615 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5616 SDOperand Result = TLI.LowerOperation(
5617 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005618 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5619 Lo = Result.Val->getOperand(0);
5620 Hi = Result.Val->getOperand(1);
5621 break;
5622 }
5623 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005624 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005625#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005626 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005627#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005628 assert(0 && "Do not know how to expand this operator!");
5629 abort();
Dale Johannesen25f1d082007-10-31 00:32:36 +00005630 case ISD::EXTRACT_VECTOR_ELT:
5631 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5632 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5633 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5634 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005635 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005636 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005637 Lo = DAG.getNode(ISD::UNDEF, NVT);
5638 Hi = DAG.getNode(ISD::UNDEF, NVT);
5639 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005640 case ISD::Constant: {
5641 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5642 Lo = DAG.getConstant(Cst, NVT);
5643 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5644 break;
5645 }
Evan Cheng00495212006-12-12 21:32:44 +00005646 case ISD::ConstantFP: {
5647 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005648 if (CFP->getValueType(0) == MVT::ppcf128) {
5649 APInt api = CFP->getValueAPF().convertToAPInt();
5650 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5651 MVT::f64);
5652 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5653 MVT::f64);
5654 break;
5655 }
Evan Cheng279101e2006-12-12 22:19:28 +00005656 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005657 if (getTypeAction(Lo.getValueType()) == Expand)
5658 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005659 break;
5660 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005661 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005662 // Return the operands.
5663 Lo = Node->getOperand(0);
5664 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005665 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005666
5667 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005668 if (Node->getNumValues() == 1) {
5669 ExpandOp(Op.getOperand(0), Lo, Hi);
5670 break;
5671 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005672 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5673 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5674 Op.getValue(1).getValueType() == MVT::Other &&
5675 "unhandled MERGE_VALUES");
5676 ExpandOp(Op.getOperand(0), Lo, Hi);
5677 // Remember that we legalized the chain.
5678 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5679 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005680
5681 case ISD::SIGN_EXTEND_INREG:
5682 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005683 // sext_inreg the low part if needed.
5684 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5685
5686 // The high part gets the sign extension from the lo-part. This handles
5687 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005688 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5689 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5690 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005691 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005692
Nate Begemand88fc032006-01-14 03:14:10 +00005693 case ISD::BSWAP: {
5694 ExpandOp(Node->getOperand(0), Lo, Hi);
5695 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5696 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5697 Lo = TempLo;
5698 break;
5699 }
5700
Chris Lattneredb1add2005-05-11 04:51:16 +00005701 case ISD::CTPOP:
5702 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005703 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5704 DAG.getNode(ISD::CTPOP, NVT, Lo),
5705 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005706 Hi = DAG.getConstant(0, NVT);
5707 break;
5708
Chris Lattner39a8f332005-05-12 19:05:01 +00005709 case ISD::CTLZ: {
5710 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005711 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005712 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5713 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005714 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5715 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005716 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5717 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5718
5719 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5720 Hi = DAG.getConstant(0, NVT);
5721 break;
5722 }
5723
5724 case ISD::CTTZ: {
5725 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005726 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005727 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5728 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005729 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5730 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005731 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5732 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5733
5734 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5735 Hi = DAG.getConstant(0, NVT);
5736 break;
5737 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005738
Nate Begemanacc398c2006-01-25 18:21:52 +00005739 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005740 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5741 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005742 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5743 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5744
5745 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005746 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005747 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5748 if (!TLI.isLittleEndian())
5749 std::swap(Lo, Hi);
5750 break;
5751 }
5752
Chris Lattner3e928bb2005-01-07 07:47:09 +00005753 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005754 LoadSDNode *LD = cast<LoadSDNode>(Node);
5755 SDOperand Ch = LD->getChain(); // Legalize the chain.
5756 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5757 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005758 int SVOffset = LD->getSrcValueOffset();
5759 unsigned Alignment = LD->getAlignment();
5760 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005761
Evan Cheng466685d2006-10-09 20:57:25 +00005762 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005763 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5764 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005765 if (VT == MVT::f32 || VT == MVT::f64) {
5766 // f32->i32 or f64->i64 one to one expansion.
5767 // Remember that we legalized the chain.
5768 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005769 // Recursively expand the new load.
5770 if (getTypeAction(NVT) == Expand)
5771 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005772 break;
5773 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005774
Evan Cheng466685d2006-10-09 20:57:25 +00005775 // Increment the pointer to the other half.
5776 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5777 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00005778 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005779 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005780 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005781 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5782 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005783
Evan Cheng466685d2006-10-09 20:57:25 +00005784 // Build a factor node to remember that this load is independent of the
5785 // other one.
5786 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5787 Hi.getValue(1));
5788
5789 // Remember that we legalized the chain.
5790 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5791 if (!TLI.isLittleEndian())
5792 std::swap(Lo, Hi);
5793 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005794 MVT::ValueType EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005795
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005796 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5797 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005798 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5799 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005800 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005801 // Remember that we legalized the chain.
5802 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5803 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5804 break;
5805 }
Evan Cheng466685d2006-10-09 20:57:25 +00005806
5807 if (EVT == NVT)
5808 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005809 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005810 else
5811 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005812 SVOffset, EVT, isVolatile,
5813 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005814
5815 // Remember that we legalized the chain.
5816 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5817
5818 if (ExtType == ISD::SEXTLOAD) {
5819 // The high part is obtained by SRA'ing all but one of the bits of the
5820 // lo part.
5821 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5822 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5823 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5824 } else if (ExtType == ISD::ZEXTLOAD) {
5825 // The high part is just a zero.
5826 Hi = DAG.getConstant(0, NVT);
5827 } else /* if (ExtType == ISD::EXTLOAD) */ {
5828 // The high part is undefined.
5829 Hi = DAG.getNode(ISD::UNDEF, NVT);
5830 }
5831 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005832 break;
5833 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005834 case ISD::AND:
5835 case ISD::OR:
5836 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5837 SDOperand LL, LH, RL, RH;
5838 ExpandOp(Node->getOperand(0), LL, LH);
5839 ExpandOp(Node->getOperand(1), RL, RH);
5840 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5841 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5842 break;
5843 }
5844 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005845 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005846 ExpandOp(Node->getOperand(1), LL, LH);
5847 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005848 if (getTypeAction(NVT) == Expand)
5849 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005850 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005851 if (VT != MVT::f32)
5852 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005853 break;
5854 }
Nate Begeman9373a812005-08-10 20:51:12 +00005855 case ISD::SELECT_CC: {
5856 SDOperand TL, TH, FL, FH;
5857 ExpandOp(Node->getOperand(2), TL, TH);
5858 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005859 if (getTypeAction(NVT) == Expand)
5860 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005861 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5862 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005863 if (VT != MVT::f32)
5864 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5865 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005866 break;
5867 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005868 case ISD::ANY_EXTEND:
5869 // The low part is any extension of the input (which degenerates to a copy).
5870 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5871 // The high part is undefined.
5872 Hi = DAG.getNode(ISD::UNDEF, NVT);
5873 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005874 case ISD::SIGN_EXTEND: {
5875 // The low part is just a sign extension of the input (which degenerates to
5876 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005877 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005878
Chris Lattner3e928bb2005-01-07 07:47:09 +00005879 // The high part is obtained by SRA'ing all but one of the bits of the lo
5880 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005881 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005882 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5883 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005884 break;
5885 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005886 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005887 // The low part is just a zero extension of the input (which degenerates to
5888 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005889 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005890
Chris Lattner3e928bb2005-01-07 07:47:09 +00005891 // The high part is just a zero.
5892 Hi = DAG.getConstant(0, NVT);
5893 break;
Chris Lattner35481892005-12-23 00:16:34 +00005894
Chris Lattner4c948eb2007-02-13 23:55:16 +00005895 case ISD::TRUNCATE: {
5896 // The input value must be larger than this value. Expand *it*.
5897 SDOperand NewLo;
5898 ExpandOp(Node->getOperand(0), NewLo, Hi);
5899
5900 // The low part is now either the right size, or it is closer. If not the
5901 // right size, make an illegal truncate so we recursively expand it.
5902 if (NewLo.getValueType() != Node->getValueType(0))
5903 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5904 ExpandOp(NewLo, Lo, Hi);
5905 break;
5906 }
5907
Chris Lattner35481892005-12-23 00:16:34 +00005908 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005909 SDOperand Tmp;
5910 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5911 // If the target wants to, allow it to lower this itself.
5912 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5913 case Expand: assert(0 && "cannot expand FP!");
5914 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5915 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5916 }
5917 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5918 }
5919
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005920 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005921 if (VT == MVT::f32 || VT == MVT::f64) {
5922 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005923 if (getTypeAction(NVT) == Expand)
5924 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005925 break;
5926 }
5927
5928 // If source operand will be expanded to the same type as VT, i.e.
5929 // i64 <- f64, i32 <- f32, expand the source operand instead.
5930 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5931 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5932 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005933 break;
5934 }
5935
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005936 // Turn this into a load/store pair by default.
5937 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00005938 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005939
Chris Lattner35481892005-12-23 00:16:34 +00005940 ExpandOp(Tmp, Lo, Hi);
5941 break;
5942 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005943
Chris Lattner27a6c732007-11-24 07:07:01 +00005944 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00005945 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5946 TargetLowering::Custom &&
5947 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00005948 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5949 assert(Tmp.Val && "Node must be custom expanded!");
5950 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00005951 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00005952 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005953 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005954 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005955
Chris Lattner4e6c7462005-01-08 19:27:05 +00005956 // These operators cannot be expanded directly, emit them as calls to
5957 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005958 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005959 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005960 SDOperand Op;
5961 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5962 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005963 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5964 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005965 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005966
Chris Lattnerf20d1832005-07-30 01:40:57 +00005967 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5968
Chris Lattner80a3e942005-07-29 00:33:32 +00005969 // Now that the custom expander is done, expand the result, which is still
5970 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005971 if (Op.Val) {
5972 ExpandOp(Op, Lo, Hi);
5973 break;
5974 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005975 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005976
Dale Johannesen161e8972007-10-05 20:04:43 +00005977 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005978 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005979 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00005980 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00005981 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00005982 else if (Node->getOperand(0).getValueType() == MVT::f80)
5983 LC = RTLIB::FPTOSINT_F80_I64;
5984 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5985 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00005986 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5987 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005988 break;
Evan Cheng56966222007-01-12 02:11:51 +00005989 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005990
Evan Cheng56966222007-01-12 02:11:51 +00005991 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005992 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005993 SDOperand Op;
5994 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5995 case Expand: assert(0 && "cannot expand FP!");
5996 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5997 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5998 }
5999
6000 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6001
6002 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00006003 if (Op.Val) {
6004 ExpandOp(Op, Lo, Hi);
6005 break;
6006 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006007 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006008
Evan Chengdaccea182007-10-05 01:09:32 +00006009 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00006010 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006011 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00006012 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006013 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006014 else if (Node->getOperand(0).getValueType() == MVT::f80)
6015 LC = RTLIB::FPTOUINT_F80_I64;
6016 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6017 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00006018 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6019 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006020 break;
Evan Cheng56966222007-01-12 02:11:51 +00006021 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006022
Evan Cheng05a2d562006-01-09 18:31:59 +00006023 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006024 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006025 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006026 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006027 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006028 Op = TLI.LowerOperation(Op, DAG);
6029 if (Op.Val) {
6030 // Now that the custom expander is done, expand the result, which is
6031 // still VT.
6032 ExpandOp(Op, Lo, Hi);
6033 break;
6034 }
6035 }
6036
Chris Lattner79980b02006-09-13 03:50:39 +00006037 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6038 // this X << 1 as X+X.
6039 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6040 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6041 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6042 SDOperand LoOps[2], HiOps[3];
6043 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6044 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6045 LoOps[1] = LoOps[0];
6046 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6047
6048 HiOps[1] = HiOps[0];
6049 HiOps[2] = Lo.getValue(1);
6050 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6051 break;
6052 }
6053 }
6054
Chris Lattnere34b3962005-01-19 04:19:40 +00006055 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006056 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006057 break;
Chris Lattner47599822005-04-02 03:38:53 +00006058
6059 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006060 TargetLowering::LegalizeAction Action =
6061 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6062 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6063 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006064 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006065 break;
6066 }
6067
Chris Lattnere34b3962005-01-19 04:19:40 +00006068 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006069 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6070 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006071 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006072 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006073
Evan Cheng05a2d562006-01-09 18:31:59 +00006074 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006075 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006076 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006077 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006078 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006079 Op = TLI.LowerOperation(Op, DAG);
6080 if (Op.Val) {
6081 // Now that the custom expander is done, expand the result, which is
6082 // still VT.
6083 ExpandOp(Op, Lo, Hi);
6084 break;
6085 }
6086 }
6087
Chris Lattnere34b3962005-01-19 04:19:40 +00006088 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006089 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006090 break;
Chris Lattner47599822005-04-02 03:38:53 +00006091
6092 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006093 TargetLowering::LegalizeAction Action =
6094 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6095 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6096 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006097 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006098 break;
6099 }
6100
Chris Lattnere34b3962005-01-19 04:19:40 +00006101 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006102 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6103 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006104 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006105 }
6106
6107 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006108 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006109 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006110 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006111 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006112 Op = TLI.LowerOperation(Op, DAG);
6113 if (Op.Val) {
6114 // Now that the custom expander is done, expand the result, which is
6115 // still VT.
6116 ExpandOp(Op, Lo, Hi);
6117 break;
6118 }
6119 }
6120
Chris Lattnere34b3962005-01-19 04:19:40 +00006121 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006122 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006123 break;
Chris Lattner47599822005-04-02 03:38:53 +00006124
6125 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006126 TargetLowering::LegalizeAction Action =
6127 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6128 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6129 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006130 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006131 break;
6132 }
6133
Chris Lattnere34b3962005-01-19 04:19:40 +00006134 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006135 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6136 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006137 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006138 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006139
Misha Brukmanedf128a2005-04-21 22:36:52 +00006140 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006141 case ISD::SUB: {
6142 // If the target wants to custom expand this, let them.
6143 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6144 TargetLowering::Custom) {
6145 Op = TLI.LowerOperation(Op, DAG);
6146 if (Op.Val) {
6147 ExpandOp(Op, Lo, Hi);
6148 break;
6149 }
6150 }
6151
6152 // Expand the subcomponents.
6153 SDOperand LHSL, LHSH, RHSL, RHSH;
6154 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6155 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006156 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6157 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006158 LoOps[0] = LHSL;
6159 LoOps[1] = RHSL;
6160 HiOps[0] = LHSH;
6161 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006162 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00006163 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006164 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006165 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006166 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00006167 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006168 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006169 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006170 }
Chris Lattner84f67882005-01-20 18:52:28 +00006171 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006172 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006173
6174 case ISD::ADDC:
6175 case ISD::SUBC: {
6176 // Expand the subcomponents.
6177 SDOperand LHSL, LHSH, RHSL, RHSH;
6178 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6179 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6180 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6181 SDOperand LoOps[2] = { LHSL, RHSL };
6182 SDOperand HiOps[3] = { LHSH, RHSH };
6183
6184 if (Node->getOpcode() == ISD::ADDC) {
6185 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6186 HiOps[2] = Lo.getValue(1);
6187 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6188 } else {
6189 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6190 HiOps[2] = Lo.getValue(1);
6191 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6192 }
6193 // Remember that we legalized the flag.
6194 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6195 break;
6196 }
6197 case ISD::ADDE:
6198 case ISD::SUBE: {
6199 // Expand the subcomponents.
6200 SDOperand LHSL, LHSH, RHSL, RHSH;
6201 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6202 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6203 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6204 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6205 SDOperand HiOps[3] = { LHSH, RHSH };
6206
6207 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6208 HiOps[2] = Lo.getValue(1);
6209 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6210
6211 // Remember that we legalized the flag.
6212 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6213 break;
6214 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006215 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006216 // If the target wants to custom expand this, let them.
6217 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006218 SDOperand New = TLI.LowerOperation(Op, DAG);
6219 if (New.Val) {
6220 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006221 break;
6222 }
6223 }
6224
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006225 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6226 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006227 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6228 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6229 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006230 SDOperand LL, LH, RL, RH;
6231 ExpandOp(Node->getOperand(0), LL, LH);
6232 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman525178c2007-10-08 18:33:35 +00006233 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
6234 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6235 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6236 // FIXME: generalize this to handle other bit sizes
6237 if (LHSSB == 32 && RHSSB == 32 &&
6238 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
6239 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
6240 // The inputs are both zero-extended.
6241 if (HasUMUL_LOHI) {
6242 // We can emit a umul_lohi.
6243 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6244 Hi = SDOperand(Lo.Val, 1);
6245 break;
6246 }
6247 if (HasMULHU) {
6248 // We can emit a mulhu+mul.
6249 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6250 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6251 break;
6252 }
Dan Gohman525178c2007-10-08 18:33:35 +00006253 }
6254 if (LHSSB > BitSize && RHSSB > BitSize) {
6255 // The input values are both sign-extended.
6256 if (HasSMUL_LOHI) {
6257 // We can emit a smul_lohi.
6258 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6259 Hi = SDOperand(Lo.Val, 1);
6260 break;
6261 }
6262 if (HasMULHS) {
6263 // We can emit a mulhs+mul.
6264 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6265 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6266 break;
6267 }
6268 }
6269 if (HasUMUL_LOHI) {
6270 // Lo,Hi = umul LHS, RHS.
6271 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6272 DAG.getVTList(NVT, NVT), LL, RL);
6273 Lo = UMulLOHI;
6274 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006275 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6276 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6277 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6278 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006279 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006280 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006281 if (HasMULHU) {
6282 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6283 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6284 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6285 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6286 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6287 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6288 break;
6289 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006290 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006291
Dan Gohman525178c2007-10-08 18:33:35 +00006292 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006293 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6294 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006295 break;
6296 }
Evan Cheng56966222007-01-12 02:11:51 +00006297 case ISD::SDIV:
6298 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6299 break;
6300 case ISD::UDIV:
6301 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6302 break;
6303 case ISD::SREM:
6304 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6305 break;
6306 case ISD::UREM:
6307 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6308 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006309
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006310 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006311 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6312 RTLIB::ADD_F64,
6313 RTLIB::ADD_F80,
6314 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006315 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006316 break;
6317 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006318 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6319 RTLIB::SUB_F64,
6320 RTLIB::SUB_F80,
6321 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006322 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006323 break;
6324 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006325 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6326 RTLIB::MUL_F64,
6327 RTLIB::MUL_F80,
6328 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006329 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006330 break;
6331 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006332 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6333 RTLIB::DIV_F64,
6334 RTLIB::DIV_F80,
6335 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006336 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006337 break;
6338 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006339 if (VT == MVT::ppcf128) {
6340 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6341 Node->getOperand(0).getValueType()==MVT::f64);
6342 const uint64_t zero = 0;
6343 if (Node->getOperand(0).getValueType()==MVT::f32)
6344 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6345 else
6346 Hi = Node->getOperand(0);
6347 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6348 break;
6349 }
Evan Cheng56966222007-01-12 02:11:51 +00006350 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006351 break;
6352 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006353 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006354 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006355 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006356 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6357 RTLIB::POWI_F64,
6358 RTLIB::POWI_F80,
6359 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006360 Node, false, Hi);
6361 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006362 case ISD::FSQRT:
6363 case ISD::FSIN:
6364 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006365 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006366 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006367 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006368 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6369 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006370 break;
6371 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006372 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6373 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006374 break;
6375 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006376 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6377 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006378 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006379 default: assert(0 && "Unreachable!");
6380 }
Evan Cheng56966222007-01-12 02:11:51 +00006381 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006382 break;
6383 }
Evan Cheng966bf242006-12-16 00:52:40 +00006384 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006385 if (VT == MVT::ppcf128) {
6386 SDOperand Tmp;
6387 ExpandOp(Node->getOperand(0), Lo, Tmp);
6388 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6389 // lo = hi==fabs(hi) ? lo : -lo;
6390 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6391 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6392 DAG.getCondCode(ISD::SETEQ));
6393 break;
6394 }
Evan Cheng966bf242006-12-16 00:52:40 +00006395 SDOperand Mask = (VT == MVT::f64)
6396 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6397 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6398 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6399 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6400 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6401 if (getTypeAction(NVT) == Expand)
6402 ExpandOp(Lo, Lo, Hi);
6403 break;
6404 }
6405 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006406 if (VT == MVT::ppcf128) {
6407 ExpandOp(Node->getOperand(0), Lo, Hi);
6408 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6409 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6410 break;
6411 }
Evan Cheng966bf242006-12-16 00:52:40 +00006412 SDOperand Mask = (VT == MVT::f64)
6413 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6414 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6415 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6416 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6417 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6418 if (getTypeAction(NVT) == Expand)
6419 ExpandOp(Lo, Lo, Hi);
6420 break;
6421 }
Evan Cheng912095b2007-01-04 21:56:39 +00006422 case ISD::FCOPYSIGN: {
6423 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6424 if (getTypeAction(NVT) == Expand)
6425 ExpandOp(Lo, Lo, Hi);
6426 break;
6427 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006428 case ISD::SINT_TO_FP:
6429 case ISD::UINT_TO_FP: {
6430 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6431 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006432 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006433 static uint64_t zero = 0;
6434 if (isSigned) {
6435 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6436 Node->getOperand(0)));
6437 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6438 } else {
6439 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6440 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6441 Node->getOperand(0)));
6442 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6443 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006444 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006445 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6446 DAG.getConstant(0, MVT::i32),
6447 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6448 DAG.getConstantFP(
6449 APFloat(APInt(128, 2, TwoE32)),
6450 MVT::ppcf128)),
6451 Hi,
6452 DAG.getCondCode(ISD::SETLT)),
6453 Lo, Hi);
6454 }
6455 break;
6456 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006457 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6458 // si64->ppcf128 done by libcall, below
6459 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6460 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6461 Lo, Hi);
6462 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6463 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6464 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6465 DAG.getConstant(0, MVT::i64),
6466 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6467 DAG.getConstantFP(
6468 APFloat(APInt(128, 2, TwoE64)),
6469 MVT::ppcf128)),
6470 Hi,
6471 DAG.getCondCode(ISD::SETLT)),
6472 Lo, Hi);
6473 break;
6474 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006475 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006476 if (Node->getOperand(0).getValueType() == MVT::i64) {
6477 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006478 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006479 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006480 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006481 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006482 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006483 LC = RTLIB::SINTTOFP_I64_F80;
6484 }
6485 else if (VT == MVT::ppcf128) {
6486 assert(isSigned);
6487 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006488 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006489 } else {
6490 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006491 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006492 else
Evan Cheng56966222007-01-12 02:11:51 +00006493 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006494 }
6495
6496 // Promote the operand if needed.
6497 if (getTypeAction(SrcVT) == Promote) {
6498 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6499 Tmp = isSigned
6500 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6501 DAG.getValueType(SrcVT))
6502 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6503 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6504 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006505
6506 const char *LibCall = TLI.getLibcallName(LC);
6507 if (LibCall)
6508 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6509 else {
6510 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6511 Node->getOperand(0));
6512 if (getTypeAction(Lo.getValueType()) == Expand)
6513 ExpandOp(Lo, Lo, Hi);
6514 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006515 break;
6516 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006517 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006518
Chris Lattner83397362005-12-21 18:02:52 +00006519 // Make sure the resultant values have been legalized themselves, unless this
6520 // is a type that requires multi-step expansion.
6521 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6522 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006523 if (Hi.Val)
6524 // Don't legalize the high part if it is expanded to a single node.
6525 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006526 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006527
6528 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006529 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006530 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006531}
6532
Dan Gohman7f321562007-06-25 16:23:39 +00006533/// SplitVectorOp - Given an operand of vector type, break it down into
6534/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006535void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6536 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006537 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006538 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006539 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006540 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006541
Dan Gohmane40c7b02007-09-24 15:54:53 +00006542 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006543
6544 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6545 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6546
6547 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6548 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6549
Chris Lattnerc7029802006-03-18 01:44:44 +00006550 // See if we already split it.
6551 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6552 = SplitNodes.find(Op);
6553 if (I != SplitNodes.end()) {
6554 Lo = I->second.first;
6555 Hi = I->second.second;
6556 return;
6557 }
6558
6559 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006560 default:
6561#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006562 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006563#endif
6564 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006565 case ISD::UNDEF:
6566 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6567 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6568 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006569 case ISD::BUILD_PAIR:
6570 Lo = Node->getOperand(0);
6571 Hi = Node->getOperand(1);
6572 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006573 case ISD::INSERT_VECTOR_ELT: {
6574 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6575 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6576 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006577 if (Index < NewNumElts_Lo)
6578 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006579 DAG.getConstant(Index, TLI.getPointerTy()));
6580 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006581 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6582 DAG.getConstant(Index - NewNumElts_Lo,
6583 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006584 break;
6585 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006586 case ISD::VECTOR_SHUFFLE: {
6587 // Build the low part.
6588 SDOperand Mask = Node->getOperand(2);
6589 SmallVector<SDOperand, 8> Ops;
6590 MVT::ValueType PtrVT = TLI.getPointerTy();
6591
6592 // Insert all of the elements from the input that are needed. We use
6593 // buildvector of extractelement here because the input vectors will have
6594 // to be legalized, so this makes the code simpler.
6595 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6596 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6597 SDOperand InVec = Node->getOperand(0);
6598 if (Idx >= NumElements) {
6599 InVec = Node->getOperand(1);
6600 Idx -= NumElements;
6601 }
6602 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6603 DAG.getConstant(Idx, PtrVT)));
6604 }
6605 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6606 Ops.clear();
6607
6608 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6609 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6610 SDOperand InVec = Node->getOperand(0);
6611 if (Idx >= NumElements) {
6612 InVec = Node->getOperand(1);
6613 Idx -= NumElements;
6614 }
6615 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6616 DAG.getConstant(Idx, PtrVT)));
6617 }
6618 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6619 break;
6620 }
Dan Gohman7f321562007-06-25 16:23:39 +00006621 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006622 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006623 Node->op_begin()+NewNumElts_Lo);
6624 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006625
Nate Begeman5db1afb2007-11-15 21:15:26 +00006626 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006627 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006628 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006629 break;
6630 }
Dan Gohman7f321562007-06-25 16:23:39 +00006631 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006632 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006633 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6634 if (NewNumSubvectors == 1) {
6635 Lo = Node->getOperand(0);
6636 Hi = Node->getOperand(1);
6637 } else {
6638 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6639 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006640 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006641
Dan Gohman7f321562007-06-25 16:23:39 +00006642 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6643 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006644 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006645 }
Dan Gohman65956352007-06-13 15:12:02 +00006646 break;
6647 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006648 case ISD::SELECT: {
6649 SDOperand Cond = Node->getOperand(0);
6650
6651 SDOperand LL, LH, RL, RH;
6652 SplitVectorOp(Node->getOperand(1), LL, LH);
6653 SplitVectorOp(Node->getOperand(2), RL, RH);
6654
6655 if (MVT::isVector(Cond.getValueType())) {
6656 // Handle a vector merge.
6657 SDOperand CL, CH;
6658 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006659 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6660 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006661 } else {
6662 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006663 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6664 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006665 }
6666 break;
6667 }
Dan Gohman7f321562007-06-25 16:23:39 +00006668 case ISD::ADD:
6669 case ISD::SUB:
6670 case ISD::MUL:
6671 case ISD::FADD:
6672 case ISD::FSUB:
6673 case ISD::FMUL:
6674 case ISD::SDIV:
6675 case ISD::UDIV:
6676 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006677 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006678 case ISD::AND:
6679 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006680 case ISD::XOR:
6681 case ISD::UREM:
6682 case ISD::SREM:
6683 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006684 SDOperand LL, LH, RL, RH;
6685 SplitVectorOp(Node->getOperand(0), LL, LH);
6686 SplitVectorOp(Node->getOperand(1), RL, RH);
6687
Nate Begeman5db1afb2007-11-15 21:15:26 +00006688 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6689 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006690 break;
6691 }
Dan Gohman82669522007-10-11 23:57:53 +00006692 case ISD::FPOWI: {
6693 SDOperand L, H;
6694 SplitVectorOp(Node->getOperand(0), L, H);
6695
Nate Begeman5db1afb2007-11-15 21:15:26 +00006696 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6697 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006698 break;
6699 }
6700 case ISD::CTTZ:
6701 case ISD::CTLZ:
6702 case ISD::CTPOP:
6703 case ISD::FNEG:
6704 case ISD::FABS:
6705 case ISD::FSQRT:
6706 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006707 case ISD::FCOS:
6708 case ISD::FP_TO_SINT:
6709 case ISD::FP_TO_UINT:
6710 case ISD::SINT_TO_FP:
6711 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006712 SDOperand L, H;
6713 SplitVectorOp(Node->getOperand(0), L, H);
6714
Nate Begeman5db1afb2007-11-15 21:15:26 +00006715 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6716 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006717 break;
6718 }
Dan Gohman7f321562007-06-25 16:23:39 +00006719 case ISD::LOAD: {
6720 LoadSDNode *LD = cast<LoadSDNode>(Node);
6721 SDOperand Ch = LD->getChain();
6722 SDOperand Ptr = LD->getBasePtr();
6723 const Value *SV = LD->getSrcValue();
6724 int SVOffset = LD->getSrcValueOffset();
6725 unsigned Alignment = LD->getAlignment();
6726 bool isVolatile = LD->isVolatile();
6727
Nate Begeman5db1afb2007-11-15 21:15:26 +00006728 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6729 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006730 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006731 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006732 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006733 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006734 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006735
6736 // Build a factor node to remember that this load is independent of the
6737 // other one.
6738 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6739 Hi.getValue(1));
6740
6741 // Remember that we legalized the chain.
6742 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006743 break;
6744 }
Dan Gohman7f321562007-06-25 16:23:39 +00006745 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006746 // We know the result is a vector. The input may be either a vector or a
6747 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006748 SDOperand InOp = Node->getOperand(0);
6749 if (!MVT::isVector(InOp.getValueType()) ||
6750 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6751 // The input is a scalar or single-element vector.
6752 // Lower to a store/load so that it can be split.
6753 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006754 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohmanc6c391d2008-01-31 00:25:39 +00006755 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr.Val);
6756 assert(FI && "Expecting CreateStackTemporary to return a frame index.\n");
Chris Lattner7692eb42006-03-23 21:16:34 +00006757
Evan Cheng786225a2006-10-05 23:01:46 +00006758 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohmanc6c391d2008-01-31 00:25:39 +00006759 InOp, Ptr,
6760 &PseudoSourceValue::FPRel, FI->getIndex());
6761 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
6762 &PseudoSourceValue::FPRel, FI->getIndex());
Chris Lattner7692eb42006-03-23 21:16:34 +00006763 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006764 // Split the vector and convert each of the pieces now.
6765 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006766 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6767 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006768 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006769 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006770 }
6771
6772 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006773 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006774 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006775 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006776}
6777
6778
Dan Gohman89b20c02007-06-27 14:06:22 +00006779/// ScalarizeVectorOp - Given an operand of single-element vector type
6780/// (e.g. v1f32), convert it into the equivalent operation that returns a
6781/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006782SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6783 assert(MVT::isVector(Op.getValueType()) &&
6784 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006785 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006786 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6787 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006788
Dan Gohman7f321562007-06-25 16:23:39 +00006789 // See if we already scalarized it.
6790 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6791 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006792
6793 SDOperand Result;
6794 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006795 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006796#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006797 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006798#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006799 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6800 case ISD::ADD:
6801 case ISD::FADD:
6802 case ISD::SUB:
6803 case ISD::FSUB:
6804 case ISD::MUL:
6805 case ISD::FMUL:
6806 case ISD::SDIV:
6807 case ISD::UDIV:
6808 case ISD::FDIV:
6809 case ISD::SREM:
6810 case ISD::UREM:
6811 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006812 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006813 case ISD::AND:
6814 case ISD::OR:
6815 case ISD::XOR:
6816 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006817 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006818 ScalarizeVectorOp(Node->getOperand(0)),
6819 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006820 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006821 case ISD::FNEG:
6822 case ISD::FABS:
6823 case ISD::FSQRT:
6824 case ISD::FSIN:
6825 case ISD::FCOS:
6826 Result = DAG.getNode(Node->getOpcode(),
6827 NewVT,
6828 ScalarizeVectorOp(Node->getOperand(0)));
6829 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006830 case ISD::FPOWI:
6831 Result = DAG.getNode(Node->getOpcode(),
6832 NewVT,
6833 ScalarizeVectorOp(Node->getOperand(0)),
6834 Node->getOperand(1));
6835 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006836 case ISD::LOAD: {
6837 LoadSDNode *LD = cast<LoadSDNode>(Node);
6838 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6839 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006840
Dan Gohman7f321562007-06-25 16:23:39 +00006841 const Value *SV = LD->getSrcValue();
6842 int SVOffset = LD->getSrcValueOffset();
6843 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6844 LD->isVolatile(), LD->getAlignment());
6845
Chris Lattnerc7029802006-03-18 01:44:44 +00006846 // Remember that we legalized the chain.
6847 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6848 break;
6849 }
Dan Gohman7f321562007-06-25 16:23:39 +00006850 case ISD::BUILD_VECTOR:
6851 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006852 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006853 case ISD::INSERT_VECTOR_ELT:
6854 // Returning the inserted scalar element.
6855 Result = Node->getOperand(1);
6856 break;
6857 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006858 assert(Node->getOperand(0).getValueType() == NewVT &&
6859 "Concat of non-legal vectors not yet supported!");
6860 Result = Node->getOperand(0);
6861 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006862 case ISD::VECTOR_SHUFFLE: {
6863 // Figure out if the scalar is the LHS or RHS and return it.
6864 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6865 if (cast<ConstantSDNode>(EltNum)->getValue())
6866 Result = ScalarizeVectorOp(Node->getOperand(1));
6867 else
6868 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006869 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006870 }
6871 case ISD::EXTRACT_SUBVECTOR:
6872 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006873 assert(Result.getValueType() == NewVT);
6874 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006875 case ISD::BIT_CONVERT:
6876 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00006877 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006878 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006879 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006880 ScalarizeVectorOp(Op.getOperand(1)),
6881 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006882 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00006883 }
6884
6885 if (TLI.isTypeLegal(NewVT))
6886 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00006887 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6888 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006889 return Result;
6890}
6891
Chris Lattner3e928bb2005-01-07 07:47:09 +00006892
6893// SelectionDAG::Legalize - This is the entry point for the file.
6894//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00006895void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00006896 if (ViewLegalizeDAGs) viewGraph();
6897
Chris Lattner3e928bb2005-01-07 07:47:09 +00006898 /// run - This is the main entry point to this class.
6899 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00006900 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006901}
6902